|
以下是我写的一个用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 &= 7F;//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
}
|
|