標準51單片機,比如STC89C52RC只有一個串口,當我們需要多串口通信時就有些捉襟見肘了,在不贈加成本的基礎上最好的辦法就是模擬一個串口;在這個項目中我們采用的單片機型號為STC12C5A60S2,晶振為11.0592MHZ,12倍頻,同樣適用于STC89C52RC,大家如果有興趣可以自己下載源碼應用,以下是工程源碼:
- #include<stc12c5a60s2.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RXD=P3^2;
- sbit TXD=P3^1;
- sbit led=P1^0;
- uchar buf;
- void delayms(unsigned int ms)
- {
- unsigned int De_Cnt;
- while( (ms--) != 0)
- {
- for(De_Cnt = 0; De_Cnt < 950; De_Cnt++);
- }
- }
- void T0_init(void)
- {
- TMOD|=0x01;
- TH0=0XFF;
- TL0=0XA0;
- IE|=0X81;
- TF0=0;
- IT0=1;
- TR1=1 ;
- }
- void waitTF0()
- {
- while(!TF0);
- TF0=0;
- TH0=0XFF;
- TL0=0XA0;
- }
- uchar readbyte()
- {
- uchar output=0;
- uchar i=8;
- TR0=1;
- TH0=0XFF;
- TL0=0XA0;
- TF0=0;
- waitTF0();
- while(i--)
- {
- output>>=1 ;
- if(RXD) output|=0x80;
- waitTF0();
- }
- TR0=0;
- return output;
- }
- void int0() interrupt 0
- {
- EX0=0;
- buf=readbyte();
- IE0=0;
- EX0=1;
- }
- void main()
- {
- T0_init();
- while(1)
- {
- delayms(300);
- if(buf=='A') led=0;
- else led=1;
- }
- }
復制代碼
原理圖: 無
仿真: 無
Keil代碼:
程序.7z
(20.16 KB, 下載次數: 19)
2024-1-2 21:47 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|