/****************************************2202**************************************************
---------------
** Modified by: songningshuai
** Modified date: 2012-07-28
** Version:
** Descriptions: UART0通讯实验,中断方式,使用FIFO。
**
********************************************************************************************************/
#include "config.h"
#include "UART.h"
#include "FrameProcess.h"
uint8 Uart_buf[UBL]; // UART0数据接收缓冲区
uint16 Uart_Position;
int Uart_Count; //接收到数据数
volatile uint8 rcv_new; // 接收新数据标志
/*
*********************************************************************************************************
** 函数名称 :IRQ_UART0()
** 函数功能 :串口0接收中断服务程序
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void __irq IRQ_UART0 (void)
{
uint8 i;
uint8 nState;
nState= U0IIR & 0x0F;
i = 0;
if (nState == 0x04)
{
rcv_new = 1; // 设置接收到新的数据标志
for (i=0; i<8; i++)
{
Uart_buf[Uart_Position] = U0RBR; // 读取FIFO的数据,并清除中断
Uart_Position = (Uart_Position+1)%UBL;
}
Uart_Count = Uart_Count+8;
}
else if (nState == 0x0c)
{
rcv_new = 1;
U0IER = U0IER & (~0x01);
while (U0LSR & 0x01)
{
Uart_buf[Uart_Position] = U0RBR;
Uart_Position = (Uart_Position+1)%UBL;
i++;
}
Uart_Count += i;
U0IER = 0x01;
}
VICVectAddr = 0x00; // 中断处理结束
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendByte()
** 函数功能 :向串口0发送1字节数据
** 入口参数 :dat 要发送的数据
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendByte (uint8 dat)
{
U0THR = dat; // 要发送的数据
while ((U0LSR & 0x40) == 0); // 等待数据发送完毕
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendBuf()
** 函数功能 :向串口发送8字节数据
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendBuf (void)
{
uint8 i;
for (i=0; i<Uart_Count; i++)
UART0_SendByte(Uart_buf[i]);
//UART0_SendByte(ReceiveData[i]);
while ((U0LSR & 0x20) == 0); // 等待数据发送完毕
}
/*
*********************************************************************************************************
** 函数名称 :UART0_Send(uint8 *sdata, int scount)
** 函数功能 :向串口发送一帧数据
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_Send(uint8 *sdata, int scount)
{
int i,j;
U0THR = *sdata;
sdata++;
U0THR = *sdata;
sdata++;
while ((U0LSR & 0x20) == 0);
for(i=0;i<((scount-4)/8);i++)
{
for (j=0;j<8;j++)
{
if (*sdata==FFLAG)
{
U0THR = *sdata;
}
U0THR = *sdata;
sdata++;
}
while ((U0LSR & 0x20) == 0); // 等待数据发送完毕
}
for(i=0;i<((scount-4)%8);i++)
{
if (*sdata==FFLAG)
{
U0THR = *sdata;
}
U0THR = *sdata;
sdata++;
}
while ((U0LSR & 0x20) == 0);
U0THR = *sdata;
sdata++;
U0THR = *sdata;
while ((U0LSR & 0x20) == 0);
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendStr()
** 函数功能 :向串口发送一字符串
** 入口参数 :str 要发送的字符串的指针
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendStr (uint8 *str)
{
while (1)
{
if (*str == '\0') break; // 遇到结束符,退出
UART0_SendByte(*str++); // 发送数据
}
}
/*
*********************************************************************************************************
** 函数名称 :UART0_Init()
** 函数功能 :串口初始化,设置工作模式和波特率。
** 入口参数 :baud 波特率
** set 模式设置(UARTMODE数据结构)
** 出口参数 :1-初始化成功, 0-初始化失败
*********************************************************************************************************
*/
uint8 UART0_Init (uint32 baud, UARTMODE set)
{
uint32 bak;
Uart_Position = 0;
/* 参数过滤 */
if ((baud ==0 ) || (baud > 115200)) return (0);
if ((set.datab <5) || (set.datab > 8)) return (0);
if ((set.stopb == 0) || (set.stopb > 2)) return (0);
if (set.parity > 4) return (0);
/* 设置串口波特率 */
U0LCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4) / baud;
U0DLM = bak >> 8;
U0DLL = bak & 0xFF;
/* 设置串口模式 */
bak = set.datab - 5; // 设置字长
if (set.stopb == 2) bak |= 0x04; // 判断是否为2位停止位
if (set.parity != 0)
{
set.parity = set.parity - 1;
bak |= 0x08;
}
bak |= set.parity << 4; // 设置奇偶校验
U0LCR = bak;
return (1);
}
/*
*********************************************************************************************************
** 函数名称 :Uart_Init()
** 函数功能 :串口总的初始化,中断,触发点。
** 入口参数 :
**
** 出口参数 :
*********************************************************************************************************
*/
void Uart_Init(void)
{
UARTMODE set;
set.datab = 8;
set.stopb = 1;
set.parity = 0;
U0FDR = 1 | // DIVADDVAL=1
12 << 4; // MULVAL=12
// 若波特率为9600,以上语句删除
UART0_Init(115200, set); // 串口初始化,波特率115200Bps 8bit数据,1bit停止位,无奇偶位
U0FCR = 0x81; // 使能FIFO,并设置触发点为8字节
U0IER = 0x01;
VICIntSelect = 0x00000000; // 设置所有的通道为IRQ中断
VICVectCntl0 = 0x20 | 0x06; // UART0分配到IRQ slot0,即最高优先级
VICVectAddr0 = (uint32)IRQ_UART0; // 设置UART0向量地址
VICIntEnable = 1 << 0x06; // 使能UART0中断
}
UART.rar_115200_uart 码率
版权申诉
23 浏览量
2022-09-24
04:53:22
上传
评论
收藏 2KB RAR 举报
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_42653672
- 粉丝: 94
- 资源: 1万+
最新资源
- MyBatisCodeHelperPro 3.3.2-2322 2023.2-2024.1
- 基于Python的简单的学生成绩管理程序设计(课程设计)
- jdk-8u20-windows-x64安装版本-jdk-8u301-linux-x64解压版
- 植物大战僵尸杂交版 修改阳光和冷却
- html css js网页设计ntion-model-for-开发笔记
- 数据库课程设计-processing开发笔记
- matlab画图sic-开发demo
- 数据库原理课程设计-图书馆数据库管理系统
- IEC101Sim IEC870-5-101协议仿真主站、子站 (含源码 C++Builder)
- 2212503007陈熙炜跨境2202作业2.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0