大家好、我已经尝试使用以下代码实现了一个简单的 UART 接口:
/*********
*丹麦南部大学
*嵌入式编程(EMP)
*
* MODULENAME。:main.c
*
项目....:EMP
*
说明:分配4、主模块。 无 main.h 文件。
*
*更改日志:
*********
*日期 ID 更改
* YYMMDD*
---
*创建了290417 MOH 模块。
*
* /
/************* 包含文件******* /#include
#include "glob_def.h"
#include "tm4c123ghp6.h"
#include "emp_type.h"
#include "uart0.h"
// LED 灯
#define RESET_RGB 0x0E
#define GREEN 0b00000110
#define Yellow 0b00001010
#define RED 0b00001100
#define LED_OFF 0b00001110
//函
数 char ReadChar (void);
void PrintChar (char c);
void PrintString (char * string);
char c;
int main (void){
//启用用于板载 LED 的 GPIO 端口。
//
SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOA | SYSCTL_RCGC2_GPIOC | SYSCTL_RCGC2_GPIOD | SYSCTL_RCGC2_GPIOF;
SYSCTL_RCGC1_R |= 0x00000001;
//init_SysTick ();
//
//为 LED 启用 GPIO 引脚(PF3)。 将方向设置为输出、然后
//启用 GPIO 引脚以实现数字功能。
//
GPIO_PORTC_DIR_R = 0xF0;
GPIO_PORTC_DEN_R = 0xF0;
GPIO_PORTD_DIR_R = 0x4C;
GPIO_PORTD_DEN_R = 0x4C;
GPIO_PORTF_DIR_R = 0x0E;
GPIO_PORTF_DEN_R = 0x1F;
//初始化 UART
uart0_init (9600、8、1、'n');
while (1)
{
PrintString ("输入\"r\"、\"g\"或\"y\":\n\n"); //显示字符串
C = ReadChar();
PrintChar (c);
PrintString ("\n\n"r);
开关(c)//根据外壳显示 LED 颜色
{
案例"R":
GPIO_PORTF_DATA_R &=~Ω(RESET_RGB);
GPIO_PORTF_DATA_R |=红色;
中断;
案例"G":
GPIO_PORTF_DATA_R &=~Ω(RESET_RGB);
GPIO_PORTF_DATA_R |=绿色;
中断;
案例"y":
GPIO_PORTF_DATA_R &=~Ω(RESET_RGB);
GPIO_PORTF_DATA_R |=黄色;
中断;
默认值:
GPIO_PORTF_DATA_R &=~Ω(RESET_RGB);
GPIO_PORTF_DATA_R |= LED_OFF;
中断;
}
}
空 PrintChar (char c)
{
while (uart0_TX_rdy ()!= 0);
uart0_putc (c);
}
字符读取字符(空)
{
字符 c;
while (uart0_Rx_rdy ()!=0);
uart0_getc();
}
空 PrintString (char * string)
{
while (*字符串);
PrintChar (*(string++));
}
函数 GetChar 和 Printchar 使用以下模块中的函数:e2e.ti.com/.../uart0.c
根据我按下键盘上的按钮、代码只应显示红色、黄色或绿色。
我没有错误、但 我没有收到任何错误、直到我正在使用的终端(PuTTy)。 值得注意的是、我也尝试过其他终端。