- #include <reg52.h>
- #include"delay.h"
- unsigned char a;
- unsigned char flag;
- sbit led_open=P2^3;
- void initiate_RS232(void);
- sbit s1=P3^4;
- sbit s2=P3^5;
- unsigned short int data sta;
- unsigned short int data count;
- uchar smg[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
- unsigned char code smg_we[]={0xef,0xdf,0xbf,0x7f};
- //unsigned short int data second;
- void main()
- {
- //led_open=0;
-
- initiate_RS232();
- while(1)
- {
- if(sta==1) //按鍵1,發(fā)送
- {
- //sta1=0;
- ES=0;
- SBUF=1; //發(fā)送
- while(!TI); //軟件清零
- TI=0;
- ES=1;
- }
- if(sta==2) //
- {
- //sta2=0;
- ES=0;
- SBUF=2;
- while(!TI); //軟件清零
- TI=0;
- ES=1;
- }
- }
-
-
-
-
- }
- void time0_isp(void) interrupt 1 //t0,周期1ms
- {
- static char data i=0;
- static unsigned char key[3];
- TL0=TL0+0X18;
- TH0=0XFC;
-
- key[2]=key[1];
- key[1]=key[0];
- if (s1==0) key[0]=1; else key[0]=0;
- if (s2==0) key[0]=key[0]+2;
-
- //讀出當(dāng)前值,如何讀,跟具體電路有關(guān)
-
-
- if((key[0]==key[1])&&(key[0]==key[2]) )
- sta=key[0];
-
- switch(i)
- {
- case 0: P0=smg[count%10]; P2=0xef; i=1;break; //個位
- case 1: P0=smg[count%100/10]; P2=0xdf; i=2;break; //十位
- case 2: P0=smg[count%1000/100];P2=0xbf;i=3;break; //百位
- case 3: P0=smg[count/1000];P2=0x7f;i=0;break;
- }
- }
- void initiate_RS232(void) //串口初始化
- {
- TH1=0xfd;
- TL1=0xfd;
-
- TL0=0X18;
- TH0=0XFC;
-
- TMOD=0x21;
- TR1=1;//開定時器
- TR0=0;
- ET0=1;//開t0中斷
- SM0=0; //
- SM1=1;//方式1,10位異步收發(fā)器(8 位數(shù)據(jù))
- SM2=0;//SM2=0時,均可使收到的數(shù)據(jù)進(jìn)SBUF,并激活RI,在中斷服務(wù)中將數(shù)據(jù)從SBUF讀走
-
- REN=1;//允許串口接收
- EA=1;
- ES=1;
- }
- void serial() interrupt 4
- {
- if(RI==1) //接收完畢
- {
- count=SBUF*256; //先 接收 高位
-
- RI=0; //清0
-
- while(RI==0); //等待
-
- RI=0; //清0
- count=count+SBUF; //
- }
- if(TI==1) //發(fā)送完畢
- {
- TI=0; //0
- }
- }
復(fù)制代碼 |