/*和泰单片机串口通许,已调试通过,抗干扰超强,一些标志及定时器请自己定义*/ unsigned char count_uartout;//发送计数器
unsigned cha datin_buf[4]; //接收寄存器4个
unsigned cha datout_buf[9]; //发寄存器9个
#define tongxun_in datin_buf[0] //接收送识辨码寄存器
#define tongxun_out datout_buf[0] //发送识辨码寄存器
#define send_max 8 //设发的数据个数
DEFINE_ISR (Interrupt_uart, 0x2c) //串口中断地址
{
if(++count_uartin>3)
count_uartin=0;
if((_usr&0b11110000)==0) //判断是否出错
{
if(_rx8) //接收到首数据
{
tongxun_in=_txr_rxr;
if(tongxun_in==0x5a) 首数据正常则
{
b_sendero=0; //清除错误标志
count_uartin=0; //接收数据地址清零
tongxun_time=0; //通许计时器清0,tongxun_time为1S计时器,到1S则b_tongxun=1;表示接收错误。
b_tongxun=0;
}
else b_sendero=1; //接收的数据出错
}
else //出错处理
{
if(!b_sendero)
datin_buf[count_uartin]=_txr_rxr; //接收的数据正常
else b_tongxun=_txr_rxr; //接收的数据出错,需读_txr_rxr清除_usr的错误标志
}
}
else
b_tongxun=_txr_rxr;// //接收的数据出错,需读_txr_rxr清除_usr的错误标志
}
//---------------------------------------------------
void send_out( ) //P_cs p_clk p_din p_dout zhu {
if(_txif)
{
if(++count_uartout>send_max)
{
count_uartout=0;
_tx8=1;
}
else _tx8=0;
_txr_rxr=datout_buf[count_uartout];
}
}
pro_100ms()
{ if(b_1000ms)
{
b_100ms=0;
if(++tongxun_time>15) b_tongxun=1;
}
}
void main()
{
datout_buf=0x5a;
_ucr1=0b11001001;
_ucr2=0b11101100;
_emi = 1; //开总中断
_brg=6;//开串口中断
_uarte=1;////开串口中断
while(1)
{
send_out( );
}
}。