📄 main.c
字号:
#include "systemInit.h"
#include <uart.h>
#include <stdio.h>
#define UARTCharPutNB UARTCharPutNonBlocking
#define LED1 GPIO_PIN_0
#define LED2 GPIO_PIN_2
// UART初始化
void uartInit(void)
{
SysCtlPeriEnable(SYSCTL_PERIPH_UART2); // enable UART model
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG); // enable GPIO port for RX/TX
GPIOPinTypeUART(GPIO_PORTG_BASE, // 配置RX/TX所在管脚为
GPIO_PIN_0 | GPIO_PIN_1); // UART收发功能
UARTConfigSet(UART2_BASE, // 配置UART 端口
9600, // 波特率:9600
UART_CONFIG_WLEN_8 | // 数据位:8
UART_CONFIG_STOP_ONE | // 停止位:1
UART_CONFIG_PAR_NONE); // 校验位:无
UARTFIFOLevelSet(UART2_BASE, // 设置收发FIFO中断触发深度
UART_FIFO_TX2_8, // 发送FIFO 为2/8深度(4B)
UART_FIFO_RX6_8); // 接收FIFO 为6/8深度(12B)
UARTIntEnable(UART2_BASE, UART_INT_TX); // 使能发送中断
IntEnable(INT_UART2); // 使能UART 总中断
IntMasterEnable( ); // 使能处理器中断
UARTEnable(UART2_BASE); // 使能UART 端口
}
// 定义待发送的数据
const char TxData[ ] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
volatile int TxIndex = 0; // 数组下标变量
volatile tBoolean TxEndFlag = false; // 发送结束的标志
volatile int TxIntNum = 0; // 统计发送中断产生的次数
// 填充发送FIFO(填满 FIFO之后就退出,不会等待)
void TxFIFOFill(void)
{
char c;
for (;;)
{
c = TxData[TxIndex];
if (c == '\0') // 若填充完毕
{
TxEndFlag = true; // 发送结束标志置位,并跳出
break;
}
if (UARTSpaceAvail(UART2_BASE)) // 若发送FIFO里有可用空间
{
UARTCharPutNB(UART2_BASE, c); // 填充发送FIFO
TxIndex++;
} // 若没有空间则跳出,不必等待
else
break;
}
}
// 主函数(程序入口)
int main(void)
{
char s[40];
char i=0;
char receive_buff[15]={0};
jtagWait( ); // 防止JTAG 失效,重要!
clockInit( ); // 时钟初始化:晶振,6MHz
uartInit( ); // UART初始化
SysCtlPeriEnable( SYSCTL_PERIPH_GPIOD); //使能GPIOD端口
SysCtlPeriEnable( SYSCTL_PERIPH_GPIOG);
GPIOPinTypeOut( GPIO_PORTD_BASE,LED1); //设置为输出
GPIOPinTypeOut( GPIO_PORTG_BASE,LED2);
for(i=0;i<=14;i++)
{
receive_buff[i]=UARTCharGet(UART2_BASE);
}
if(receive_buff[0]=='a'&&receive_buff[1]=='b'&&receive_buff[2]=='c'&&receive_buff[3]=='3'&&receive_buff[4]=='1')
TxFIFOFill( ); // 启动发送过程
for (;;)
{
if (TxEndFlag) // 若发送结束则跳出
{
TxEndFlag = false;
break;
}
// 对实际的应用,在等待发送的同时,还可以做很多其它事情
}
/* sprintf(s, "TxIntNum = %d\r\n", TxIntNum); // 显示中断产生的次数
uartPuts(s); */
for (;;)
{
}
}
// // UART2中断服务函数
void UART2_ISR(void)
{
unsigned long ulStatus;
ulStatus = UARTIntStatus(UART2_BASE, true); // 读取当前中断状态
UARTIntClear(UART2_BASE, ulStatus); // 清除中断状态
if (ulStatus & UART_INT_TX) // 若是发送中断
{
TxFIFOFill( ); // 填充发送FIFO
TxIntNum++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -