|
串口已通,但有幾點問題我不太明白:
1.數(shù)據(jù)包用串口助手發(fā)送幾十次后才能收到數(shù)據(jù),接收73位數(shù)據(jù)后后面收不到數(shù)
2.接收到的數(shù)據(jù)和發(fā)送的內(nèi)容完全不同
#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) //在停止位開始發(fā)送時,該位置1
{
S3CON &= ~0x02; //清除S3CON寄存器對應(yīng)S3TI位(該位必須軟件清零)
busy=0;
}
if(S3CON&0x01) //串行接收到停止位的中間時刻時,該位置1
{
S3CON &= ~0x01; //清除S3CON寄存器對應(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中斷打開
}
void Uart3Init()
{
RE485=0;//RS485設(shè)置為接收方向
//S3CON=0x50;
S3CON |= 0x50; //串口3選擇定時器3為波特率發(fā)生器,啟動串行接收器
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中斷打開
IE2 &= 0xDF; // 關(guān)閉定時器3中斷
EA=1;
while(1)
{
if(rptr!=wptr)
{
Uart3Send(buffer[rptr++]);
rptr&=0x0f;
}
}
}
|
|