📄 16c2552.c
字号:
#include "include.h"
/**************************************************************************************
* 变量原型:uchar data Recive_Data[20]
* 变量说明:接收数据缓冲区
**************************************************************************************/
uchar data Recive_Data[20];
/**************************************************************************************
* 函数原型:void Enable_16C2552( void )
* 函数功能:片选有效16C2552
* 输入参数:
* 输出参数:
* 函数说明:
**************************************************************************************/
void Enable_16C2552( void )
{
CS_162552 = 0;
}
/**************************************************************************************
* 函数原型:void Disable_16C2552( void )
* 函数功能:片选无效16C2552
* 输入参数:
* 输出参数:
* 函数说明:
**************************************************************************************/
void Disable_16C2552( void )
{
CS_162552 = 1;
}
/**************************************************************************************
* 函数名称:Initialization_16C2552
* 函数功能:16C550初始化函数
* 输入参数:Com->串口选择; bps->波特率; WordLength->数据位长度;
StopLength->停止位长度; Parity->校验选择
* 输出参数:无
* 函数说明:
**************************************************************************************/
uchar Initialization_16C2552( uchar Com, uchar bps, uchar WordLength,
uchar StopLength, uchar Parity )
{uchar offset;
uchar bpstemp,bpsbuff[]={0x06,0x30,0x18,0x0c,0x06,0x03,0x02,0x01};
uchar LCR_Temp;
//设置初始化哪个串口
if(Com==COM1)
{
offset=0x08;
}
else if(Com==COM2)
{
offset=0x00;
}
else
{
return(COM_ERR);
}
//设置波特率
if(bps<7)
{
bpstemp=bpsbuff[bps];
}
else
{
return(BPS_ERR);
}
//设置数据位LCR[1]LCR[0]
LCR_Temp=0x00;
if(WordLength>4 && WordLength<9)
{
LCR_Temp |= WordLength-5;
}
else
{
return(WORDLENGTH_ERR);
}
//设置停止位LCR[2]
if(StopLength==2 || StopLength==3)
{
LCR_Temp |= 0x04;
}
else if(StopLength!=1)
{
return(STOPLENGTH_ERR);
}
//设置校验选择LCR[5]LCR[4]LCR[3]
if(Parity==Force_0)
{
LCR_Temp |= 0x38;
}
else if(Parity==Force_1)
{
LCR_Temp |= 0x28;
}
else if(Parity==NO_Parity)
{
LCR_Temp |= 0x00;
}
else if(Parity==ODD_Parity)
{
LCR_Temp |= 0x08;
}
else if(Parity==EVEN_Parity)
{
LCR_Temp |= 0x18;
}
else
{
return(PARITY_ERR);
}
Enable_16C2552();//片选有效
//接收缓冲区触发电平为1个字节 位6,7
//清空发送器和接收器 位1,2
//发送器和接收器有效 位0
Writedata_Short( FCR162552_Address|offset, 0x03 );
//为设置波特率而置位最高位 位7
//8位数据 位0,1
Writedata_Short( LCR162552_Address|offset, 0x83 );
//9600波特率
Writedata_Short( DLM162552_Address|offset, 0x00 );
Writedata_Short( DLL162552_Address|offset, bpstemp );
//清除设置波特率而置位最高位 位7
//8位数据 位0,1
//1位停止位 位2
//奇偶校验使能位 位3
//奇偶校验选择位 位4
//奇偶校验位是否为固定值保持位 位5
Writedata_Short( LCR162552_Address|offset, LCR_Temp );
//清除中断使能寄存器,采用查询方式
Writedata_Short( IER162552_Address|offset, 0x00 );
Disable_16C2552();//片选无效
}
/**************************************************************************************
* 函数原型:uchar Read_16C2552( uchar Com, uchar *data receive_address )
* 函数功能:读取16C2552的接收数据
* 输入参数:Com->串口选择;
* 输出参数:读取成功,数据保存在*receive_address指向的地址,以及本次接收数据的数量
* 函数说明:读取线状态寄存器,根据线状态寄存器的0位判断是否有数据到达,当读取数据后,该位
自动清0
**************************************************************************************/
uchar Read_16C2552( uchar Com, uchar *receive_address )
{uchar data length;
uchar data state;
uchar data offset;
length = 0;
Enable_16C2552();//片选有效
if(Com==COM1)
{
offset=0x08;
}
else //if(Com==COM2)
{
offset=0x00;
}
while(1)
{ //读取线状态寄存器,根据线状态寄存器决定是否读取接收器
state = Readdata_Short( LSR162552_Address|offset );
if( (state & 0x01)==1 )
{
*receive_address = Readdata_Short( RHR162552_Address|offset );
length++;
//如果奇偶校验出错,保留出错的地址以便利用
//if ( state & 0x04 )
// *command_position = length;
receive_address++;
}
else
{
break;
}
}
Disable_16C2552();//片选无效
return ( length );//返回状态
}
/**************************************************************************************
函数原型:uchar Write_16C2552( uchar Com, uchar* send_address, uchar data_length )
函数功能:利用16C2552的发送数据
输入参数:Com->串口选择; 数据保存在*send_address指向的地址,数据长度保存在data_length中
输出参数:发送成功返回标志0,发送失败返回标志1
函数说明:进行了超时中断处理,如果波特率设置过低,可以加大超时等待时间的初始值
读取线状态寄存器,根据线状态寄存器的5位判断是否可以发送数据,如果定时范围内无法发送
数据,则中断返回
**************************************************************************************/
uchar Write_16C2552( uchar Com, uchar* send_address, uchar data_length )
{uchar state = 0;
uchar waittime;
uchar offset;
Enable_16C2552();//片选有效
if(Com==COM1)
{
offset=0x08;
}
else //if(Com==COM2)
{
offset=0x00;
}
//读取线状态寄存器,根据线状态寄存器决定是否能够发送数据
waittime = 255;
while ( ( ( Readdata_Short( LSR162552_Address|offset ) & 0x20 ) == 0 ) &&
( waittime != 0 ) )
{
waittime--;
}
//修改奇偶数据固定位
//Writedata_Short( LCR162552_Address, 0x2B );
//发送命令头
if ( data_length > 0 )
{
Writedata_Short( THR162552_Address|offset, *send_address );
send_address++;
data_length--;
}
//读取线状态寄存器,根据线状态寄存器决定是否能够发送数据
waittime = 255;
while ( ( ( Readdata_Short( LSR162552_Address|offset ) & 0x40 ) == 0 ) &&( waittime != 0 ) )
{
waittime--;
}
//Delay( 10 );
//修改奇偶数据固定位
// Writedata_Short( LCR162552_Address, 0x3B );
//发送器空,可以发送数据
if ( ( Readdata_Short( LSR162552_Address|offset ) & 0x40 ) != 0 )
{
while ( data_length > 0 )
{
Writedata_Short( THR162552_Address|offset, *send_address );
send_address++;
data_length--;
waittime = 255;
while ( ( ( Readdata_Short( LSR162552_Address|offset ) & 0x40 ) == 0 ) && ( waittime != 0 ) )
{
waittime--;
}
if ( ( Readdata_Short( LSR162552_Address|offset ) & 0x40 ) != 0 )
{
continue;
}
else
{
break;
state = 1;
}
}
}
else
{
state = 1;
}
//Delay(10);
Disable_16C2552(); //片选无效
return ( state );//返回状态
}
/**************************************************************************************
函数原型:void main(void)
函数功能:将从串口收到的数据,再通过串口发送出去
输入参数:
输出参数:
函数说明:
**************************************************************************************/
void main(void)
{uchar data length;
uchar data com;
com=COM1;
//Initialization_16C2552( COM2, B9600bps, 8, 1, NO_Parity );
Initialization_16C2552( com, B19200bps, 8, 1, NO_Parity );
while(1)
{
length=Read_16C2552(com, Recive_Data);
if(length!=0)
{
Write_16C2552(com, Recive_Data,length);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -