📄 uart0.c
字号:
/****************************************Copyright (c)**************************************************
**
**--------------File Info-------------------------------------------------------------------------------
** File name: UART0
** Last Version: 1.0
**
**------------------------------------------------------------------------------------------------------
** Created by: 肖继达
** Created date: 2007-03-22
** Version: 1.0
** Descriptions: ARM串口0函数库,
** 注意:::!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
使用需在main.c中声明extern uint8 *UART0_p,使串口指针指向指定的缓冲区
********************************************************************************************************/
#include "config.h"
#include "UART0.h"
uint8 *UART0_p; //串口0接受数据指针
const uint32 UART_BPS=9600; //设置波特率9600
/*
*********************************************************************************************************
** 函数名称 :IRQ_UART0(void)
** 函数功能 :串口0中断处理函数
** 入口参数 :无
** 出口参数 :无
*********************************************************************************************************
*/
void __irq IRQ_UART0(void) //串口0中断响应程序
{
*UART0_p=U0RBR;
UART0_p++;
VICVectAddr=0x00;
}
/*
*********************************************************************************************************
** 函数名称 :UART0_Init()
** 函数功能 :串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率9600,
** 入口参数 :UIE: 中断标志位
0:不使用中断
1:使用中断
VICVectC: 中断向量号
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_Init (uint8 UIE,uint8 VICVectC)
{
uint16 Fdiv;
U0LCR = 0x83; // DLAB=1,允许设置波特率
Fdiv = (Fpclk / 16) / UART_BPS; // 设置波特率
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03;
PINSEL0 = (PINSEL0&(~0x0f))|0x05; // UART0引脚功能设定
U0FCR = 0x00; // UART0控制器设置,不设FIFO
if(UIE)
{
U0IER = 0x01; // UART0中断使能器,使能RBR
IRQEnable(); //总中断使能
(*((unsigned long *) 0xFFFFF200+VICVectC*4))=0x20 | 0x06;
(*((unsigned long *) 0xFFFFF100+VICVectC*4)) = (uint32)IRQ_UART0;
VICIntEnable = 1 << 0x06;
}
}
/*
*********************************************************************************************************
** 函数名称 :UART0_GetByte()
** 函数功能 :从串口接收1字节数据,使用查询方式接收。
** 入口参数 :无
** 出口参数 :接收到的数据
*********************************************************************************************************
*/
uint8 UART0_GetByte (void)
{
uint8 rcv_dat;
while ((U0LSR & 0x01) == 0);
rcv_dat = U0RBR;
return (rcv_dat);
}
/*
*********************************************************************************************************
** 函数名称 :UART0_GetStr()
** 函数功能 :从串口接收
** 入口参数 : s 指向接收数据数组的指针
** n 接收的个数
** 出口参数 : 无
*********************************************************************************************************
*/
void UART0_GetStr (uint8 *s, uint32 n)
{
for ( ; n>0; n--)
{
*s++ = UART0_GetByte();
}
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendByte()
** 函数功能 :向串口发送字节数据,并等待发送完毕,查询方式。
** 入口参数 :dat 要发送的数据
** 出口参数 :无
*********************************************************************************************************
*/
void U0_SendByte(uint8 dat)
{
U0THR = dat;
while ((U0LSR & 0x40) == 0); // 等待数据发送完毕
}
/*
*********************************************************************************************************
** 函数名称 :UART0_SendStr()
** 函数功能 :向串口发送一字符串
** 入口参数 :str 要发送的字符串的指针
** 出口参数 :无
*********************************************************************************************************
*/
void UART0_SendStr (uint8 *str)
{
while (1)
{
if (*str == '\0') break; // 遇到结束符,退出
U0_SendByte(*str++); // 发送数据
}
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -