我写的程序是让单片机接收数据已数码管的形式显示,为什么我的我一个程序十位数字出现就很快消失,个位显示;而第二个程序确实现的很好?大佬们,求解答
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit we = P2^7;
- sbit du = P2^6;
- uchar counter,time_counter;
- uchar code leddata[]={
- 0x3F, //"0"
- 0x06, //"1"
- 0x5B, //"2"
- 0x4F, //"3"
- 0x66, //"4"
- 0x6D, //"5"
- 0x7D, //"6"
- 0x07, //"7"
- 0x7F, //"8"
- 0x6F, //"9"
- 0x77, //"A"
- 0x7C, //"B"
- 0x39, //"C"
- 0x5E, //"D"
- 0x79, //"E"
- 0x71, //"F"
- 0x76, //"H"
- 0x38, //"L"
- 0x37, //"n"
- 0x3E, //"u"
- 0x73, //"P"
- 0x5C, //"o"
- 0x40, //"-"
- 0x00, //熄灭
- 0x00 //自定义
- };
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- void UART_init()
- {
- TMOD = 0x20; //T1工作模式2 8位自动重装
- TH1 = 0xfa;
- TL1 = 0xfa; //比特率9600
- TR1 = 1;
- SM0 = 0;
- SM1 = 1; //串口工作方式1 10位异步
- REN = 1; //串口允许接收
- /* EA = 1; //开总中断
- ES = 1; //串口中断打开 */
- }
- void display(uchar i)
- {
- uchar shi,ge;
- shi = i / 10; //求模
- ge = i % 10; //求余
- P0 = 0xff;
- we = 1;
- P0 = 0xfe; //点亮第一位数码管
- we = 0;
- du = 1;
- P0 = leddata[shi];
- du = 0;
- delay(1);
- P0 = 0xff;
- we = 1;
- P0 = 0xfd;//点亮第二位数码管
- we = 0;
- du = 1;
- P0 = leddata[ge];
- du = 0;
- delay(1);
- }
- void main()
- {
- UART_init(); //串口初始化
- while(1)
- {
- while(!RI)
- RI = 0;
- display(SBUF);
- }
- }
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit we = P2^7;
- sbit du = P2^6;
- uchar counter,time_counter;
- uchar code leddata[]={
- 0x3F, //"0"
- 0x06, //"1"
- 0x5B, //"2"
- 0x4F, //"3"
- 0x66, //"4"
- 0x6D, //"5"
- 0x7D, //"6"
- 0x07, //"7"
- 0x7F, //"8"
- 0x6F, //"9"
- 0x77, //"A"
- 0x7C, //"B"
- 0x39, //"C"
- 0x5E, //"D"
- 0x79, //"E"
- 0x71, //"F"
- 0x76, //"H"
- 0x38, //"L"
- 0x37, //"n"
- 0x3E, //"u"
- 0x73, //"P"
- 0x5C, //"o"
- 0x40, //"-"
- 0x00, //熄灭
- 0x00 //自定义
- };
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- void UART_init()
- {
- TMOD = 0x20; //T1工作模式2 8位自动重装
- TH1 = 0xfa;
- TL1 = 0xfa; //比特率9600
- TR1 = 1;
- SM0 = 0;
- SM1 = 1; //串口工作方式1 10位异步
- REN = 1; //串口允许接收
- /* EA = 1; //开总中断
- ES = 1; //串口中断打开 */
- }
- void display(uchar i)
- {
- uchar shi,ge;
- shi = i / 10; //求模
- ge = i % 10; //求余
- P0 = 0xff;
- we = 1;
- P0 = 0xfe; //点亮第一位数码管
- we = 0;
- du = 1;
- P0 = leddata[shi];
- du = 0;
- delay(1);
- P0 = 0xff;
- we = 1;
- P0 = 0xfd;//点亮第二位数码管
- we = 0;
- du = 1;
- P0 = leddata[ge];
- du = 0;
- delay(1);
- }
- void main()
- {
- UART_init(); //串口初始化
- while(1)
- {
- if(RI)
- {
- RI = 0;
- }
- display(SBUF);
- }
- }
复制代码
|