查看: 1195|回复: 1

[原创] 【LPC1768-DEV】④串口收发

[复制链接]
  • TA的每日心情
    奋斗
    6 小时前
  • 签到天数: 2001 天

    [LV.Master]伴坛终老

    23

    主题

    6170

    帖子

    21

    金牌会员

    Rank: 6Rank: 6

    积分
    11921
    最后登录
    2024-4-25
    发表于 2019-3-31 17:59:03 | 显示全部楼层 |阅读模式
    本帖最后由 yinwuqing 于 2019-3-31 18:07 编辑

         根据官方给出的原理图,结合板子实物,我们知道LPC1768是提供两串口,UART0是母头型插口,而UART1与两个CAN总线的接口一样,是公头型的。此次实验用到是UART0打印调试信息,为此特意从网上买了个USB转9针串口的数据线。如下图所示: 串口原理图.png
         然后将JP9的插针用跳线帽连接好,接下来在KEIL中搭建好软件平台开发包,首先新建工程选择LPC1768的芯片型号,然后勾选微控制器软件接口标准库如下下图所示:

    微控制器软件接口标准库.png
        编写串口实现函数,并在“RTE_Device.h”头文件中将UART0使能设置打开,将字符输入到LCD显示屏上并经UART0打印输出到串口调试助手上。部分代码如下:
    1. #include "LPC17xx.h"                    /* LPC17xx Definitions                */
    2. #include "GPIO_LPC17xx.h"
    3. #include "PIN_LPC17xx.h"
    4. #include "UART_LPC17xx.h"
    5. #include <string.h>
    6. #include <stdio.h>
    7. /*---------------------------------------------------------------------------*
    8. * Constants & Macros:
    9. *---------------------------------------------------------------------------*/
    10. #define         INSTR_CLEAR                 0x01                // 1.53 ms

    11. #define         INSTR_RETURN_HOME(settings) 0x02                // 1.53 ms

    12. #define         INSTR_ENTRY_MODE(settings)  (0x06|(settings))   // 39 uS
    13. #define     IENTRY_INCREMENT            (1<<1)
    14. #define     IENTRY_DECREMENT            (0<<1)
    15. #define     IENTRY_SHIFT_ON             (1<<0)
    16. #define     IENTRY_SHIFT_OFF            (0<<0)

    17. #define         INSTR_DISPLAY(settings)     (0x08|(settings))   // 39 uS
    18. #define     IDISPLAY_ON                 (1<<2)
    19. #define     IDISPLAY_OFF                (0<<2)
    20. #define     IDISPLAY_CURSOR_ON          (1<<1)
    21. #define     IDISPLAY_CURSOR_OFF         (0<<1)
    22. #define     IDISPLAY_BLINK_ON           (1<<0)
    23. #define     IDISPLAY_BLINK_OFF          (0<<0)

    24. #define         INSTR_SHIFT(settings)       (0x10|(settings))   // 39 uS
    25. #define     ISHIFT_SCREEN               (1<<3)
    26. #define     ISHIFT_CURSOR               (0<<3)
    27. #define     ISHIFT_RIGHT                (1<<2)
    28. #define     ISHIFT_LEFT                 (0<<2)

    29. #define         INSTR_FUNC(settings)        (0x20|(settings))   // 39 uS
    30. #define     IFUNC_8BIT                  (1<<4)
    31. #define     IFUNC_4BIT                  (0<<4)
    32. #define     IFUNC_TWO_LINE              (1<<3)
    33. #define     IFUNC_ONE_LINE              (0<<3)
    34. #define     IFUNC_DISPLAY_ON            (1<<2)
    35. #define     IFUNC_DISPLAY_OFF           (0<<2)

    36. #define         INSTR_SET_CGRAM_ADDR(x)     (0x40|((x)&0x3F))   // 39 uS
    37. #define         INSTR_SET_DDRAM_ADDR(x)     (0x80|((x)&0x7F))   // 39 uS
    38. #define         LUMEX_SO2004DSR_NUM_COLUMNS 20
    39. #define         LUMEX_SO2004DSR_NUM_ROWS    4
    40. #define         LED_PORT                             0
    41. #define         LED_PIN                              7

    42. uint32_t         LEDOn, LEDOff;
    43. uint32_t         uTimeCnt=0;
    44. extern ARM_DRIVER_USART Driver_USART0;
    45. static void USART0_Callback(uint32_t event);
    46. static uint8_t rxBuffer[8]         = {0};
    47. static uint8_t txBuffer[1024] = {0};

    48. struct LPC_PORT
    49. {
    50.         uint32_t uPort;
    51.         uint32_t uPin;
    52. };
    53. struct LPC_PORT LCD_Pin[11] =
    54. {
    55.         {2,0},
    56.         {2,1},
    57.         {2,2},
    58.         {2,3},
    59.         {2,4},
    60.         {2,5},
    61.         {2,6},
    62.         {2,7},  //data
    63.         {2,8},  //rs
    64.         {0,22}, //rw
    65.         {2,10}, //enable
    66. };
    67. int32_t Delayms(uint32_t ms)
    68. {
    69.         int n,m=100000;
    70.         for(n=0;n<ms;n++)
    71.         {
    72.                 while(m)
    73.                 {
    74.                         m--;
    75.                 }
    76.         }
    77.         return 0;
    78. }
    79. /*----------------------------------------------------------------------------
    80.   SysTick IRQ Handler
    81. *----------------------------------------------------------------------------*/
    82. void SysTick_Handler (void)
    83. {
    84.   static uint32_t ticks;
    85.   switch(ticks++)
    86.   {
    87.         case  0: LEDOn  = 1; break;
    88.         case  5: LEDOff = 1; break;
    89.         case  9: ticks  = 0; break;
    90.         default:
    91.       if (ticks > 10)
    92.         {
    93.         ticks = 0;
    94.       }
    95.   }
    96. }
    97. int32_t LED_Initialize(void)
    98. {
    99.   /* Enable GPIO clock */
    100.   GPIO_PortClock     (1);

    101.   /* Configure pins: Output Mode with Pull-down resistors */
    102.   PIN_Configure (LED_PORT, LED_PIN, PIN_FUNC_0, PIN_PINMODE_PULLDOWN, PIN_PINMODE_NORMAL);
    103.   GPIO_SetDir   (LED_PORT, LED_PIN, GPIO_DIR_OUTPUT);
    104.   GPIO_PinWrite (LED_PORT, LED_PIN, 0);
    105.   return 0;
    106. }
    复制代码
    1. /*----------------------------------------------------------------------------
    2.   Main function
    3. *----------------------------------------------------------------------------*/
    4. int main (void)
    5. {
    6.   SystemCoreClockUpdate();                 /* Update system core clock       */  
    7.   SysTick_Config(SystemCoreClock/10);      /* Generate interrupt each 10 ms  */
    8.   LED_Initialize();                        /* LED Initialization             */
    9.   LCD_Init();
    10.   LCD_WR_Str("Welcome to NXP      Community!");
    11.   Driver_USART0.Initialize(USART0_Callback);        
    12.   Driver_USART0.PowerControl(ARM_POWER_FULL);
    13.   Driver_USART0.Control(ARM_USART_MODE_ASYNCHRONOUS |
    14.                         ARM_USART_DATA_BITS_8 |
    15.                         ARM_USART_PARITY_NONE |
    16.                         ARM_USART_STOP_BITS_1 |
    17.                         ARM_USART_FLOW_CONTROL_NONE, 115200);
    18.   Driver_USART0.Control(ARM_USART_CONTROL_TX,1);
    19.   Driver_USART0.Control(ARM_USART_CONTROL_RX,1);
    20.   Driver_USART0.Receive(rxBuffer,sizeof(rxBuffer));

    21.   while (1)
    22.   {
    23.     if(LEDOn)
    24.     {
    25.       LEDOn = 0;
    26.       GPIO_PinWrite(LED_PORT, LED_PIN, 1);                /* Turn specified LED on          */
    27.         Driver_USART0.Send("Welcome to NXP Community!",25);
    28.     }
    29.     if (LEDOff)
    30.     {
    31.       LEDOff = 0;
    32.       GPIO_PinWrite(LED_PORT, LED_PIN, 0);                /* Turn specified LED off         */
    33.     }
    34.   }
    35. }

    36. static void USART0_Callback(uint32_t event)
    37. {
    38.   if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
    39.   {
    40.     Driver_USART0.Control(ARM_USART_ABORT_RECEIVE, 1);
    41.     uint32_t length = Driver_USART0.GetRxCount();
    42.     memcpy(txBuffer, rxBuffer, length);
    43.    
    44.     Driver_USART0.Send(txBuffer, length);
    45.     Driver_USART0.Receive(rxBuffer, sizeof(rxBuffer));
    46.   }
    47. }
    复制代码



    微信图片_20190331165759.jpg
    微信图片_20190331165811.jpg
    微信截图_20190331165634.png
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2019-5-1 06:15
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    0

    主题

    91

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    207
    最后登录
    2021-8-4
    发表于 2019-4-30 10:31:31 | 显示全部楼层
    谢谢分享
    该会员没有填写今日想说内容.
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-25 16:36 , Processed in 0.109221 second(s), 21 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表