|
求助LM4F120 LaunchPad UART 操作,收发的内容不一致,内附代码
[复制链接]
我是TI M4的新手,刚刚在某宝买来LaunchPad,参照论坛里的朋友的UART程序,自己也写了一小段代码,可是运行结果不正确。程序的功能是UART接受PC串口的字符,然后将接收到的字符发送回去,问题在于PC端发送的字符和接收到的来自LaunchPad 的字符不一致,比如PC端串口发送字符a,接收到的字符确实X,着实费解,看了别人的代码,看起来和我的没什么区别。满腹疑惑。代码如下,求斧正。- #include <stdint.h>
- #include <stdbool.h>
- #include "inc/tm4c1233h6pm.h"
- #include "inc/hw_memmap.h"
- #include "inc/hw_types.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/interrupt.h"
- #include "driverlib/gpio.h"
- #include "driverlib/uart.h"
- #define GPIO_PA0_U0RX 0x00000001
- #define GPIO_PA1_U0TX 0x00000401
- #define GPIO_PE4_U5RX 0x00041001
- #define GPIO_PE5_U5TX 0x00041401
- int main(void)
- {
- // Set the system clock
- SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_XTAL_16MHZ | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN);
- // Enable the UART and the GPIO port of the UART we use
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
- SysCtlPeripheralEnable(SYSCTL_PERIPH_UART5);
- // Set the corresponding GPIO pins work as UART
- GPIOPinConfigure(GPIO_PE4_U5RX);
- GPIOPinConfigure(GPIO_PE5_U5TX);
- GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);
- // Set the clock of UART function
- UARTClockSourceSet(UART5_BASE, UART_CLOCK_SYSTEM);
- // Set the properties the the UART port
- UARTConfigSetExpClk(UART5_BASE, SysCtlClockGet(), 9600,
- (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
- UART_CONFIG_PAR_NONE));
- // Start the UART
- UARTEnable(UART5_BASE);
- // Enable the interrupt function
- IntEnable(INT_UART5);
- // Enable the UART interrupt
- UARTIntEnable(UART5_BASE, UART_INT_RX | UART_INT_RT);
- IntMasterEnable();
- while(1)
- {
- }
- }
- void UART5_ISR(void)
- {
- unsigned long ulStatus;
- unsigned char ch;
- ulStatus = UARTIntStatus(UART5_BASE, true); /* 获取中断状态 */
- UARTIntClear(UART5_BASE, ulStatus); /* 清除中断标志位 */
- while(UARTCharsAvail(UART5_BASE)) /* 这里等待接收字符 */
- {
- /* 将接收的字符发送出去 */
- ch = (uint8_t)UARTCharGetNonBlocking(UART5_BASE);
- //SysCtlDelay(1000);
- UARTCharPutNonBlocking(UART5_BASE, ch);
- }
- }
复制代码
|
|