|
用串口助手模擬UART,發(fā)送數(shù)據(jù)+1,發(fā)回,但是這段程序發(fā)回的數(shù)據(jù)不是+1,好像有點(diǎn)沒規(guī)律,請(qǐng)問是什么原因,看了大半天沒看出來問題。
單片機(jī)源程序如下:
- #include<reg52.h>
- sbit pRXD=P3^0;
- sbit pTXD=P3^1;
- bit RxDorTXD=0; //給中斷程序來判斷是處于接收狀態(tài)還是發(fā)送狀態(tài), 1:表示發(fā)送 0:表示接收
- bit RXDEnd=0; //接收結(jié)束標(biāo)識(shí)
- bit TXDEnd=0;
- unsigned char RXDBuf; //接收緩沖器
- unsigned char TXDBuf; //發(fā)送緩沖器
- void configUART(unsigned int baud);
- void receive();
- void sent(unsigned char dat);
-
- void main(){
- EA=1; //開啟中斷總開關(guān)
- configUART(9600); //配置波特率
- while(1){
- while (pRXD);
- receive(); //接收數(shù)據(jù)
- sent(RXDBuf); //發(fā)送數(shù)據(jù)
- while(!TXDEnd);
- }
- }
- void configUART (unsigned int baud){
- TMOD=0x02; //讓TMOD工作在02模式下,即TH0可以直接重置到TL0上 書本P59
- TH0=256-11059200/baud/12; //配置TH0的重置值
- }
- void receive (){
- TR0=1;
- ET0=1;
- TL0=256-((256-TH0)>>1);
- RxDorTXD=0;
- }
- void sent(unsigned char dat){
- ET0=1;
- TR0=1;
- RxDorTXD=1;
- TXDBuf=dat+1;
- }
-
- void interrupttimer0() interrupt 1{
- static int cnt=0;
- if(RxDorTXD==0){ //如果RXDorTXD為0,則開始接收數(shù)據(jù)
- if(cnt==0){
- if(pRXD==0){
- RXDBuf=0;
- cnt++;
- }
- else {
- TR0=0;
- }
- }
- else if(cnt<=8){
- RXDBuf>>=1;
- if(pRXD==1){
- RXDBuf|=0x80;
- }
- cnt++;
- }
- else {
- cnt=0;
- TR0=0;
- if(pRXD==1){
- RXDEnd=1;
- }
- } }
-
-
- if(RxDorTXD==1){ //RXDorTXD為1,則開始發(fā)送數(shù)據(jù)
- cnt++;
- if(cnt<=8){
- pTXD=TXDBuf & 0x01;
- TXDBuf>>=1;
- }
- else if(cnt==9){
- pTXD=1;}
- else {
- cnt=0;
- TR0=0;
- TXDEnd=1;
- }
- }
- }
復(fù)制代碼
|
|