📄 uart_init.c
字号:
#include "hw_types.h"
#include "hw_memmap.h"
#include <hw_sysctl.h>
#include <sysctl.h>
#include "hw_uart.h"
#include "uart.h"
#include <hw_gpio.h>
#include "gpio.h"
#include <hw_ints.h>
#include <interrupt.h>
#include "define.h"
#define SysCtlPeriEnable SysCtlPeripheralEnable
#define SysCtlPeriDisable SysCtlPeripheralDisable
unsigned char Uart_recBuf[MAX_UART_REC_NUMBER];
unsigned char TxData[Tx_COMMAND_NUMBER+1];//这里添加结束符
extern unsigned char new_data_flag;
extern unsigned char TxEndFlag;
unsigned char UartBufWp = 0;
void TxFIFOFill(void);
void uart0Init(void)
{
short i;
for ( i = 0; i < MAX_UART_REC_NUMBER; i++ ) { /* 清除缓冲区 */
Uart_recBuf[i] = '\0';
}
for(i=0;i<Tx_COMMAND_NUMBER+1;i++)
{
TxData[i] = '#';
}
TxData[0] = FRAME_HEADER;
//TxData[Tx_COMMAND_NUMBER - 1] = FRAME_TAIL;
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeriEnable(SYSCTL_PERIPH_UART0);
GPIOPinTypeUART(GPIO_PORTA_BASE ,
GPIO_PIN_0 | GPIO_PIN_1);
UARTConfigSet(UART0_BASE ,
9600 , /* 波特率:9600 */
UART_CONFIG_WLEN_8 | /* 数据位:8 */
UART_CONFIG_STOP_ONE | /* 停止位:1 */
UART_CONFIG_PAR_NONE); /* 校验位:无 */
UARTFIFOLevelSet(UART0_BASE , /* 设置发送和接收的FIFO深度 */
UART_FIFO_TX1_8 ,
UART_FIFO_RX7_8);
UARTIntEnable(UART0_BASE , /* 使能接收中断和接收超时中断 */
UART_INT_RX| UART_INT_RT |UART_INT_TX);
IntEnable(INT_UART0); /* 使能UART0总中断 */
UARTEnable(UART0_BASE); /* 使能UART0端口 */
}
void UART0_ISR(void)
{
unsigned char c=0;
unsigned long ulStatus;
ulStatus = UARTIntStatus(UART0_BASE, true);
UARTIntClear(UART0_BASE , ulStatus);
if (ulStatus & UART_INT_TX) // 若是发送中断
{
//TxFIFOFill(); // 填充发送FIFO
}
else //接收中断或者接收超时中断取出数据
{
for (;;) {
if ( !UARTCharsAvail(UART0_BASE) ) { /* 如果接收FIFO里没有字符 */
//UartBufWp = 0;//这句指令太依赖FIFO,要求发送的数据帧长度<=14个字节
break; //这条这令放在这里是败笔,如果是超时中断。
}
c = UARTCharGetNonBlocking(UART0_BASE); /* 读取FIFO里的字符 */
if ( Uart_recBuf[UartBufWp] == '\0' ) { /* 如果缓冲区有可用空间 */
Uart_recBuf[UartBufWp++] = c; /* 保存接收到的数据 */
/*(if ( UartBufWp >= RX_MAX ) { // 如果UartBufWp超过边界,则归0
UartBufWp = 0;
}*/
}
}
}
}
void TxFIFOFill(void)
{
unsigned char c;
static unsigned char TxIndex = 0;
//要不要做保护临界区处理呢
for (;;)
{
if(new_data_flag)
{
c = TxData[TxIndex];
if (TxIndex == 14) // 若填充完毕
{
TxEndFlag = true; // 发送结束标志置位,并跳出
TxIndex = 0;
new_data_flag = 0;
break;
}
if (UARTSpaceAvail(UART0_BASE)) // 若发送FIFO里有可用空间
{
UARTCharPutNB(UART0_BASE, c);
TxIndex++;
}
else // 若没有空间则跳出,不必等待
{
break;
}
}
else
{
break;
}
}
}
void clear_send_buf(void)
{
unsigned char i;
for(i=0;i<Tx_COMMAND_NUMBER+1;i++)
{
TxData[i] = '#';
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -