zoukankan
html css js c++ java
16V554 的测试代码
//------------------------------------------------------------------------
#include "AT16C554.H"
//--------------------------------------------------------------------------------------------------
code Uint PORTAddrTABLE[PortNumber]={STARTADDR_SERIAL1,
STARTADDR_SERIAL2,
STARTADDR_SERIAL3,
STARTADDR_SERIAL4};
//串口端口地址映射表
xdata TCommHandle CommDevice[PortNumber];
sbit reset_16c554 = P1^4;
//ST16C554 复位控制线
//---------------------------------------------------------------------------------------------------
// Define Private Faction
//-----------------------------------------------------------------------------------------------------
bool ReadPortRecBuf(uchar Port,Uchar *Byte);
bool WritePortSendBuf(uchar Port,Uchar Byte);
void Port_open(Uchar PortID,Uint Baud);
//-------------------------------------------------------------------------
// Define Public Faction
//-------------------------------------------------------------------------
bool Init_16c554(
void);
void Port_open(Uchar PortID,Uint Baud);
//void Port_open(Uchar PortID,int Baud,Uchar DataBitNum,Uchar Parit,Uchar Stop);
void Port_close(Uchar PortID);
int Port_Receive(Uchar PortID,
void *Buffer,
int Count);
int Port_Send(Uchar PortID,
void *Buffer,
int Count);
//---------------------------------------------------------------------------------------------
// Public Faction Code
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// 初始化ST16C554
//---------------------------------------------------------------------------------------------
bool Init_16c554(
void)
{
uchar xdata *ptr;
uchar i=Init_16c554_Count;
bool Result=
false;
while(--i)
// 复位10次不成功,则返回出错
{
_nop_();
reset_16c554 = FREE_RESET;
ms_delay(5);
reset_16c554 = RESET;
// 复位16C554
ms_delay(10);
// 延时1毫秒
reset_16c554 = FREE_RESET;
ms_delay(10);
ptr = STARTADDR_SERIAL1+SPR;
// 通道1临时寄存器
*ptr = 0xaa;
// 写测试值到临时寄存器
_nop_();
_nop_();
if(*ptr == 0xaa)
{
Result=
true;
break;
};
//end if
};
//end while
for(i=0;i<PortNumber;i++) CommDevice[i].RAMAddress_Map=PORTAddrTABLE[i];
return Result;
}
//---------------------------------------------------------------------------------------------
// 打开串口
//---------------------------------------------------------------------------------------------
void Port_open(Uchar PortID,Uint Baud)
{
uchar xdata *ptr;
ptr = CommDevice[PortID].RAMAddress_Map;
// 取端口的基地址
*(ptr+LCR) = LCR_DIV_EN;
// 允许发送波特率
*(ptr+DLM) = (uchar)(Baud>>8);
*(ptr+DLL) = (uchar)Baud;
// 设置默任波特率为19200
*(ptr+LCR) = LCR_WORD_LEN8|LCR_STOP_LEN1;
// 设置字长为8,1个停止位,无校验位
*(ptr+FCR) = FIFO_EN|FIFO_TRI_LEVEL_1|FIFO_CLEAR_RCVR|FIFO_CLEAR_XMIT|FIFO_DMA_MODE;
// 设置FIFO 控制寄存器
*(ptr+IER) = IER_TRANSMITHOLD_INT|IER_RECEIVEHOLD_INT;
// 设置中断屏蔽寄存器
*(ptr+MCR) = MCR_INTX_EN;
// 允许中断引脚输出
if(PortID==0) EX0 =
true;
// 开外部中断0(通道1中断)
if(PortID==1) EX1 =
true;
// 开外部中断1(通道2中断)
if(PortID==2) EX2 =
true;
// 开外部中断2(通道3中断)
if(PortID==3) EX3 =
true;
// 开外部中断3(通道4中断)
CommDevice[PortID].CommReceive_Head=0;
CommDevice[PortID].CommReceive_Trail=0;
CommDevice[PortID].CommSend_Head=0;
CommDevice[PortID].CommSend_Trail=0;
CommDevice[PortID].Comm_Stu=SERIAL_STU_FREE;
}
//---------------------------------------------------------------------------------------------
// 关闭指定的端口
//---------------------------------------------------------------------------------------------
/*void Port_close(Uchar PortID)
{
uchar xdata *ptr;
ptr = CommDevice[PortID].RAMAddress_Map; // 取端口的基地址
*(ptr+LCR) = LCR_DIV_Dis; // 禁止发送波特率
*(ptr+MCR) = MCR_INTX_Dis; // 允许中断引脚输出
CommDevice[PortID].CommReceive_Head=0;
CommDevice[PortID].CommReceive_Trail=0;
CommDevice[PortID].CommSend_Head=0;
CommDevice[PortID].CommSend_Trail=0;
CommDevice[PortID].Comm_Stu=SERIAL_STU_FREE;
if(PortID==0) EX0 = false; // 关外部中断0(通道1中断)
if(PortID==1) EX1 = false; // 关外部中断1(通道2中断)
if(PortID==2) EX2 = false; // 关外部中断2(通道3中断)
if(PortID==3) EX3 = false; // 关外部中断3(通道4中断)
}*/
//--------------------------------------------------------------------------------------------------
//功 能:从串口中接收数据
//返回值:从串口中接收到的数据个数
//参 数:
// Buffer:接收数的缓冲区
// Count: 想从串口缓冲区中接收数据个数
//--------------------------------------------------------------------------------------------------
/*int Port_Receive(uchar Port,register void *Buffer,int Count)
{
Uchar Size=0;
if(Port>PortNumber-1) return Size;
while((Size<Count)&&(ReadPortRecBuf(Port,Buffer)))
{
Size++;
Buffer=Buffer+1;
};//end while
return Size;
}*/
//--------------------------------------------------------------------------------------------------
//功 能:往串口发送数据
//返回值:已发送的数据个数
//参 数:
// Buffer:发送数据的缓冲区
// Count: 想从串口发送数据的个数
//--------------------------------------------------------------------------------------------------
int Port_Send(uchar Port,
register
void *Buffer,
int Count)
{
uchar i=0;
Uchar Size=0;
uchar xdata *ptr;
if(Port>PortNumber-1)
return Size;
ptr = CommDevice[Port].RAMAddress_Map;
// 取端口基地址
while(Size<Count)
{
if(WritePortSendBuf(Port,*((Uchar *)Buffer)))
{
Size=Size+1;
Buffer=Buffer+1;
};
//end if
if(CommDevice[Port].Comm_Stu==SERIAL_STU_FREE)
{
for(i=0;(i<FIFO_SIZE)&&(CommDevice[Port].CommSend_Head!=CommDevice[Port].CommSend_Trail);i++)
{
CommDevice[Port].CommSend_Trail=(CommDevice[Port].CommSend_Trail+1)%CommMaxSendBuffer;
*(ptr+THR) = CommDevice[Port].CommSendBuffer[CommDevice[Port].CommSend_Trail];
// 发送数据
};
//end for
CommDevice[Port].Comm_Stu = SERIAL_STU_BUSY;
};
//end if
};
//end while
return Size;
}
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//私有函数
//----------------------------------------------------------------------------------------------------
//功 能:从串口接收缓冲区中取得数据
//返回值:
// true: 从缓冲区中成功取得数据
// false: 从缓冲区中无法取得数据
//参 数: *Byte:从缓冲区中取得数据返回
//-----------------------------------------------------------------------------------------------------
/*bool ReadPortRecBuf(uchar Port,Uchar *Byte)
{
if(CommDevice[Port].CommReceive_Trail==CommDevice[Port].CommReceive_Head) return false;
*Byte=CommDevice[Port].CommReceiveBuffer[CommDevice[Port].CommReceive_Trail];
CommDevice[Port].CommReceive_Trail=(CommDevice[Port].CommReceive_Trail+1)%CommMaxReceiveBuffer;
return true;
}*/
//-----------------------------------------------------------------------------------------------------
//功 能:往串口发送缓冲区中写数据
//返回值:
// true: 往缓冲区中写数据成功
// false: 从缓冲区中写数据失败
//参 数: Byte: 往缓冲区中写入的数据
//------------------------------------------------------------------------------
bool WritePortSendBuf(uchar Port,Uchar Byte)
{
if((CommDevice[Port].CommSend_Head+1)%CommMaxSendBuffer==CommDevice[Port].CommSend_Trail)
return
false;
CommDevice[Port].CommSend_Head=(CommDevice[Port].CommSend_Head+1)%CommMaxSendBuffer;
CommDevice[Port].CommSendBuffer[CommDevice[Port].CommSend_Head]=Byte;
return
true;
}
//--------------------------------------------------------------------------------
// 外部扩展串行口1中断处理程序(外部中断0)
//--------------------------------------------------------------------------------
void serial1_int() interrupt 0
{
uchar xdata *ptr;
uchar event;
uchar port;
port = CHANL1_PORT;
ptr = CommDevice[port].RAMAddress_Map;
// 读ISR 中断状态寄存器
event = *(ptr+ISR)&0x0f;
// 读最高优先级别的中断码
_nop_();
switch(event)
{
case EVENT_RXRDY:
// 接受数据到中断
case EVENT_RXRDY_TIMEOUT:
for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)
{
CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);
if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)
CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;
};
//end for
break;
case EVENT_TXRDY:
//发送数据中断
if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)
//判断数据包是否发送完毕
{
for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)
{
CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;
*(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];
};
//end for
CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;
}
else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;
break;
};
//end switch
}
//--------------------------------------------------------------------------------
// 外部扩展串行口2中断处理程序(外部中断2)
//--------------------------------------------------------------------------------
void serial2_int() interrupt 2
{
uchar xdata *ptr;
uchar event;
uchar port;
port = CHANL2_PORT;
ptr = CommDevice[port].RAMAddress_Map;
//读ISR 中断状态寄存器
event = *(ptr+ISR)&0x0f;
//读最高优先级别的中断码
_nop_();
switch(event)
{
case EVENT_RXRDY:
//接受数据到中断
case EVENT_RXRDY_TIMEOUT:
for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)
{
CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);
if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)
CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;
};
//end for
break;
case EVENT_TXRDY:
//发送数据中断
if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)
//判断数据包是否发送完毕
{
for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)
{
CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;
*(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];
};
//end for
CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;
}
else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;
break;
};
//end switch
}
//--------------------------------------------------------------------------------
// 外部扩展串行口3中断处理程序(外部中断6)
//--------------------------------------------------------------------------------
void serial3_int () interrupt 6
{
uchar xdata *ptr;
uchar event;
uchar port;
port = CHANL3_PORT;
ptr = CommDevice[port].RAMAddress_Map;
// 读ISR 中断状态寄存器
event = *(ptr+ISR)&0x0f;
// 读最高优先级别的中断码
_nop_();
switch(event)
{
case EVENT_RXRDY:
// 接受数据到中断
case EVENT_RXRDY_TIMEOUT:
for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)
{
CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);
if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)
CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;
};
//end for
break;
case EVENT_TXRDY:
//发送数据中断
if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)
//判断数据包是否发送完毕
{
for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)
{
CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;
*(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];
};
//end for
CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;
}
else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;
break;
};
//end switch
}
//--------------------------------------------------------------------------------
// 外部扩展串行口4中断处理程序(外部中断7)
//--------------------------------------------------------------------------------
void serial4_int () interrupt 7
{
uchar xdata *ptr;
uchar event;
uchar port;
port = CHANL4_PORT;
ptr = CommDevice[port].RAMAddress_Map;
// 读ISR 中断状态寄存器
event = *(ptr+ISR)&0x0f;
// 读最高优先级别的中断码
_nop_();
switch(event)
{
case EVENT_RXRDY:
// 接受数据到中断
case EVENT_RXRDY_TIMEOUT:
for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)
{
CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);
if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)
CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;
};
//end for
break;
case EVENT_TXRDY:
//发送数据中断
if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)
//判断数据包是否发送完毕
{
for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)
{
CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;
*(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];
};
//end for
CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;
}
else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;
break;
};
//end switch
}
//--------------------------------------------------------------------------------
查看全文
相关阅读:
Linux 桌面玩家指南:18. 使用 Docker 隔离自己的开发环境和部署环境
Linux 桌面玩家指南:17. 在 Ubuntu 中使用 deepin-wine,解决一些依赖 Windows 的痛点问题
Linux 桌面玩家指南:16. 使用 CUDA 发挥显卡的计算性能
Linux 桌面玩家指南:15. 深度学习可以这样玩
Linux 桌面玩家指南:14. 数值计算和符号计算
Linux 桌面玩家指南:13. 使用 Git 及其和 Eclipse 的集成
Linux 桌面玩家指南:12. 优秀的文本化编辑思想大碰撞(Markdown、LaTeX、MathJax)
Linux 桌面玩家指南:11. 在同一个硬盘上安装多个 Linux 发行版以及为 Linux 安装 Nvidia 显卡驱动
Linux 桌面玩家指南:10. 没有 GUI 的时候应该怎么玩
Linux 桌面玩家指南:09. X Window 的奥秘
原文地址:https://www.cnblogs.com/zym0805/p/5089359.html
最新文章
Python学习笔记(三)
Python学习笔记(二)
Python 学习笔记(一)
大漠插件使用
EF执行存储过程时超时问题
Android关于Theme.AppCompat相关问题的深入分析(转)
NetBPM的安装 -转
导出jqgrid表格数据为EXCEL文件,通过tableExport.js插件。
解决webdiyer:AspNetPager分页控件在IE模式下点击页号不能翻页的问题。
解决使用微软模拟器VS Emulator for Android在VS2017 Xamarin开发中不能调试程序的问题。
热门文章
如何在页面中根据输入的内容自动生成二维码
Winform中进行MD5加密
winform C#获得Mac地址,IP地址,子网掩码,默认网关
在Win8.1系统下如何安装运行SQL Server 2005 (以及安装SQL Server 2005 Express打补丁)
dsoframer控件学习小结(打开WORD,EXCEL等文件)
C# winForm 窗体闪烁问题
eclipse新建一个Android项目,就会报错android.support.v7.app.ActionBarActivity
在4K屏下以超过VMWare默认的最高分辨率运行Linux系统
Linux 桌面玩家指南:20. 把 Linux 系统装入移动硬盘打包带走
Linux 桌面玩家指南:19. 深入理解 JavaScript,及其开发调试工具
Copyright © 2011-2022 走看看