📄 uart.c
字号:
/****************************************Copyright (c)**************************************************/
/**
;** 西南科技大学计算机科学学院
;**
;** http://www.cs.suswt.edu.cn
;**
;** 日期: 2006.4.1
;** 描述: DataLedDriver.c 西南科技大学计算机学院CS-II型实验板数码管工作程序,控制
;** 数码管底层显示功能,负责将数据传输到目标,并控制595对数码管键盘进行扫描,
;** 完成键盘工作的功能,该文件给DataLed.c提供底层接口
;** 作者:
;**
;**--------------LedDriver.c文件
;**------------------------------------------------------------------------------------------------------*/
/**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define _UART_C_
#ifdef _UART_C_
/************************************************
* Header File *
***********************************************/
#include "uart.h"
#include "string.h"
/************************************************
* Globale variable *
***********************************************/
uint8 gc_uart0_receivedata = 0; //保存串口0传过来的一个字节的数据
uint8 gc_uart0_receivebufpointer = 0; //患口0接收字符指针
uint8 gc_uart0_sendbufpointer = 0; //串口0发送字符指针
uint8 gc_uart0_receivebuf[UART0_BUFFERDATACOUNT]; //串口0接收数组(接收缓冲区)
/******************************************************************************
* Function Name: void UART0SendByte(uint8 data) *
* Description : 串口0发送函数,一次发送一个字节的数据(查询方式) *
*****************************************************************************/
void UART0SendByte(uint8 data)
{
U0THR = data;
while((U0LSR & 0x40) == 0);
}
/******************************************************************************
* Function Name: void UART0SendString(char data[]) *
* Description : 串口0发送字符串函数 *
*****************************************************************************/
void UART0SendString(char data[])
{
uint8 string_size; //要发送的字符数数组的大小
uint8 string_pointer = 0; //字符数组中待发送的数据指针
string_size = strlen(data);
for(string_pointer = 0; string_pointer <= string_size; string_pointer++)
{
U0THR = data[string_pointer];
while((U0LSR & 0x40) == 0);
//UART0SendByte(data[string_pointer]); //可以用发送字符来实现,但效率相对低。
}
}
/******************************************************************************
* Function Name: void __irq IRQ_UART0Receive(void) *
* Description : 串口0接收中断服务程序,通过判断中断标志寄存器中的标识来识别是
否有接收中断。实现把串口0传来的数据保存 *
*****************************************************************************/
void __irq IRQ_UART0Receive(void)
{
if((U0IIR & 0x0e) == 0x04) //清除中断
{
gc_uart0_receivedata = U0RBR; //保存数据
//实现对字符串的保存
gc_uart0_receivebuf[gc_uart0_receivebufpointer] = gc_uart0_receivedata;
gc_uart0_receivebufpointer++;
if(gc_uart0_receivebufpointer >= UART0_BUFFERDATACOUNT)
{
gc_uart0_receivebufpointer = 0;
}
}
VICVectAddr = 0x00; //清除中断地址
}
/******************************************************************************
* Function Name: uint8 UART0RecevieByte(void) *
* Description : 返回从串口接收到的数据 *
*****************************************************************************/
uint8 UART0RecevieByte(void)
{
return gc_uart0_receivedata;
}
/******************************************************************************
* Function Name: void UART0InterruptSet(void) *
* Description : UAART0中断设置,设置相应的中断类型和优先级,分配地址,并使能 *
*****************************************************************************/
void UART0InterruptSet(void)
{
VICIntSelect &= 0xffffffbf; //中断类型设置,为IRQ类型
VICVectCntl0 = 0x26; //中断优先级设置
VICVectAddr0 = (int)IRQ_UART0Receive; //分配地址
VICIntEnable = 0x00000040; //使能该中断
}
/******************************************************************************
* Function Name: void UART0Init(uint16 baud) *
* Description : 串口0初使化函数,设置波特率,帧格式,中断设置 *
*****************************************************************************/
void UART0Init(uint16 baud)
{
uint32 temp;
PINSEL0 = 0x05;
temp = Fpclk / 16;
temp = temp / baud;
U0LCR = 0x80;
U0DLL |= temp;
U0DLM |= temp >> 8;
U0LCR = 0x13;
U0IER = 0x01;
U0FCR = 0x01;
UART0InterruptSet();
}
#endif
/*****************************************************************************
* End of Entire File *
****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -