/*這是一個(gè)使用單片機(jī)串行通信口通過(guò)RS232總線與PC進(jìn)行通信的測(cè)試程序,無(wú)需擴(kuò)展.可將DB9的第2和第3腳短路,進(jìn)行自收自發(fā)測(cè)試.采用串行中斷接收,速率:9600,通信方式2,晶振使用:osc=11.0592MHz。使用SW1-SW6鍵盤發(fā)送文本數(shù)字1-6,數(shù)碼管顯示接收的ASCII數(shù)據(jù)*/ void main(void) { SCON=0x50; TMOD=0x21; TH1=0xfd; TL1=0xfd; TH0=0xf4; TL0=0x48; ES=1;ET0=1;EA=1; TR1=1; TR0=1; while(1) { if(!key){ Delay(30000); a=Key_down(); SBUF=table[a]; while(TI==0); TI=0; } } } uchar Key_down(void) { uchar x; if(!sw1)x=1; else if(!sw2)x=2; else if(!sw3)x=3; else if(!sw4)x=4; else if(!sw5)x=5; else if(!sw6)x=6; return(x); } void Int_T0(void) interrupt 1 { TH0=0xf4; TL0=0x48; if(count==5)count=0; else count++; switch(count) { case 0:DIG_NUM=0x7f; LED_DISPLAY=buff_led1; break; case 1:DIG_NUM=0xbf; LED_DISPLAY=buff_led2; break; case 2:DIG_NUM=0xdf; LED_DISPLAY=buff_led3; break; case 3:DIG_NUM=0xef; LED_DISPLAY=buff_led4; break; case 4:DIG_NUM=0xf7; LED_DISPLAY=buff_led5; break; case 5:DIG_NUM=0xfb; LED_DISPLAY=buff_led6; break; default:break; } } void Int_Serial(void) interrupt 4 { while(RI==0); a=SBUF;RI=0; buff_led1=buff_led2; buff_led2=buff_led3; buff_led3=buff_led4; buff_led4=buff_led5; buff_led5=buff_led6; buff_led6=a; }
void Delay(int t) {while(--t);}
參考鏈接:http://www.picavr.com/news/2008-02/3985.htm
|