/**********************************************************************
* 文件名: serial.c *
* 功能:串口上相关功能的实现 *
**********************************************************************/
#define NEED_SERIAL_PROTOCOL
#ifdef NEED_SERIAL_PROTOCOL
/************************************************
*Header file segment *
***********************************************/
#include "at89x52.h"
#include "SERIAL.h"
#include "DATATYPE.h"
/************************************************
*Globale variable segment *
***********************************************/
uchar receive_number;
uchar receive_bool = FALSE; //判断是否有接收到数据
/************************************
* 名称:void DeviceIniSerial(uchar rcap2h, uchar rcap2l)
* 功能:设置串口工作方式1, T/C2波特率由rcap2h,rcap2l决定
(当rcap2h = 0xff, rcap2l = 0xb8时,波特率为:9600)
* 入口参数:uchar rcap2h, uchar rcap2l
* 返回值: 无
************************************/
void DeviceIniSerial(uchar rcap2h, uchar rcap2l)
{
RCLK = 1; //由T/C2的溢出脉冲形成串行口的 接收 脉冲
TCLK = 1; //由T/C2的溢出脉冲形成串行口的 发送 脉冲
RCAP2H = rcap2h; //设置T/C2的初置,即设置波特率
RCAP2L = rcap2l;
REN = 1; //允许接收
SM0 = 0; //设置为串行口方式1:10位异步收发,波特率由定时器控制
SM1 = 1;
TR2 = 1; //T/C2运行工作
}
/***********************************
* 名称:Send(uchar send_number)
* 功能:发送send_number
* 入口参数:uchar send_number
* 返回值: 无
************************************/
void ComSend(uchar send_number)
{
SBUF = send_number; //把要发送的数据放入SBUF中
while(TI == 0); //当发送完一个数据后,TI由硬件置位,跳出循环,结束等待
TI = 0; //软件复位,以便发送下一个数据
}
/***********************************
* 函数名:SerialInterrupt(void) (串口中断服务程序).
* 功能: 主要实现接收数据
* 入口参数:无
* 返回值:无
************************************/
void SerialInterrupt(void) interrupt 4 //当发送或接收完一个数据的时候,都会产生串行口中断,
//但此中断服务程序主要是作用于 接收数据
{
if(RI == 1)
{
receive_number = SBUF;
RI = 0;
receive_bool = 1;
}
}
#endif
/******************************************************************************
* End of Entire File *
*****************************************************************************/