標(biāo)題: [零基礎(chǔ)學(xué)習(xí)STM32]第四講:UART通訊實(shí)驗(yàn)—控制LED [打印本頁(yè)]

作者: kkhkbb    時(shí)間: 2018-3-2 17:18
標(biāo)題: [零基礎(chǔ)學(xué)習(xí)STM32]第四講:UART通訊實(shí)驗(yàn)—控制LED
一、概述
1、UART簡(jiǎn)介
  UART:通用同步/異步串行接收/發(fā)送器,由時(shí)鐘發(fā)生器、數(shù)據(jù)發(fā)送器和接收器三大部分組成。UART是一個(gè)全雙工通用同步/異步串行收發(fā)模塊,該接口是一個(gè)高度靈活的串行通信設(shè)備。STM32F407IGT6具有6個(gè)UART收發(fā)器,可使用相應(yīng)的代碼使能后使用。
2、UART特點(diǎn)
3、UART時(shí)序
           圖4_0 UART時(shí)序
空閑位:高電平。
啟動(dòng)位:一個(gè)低電平。
字符數(shù)據(jù):可以選擇8和9位數(shù)據(jù)位。
奇偶校驗(yàn)位:根據(jù)需要選擇是否進(jìn)行校驗(yàn)。
停止位:一個(gè)高電平。
4、TTL、RS232、RS485、RS422通信
TTL、RS232、RS485都是指電平信號(hào),USART可使用相應(yīng)的電平轉(zhuǎn)換芯片,實(shí)現(xiàn)這三種通信。
二、硬件電路

                圖4_1UART引腳圖
             圖4_2UART轉(zhuǎn)USB
  本試驗(yàn)使用的芯片STM32F407IGT6,使用UART為UART_4,引腳位PA0和PA1,經(jīng)過(guò)串口轉(zhuǎn)USB芯片CH340轉(zhuǎn)換后,可通過(guò)USB接口與計(jì)算機(jī)通訊。
三、 實(shí)驗(yàn)原理
  計(jì)算機(jī)安裝 CH340 驅(qū)動(dòng)后,可通過(guò)串口工具來(lái)接收串口發(fā)送的數(shù)據(jù)和向串口發(fā)送給數(shù)據(jù)。用串口工具打開(kāi)iCore3對(duì)應(yīng)的端口,波特率設(shè)為115200,發(fā)送相應(yīng)的命令,便可以控制ARM LED的亮滅情況。串口命令如下表:
表4_1: 串口控制命令
LED_RED_ON\cr\lf
LED 紅燈亮
LED_RED_OFF\cr\lf
LED 紅燈滅
LED_BLUE_ON\cr\lf
LED 藍(lán)燈亮
LED_BLUE_OFF\cr\lf
LED 藍(lán)燈滅
LED_GREEN_ON\cr\lf
LED 綠燈亮
  LED_GREEN_OFF\cr\lf
LED 綠燈滅

                          圖:4_3 控制實(shí)例
四、源代碼
1.USART初始化結(jié)構(gòu)體介紹
  1. typedefstruct
  2. {
  3. uint32_tUSART_BaudRate;           
  4. uint16_tUSART_WordLength;         
  5. uint16_tUSART_StopBits;           
  6. uint16_tUSART_Parity;              
  7. uint16_tUSART_Mode;               
  8. uint16_tUSART_HardwareFlowControl;
  9. } USART_InitTypeDef;
復(fù)制代碼
2.主函數(shù)
  1. /*
  2. * Name                : main
  3. * Description         : ---
  4. * Author              : ysloveivy.
  5. *
  6. * History
  7. * --------------------
  8. * Rev                 : 0.00
  9. * Date                : 11/21/2015
  10. *
  11. * create.
  12. * --------------------
  13. */
  14. int main(void)
  15. {
  16.         inti;
  17.         char buffer[20];
  18.         led.initialize();
  19.         usart4.initialize(9600);
  20.         usart4.printf("hello! I am iCore3!\r\n");                              //串口4輸出
  21.         while(1){
  22.                 if(usart4.receive_ok_flag){                                          //接收完成
  23. usart4.receive_ok_flag = 0;
  24.                         for(i = 0;i <20;i++){
  25.                                 buffer[i] = tolower(usart4.receive_buffer[i]);
  26.                         }
  27.                         //比較接收信息
  28.                         if(memcmp(buffer,"led_red_on",strlen("led_red_on")) == 0){
  29.                                 LED_RED_ON;
  30.                                 usart4.printf("ok!\r\n");
  31.                         }
  32.                         if(memcmp(buffer,"led_red_off",strlen("led_red_off")) == 0){
  33.                                 LED_RED_OFF;
  34.                                 usart4.printf("ok!\r\n");
  35.                         }
  36.                         if(memcmp(buffer,"led_green_on",strlen("led_green_on")) == 0){
  37.                                 LED_GREEN_ON;
  38.                                 usart4.printf("ok!\r\n");
  39.                         }
  40.                         if(memcmp(buffer,"led_green_off",strlen("led_green_off")) == 0){
  41.                                 LED_GREEN_OFF;
  42.                                 usart4.printf("ok!\r\n");
  43.                         }
  44.                         if(memcmp(buffer,"led_blue_on",strlen("led_blue_on")) == 0){
  45.                                 LED_BLUE_ON;
  46.                                 usart4.printf("ok!\r\n");
  47.                         }
  48.                         if(memcmp(buffer,"led_blue_off",strlen("led_blue_off")) == 0){
  49.                                 LED_BLUE_OFF;
  50.                                 usart4.printf("ok!\r\n");
  51.                         }
  52.                 }
  53.         }
  54. }
復(fù)制代碼
3.USART初始化及相關(guān)配置
  1. /*
  2. * Name               : initialize_usart4
  3. * Description        : ---
  4. * Author             : XiaomaGee
  5. *
  6. * History
  7. * -------------------
  8. * Rev                : 0.00
  9. * Date               : 11/21/2015
  10. *
  11. * create.
  12. * -------------------
  13. */
  14. staticint initialize_usart4(unsignedlongintbaudrate)
  15. {
  16.         GPIO_InitTypeDefGPIO_InitStructure;
  17.         USART_InitTypeDefUSART_InitStructure;
  18.         NVIC_InitTypeDef NVIC_InitStructure;

  19.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);             // GPIO時(shí)鐘使能

  20.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                        //PA0為復(fù)用推挽輸出
  21.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  22.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  24.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                        //PA1為浮空輸入
  25.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  26.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  28.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4);           //PA0引腳復(fù)用為UART4
  29.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_UART4);           //PA1引腳復(fù)用為UART4

  30.         USART_DeInit(UART4);
  31.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);            //UART4時(shí)鐘使能
  32. USART_InitStructure.USART_BaudRate = baudrate;                   //波特率
  33.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;      //8個(gè)數(shù)據(jù)位
  34.         USART_InitStructure.USART_StopBits = USART_StopBits_1;           //1個(gè)停止位
  35.         USART_InitStructure.USART_Parity = USART_Parity_No ;             //無(wú)奇偶校驗(yàn)位
  36.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  37.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //收發(fā)模式
  38.         USART_Init(UART4, &USART_InitStructure);                         //初始化UART4
  39.         USART_Cmd(UART4, ENABLE);                                        //使能UART4
  40.         USART_ITConfig(UART4,USART_IT_PE,ENABLE);
  41.         USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);                      //打開(kāi)UART4的中斷

  42.         NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
  43.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  44.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  45.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  46.         NVIC_Init(&NVIC_InitStructure);

  47.         return0;
  48. }
  49. /*
  50. * Name               : send_buffer4
  51. * Description        : ---
  52. * Author             : XiaomaGee
  53. *
  54. * History
  55. * -------------------
  56. * Rev                : 0.00
  57. * Date               : 11/21/2015
  58. *
  59. * create.
  60. * -------------------
  61. */
  62. staticint
  63. send_buffer4(void * buf,intlen)
  64. {
  65.         char *p = (char *)buf;

  66.         if(len<= 0)return -1;

  67.         while(len --){
  68.                 send_byte_to_usart4(*p);
  69.                 p ++;
  70.         }
  71.         return0;
  72. }

  73. /*
  74. * Name               : send_byte_to_usart4
  75. * Description        : ---
  76. * Author             : XiaomaGee
  77. *
  78. * History
  79. * -------------------
  80. * Rev                : 0.00
  81. * Date               : 11/21/2015
  82. *
  83. * create.
  84. * -------------------
  85. */
  86. staticint
  87. send_byte_to_usart4(char data)                  //TTL通信
  88. {
  89.         while(!(USART_GetFlagStatus(UART4,USART_FLAG_TC) == 1));          //等待發(fā)送數(shù)據(jù)完成
  90.         USART_SendData(UART4,data);                                       //將數(shù)據(jù)寫(xiě)入數(shù)據(jù)寄存器中

  91.         return0;
  92. }

  93. /*
  94. * Name               : send_string_to_usart4
  95. * Description        : ---
  96. * Author             : XiaomaGee
  97. *
  98. * History
  99. * -------------------
  100. * Rev                : 0.00
  101. * Date               : 11/21/2015
  102. *
  103. * create.
  104. * -------------------
  105. */
  106. staticint
  107. send_string_to_usart4(char * str)
  108. {
  109.         while(*str!='\0'){
  110.                 while(!(USART_GetFlagStatus(UART4,USART_FLAG_TC) == 1));
  111.                 USART_SendData(UART4,*str++);
  112.         }
  113.         return0;
  114. }
  115. /*
  116. * Name               : UART4_IRQHandler
  117. * Description        : ---
  118. * Author             : XiaomaGee
  119. *
  120. * History
  121. * -------------------
  122. * Rev                : 0.00
  123. * Date               : 11/21/2015
  124. *
  125. * create.
  126. * -------------------
  127. */
  128. int
  129. UART4_IRQHandler(void)
  130. {
  131. while(USART_GetFlagStatus(UART4,USART_FLAG_RXNE) == 0);
  132.         usart4.receive_buffer[usart4.counter++]=USART_ReceiveData(UART4); //接收數(shù)據(jù)

  133.         if(usart4.receive_buffer[usart4.counter - 1] == '\n'&& usart4.receive_buffer[usart4.counter - 2] == '\r'){
  134.                         usart4.receive_buffer[usart4.counter-1]=0;
  135.                         usart4.counter=0;
  136.                 usart4.receive_ok_flag=1;
  137. return0;
  138. }
  139. /*
  140. * Name               : printf
  141. * Description        : ---
  142. * Author             : XiaomaGee
  143. *
  144. * History
  145. * -------------------
  146. * Rev                : 0.00
  147. * Date               : 11/21/2015
  148. *
  149. * create.
  150. * -------------------
  151. */
  152. staticint
  153. my_printf4(constchar * fmt,...)                             //串口4輸出
  154. {
  155.         __va_listarg_ptr;
  156.         charbuf[UART_BUFFER_SIZE];

  157.         memset(buf,'\0',sizeof(buf));

  158.         va_start(arg_ptr,fmt);
  159.         vsprintf(buf,fmt,arg_ptr);
  160.         va_end(arg_ptr);

  161.         send_string_to_usart4(buf);

  162.         return0;
復(fù)制代碼
五、 實(shí)驗(yàn)現(xiàn)象
通過(guò)串口調(diào)試工具輸入相應(yīng)的命令,可以看到iCore3執(zhí)行相應(yīng)的明命令,LED按控制命令亮滅。
六、代碼包下載
iCore3_4_USART.rar (1.55 MB, 下載次數(shù): 27)

作者: 2272607948    時(shí)間: 2019-9-10 18:37
寫(xiě)的好
作者: pjzmj2012    時(shí)間: 2020-7-2 08:57
51hei凈出好東西
作者: guoxiaodong    時(shí)間: 2020-7-5 15:57
挺好的,如果有加更全的通信協(xié)議就好了




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1