|
|
如果不仔細(xì),任何一點(diǎn)疏漏都會(huì)導(dǎo)致失敗。下面這個(gè)程序基本沒(méi)有改動(dòng),經(jīng)實(shí)物驗(yàn)證無(wú)誤。
無(wú)標(biāo)題.jpg (20.79 KB, 下載次數(shù): 16)
下載附件
2020-5-13 20:58 上傳
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LED1=P3^4;
- sbit LED2=P3^3;
- void UartInit(void)//初始化
- {
- SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收 串行控制寄存器
- TMOD |= 0x20; // TMOD: timer 1, 模式 2, 8-bit 重裝
- PCON = 0x00; //波特率不加倍
- TH1 = 0xFD; // TH1: 重裝值 9600 波特率 晶振 11.0592MHz
- TL1 = TH1; //波特率9600
- TR1 = 1; // TR1: timer 1 打開(kāi) 啟動(dòng)T
- EA = 1; //打開(kāi)總中斷
- ES = 1; //打開(kāi)串口中斷
- }
- void UartISR(void) interrupt 4 //中斷服務(wù)響應(yīng)
- {
- uchar tempdata;
- RI=0;
- tempdata=SBUF;
- if(tempdata=='1')
- {
- LED1=1;
- LED2=0;
- }
- if(tempdata=='2')
- {
- LED2=1;
- LED1=0;
- }
- }
- void main(void)
- {
- UartInit();
- LED1=0;
- LED2=0;
- while(1)
- {
- }
- }
復(fù)制代碼
|
|