当前位置:   article > 正文

[STM32F103C8T6]基于stm32的循迹,跟随,避障智能小车_基于stm32c8t6的电磁循迹小车

基于stm32c8t6的电磁循迹小车

目录

1.小车驱动主要是通过L9110S模块来驱动电机

motor.c

2.我们可以加入串口控制电机驱动(重写串口接收回调函数,和重定向printf)

Uart.c

main.c 

3.点动功能

uart.c

main.c

为什么使用的是HAL_Delay()要设置滴答定时器的中断优先级呢?

4.小车PWM调速, 

6.跟随功能

7.避障功能

超声波测距流程

 CSB.c

SG90.c

main.c


1.小车驱动主要是通过L9110S模块来驱动电机

 本次STM32与L9110s的接线是:

B-1A -- PB0
B-1B -- PB1
A-1A -- PB2
A-1B -- PB10

通过对GPIO口的配置,可以写出电机的驱动程序(全速模式)

motor.c

  1. #include "motor.h"
  2. #include "gpio.h"
  3. void GoForward(void)
  4. {
  5. //右轮
  6. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  7. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  8. //左轮
  9. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
  10. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET);
  11. }
  12. void GoBack(void)
  13. {
  14. //右轮
  15. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  16. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
  17. //左轮
  18. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
  19. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET);
  20. }
  21. void GoLeft(void)
  22. {
  23. //右轮
  24. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  25. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  26. //左轮
  27. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
  28. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET);
  29. }
  30. void GoRight(void)
  31. {
  32. //右轮
  33. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  34. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  35. //左轮
  36. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
  37. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET);
  38. }
  39. void Stop(void)
  40. {
  41. //右轮
  42. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  43. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  44. //左轮
  45. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
  46. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET);
  47. }

2.我们可以加入串口控制电机驱动(重写串口接收回调函数,和重定向printf)

加入串口控制,我们需要在cubeMX中开启串口中断用于接收串口发来的数据

Uart.c

  1. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  2. {
  3. if(uartHandle->Instance==USART1)
  4. {
  5. /* USER CODE BEGIN USART1_MspDeInit 0 */
  6. /* USER CODE END USART1_MspDeInit 0 */
  7. /* Peripheral clock disable */
  8. __HAL_RCC_USART1_CLK_DISABLE();
  9. /**USART1 GPIO Configuration
  10. PA9 ------> USART1_TX
  11. PA10 ------> USART1_RX
  12. */
  13. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  14. /* USART1 interrupt Deinit */
  15. HAL_NVIC_DisableIRQ(USART1_IRQn);
  16. /* USER CODE BEGIN USART1_MspDeInit 1 */
  17. /* USER CODE END USART1_MspDeInit 1 */
  18. }
  19. }
  20. /* USER CODE BEGIN 1 */
  21. //串口接收缓存(1字节)
  22. uint8_t buf=0;
  23. //定义最大接收字节数 200,可根据需求调整
  24. #define UART1_REC_LEN 200
  25. #define SIZE 12
  26. // 接收缓冲, 串口接收到的数据放在这个数组里,最大UART1_REC_LEN个字节
  27. uint8_t UART1_RX_Buffer[UART1_REC_LEN];
  28. // 接收状态
  29. // bit15, 接收完成标志
  30. // bit14, 接收到0x0d
  31. // bit13~0, 接收到的有效字节数目
  32. uint16_t UART1_RX_STA=0;
  33. char buffer[SIZE];
  34. // 接收完成回调函数,收到一个数据后,在这里处理
  35. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  36. {
  37. // 判断中断是由哪个串口触发的
  38. if(huart->Instance == USART1)
  39. {
  40. // 判断接收是否完成(UART1_RX_STA bit15 位是否为1
  41. if((UART1_RX_STA & 0x8000) == 0)
  42. {
  43. // 如果已经收到了 0x0d (回车),
  44. if(UART1_RX_STA & 0x4000)
  45. {
  46. // 则接着判断是否收到 0x0a (换行)
  47. if(buf == 0x0a)
  48. {
  49. // 如果 0x0a 和 0x0d 都收到,则将 bit15 位置为1
  50. UART1_RX_STA |= 0x8000;
  51. // printf("1");
  52. // 车控指令
  53. if(!strcmp((const char*)UART1_RX_Buffer, "M1"))
  54. {
  55. printf("Forwad......");
  56. GoForward();
  57. }
  58. else if(!strcmp((const char*)UART1_RX_Buffer, "M2"))
  59. {
  60. printf("Back......");
  61. GoBack();
  62. }
  63. else if(!strcmp((const char*)UART1_RX_Buffer, "M3"))
  64. {
  65. printf("Left......");
  66. GoLeft();
  67. }
  68. else if(!strcmp((const char*)UART1_RX_Buffer, "M4"))
  69. {
  70. printf("Right......");
  71. GoRight();
  72. }
  73. else if(!strcmp((const char*)UART1_RX_Buffer, "Stop"))
  74. {
  75. printf("Stop......");
  76. Stop();
  77. }
  78. memset(UART1_RX_Buffer, 0, UART1_REC_LEN);
  79. UART1_RX_STA = 0;
  80. }
  81. else
  82. // 否则认为接收错误,重新开始
  83. UART1_RX_STA = 0;
  84. }
  85. else // 如果没有收到了 0x0d (回车)
  86. {
  87. //则先判断收到的这个字符是否是 0x0d (回车)
  88. if(buf == 0x0d)
  89. {
  90. // 是的话则将 bit14 位置为1
  91. UART1_RX_STA |= 0x4000;
  92. }
  93. else
  94. {
  95. // 否则将接收到的数据保存在缓存数组里
  96. UART1_RX_Buffer[UART1_RX_STA & 0X3FFF] = buf;
  97. UART1_RX_STA++;
  98. // 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收
  99. if(UART1_RX_STA > UART1_REC_LEN - 1)
  100. UART1_RX_STA = 0;
  101. }
  102. }
  103. }
  104. // 重新开启中断
  105. HAL_UART_Receive_IT(&huart1, &buf, 1);
  106. }
  107. }
  108. int fputc(int ch, FILE *f)
  109. {
  110. unsigned char temp[1]={ch};
  111. HAL_UART_Transmit(&huart1,temp,1,0xffff);
  112. return ch;
  113. }

main.c 

  1. extern uint8_t buf;
  2. int main(void)
  3. {
  4. /* USER CODE BEGIN 1 */
  5. /* USER CODE END 1 */
  6. /* MCU Configuration--------------------------------------------------------*/
  7. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  8. HAL_Init();
  9. /* USER CODE BEGIN Init */
  10. /* USER CODE END Init */
  11. /* Configure the system clock */
  12. SystemClock_Config();
  13. /* USER CODE BEGIN SysInit */
  14. /* USER CODE END SysInit */
  15. /* Initialize all configured peripherals */
  16. MX_GPIO_Init();
  17. MX_USART1_UART_Init();
  18. /* USER CODE BEGIN 2 */
  19. HAL_UART_Receive_IT(&huart1, &buf, 1);//开启串口接收
  20. /* USER CODE END 2 */
  21. /* Infinite loop */
  22. /* USER CODE BEGIN WHILE */
  23. while (1)
  24. {
  25. /* USER CODE END WHILE */
  26. /* USER CODE BEGIN 3 */
  27. }
  28. /* USER CODE END 3 */
  29. }

 切记主函数内需要调用开启串口接收函数!!!!!!

将HC08模块接入STM32,连接蓝牙就可以通过蓝牙APP控制

3.点动功能

如果用蓝牙app实现遥控车模式,我们会发现,点了前进按钮,小车会一直前进,按下左转会一直左转,而遥控车应该是点动功能,按一下向前就向前走一下,一直按一直走

我的思路是,主程序一直跑Stop(); 接收到来自蓝牙(串口)的数据后执行动作(几毫秒),从而实现点动

uart.c

  1. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  2. {
  3. if(uartHandle->Instance==USART1)
  4. {
  5. /* USER CODE BEGIN USART1_MspDeInit 0 */
  6. /* USER CODE END USART1_MspDeInit 0 */
  7. /* Peripheral clock disable */
  8. __HAL_RCC_USART1_CLK_DISABLE();
  9. /**USART1 GPIO Configuration
  10. PA9 ------> USART1_TX
  11. PA10 ------> USART1_RX
  12. */
  13. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  14. /* USART1 interrupt Deinit */
  15. HAL_NVIC_DisableIRQ(USART1_IRQn);
  16. /* USER CODE BEGIN USART1_MspDeInit 1 */
  17. /* USER CODE END USART1_MspDeInit 1 */
  18. }
  19. }
  20. /* USER CODE BEGIN 1 */
  21. //串口接收缓存(1字节)
  22. uint8_t buf=0;
  23. //定义最大接收字节数 200,可根据需求调整
  24. #define UART1_REC_LEN 200
  25. #define SIZE 12
  26. // 接收缓冲, 串口接收到的数据放在这个数组里,最大UART1_REC_LEN个字节
  27. uint8_t UART1_RX_Buffer[UART1_REC_LEN];
  28. // 接收状态
  29. // bit15, 接收完成标志
  30. // bit14, 接收到0x0d
  31. // bit13~0, 接收到的有效字节数目
  32. uint16_t UART1_RX_STA=0;
  33. char buffer[SIZE];
  34. // 接收完成回调函数,收到一个数据后,在这里处理
  35. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  36. {
  37. // 判断中断是由哪个串口触发的
  38. if(huart->Instance == USART1)
  39. {
  40. // 判断接收是否完成(UART1_RX_STA bit15 位是否为1
  41. if((UART1_RX_STA & 0x8000) == 0)
  42. {
  43. // 如果已经收到了 0x0d (回车),
  44. if(UART1_RX_STA & 0x4000)
  45. {
  46. // 则接着判断是否收到 0x0a (换行)
  47. if(buf == 0x0a)
  48. {
  49. // 如果 0x0a 和 0x0d 都收到,则将 bit15 位置为1
  50. UART1_RX_STA |= 0x8000;
  51. // printf("1");
  52. // 车控指令
  53. if(!strcmp((const char*)UART1_RX_Buffer, "M1"))
  54. {
  55. printf("Forwad......");
  56. GoForward();
  57. HAL_Delay(10);
  58. }
  59. else if(!strcmp((const char*)UART1_RX_Buffer, "M2"))
  60. {
  61. printf("Back......");
  62. GoBack();
  63. HAL_Delay(10);
  64. }
  65. else if(!strcmp((const char*)UART1_RX_Buffer, "M3"))
  66. {
  67. printf("Left......");
  68. GoLeft();
  69. HAL_Delay(10);
  70. }
  71. else if(!strcmp((const char*)UART1_RX_Buffer, "M4"))
  72. {
  73. printf("Right......");
  74. GoRight();
  75. HAL_Delay(10);
  76. }
  77. else if(!strcmp((const char*)UART1_RX_Buffer, "Stop"))
  78. {
  79. printf("Stop......");
  80. Stop();
  81. HAL_Delay(10);
  82. }
  83. memset(UART1_RX_Buffer, 0, UART1_REC_LEN);
  84. UART1_RX_STA = 0;
  85. }
  86. else
  87. // 否则认为接收错误,重新开始
  88. UART1_RX_STA = 0;
  89. }
  90. else // 如果没有收到了 0x0d (回车)
  91. {
  92. //则先判断收到的这个字符是否是 0x0d (回车)
  93. if(buf == 0x0d)
  94. {
  95. // 是的话则将 bit14 位置为1
  96. UART1_RX_STA |= 0x4000;
  97. }
  98. else
  99. {
  100. // 否则将接收到的数据保存在缓存数组里
  101. UART1_RX_Buffer[UART1_RX_STA & 0X3FFF] = buf;
  102. UART1_RX_STA++;
  103. // 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收
  104. if(UART1_RX_STA > UART1_REC_LEN - 1)
  105. UART1_RX_STA = 0;
  106. }
  107. }
  108. }
  109. // 重新开启中断
  110. HAL_UART_Receive_IT(&huart1, &buf, 1);
  111. }
  112. }
  113. int fputc(int ch, FILE *f)
  114. {
  115. unsigned char temp[1]={ch};
  116. HAL_UART_Transmit(&huart1,temp,1,0xffff);
  117. return ch;
  118. }

main.c

  1. extern uint8_t buf;
  2. int main(void)
  3. {
  4. /* USER CODE BEGIN 1 */
  5. /* USER CODE END 1 */
  6. /* MCU Configuration--------------------------------------------------------*/
  7. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  8. HAL_Init();
  9. /* USER CODE BEGIN Init */
  10. /* USER CODE END Init */
  11. /* Configure the system clock */
  12. SystemClock_Config();
  13. /* USER CODE BEGIN SysInit */
  14. /* USER CODE END SysInit */
  15. /* Initialize all configured peripherals */
  16. MX_GPIO_Init();
  17. MX_USART1_UART_Init();
  18. /* USER CODE BEGIN 2 */
  19. HAL_UART_Receive_IT(&huart1, &buf, 1);//开启串口接收
  20. HAL_NVIC_SetPriority(SysTick_IRQn,0,0);//提高滴答定时器优先级
  21. /* USER CODE END 2 */
  22. /* Infinite loop */
  23. /* USER CODE BEGIN WHILE */
  24. while (1)
  25. {
  26. /* USER CODE END WHILE */
  27. Stop();
  28. /* USER CODE BEGIN 3 */
  29. }
  30. /* USER CODE END 3 */
  31. }

提高滴答定时器优先级,这样的话在串口中断中使用HAL_Delay()不会出现卡死的bug

为什么使用的是HAL_Delay()要设置滴答定时器的中断优先级呢?

从英文解释中(别说看不懂哈),Systick被配置为系统时基,并且被配置为了1ms,做技术,要有刨根问底的精神,奥利给,继续跟进去看看

 

 

 关于滴答定时器可以看这篇文章,us级延时怎么实现

(77条消息) HAL库与Cubemx系列|Systick-系统滴答定时器详解_hal库 systick 中断_小飞哥玩嵌入式的博客-CSDN博客https://blog.csdn.net/qq_16519885/article/details/117756815?ops_request_misc=&request_id=&biz_id=102&utm_term=HAL_Delay%E6%98%AF%E6%BB%B4%E7%AD%94%E5%AE%9A%E6%97%B6%E5%99%A8%E5%90%97&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-117756815.142%5Ev83%5Epc_search_v2,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

  1. //使用TIM2来做us级延时函数
  2. void TIM2_Delay_us(uint16_t n_us)
  3. {
  4. /* 使能定时器2计数 */
  5. __HAL_TIM_ENABLE(&htim2);
  6. __HAL_TIM_SetCounter(&htim2, 0);
  7. while(__HAL_TIM_GetCounter(&htim2) < ((1 * n_us)-1) );
  8. /* 关闭定时器2计数 */
  9. __HAL_TIM_DISABLE(&htim2);
  10. }

4.小车PWM调速, 

通过实验我们又发现了bug,如果是全速驱动的话,小车转弯,比如左转就会左边轮子不动右边动,理论上是这样的,但是实际上会出现小车转弯一卡一卡的bug,于是我想到了用PWM调速,转弯的时候左右轮分开调速,左转就左轮速度低于右轮,右转就右轮速度低于左轮

STM32与51单片机不同,STM32具有硬件PWM调速

根据查数据手册可知,我使用的是TIM2的CH1和CH2,CH1,CH2分别对应左右轮

更改后连线为

B-1A -- PA0
B-1B -- PB1
A-1A -- PA1
A-1B -- PB10

  

具体PWM可以参考之前写的PWM实现呼吸灯文章

(77条消息) [STM32F103C8T6]PWM呼吸灯_stm32f103c8t6呼吸灯_TX564的博客-CSDN博客https://blog.csdn.net/weixin_63303786/article/details/129047166?spm=1001.2014.3001.5502

 根据前面L9110s模块的运用可知,A-1A,A-1B,B-1A,B-2B是分别为一高一低电平才能驱动

当PWM调速时,PWM使用的TIM2对应端口PA0,PA1会出现占空比,占空比内为高电平,那么其他两个IO口就必须为低电平,才能驱动L9110s,所以PWM调速,必须将对应电机驱动的所有的IO口都拉低

 

 PWM主要用的两个函数 一个是PWM启动函数,一个是PWM比较函数

HAL_TIM_PWM_Start ( & htim2 , TIM_CHANNEL_1 );
HAL_TIM_PWM_Start ( & htim2 , TIM_CHANNEL_2 );
__HAL_TIM_SetCompare ( & htim2 , TIM_CHANNEL_1 , 8 );//8是PWM_Val的值,小于8都是高电平,大于8是低电平,这个数的值要小于设定的 CCRx
  1. void main()
  2. {
  3. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
  4. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
  5. while(1)
  6. {
  7. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,8);
  8. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,15);
  9. HAL_Delay(1000);
  10. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,15);
  11. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,8);
  12. HAL_Delay(1000);
  13. }
  14. }

解决过弯一卡一卡的bug就可以将

         __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,8);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,15);

替换motor.c中的GPIO_PIN_WritePin函数

5.小车循迹功能

小车循迹主要用到的模块是循迹模块

  

小车循迹主要是通过循迹模块,黑色会续收红外线,当红外线被吸收,就没法返回,于是模块输出高电平,灯灭,如果是白色区域,红外线会返回,灯亮,输出低电平

本次接线是将两个循迹模块的DO接入PB5,PB6

于是循迹的逻辑就是,左右两边各安一个循迹模块,当两个模块都返回红外线输出低电平灯亮的时候,小车向前进,如果左边模块没有返回红外线高电平灯灭,右边模块返回红外线低电平灯亮,那么就说明遇到左转弯道,左转,相反就右转----->哪边高电平往哪边转

  1. #define LeftWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3)
  2. #define RightWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4)
  3. void xunji(void)
  4. {
  5. if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_RESET)
  6. {
  7. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,19);
  8. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,19);
  9. }
  10. if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_RESET)
  11. {
  12. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,15);
  13. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,8);
  14. }
  15. if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_SET)
  16. {
  17. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,8);
  18. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,15);
  19. }
  20. if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_SET)
  21. {
  22. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1,0);
  23. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2,0);
  24. }
  25. }
  26. // main函数里
  27. while (1)
  28. {
  29. xunji();
  30. }

6.跟随功能

跟随功能的实现主要是跟随模块

 

 哪边低电平,往哪边转(因为跟随模块是有物体挡着才会返回红外线,返回为低电平)

  1. #define LeftWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5)
  2. #define RightWheel_Value HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)
  3. void gensui(void)
  4. {
  5. if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_RESET)
  6. goForward();
  7. if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_RESET)
  8. goRight();
  9. if(LeftWheel_Value == GPIO_PIN_RESET && RightWheel_Value == GPIO_PIN_SET)
  10. goLeft();
  11. if(LeftWheel_Value == GPIO_PIN_SET && RightWheel_Value == GPIO_PIN_SET)
  12. stop();
  13. }
  14. // main函数里
  15. while (1)
  16. {
  17. gensui();
  18. }

7.避障功能

避障功能主要是依靠SG90舵机和超声波实现的,当超声波测距小于一个值,比如35cm,SG90舵机开始转动角度,实现超声波摇头,摇头:显示中间位,然后左转,然后右转

超声波测距流程

1.Trig至少10us的高电平

2.发送波,定时器启动,开始计时

启动定时器的函数:HAL_TIM_Base_Start(&htim2);//启动定时器2

开始计算时间函数:__HAL_TIM_SetCounter(&htim2,0);//计算时间

2.1怎么判断是否发送波了呢?:当波发送出去echo会由低电平变为高电平 while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7) == GPIO_PIN_RESET);//卡低电平

3.接收到波,定时器关闭停止计时

停止定时器的函数:HAL_TIM_Base_Stop(&htim2);//停止定时器

3.1怎么判断接收到波了呢?:当接收到波echo会由高电平变为低电平

while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7) == GPIO_PIN_SET);//卡高电平

4.读取定时器计时时间

获取时间的函数:cnt = __HAL_TIM_GetCounter(&htim2);

5.distance = (340m/s * 时间)/2(注意换算单位 100cm/1000000us) 

 CSB.c

  1. //使用TIM2来做us级延时函数
  2. void TIM2_Delay_us(uint16_t n_us)
  3. {
  4. /* 使能定时器2计数 */
  5. __HAL_TIM_ENABLE(&htim2);
  6. __HAL_TIM_SetCounter(&htim2, 0);
  7. while(__HAL_TIM_GetCounter(&htim2) < ((1 * n_us)-1) );
  8. /* 关闭定时器2计数 */
  9. __HAL_TIM_DISABLE(&htim2);
  10. }
  11. double get_distance(void)
  12. {
  13. int cnt=0;
  14. //1. Trig ,给Trig端口至少10us的高电平
  15. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);//拉高
  16. TIM2_Delay_us(20);
  17. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);//拉低
  18. //2. echo由低电平跳转到高电平,表示开始发送波
  19. //波发出去的那一下,开始启动定时器
  20. while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET);//等待输入电平拉高
  21. HAL_TIM_Base_Start(&htim2);//启动定时器2
  22. __HAL_TIM_SetCounter(&htim2,0);//计算时间
  23. //3. 由高电平跳转回低电平,表示波回来了
  24. while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET);//等待输入电平变低
  25. //波回来的那一下,我们开始停止定时器
  26. HAL_TIM_Base_Stop(&htim2);//停止定时器
  27. //4. 计算出中间经过多少时间
  28. cnt = __HAL_TIM_GetCounter(&htim2);//获取时间
  29. //5. 距离 = 速度 (340m/s)* 时间/2(计数1次表示1us)
  30. return (cnt*340/2*0.000001*100); //单位:cm
  31. }

SG90.c

  1. void initSG90(void)
  2. {
  3. HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_4); //启动定时器4,启动PWM
  4. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 9); //将舵机置为90
  5. }
  6. void sgMiddle(void)
  7. {
  8. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 9); //将舵机置为75
  9. }
  10. void sgRight(void)
  11. {
  12. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 5); //将舵机置为0
  13. }
  14. void sgLeft(void)
  15. {
  16. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 14); //将舵机置为135
  17. }

main.c

  1. #define MIDDLE 0
  2. #define RIGHT 2
  3. #define LEFT 1
  4. int main(void)
  5. {
  6. /* USER CODE BEGIN 1 */
  7. char dir;
  8. double disMiddle;
  9. double disLeft;
  10. double disRight;
  11. /* USER CODE END 1 */
  12. /* MCU Configuration--------------------------------------------------------*/
  13. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  14. HAL_Init();
  15. /* USER CODE BEGIN Init */
  16. /* USER CODE END Init */
  17. /* Configure the system clock */
  18. SystemClock_Config();
  19. /* USER CODE BEGIN SysInit */
  20. /* USER CODE END SysInit */
  21. /* Initialize all configured peripherals */
  22. MX_GPIO_Init();
  23. MX_TIM4_Init();
  24. MX_TIM2_Init();
  25. /* USER CODE BEGIN 2 */
  26. initSG90();//一开始先让超声波在中间位
  27. HAL_Delay(1000);
  28. /* USER CODE END 2 */
  29. void csb(void)
  30. {
  31. /*为了不歪头卡死,每次必须测完回到中间位*/
  32. if(dir != MIDDLE){
  33. sgMiddle();
  34. dir = MIDDLE;
  35. HAL_Delay(300);
  36. }
  37. /*为了不歪头卡死,每次必须测完回到中间位*/
  38. disMiddle = get_distance();
  39. if(disMiddle > 35){
  40. //前进
  41. GoForward();
  42. }else if(disMiddle < 10){
  43. GoBack();
  44. }else
  45. {
  46. //停止
  47. Stop();
  48. //测左边距离
  49. sgLeft();
  50. HAL_Delay(300);
  51. disLeft = get_distance();
  52. sgMiddle();
  53. HAL_Delay(300);
  54. sgRight();
  55. dir = RIGHT;
  56. HAL_Delay(300);
  57. disRight = get_distance();
  58. if(disLeft < disRight){
  59. GoRight();
  60. HAL_Delay(150);
  61. Stop();
  62. }
  63. if(disRight < disLeft){
  64. GoLeft();
  65. HAL_Delay(150);
  66. Stop();
  67. }
  68. }
  69. }
  70. /* Infinite loop */
  71. /* USER CODE BEGIN WHILE */
  72. while (1)
  73. {
  74. /* USER CODE END WHILE */
  75. csb();
  76. HAL_Delay(50);//必须要延时,不然执行代码速度太快就会一直前进或者后退,不会摇头
  77. /* USER CODE BEGIN 3 */
  78. }
  79. /* USER CODE END 3 */
  80. }

将所有模块的函数封装好后,可以通过按键或者其他方式切换循迹,跟随,避障模式

  1. #define Key_On 0
  2. #define Key_Off 1
  3. unsigned char Scanf_Key(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  4. {
  5. unsigned int status;
  6. status = HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
  7. if(status == GPIO_PIN_RESET)
  8. return 0;
  9. else if(status == GPIO_PIN_SET)
  10. return 1;
  11. }
  12. void main()
  13. {
  14. while(1)
  15. {
  16. if(Scanf_Key(GPIOA,GPIO_PIN_0) == Key_On)
  17. {
  18. csb();
  19. }
  20. if(Scanf_Key(GPIOA,GPIO_PIN_1) == Key_On)
  21. {
  22. gensui();
  23. }
  24. if(Scanf_Key(GPIOA,GPIO_PIN_3) == Key_On)
  25. {
  26. xunji();
  27. }
  28. }
  29. }

测速功能

测速功能主要是用测速模块配合码盘实现

 

配置cubeMX

使用定时器计时1s,获取码盘每1s转了多少圈,通过计算可获得速度

 定时器公式:

T = {(PSC+1)*(Counter Period + 1)}/72000000HZ

轮子走一圈,经过一个周长, C = 2x3.14x 半径 = 3.14 x 直径( 6.5cm
对应的码盘也转了一圈,码盘有 20 个格子,每经过一个格子,会遮挡(高电平)和不遮挡(低电平),
那么一个脉冲就是走了 3.14 * 6.5 cm /20 = 1.0205CM
定时器可以设计成一秒,统计脉冲数,一个脉冲就是 1cm
假设一秒有 80 脉冲,那么就是 80cm/s

 开启定时器中断和外部中断

 代码实现

先找到定时器中断服务函数,外部中断服务函数,再重写回调函数

  1. void HAL_GPIO_EXIT_Callback(uint_t GPIO_pin)
  2. {
  3. if(GPIO_pin == GPIO_PIN_14)//判断是不是PB14产生的中断
  4. if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14) == GPIO_PIN_RESET)//判断PB14是否为低电平,从而判断是否产生下降沿
  5. SpeedCnt++;// dis = SpeedCnt * time 定时器1s产生中断所以dis = SpeedCnt
  6. }
  7. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  8. {
  9. printf("speed:%d\r\n",SpeedCnt);//重定向printf,打印到串口
  10. SpeedCnt = 0;
  11. }
  12. int main(void)
  13. {
  14. HAL_Init();
  15. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); //1ms
  16. SystemClock_Config();
  17. MX_GPIO_Init();
  18. MX_USART1_UART_Init();
  19. MX_TIM2_Init();
  20. HAL_UART_Receive_IT(&huart1, &buf, 1);//开启串口接收中断
  21. HAL_TIM_Base_Start_IT(&htim2);//开启定时器中断
  22. while (1)
  23. {
  24. }
  25. }

通过oled显示实时的车速

oled.c

  1. #include "oled.h"
  2. #include "i2c.h"
  3. #include "oledfont.h"
  4. void Oled_Write_Cmd(uint8_t dataCmd)
  5. {
  6. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
  7. &dataCmd, 1, 0xff);
  8. }
  9. void Oled_Write_Data(uint8_t dataData)
  10. {
  11. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
  12. &dataData, 1, 0xff);
  13. }
  14. void Oled_Init(void)
  15. {
  16. Oled_Write_Cmd(0xAE);//--display off
  17. Oled_Write_Cmd(0x00);//---set low column address
  18. Oled_Write_Cmd(0x10);//---set high column address
  19. Oled_Write_Cmd(0x40);//--set start line address
  20. Oled_Write_Cmd(0xB0);//--set page address
  21. Oled_Write_Cmd(0x81); // contract control
  22. Oled_Write_Cmd(0xFF);//--128
  23. Oled_Write_Cmd(0xA1);//set segment remap
  24. Oled_Write_Cmd(0xA6);//--normal / reverse
  25. Oled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
  26. Oled_Write_Cmd(0x3F);//--1/32 duty
  27. Oled_Write_Cmd(0xC8);//Com scan direction
  28. Oled_Write_Cmd(0xD3);//-set display offset
  29. Oled_Write_Cmd(0x00);//
  30. Oled_Write_Cmd(0xD5);//set osc division
  31. Oled_Write_Cmd(0x80);//
  32. Oled_Write_Cmd(0xD8);//set area color mode off
  33. Oled_Write_Cmd(0x05);//
  34. Oled_Write_Cmd(0xD9);//Set Pre-Charge Period
  35. Oled_Write_Cmd(0xF1);//
  36. Oled_Write_Cmd(0xDA);//set com pin configuartion
  37. Oled_Write_Cmd(0x12);//
  38. Oled_Write_Cmd(0xDB);//set Vcomh
  39. Oled_Write_Cmd(0x30);//
  40. Oled_Write_Cmd(0x8D);//set charge pump enable
  41. Oled_Write_Cmd(0x14);//
  42. Oled_Write_Cmd(0xAF);//--turn on oled panel
  43. }
  44. void Oled_Screen_Clear(void)
  45. {
  46. int i,n;
  47. Oled_Write_Cmd (0x20); //set memory addressing mode
  48. Oled_Write_Cmd (0x02); //page addressing mode
  49. for(i=0;i<8;i++){
  50. Oled_Write_Cmd(0xb0+i); //PAGE0 - PAGE7
  51. Oled_Write_Cmd(0x00); //每个page从第0列开始
  52. Oled_Write_Cmd(0x10); //每个page从第0列开始
  53. for(n=0;n<128;n++)Oled_Write_Data(0x00);//每个page127列全写0
  54. }
  55. }
  56. /*移植源代码*/
  57. /显示单个字符/
  58. void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
  59. unsigned int i;
  60. Oled_Write_Cmd(0xb0+(row*2-2)); //page 0
  61. Oled_Write_Cmd(0x00+(col&0x0f)); //low
  62. Oled_Write_Cmd(0x10+(col>>4)); //high
  63. for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
  64. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  65. }
  66. Oled_Write_Cmd(0xb0+(row*2-1)); //page 1
  67. Oled_Write_Cmd(0x00+(col&0x0f)); //low
  68. Oled_Write_Cmd(0x10+(col>>4)); //high
  69. for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
  70. Oled_Write_Data(F8X16[i]); //写数据oledTable1
  71. }
  72. }
  73. /显示字符串/
  74. void Oled_Show_Str(char row,char col,char *str){
  75. while(*str!=0){
  76. Oled_Show_Char(row,col,*str);
  77. str++;
  78. col += 8;
  79. }
  80. }

oledfont.h

  1. const unsigned char F8X16[]=
  2. {
  3. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  4. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  5. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  6. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  7. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  8. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  9. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  10. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  11. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  12. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  13. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  14. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  15. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  16. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  17. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  18. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  19. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  20. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  21. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  22. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  23. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  24. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  25. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  26. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  27. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  28. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  29. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  30. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  31. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  32. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  33. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  34. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  35. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  36. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  37. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  38. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  39. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  40. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  41. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  42. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  43. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  44. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  45. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  46. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  47. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  48. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  49. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  50. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  51. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  52. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  53. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  54. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  55. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  56. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  57. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  58. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  59. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  60. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  61. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  62. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  63. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  64. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  65. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  67. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  68. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  69. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  70. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  71. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  72. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  73. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  74. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  75. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  76. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  77. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  78. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  79. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  80. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  81. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  82. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  83. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  84. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  85. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  86. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  87. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  88. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  89. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  90. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  91. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  92. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  93. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  94. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  95. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  96. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  97. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  98. };
  1. #include "oled.h"
  2. #include "main.h"
  3. #include "i2c.h"
  4. #include "tim.h"
  5. #include "gpio.h"
  6. char speedMES[24];
  7. void HAL_GPIO_EXIT_Callback(uint_t GPIO_pin)
  8. {
  9. if(GPIO_pin == GPIO_PIN_14)//判断是不是PB14产生的中断
  10. if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14) == GPIO_PIN_RESET)//判断PB14是否为低电平,从而判断是否产生下降沿
  11. SpeedCnt++;// dis = SpeedCnt * time 定时器1s产生中断所以dis = SpeedCnt
  12. }
  13. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  14. {
  15. sprintf(speedMES,"speed:%dcm/s",SpeedCnt);//将数据以speed:xx的形式传给speedMES数组
  16. Oled_Show_Str(2,2,speedMES);
  17. printf("speed:%d\r\n",SpeedCnt);//重定向printf,打印到串口
  18. SpeedCnt = 0;
  19. }
  20. int main(void)
  21. {
  22. HAL_Init();
  23. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); //1ms
  24. SystemClock_Config();
  25. MX_GPIO_Init();
  26. MX_USART1_UART_Init();
  27. MX_TIM2_Init();
  28. HAL_UART_Receive_IT(&huart1, &buf, 1);//开启串口接收中断
  29. HAL_TIM_Base_Start_IT(&htim2);//开启定时器中断
  30. Oled_Init();
  31. Oled_Screen_Clear();//实时显示其实就是不断刷屏显示新的数据
  32. while (1)
  33. {
  34. }
  35. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46402
推荐阅读
相关标签
  

闽ICP备14008679号