|
|
串口已通,但有幾點(diǎn)問(wèn)題我不太明白:
1.用串口助手發(fā)11個(gè)字節(jié),只能回來(lái)10個(gè)字節(jié)
2.接收到的數(shù)據(jù)和發(fā)送的內(nèi)容完全不同
求大神提點(diǎn)!
單片機(jī)源程序如下:
- #include "STC81.h"
- #include "intrins.h"
- #include "485.h"
- #define FOSC 24000000UL
- #define BRT (65536-FOSC/9600/4)
- #define uchar unsigned char
- bit busy;
- char wptr;
- char rptr;
- char buffer[16];
- void Uart3Isr() interrupt 17 using 1
- {
- IE2 &= 0xF7; // 串口3中斷關(guān)閉
- if(S3CON&0x02) //在停止位開(kāi)始發(fā)送時(shí),該位置1
- {
- S3CON &= ~0x02; //清除S3CON寄存器對(duì)應(yīng)S3TI位(該位必須軟件清零)
- busy=0;
- }
- if(S3CON&0x01) //串行接收到停止位的中間時(shí)刻時(shí),該位置1
- {
- S3CON &= ~0x01; //清除S3CON寄存器對(duì)應(yīng)S3RI位(該位必須軟件清零)
- buffer[wptr++]=S3BUF;
- wptr&=0x0f;
- if((buffer[7]==0x00)&&(buffer[8]==0x00))
- {
- RUN11=RUN21=RUN31=RUN41=0;
- STOP11=STOP21=STOP31=STOP41=0;
- }
- }
- IE2 |= 0x08; // 串口3中斷打開(kāi)
- }
- void Uart3Init()
- {
- RE485=0;//RS485設(shè)置為接收方向
- //S3CON=0x50;
- S3CON |= 0x50; //串口3選擇定時(shí)器3為波特率發(fā)生器,啟動(dòng)串行接收器
- S3CON &= 0x70; //8位數(shù)據(jù),可變波特率
- T3L=BRT;
- T3H=BRT>>8;
- T4T3M|=0x0a;
- wptr=0x00;
- rptr=0x00;
- busy=0;
- }
- void Uart3Send(char dat)
- {
- while(busy);
- busy=1;
- S3BUF=dat;
- }
- void main()
- {
- P0M1 &= 0xFE; P0M0 &= 0xFE; //設(shè)置P0.0為準(zhǔn)雙向口
- P0M1 &= 0xFD; P0M0 |= 0x02; //設(shè)置P0.1為推挽輸出
- P_SW2=0x02;
- Uart3Init();
- //IE2=0x08;
- IE2 |= 0x08; // 串口3中斷打開(kāi)
- IE2 &= 0xDF; // 關(guān)閉定時(shí)器3中斷
- EA=1;
- while(1)
- {
- if(rptr!=wptr)
- {
- Uart3Send(buffer[rptr++]);
- rptr&=0x0f;
- }
- }
- }
復(fù)制代碼
|
|