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
}
//--------------------------------------------------------------------------------
查看全文
相关阅读:
《剑指offer》第三十五题(复杂链表的复制)
《剑指offer》第三十四题(二叉树中和为某一值的路径)
分支限界法
回溯法小实例
BUFSIZ解析
ofstream和ifstream详细用法
回溯法——最大团问题(Maximum Clique Problem, MCP)
位运算应用之二——大小写转换
回溯法——n后问题
回溯法——批处理作业调度
原文地址:https://www.cnblogs.com/zym0805/p/5089359.html
最新文章
InetAddress(互联网协议ip地址)
一级菜单 二级菜单的联动
c++ 迭代器库
127 Word Ladder
162 Find Peak Element
154 Find Minimum in Rotated Sorted Array II
153 Find Minimum in Rotated Sorted Array
81 Search in Rotated Sorted Array II
33 Search in Rotated Sorted Array
设计模式 uml元素
热门文章
笔记-线程安全的生命期管理
来自知乎的pthread_cond_wait为什么总是带着mutex
《剑指offer》第四十一题(数据流中的中位数)
《剑指offer》第四十题(最小的k个数)
《剑指offer》第三十九题(数组中出现次数超过一半的数字)
排序算法总结
查找算法总结
《剑指offer》第三十八题(字符串的排列)
《剑指offer》第三十七题(序列化二叉树)
《剑指offer》第三十六题(二叉搜索树与双向链表)
Copyright © 2011-2022 走看看