找回密码
 注册会员
img_loading
智能检测中
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

AT89C51AC2的串口初始化和中断问题

[复制链接]
admin 发表于 2012-9-3 02:44:35 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
以下是我写的一个用timer2做串口波特率触发器的程序。
与PC通讯,接收回来的数据和发送的不一样,而且有的时候接收到的数据是错误的
好象根本没执行中断发送部分。
struct SEND_STRUCT{
unsigned char data_idx;
unsigned char data_buf[10];
}SEND;
struct RECEIVE_STRUCT{
unsigned char data_idx;
unsigned char data_buf[10];
}RECEIVE;
/************************************************************/
void serial_init(void)
{
PCON &= 0x7F;//no double baud rate, SMOD1=0 of PCON
SCON = 0x50;//8-bit UART, mode 1
T2CON &= 0xF0;//EXEN2=0, TR2=0, C/T2=0; CP/RL2=0
T2CON ¦= 0x30;//RCLK=1, TCKL=1
TH2 = 0xFF;
TL2 = 0xDC;//[init value]=2^n-(Fosc/(baud*16*2)), Fosc=11.0592MHz, baud=9600
RCAP2H = 0xFF;
RCAP2L = 0xDC;//reload baud 9600
REN = 1;//enable receive
TX_EN = 1;//disenable send

TR2 = 1;/startup T2
ES = 1;//serial isr enable
}
/************************************************************/
void uart_isr(void) interrupt 4
{
unsigned char rec_data;

if(_testbit_(TI))
{
if(SEND.data_index <=9)//send all data
{
SUBF = SEND.data_buf[SEND.data_idx];
++SEND.data_idx;
}
else if(SEND.data_idx> 9)
{
TI = 0;
SEND.data_idx = 0;
TX_EN = 1;//disenable send
REN = 1;//enable receive
}
}//send end
else if(_testbit_(RI))
{
rec_data = SBUF;//read serial data buf

if(RECEIVE.data_idx==0)
{
if(rec_data==x0FF)//communication with PC, the first data is 0xFF
{
RECEIVE.data_buf[0] = rec_data;
RECEIVE.data_buf[9] ^= rec_data;//checksum=data_buf[0]^data_buf[1]^...^data_buf[8]
//&0x7F
RECEIVE.data_idx = 1;
}
else
{
RECEIVE.data_idx = 0;
}
}
else if(RECEIVE.data_idx)
{
if(RECEIVE.data_idx <9)//receive 1~8 data and checksum=data_buf[0]^data_buf[1]^...
//^data_buf[8]&0x7F
{
RECEIVE.data_buf[RECEIVE.data_idx] = rec_data;
RECEIVE.data_buf[9] ^= rec_data;
++RECEIVE.data_idx;
}
else if((RECEIVE.data_idx==9) && ((RECEIVE.data_buf[9])&0x7F)==rec_data))
{
REN = 0;//disenable receive
TX_EN = 0;//enable send
RI = 0;
}
else
{
RECEIVE.data_idx = 0;
RI = 0;
}
}
}//receive end
}
您需要登录后才可以回帖 登录 | 注册会员

*滑块验证:
img_loading
智能检测中
本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2025-6-9 15:29 , Processed in 0.058780 second(s), 11 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表