求助LM4F120 LaunchPad UART 操作,收发的内容不一致,内附代码

2019-03-24 11:45发布

我是TI M4的新手,刚刚在某宝买来LaunchPad,参照论坛里的朋友的UART程序,自己也写了一小段代码,可是运行结果不正确。程序的功能是UART接受PC串口的字符,然后将接收到的字符发送回去,问题在于PC端发送的字符和接收到的来自LaunchPad 的字符不一致,比如PC端串口发送字符a,接收到的字符确实X,着实费解,看了别人的代码,看起来和我的没什么区别。满腹疑惑。代码如下,求斧正。
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "inc/tm4c1233h6pm.h"
  4. #include "inc/hw_memmap.h"
  5. #include "inc/hw_types.h"
  6. #include "driverlib/sysctl.h"
  7. #include "driverlib/interrupt.h"
  8. #include "driverlib/gpio.h"
  9. #include "driverlib/uart.h"

  10. #define GPIO_PA0_U0RX           0x00000001
  11. #define GPIO_PA1_U0TX           0x00000401

  12. #define GPIO_PE4_U5RX           0x00041001
  13. #define GPIO_PE5_U5TX           0x00041401


  14. int main(void)
  15. {
  16.         // Set the system clock
  17.         SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_XTAL_16MHZ | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN);

  18.         // Enable the UART and the GPIO port of the UART we use
  19.         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
  20.         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART5);

  21.         // Set the corresponding GPIO pins work as UART
  22.         GPIOPinConfigure(GPIO_PE4_U5RX);
  23.         GPIOPinConfigure(GPIO_PE5_U5TX);

  24.         GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);

  25.         // Set the clock of UART function
  26.         UARTClockSourceSet(UART5_BASE, UART_CLOCK_SYSTEM);

  27.         // Set the properties the the UART port
  28.         UARTConfigSetExpClk(UART5_BASE, SysCtlClockGet(), 9600,
  29.                                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
  30.                                           UART_CONFIG_PAR_NONE));

  31.         // Start the UART
  32.         UARTEnable(UART5_BASE);

  33.         // Enable the interrupt function
  34.         IntEnable(INT_UART5);

  35.         // Enable the UART interrupt
  36.         UARTIntEnable(UART5_BASE, UART_INT_RX | UART_INT_RT);
  37.         IntMasterEnable();

  38.         while(1)
  39.         {
  40.         }
  41. }

  42. void UART5_ISR(void)
  43. {
  44.     unsigned long ulStatus;
  45.     unsigned char ch;

  46.     ulStatus = UARTIntStatus(UART5_BASE, true);                 /* 获取中断状态   */
  47.     UARTIntClear(UART5_BASE, ulStatus);                                 /* 清除中断标志位 */

  48.     while(UARTCharsAvail(UART5_BASE))                                 /* 这里等待接收字符  */
  49.     {
  50.                 /* 将接收的字符发送出去 */
  51.             ch = (uint8_t)UARTCharGetNonBlocking(UART5_BASE);
  52.             //SysCtlDelay(1000);
  53.             UARTCharPutNonBlocking(UART5_BASE, ch);
  54.     }
  55. }
复制代码

此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
zgbkdlm
2019-03-24 14:15
可以参照下stellarisware里面的uart例程

一周热门 更多>

相关问题

    相关文章