📄 seriesio.c
字号:
//***************************************************************
// SimSeries.c
//
//功能:模拟串口程序,程序中接收时通过外中断1驱动接收,通过定时器2进行定时,
//采用T0口进行发送数据,两者都通过定时器2进行定时进行数据的位接收和发送
//
// By:sg 2004-8-13
//***************************************************************
#define Modul_SimSeries 1
#include "SimSeries.h" //包含的头文件
/*---------------------------------------------------------------
软件调用:
(1)发送的数据时候,调用void SimSBUFTx(byte SimSBUFTxData);
(2)接收时,直接通过外中断对接收程序的调用
---------------------------------------------------------------*/
//---------------------------------------------------------------
// 变量定义
//---------------------------------------------------------------
BYTE data ByRxValue; //接收的数据值
BYTE data ByTxValue; //发送的数据值
BYTE data ByTxBitCount; //发送的位数
BYTE data ByRxBitCount; //接受的位数
BOOL bComTxDataOn; //发送标志
BOOL bComRxDataOn; //接收标志
BOOL bTxOver; //发送完毕标志
//------------------------------
BYTE data SimRxDataBufRdPtr; // 模拟串口接收数据缓存读指针
BYTE data SimRxDataBufWrPtr; // 模拟串口接收数据缓存写指针
BYTE pdata SimRxDataBuf[cnSimRxDataBufLen]; // 模拟串口接收数据缓存
//***************************************************************
// void SeriesIOInit(void)
//
// Func: 模拟串口初始化
//***************************************************************
void SeriesIOInit(void)
{
bComTxDataOn = FALSE; //发送标志
bComRxDataOn = FALSE; //接收标志
bTxOver = FALSE; //发送完毕标志
ByRxValue = 0x00; //接收的数据值
ByTxValue = 0x00; //发送的数据值
ByTxBitCount = 0x00; //发送的位数
ByRxBitCount = 0x00; //接受的位数
SimRxDataBufRdPtr = 0x00;
SimRxDataBufWrPtr = 0x00;
ResetATcmdRx();
T2CON = 0x00;
T2MOD = 0x00;
PT2 = TRUE; //timer2的中断优先级最高
IT1 = TRUE; //采用边缘触发方式
EX1 = TRUE; //外中断允许
EA = TRUE; //中断总允许
}
//***************************************************************
//void SimSBUFTx(void)
//
// 功能:接收使能,对定时器进行初始化
//IN : SimSBUFTxDataIN --> 需要从T0口发送的单字节数据
//***************************************************************
void SimTx1bitDelay(void)
{
BYTE i;
for(i = 0; i < 35 ; i++)
{
_nop_();
_nop_();
}
}
//------------------------------
void SimSBUFTx(BYTE SimSBUFTxData)
{
BYTE i;
while(bComRxDataOn); // 正在接收数据时,暂停发送
TR0 = FALSE;
TR2 = FALSE;
EX0 = FALSE;
EX1 = FALSE;
boData = FALSE; // 发送起始位
SimTx1bitDelay();
for(i = 8; i > 0; i--)
{ // 发送8位数据
boData = (BOOL)(SimSBUFTxData & 0x01);
SimSBUFTxData >>= 1;
SimTx1bitDelay();
}
boData = TRUE; // 发送结束位
SimTx1bitDelay();
SoftWDTimer = 0x00;
ResetSysWDT();
}
//***************************************************************
//void Timer2TxOneBitInit()
//
//功能:发送时 对T2进行1位初始化,用到公式:(2EXP16-X)*12/(22.1184E6)=1/9600
//***************************************************************
void Timer2TxOneBitInit(void)
{
TL2 = RCAP2L = 0x70; //0xD0;
TH2 = RCAP2H = 0xFF;
TR2 = TRUE;
ET2 = TRUE;
}
//***************************************************************
//void Timer2RxOneBitInit()
//
//功能:接收时 对T2进行一位长初始化,用到公式:(2EXP16-X)*12/(22.1184E6)=1/9600
//***************************************************************
// ____ __ __ __ __ ____
// | | | | | | | | | |
// |0 |1 |2 |3 |4 |5 |6 |7 |8 |9
// |__| |__| |__| |__| |__|
//
void Timer2RxOneBitInit(void)
{
TL2 = RCAP2L = 0x6F; //60; //0xDE;
TH2 = RCAP2H = 0xFF;
TR2 = TRUE;
ET2 = TRUE;
}
//***************************************************************
//void Rxint1Service(void)
//
// 功能:外中断1的服务程序,先关中断,然后调用接收使能
//***************************************************************
#define cnRx1d5Bit 0xFF2A
//-------------------
void Rxint1Service(void) interrupt 2 using 1
{
BYTE ByRxWaitLowTimer;
EX1 = FALSE; //关外中断1
TR2 = FALSE;
ET2 = FALSE;
ByRxValue = 0x00;
ByRxBitCount = 0x00;
bComRxDataOn = TRUE;
TL2 = RCAP2L = (BYTE)cnRx1d5Bit; //0x2A; //0x96; // 等待1.5 bit的定时器初始值
TH2 = RCAP2H = (BYTE)(cnRx1d5Bit >> 8); //0xFF; //
ByRxWaitLowTimer = 0x00;
while(biData)
{
if(++ByRxWaitLowTimer > 150)
{
bComRxDataOn = FALSE;
EX1 = TRUE; //开中断1
return;
}
}
TR2 = TRUE;
ET2 = TRUE;
}
//***************************************************************
//void Timer2InterService()
//
//note : 在中断程序中,完成对数据的接收和数据的发送
//***************************************************************
void Timer2InterService() interrupt TF2_VECTOR using 1
{
BYTE ByRxValueBuf;
TF2 = FALSE;
TR2 = FALSE;
if(bComRxDataOn) //接收中断标志位"1",开始接收
{
++ByRxBitCount;
if(ByRxBitCount <= 8)
{
if(biData) //如果读入的是1,使接收值加上0X80
{
ByRxValue |= 0x80;
}
if(ByRxBitCount < 8) //最多移动7位
{
ByRxValue >>= 1; //接收完右移一位
}
Timer2RxOneBitInit(); //等待下一位长
}
else //对停止位进行检测
{
ByRxValueBuf = ByRxValue;
bComRxDataOn = FALSE;
ByRxBitCount = 0x00;
ByRxValue = 0x00;
EX1 = TRUE;
if(!biData) //停止位为1,出中断,接收完毕标志为1.
{ // 停止位为0,接收数据错误
TL2 = RCAP2L = (BYTE)cnRx1d5Bit; //0x2A; //0x96; // 等待1.5 bit的定时器初始值
TH2 = RCAP2H = (BYTE)(cnRx1d5Bit >> 8); //0xFF; //
TR2 = TRUE;
}
if(ByRxValueBuf == '>')
{ // 发送短消息体标志
bSendSMSContent = TRUE;
}
else
{
if((++SimRxDataBufWrPtr) >= cnSimRxDataBufLen) SimRxDataBufWrPtr = 0x00;
SimRxDataBuf[SimRxDataBufWrPtr] = ByRxValueBuf;
}
}
}
if(bComTxDataOn) //发送中断
{
++ByTxBitCount;
if(ByTxBitCount <= 8) //从D0开始发送,发送8位数据位
{
boData = (BOOL)(ByTxValue & 0x01);
ByTxValue >>= 1;
Timer2TxOneBitInit();
}
else
{
if(ByTxBitCount == 9) //发送完8位,发停止位
{
boData = TRUE;
Timer2TxOneBitInit();
}
else //停止位发送完,清发送完毕标志,请发送标志
{
bTxOver = TRUE;
ByTxBitCount = 0x00;
bComTxDataOn = FALSE;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -