C8051F310串口通讯问题
时间:10-02
整理:3721RD
点击:
下面程序通讯不正确,发送和接收的数据不匹配,望各路大侠帮我找出问题,在下感激不尽。
#include <c8051f310.h>
#include <systeminit_manager.h>
sfr16 TMR3RL = 0x92; //定时器3重装载寄存器
sfr16 TMR3 = 0x94; //定时器3计数器
#define uchar unsigned char
#define uint unsigned int
#define BAUDRATE 9600 //波特率bps
#define CLKOUT 11059200 //外部晶振,修改也要修改OSCXCN
#define SMODVAL 0 //SMOD的值,修改请也修改PCONVAL
#define PCONVAL 0x00 //PCON的值,=0x00时SMOD0=0; =0x80时SMOD0=1
#define TXVAL (256-CLKOUT*(SMODVAL+1)/BAUDRATE/384) //定时器初值
bit readFlag = 0; //读标志
uchar readCounts = 0; //已经读取的字符个数,与MAX_LEN比较
uchar trdata[6]="0e0f0f"; //要接收/发送的字符串
uchar high;
uchar low;
void UART0_Init(void); //串口UART0初始化
void Send_Char(uchar ch); //发送单个字符
void Send_String(uchar * str, uint len); //发送一个字符串
void UART0_ISR(); //串口中断服务程序,接收字符
main()
{
C8051F310DeviceInit();
UART0_Init();
EA = 1;
while(1)
{
if(readFlag) //已经读取
{
readFlag = 0; //清零
Send_String(trdata,6); //发送字符串
}
}
}
void UART0_Init(void) //串口初始化
{
SCON0 = 0x50; //选择串口方式1,波特率可变 SCON0=0101,0000
TMOD = 0x20; //选择T1,方式2,自动再装入8位计数器
TH1 = (int)TXVAL;//T1初值,根据波特率,时钟等计算. 0xF4, bps=4800bps
TL1 = (int)TXVAL;
ES0 = 1; //UART0中断开启
TR1 = 1; //启动定时器T1
PCON |= PCONVAL; //PCON=0x00,SMOD = 0 PCON=0x80,SMOD=1
TI0 = 1; //声明TX0就绪,可以发送
TR0 = 1;
}
void Send_Char(uchar ch) //发送单个字符
{
SBUF0 = ch; //送入缓冲区
while(TI0 == 0); //等待发送完毕
TI0 = 0; //软件清零
}
void Send_String(uchar * str,uint len) //发送字符串,调用Send_Char() len字符串长度
{
uint k = 0;
do
{
Send_Char(*(str + k));
k++;
}
while(k < len);
}
void UART0_ISR(void) interrupt 4 using 1 //UART0中断服务程序. 接收字符
{
uchar rxch;
if(RI0) //中断标志 RI0=1 数据完整接收
{
RI0 = 0; //软件清零
rxch = SBUF0; //读缓冲
if(readCounts>=6)
{
readCounts = 0;
readFlag = 1;
}
trdata[readCounts] = rxch; //存入数组,供发送
readCounts++;
}
}
#include <c8051f310.h>
#include <systeminit_manager.h>
static void WatchDogInit (void)
{
PCA0MD &= ~0x40;
}
static void SysClkInit (void)
{
unsigned int i;
OSCXCN = 0x67; //采用外部晶振11.0592MHz,不分频. 选型OSCXCN=0110,0111
for(i=0;i<256;i++); //等待>1ms
while(!(OSCXCN&0x80)); //查询直到XTLVLD=1,晶振稳定
OSCICN = 0x88; //切换到外部振荡器,允许时钟失效监测器. OSCICN=1000,1000
}
static void PortInit (void)
{
XBR0 = 0x05; //允许UART0,RX,TX连到2个端口引脚. XBR0=0000,0101
XBR1 = 0x40; //允许交叉开关和弱上电
P0MDIN |= 0x30; //P0.4/5不配置为模拟输入
P0MDOUT |= 0x30; //P0.4/5为推拉方式输出,即TX0,RX0所在的端口
P0SKIP = 0x0c;
P1MDIN=0xff;
P1MDOUT=0xff;
P2MDIN=0xff;
P2MDOUT=0xff;
}
void C8051F310DeviceInit (void)
{
WatchDogInit();
SysClkInit();
PortInit();
}
#include <c8051f310.h>
#include <systeminit_manager.h>
sfr16 TMR3RL = 0x92; //定时器3重装载寄存器
sfr16 TMR3 = 0x94; //定时器3计数器
#define uchar unsigned char
#define uint unsigned int
#define BAUDRATE 9600 //波特率bps
#define CLKOUT 11059200 //外部晶振,修改也要修改OSCXCN
#define SMODVAL 0 //SMOD的值,修改请也修改PCONVAL
#define PCONVAL 0x00 //PCON的值,=0x00时SMOD0=0; =0x80时SMOD0=1
#define TXVAL (256-CLKOUT*(SMODVAL+1)/BAUDRATE/384) //定时器初值
bit readFlag = 0; //读标志
uchar readCounts = 0; //已经读取的字符个数,与MAX_LEN比较
uchar trdata[6]="0e0f0f"; //要接收/发送的字符串
uchar high;
uchar low;
void UART0_Init(void); //串口UART0初始化
void Send_Char(uchar ch); //发送单个字符
void Send_String(uchar * str, uint len); //发送一个字符串
void UART0_ISR(); //串口中断服务程序,接收字符
main()
{
C8051F310DeviceInit();
UART0_Init();
EA = 1;
while(1)
{
if(readFlag) //已经读取
{
readFlag = 0; //清零
Send_String(trdata,6); //发送字符串
}
}
}
void UART0_Init(void) //串口初始化
{
SCON0 = 0x50; //选择串口方式1,波特率可变 SCON0=0101,0000
TMOD = 0x20; //选择T1,方式2,自动再装入8位计数器
TH1 = (int)TXVAL;//T1初值,根据波特率,时钟等计算. 0xF4, bps=4800bps
TL1 = (int)TXVAL;
ES0 = 1; //UART0中断开启
TR1 = 1; //启动定时器T1
PCON |= PCONVAL; //PCON=0x00,SMOD = 0 PCON=0x80,SMOD=1
TI0 = 1; //声明TX0就绪,可以发送
TR0 = 1;
}
void Send_Char(uchar ch) //发送单个字符
{
SBUF0 = ch; //送入缓冲区
while(TI0 == 0); //等待发送完毕
TI0 = 0; //软件清零
}
void Send_String(uchar * str,uint len) //发送字符串,调用Send_Char() len字符串长度
{
uint k = 0;
do
{
Send_Char(*(str + k));
k++;
}
while(k < len);
}
void UART0_ISR(void) interrupt 4 using 1 //UART0中断服务程序. 接收字符
{
uchar rxch;
if(RI0) //中断标志 RI0=1 数据完整接收
{
RI0 = 0; //软件清零
rxch = SBUF0; //读缓冲
if(readCounts>=6)
{
readCounts = 0;
readFlag = 1;
}
trdata[readCounts] = rxch; //存入数组,供发送
readCounts++;
}
}
#include <c8051f310.h>
#include <systeminit_manager.h>
static void WatchDogInit (void)
{
PCA0MD &= ~0x40;
}
static void SysClkInit (void)
{
unsigned int i;
OSCXCN = 0x67; //采用外部晶振11.0592MHz,不分频. 选型OSCXCN=0110,0111
for(i=0;i<256;i++); //等待>1ms
while(!(OSCXCN&0x80)); //查询直到XTLVLD=1,晶振稳定
OSCICN = 0x88; //切换到外部振荡器,允许时钟失效监测器. OSCICN=1000,1000
}
static void PortInit (void)
{
XBR0 = 0x05; //允许UART0,RX,TX连到2个端口引脚. XBR0=0000,0101
XBR1 = 0x40; //允许交叉开关和弱上电
P0MDIN |= 0x30; //P0.4/5不配置为模拟输入
P0MDOUT |= 0x30; //P0.4/5为推拉方式输出,即TX0,RX0所在的端口
P0SKIP = 0x0c;
P1MDIN=0xff;
P1MDOUT=0xff;
P2MDIN=0xff;
P2MDOUT=0xff;
}
void C8051F310DeviceInit (void)
{
WatchDogInit();
SysClkInit();
PortInit();
}
到底是什么问题呢?