- /***********************************************************************************************************
- 实现功能:上位机发送十六进制数据01、02、03、04到单片机,单片机
- 收到数据后再发送对应的table数组到上位机,并且在数码管
- 上面显示上位机发送的数据。如果上位机发送01~04以外的数
- 据,单片机就发送"Please Enter number between 01 and 04"
- 到上位机。
- 用到的单片机知识:1、定时器0工作方式1;
- 2、串口通信;
- 3、数码管显示(数码管显示功能放在定时器0中断中可以避免连续发送同一个数据时数码管闪动)。
- 注意事项:将数码管显示功能放在定时器中断中时,应将中断时间尽量缩短,但是最小时间必须大于数码管中所有延时
- 的总和,如果太长会出现前面的数码管会较后面的数码管暗。
- **************************************************************************************************************/
- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table01[] = {1};
- uchar code table02[] = {1,2};
- uchar code table03[] = {1,2,3};
- uchar code table04[] = {1,2,3,4};
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,
- 0x77,0x1F,0x4E,0x2D,
- 0x4F,0x47};
- ***it dula = P2^6;
- ***it wela = P2^7;
- uchar flag = 0;
- uchar RXD_DATA = 0;
- uchar *Pt_table;
- uchar cnt_table;
- uchar error = 1;
- void ConfigUART(uint baud);
- void display(uchar);
- void delayms(uint);
- void main()
- {
- TMOD = 0x01;
- TH0 = (65536 - 2752)/256;
- TL0 = (65536 - 2752)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- ConfigUART(9600);
- while(1)
- {
- //display(RXD_DATA);
- if(flag == 1)
- {
- flag = 0;
- switch(RXD_DATA)
- {
- case 1:
- Pt_table = table01;
- cnt_table = sizeof(table01);
- ti = 1;
- break;
- case 2:
- Pt_table = table02;
- cnt_table = sizeof(table02);
- TI = 1;
- break;
- case 3:
- Pt_table = table03;
- cnt_table = sizeof(table03);
- TI = 1;
- break;
- case 4:
- Pt_table = table04;
- cnt_table = sizeof(table04);
- TI = 1;
- break;
- default:
- error = 0;
- TI = 1;
- printf("Please Enter number between 01 and 04");
- TI = 0;
- error = 1;
- break;
- }
- }
- }
- }
- void ConfigUART(uint baud)
- {
- SCON = 0x50;
- TMOD = TMOD&0x0F;
- TMOD = TMOD|0x20;
- TH1 = 256 - (11059200/12/2/16)/baud;
- TL1 = TH1;
- //EA = 1;
- ES = 1;
- ET1 = 0;//禁止T1中断
- TR1 = 1;
- }
- void InterruptUART()interrupt 4
- {
- if(RI)
- {
- RI = 0;
- RXD_DATA = SBUF;
- flag = 1;
- }
- if(TI & error)
- {
- TI = 0;
- if(cnt_table > 0)
- {
- SBUF = *Pt_table;
- cnt_table--;
- Pt_table++;
- }
- }
- }
- void Interrupt_T0()interrupt 1
- {
- TH0 = (65536 - 2752)/256;
- TL0 = (65536 - 2752)%256;
- display(RXD_DATA);
- }
- void display(uchar a)
- {
- uchar ge = a&0x0f;
- uchar shi = a>>4;
-
- dula = 1;
- P0 = table[shi];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xfe;
- wela = 0;
- delayms(1);
-
- dula = 1;
- P0 = table[ge];
- dula = 0;
- P0 = 0xff;
- wela = 1;
- P0 = 0xfd;
- wela = 0;
- delayms(1);
- }
- void delayms(uint ms)
- {
- uint i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--);
- }
复制代码
0
|
|
|
|