最近在调试ADE5169串口程序,总是出现无法接收数据情况,附上程序,有懂的帮忙看看,谢谢!
#include "ADE5xxx.h"
#include "uart.h"
#define INBUF_LEN 9 //数据长度
unsigned char inbuf[5];
bit read_flag= 0 ;
void init_serialcomm( void )
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
SBAUDT = 0x01; //115200,n,8,1
SBAUDF = 0x87;
TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
IE |= 0x90 ; //Enable Serial Interrupt
PCON |= 0x80 ; //SMOD=1;
// TH1 = 0xFD ; //定时器值
// TH0 = 0xFD;
// TR1 = 1 ; // timer 1 run
REN=1;
}
//向串口发送一个字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 );
TI= 0 ;
}
//向串口发送一个字符串,strlen为该字符串长度
void send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
}
while (k < strlen);
}
unsigned char getchar(void)
{
unsigned char dataout;
if(RI)
RI=0;
dataout = SBUF;
return dataout;
}
void getchars(void)
{
unsigned char i=0;
for(i=0;i<5;i++)
{
inbuf[i]=getchar();
}
}
void serial (void)
{
unsigned char count=0;
unsigned char ch;
if(RI)
{
ch=SBUF;
RI = 0;
{
if(count <5)
inbuf[count]=ch;
else if(count ==5)
{
read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错,
count = 0;
}
count++;
}
}
}
一周热门 更多>