|
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit led=P1^1;
- uchar flag,num;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void delayms(uint xms) //延时函数
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void display(uchar num) //数码管显示函数
- {
- uchar bai,shi,ge;
- bai=num/100;
- shi=num%100/10;
- ge =num%10;
- dula=1;
- P0=table[bai];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0x7e;
- wela=0;
- delayms(5);
- dula=1;
- P0=table[shi];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0x7d;
- wela=0;
- delayms(5);
- dula=1;
- P0=table[ge];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0x7b;
- wela=0;
- delayms(5);
- }
- void ser() interrupt 4 //串口中断
- {
- RI = 0;
- num = SBUF; //从串口缓冲区取得数据
- flag=1;
-
- }
-
-
-
- void init()
- {
- SCON=0x50; //设定串口工作方式
- PCON=0x00; //波特率不倍增
- TMOD=0x20; //定时器1工作于8位自动重载模式, 用于产生波特率
- EA=1;
- ES = 1; //允许串口中断
- TL1=0xfd;
- TH1=0xfd; //波特率9600
- TR1=1;
- }
-
- void main()
- {
- led=0;
- wela=1;
- P0=0x7f;
- wela=0;
- init();
- while(1)
- {
- display(num) ;
- if(flag==1) //标志判断
- {
-
- SBUF=num;
- while(!TI);
- TI=0;
- flag=0; //标志清除
- }
- }
-
- }
复制代码
全部资料下载地址:
串口实验.zip
(21.88 KB, 下载次数: 28)
|
|