[N32G45x] 【N32G45XVL-STB_V1.1】开发板评测之驱动蓝牙遥控小车

[复制链接]
1500|6
 楼主| gaoyang9992006 发表于 2022-3-7 21:32 | 显示全部楼层 |阅读模式
本帖最后由 gaoyang9992006 于 2022-4-12 17:24 编辑

@21小跑堂

使用开发板的USART1,PA9---TX/PA10---RX
使用GPIOE(2/4;3/5;/10/12;11/13),因为使用这几个方便我插杜邦线
串口的使用与IO的使用,请参考我前面的帖子
串口使用方法与注意事项
https://bbs.21ic.com/icview-3203688-1-1.html
IO点灯与解决烧录不了的方法
https://bbs.21ic.com/icview-3202554-1-1.html
最后我们上最终的代码
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/

  27. /**
  28. * [url=home.php?mod=space&uid=288409]@file[/url] main.c
  29. * [url=home.php?mod=space&uid=187600]@author[/url] Nations
  30. * [url=home.php?mod=space&uid=895143]@version[/url] v1.0.0
  31. *
  32. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include <stdio.h>
  35. #include "main.h"

  36. /** @addtogroup N32G45X_StdPeriph_Examples
  37. * @{
  38. */

  39. /** @addtogroup USART_Printf
  40. * @{
  41. */

  42. typedef enum
  43. {
  44.     FAILED = 0,
  45.     PASSED = !FAILED
  46. } TestStatus;

  47. #define countof(a) (sizeof(a) / sizeof(*(a)))

  48. USART_InitType USART_InitStructure;

  49. void RCC_Configuration(void);
  50. void GPIO_Configuration(void);
  51. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

  52. uint8_t get_1byte(void);


  53. void stop(void)
  54. {
  55.         GPIOE->PBC = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_5;        
  56. }
  57. void up(void)
  58. {
  59.         GPIOE->PBSC = GPIO_PIN_10;
  60.         GPIOE->PBC  = GPIO_PIN_12;
  61.         
  62.         GPIOE->PBSC = GPIO_PIN_11;
  63.         GPIOE->PBC  = GPIO_PIN_13;

  64.         GPIOE->PBSC = GPIO_PIN_2;
  65.         GPIOE->PBC  = GPIO_PIN_4;

  66.         GPIOE->PBSC = GPIO_PIN_3;
  67.         GPIOE->PBC  = GPIO_PIN_5;        
  68. }

  69. void down(void)
  70. {
  71.         GPIOE->PBC  = GPIO_PIN_10;
  72.         GPIOE->PBSC = GPIO_PIN_12;
  73.         
  74.         GPIOE->PBC  = GPIO_PIN_11;
  75.         GPIOE->PBSC = GPIO_PIN_13;

  76.         GPIOE->PBC  = GPIO_PIN_2;
  77.         GPIOE->PBSC = GPIO_PIN_4;

  78.         GPIOE->PBC  = GPIO_PIN_3;
  79.         GPIOE->PBSC = GPIO_PIN_5;        
  80. }

  81. void left(void)
  82. {
  83.         GPIOE->PBSC = GPIO_PIN_10;
  84.         GPIOE->PBC  = GPIO_PIN_12;
  85.         
  86.         GPIOE->PBC = GPIO_PIN_11;
  87.         GPIOE->PBC  = GPIO_PIN_13;
  88. }

  89. void right(void)
  90. {
  91.         GPIOE->PBC = GPIO_PIN_10;
  92.         GPIOE->PBC  = GPIO_PIN_12;
  93.         
  94.         GPIOE->PBSC = GPIO_PIN_11;
  95.         GPIOE->PBC  = GPIO_PIN_13;
  96. }


  97. /**
  98. * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  99. */
  100. int main(void)
  101. {
  102.     uint8_t x;
  103.     uint8_t cmd[15];
  104.     unsigned int i=0;

  105.         /* System Clocks Configuration */
  106.     RCC_Configuration();

  107.     /* Configure the GPIO ports */
  108.     GPIO_Configuration();

  109.     /* USARTy and USARTz configuration ------------------------------------------------------*/
  110.     USART_InitStructure.BaudRate            = 9600;
  111.     USART_InitStructure.WordLength          = USART_WL_8B;
  112.     USART_InitStructure.StopBits            = USART_STPB_1;
  113.     USART_InitStructure.Parity              = USART_PE_NO;
  114.     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
  115.     USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

  116.     /* Configure USARTx */
  117.     USART_Init(USARTx, &USART_InitStructure);
  118.     /* Enable the USARTx */
  119.     USART_Enable(USARTx, ENABLE);
  120.                 printf("Hello MyCar\n");
  121.                

  122.                
  123.                

  124.         

  125.     while (1)
  126.     {
  127.                                 while(get_1byte()=='{')
  128.         {
  129.             cmd[0] = '{';
  130.             for(i=1;i<15;i++)
  131.                 cmd[i] = get_1byte();
  132.             if(cmd[14]=='}')
  133.             {
  134.                 x=cmd[12];
  135.                 switch(x)
  136.                 {
  137.                 case 'U':
  138.                     up();
  139.                     printf("UP\n");
  140.                     break;
  141.                 case 'D':
  142.                     down();
  143.                     printf("DOWN\n");
  144.                     break;
  145.                 case 'L':
  146.                     left();
  147.                     printf("LEFT\n");
  148.                     break;
  149.                 case 'R':
  150.                     right();
  151.                     printf("RIGHT\n");
  152.                     break;
  153.                 case 'S':
  154.                     stop();
  155.                     printf("STOP\n");
  156.                     break;
  157.                 }
  158.             }
  159.         }
  160.     }
  161. }

  162. /**
  163. * [url=home.php?mod=space&uid=247401]@brief[/url]  Configures the different system clocks.
  164. */
  165. void RCC_Configuration(void)
  166. {
  167.     /* Enable GPIO clock */
  168.     GPIO_APBxClkCmd(USARTx_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);
  169.     /* Enable USARTy and USARTz Clock */
  170.     USART_APBxClkCmd(USARTx_CLK, ENABLE);
  171. }

  172. /**
  173. * [url=home.php?mod=space&uid=247401]@brief[/url]  Configures the different GPIO ports.
  174. */
  175. void GPIO_Configuration(void)
  176. {
  177.     GPIO_InitType GPIO_InitStructure;

  178.     /* Configure USARTx Tx as alternate function push-pull */
  179.     GPIO_InitStructure.Pin        = USARTx_TxPin;
  180.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  181.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
  182.     GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);

  183.     /* Configure USARTx Rx as input floating */
  184.     GPIO_InitStructure.Pin       = USARTx_RxPin;
  185.      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  186. //            GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
  187.     GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
  188.         
  189. /*
  190.         对使用的IO进行初始化
  191. */
  192.                 RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
  193.                 GPIO_InitStructure.Pin        = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_5;
  194.                 GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  195.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  196.                 GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);        
  197. }

  198. /**
  199. * @brief  Compares two buffers.
  200. * @param  pBuffer1, pBuffer2: buffers to be compared.
  201. * @param BufferLength buffer's length
  202. * [url=home.php?mod=space&uid=266161]@return[/url] PASSED: pBuffer1 identical to pBuffer2
  203. *         FAILED: pBuffer1 differs from pBuffer2
  204. */
  205. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  206. {
  207.     while (BufferLength--)
  208.     {
  209.         if (*pBuffer1 != *pBuffer2)
  210.         {
  211.             return FAILED;
  212.         }

  213.         pBuffer1++;
  214.         pBuffer2++;
  215.     }

  216.     return PASSED;
  217. }

  218. uint8_t get_1byte(void)
  219. {
  220.         uint8_t ch;
  221.         while (USART_GetFlagStatus(USARTx, USART_FLAG_RXDNE) == RESET)
  222.         {
  223.         }
  224.         ch = (USART_ReceiveData(USARTx)&0xFF);
  225. //        printf("%c",ch);
  226.         return ch;
  227. }


  228. /* retarget the C library printf function to the USART */
  229. int fputc(int ch, FILE* f)
  230. {
  231.     USART_SendData(USARTx, (uint8_t)ch);
  232.     while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET)
  233.         ;
  234.     return (ch);
  235. }

  236. #ifdef USE_FULL_ASSERT

  237. /**
  238. * @brief  Reports the name of the source file and the source line number
  239. *         where the assert_param error has occurred.
  240. * @param file pointer to the source file name
  241. * @param line assert_param error line source number
  242. */
  243. void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
  244. {
  245.     /* User can add his own implementation to report the file name and line number,
  246.        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  247.     /* Infinite loop */
  248.     while (1)
  249.     {
  250.     }
  251. }

  252. #endif

  253. /**
  254. * @}
  255. */

  256. /**
  257. * @}
  258. */
MyCar_Printf.zip (1.26 MB, 下载次数: 13)
B站视频链接,因为手机又要拍照,又要遥控,所以比较尴尬。
https://www.bilibili.com/video/BV1i44y1M7LR/
手机端软件是我自己编写的,已经开源,请到上位机板块查找,这里提供编译好的
MyCar.zip (550.45 KB, 下载次数: 10)
微信图片_20220308085914.jpg
四驱纸糊车,蓝牙遥控。

  
huquanz711 发表于 2022-3-8 07:58 来自手机 | 显示全部楼层
车车呢?之前发过?
 楼主| gaoyang9992006 发表于 2022-3-8 08:58 | 显示全部楼层
huquanz711 发表于 2022-3-8 07:58
车车呢?之前发过?

https://bbs.21ic.com/icview-3173112-1-1.html
这是手机APP的开发教程贴。
海滨消消 发表于 2022-3-8 16:46 来自手机 | 显示全部楼层
感谢楼主分享
zhuotuzi 发表于 2022-5-19 10:26 | 显示全部楼层
学习一下这个解读指令的方法。
lzbf 发表于 2022-7-8 09:44 | 显示全部楼层
这个做的不错。  
sdCAD 发表于 2022-7-8 10:16 | 显示全部楼层
经典蓝牙吗   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2052

主题

16403

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部