此程序在STC89C52上实验通过!
晶振11.05926,
占用资源,外部中断1,
LCD1602,显示解码!
串口通讯波特率4800KBS;
闫伟东,提示,在中端中不可以用while(p3_2);
否则,不能顺利跳出终端!
#include <AT89X52.H> #include "LCD.h" #define uchar unsigned char #define uint unsigned int //void Delay(unsigned int i); void chuangkou(void); void unint0 (void); void senddata(unsigned char outdata); unsigned char yandata[]= { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; unsigned char Iid[4]={0,0,0,0}; void main(void) { LCD1602(); Init_Lcd(); unint0(); chuangkou(); P3_2=1; senddata('1'); while(1) { Display_LCD_Byte(0,0,'I'); Display_LCD_Byte(0,1,'D'); Display_LCD_Byte(0,2,':'); Display_LCD_Byte(0,5,'0'); Display_LCD_Byte(0,6,'X'); Display_LCD_Byte(0,7,yandata[ Iid[0] / 16] ); Display_LCD_Byte(0,8,yandata[ Iid[0] % 16] ); Display_LCD_Byte(1,0,'D'); Display_LCD_Byte(1,1,'A'); Display_LCD_Byte(1,2,'T'); Display_LCD_Byte(1,3,'A'); Display_LCD_Byte(1,4,':'); Display_LCD_Byte(1,5,'0'); Display_LCD_Byte(1,6,'X'); Display_LCD_Byte(1,7,yandata[ Iid[2] / 16] ); Display_LCD_Byte(1,8,yandata[ Iid[2] % 16] ); } } /*void Delay(unsigned int i) { while(-- i); }*/ void chuangkou(void) { TMOD = 0X20;// JI SHU QI 1 TH1=0XFA; //11.0592, 4800KPS TL1 =0XFA; TR1=1; //QI DONG SCON=0X50; // PCON=0X00; //BU JIA BEI } void unint0 (void) { EA=1; EX0=1; IT0=1; } void senddata(unsigned char outdata) { SBUF=outdata; while(!TI); TI=0; } void INT_0() interrupt 0 using 1 { unsigned char id[4]={0,0,0,0}; unsigned char cishu0,cishu1,cishu2; EX0=0; for(cishu0=0;cishu0<10;cishu0++) { Delay(100); if(P3_2) goto exit; } if(!P3_2) { Delay(534); } while(!P3_2);//等待高电平 gou qu for(cishu1=0;cishu1< 4 ; cishu1 ++) { for(cishu2=0;cishu2 < 8 ; cishu2 ++) { while(!P3_2);//等待该gao电平开始延时,判断高低 Delay(90); if(P3_2) { id[cishu1]|= 0x01 << cishu2; Delay(100); //while(P3_2);在解码时千万不要有这样的口令 } /* else { id[cishu1]&= 0xfe << cishu2; }*/ } } if(id[0]!=~id[1]) goto exit; if(id[0]!=0x02) goto exit; if(id[2]!=~id[3]) goto exit; senddata(id[0]); // senddata(id[1]); senddata(id[2]);//数据码 // senddata(id[3]);//数据码饭吗 for(cishu0=0;cishu0<200;cishu0++) { P2_0=~P2_0; Delay(50); } Iid[0]=id[0]; Iid[2]=id[2]; exit: // while(!P3_2); EX0=1; }