当前位置:   article > 正文

基于stm32超声波+蓝牙一体式小车_stm32小车那个蓝牙和超声波测距可以同时用吗

stm32小车那个蓝牙和超声波测距可以同时用吗

最近制作了一款基于stm32的有两个功能的小车,自我感觉还算不错,于是想分享出来,如果能帮到你那我很开心。如果你感觉哪里有错误和需要改进的地方,欢迎发表意见,我会真诚接受并虚心学习!谢谢哦!

一、成品图片+思维导图

  1. 成品图片

  1. 思维导图

二、超声波小车

  1. 时钟模块

超声波的使用需要配置一个TIM定时器,是用来计算超声波模块的TRIG引脚发射脉冲信号到ECHO引脚接收到返回的脉冲信号这一区间段的时间(单位换算成秒),在我的这个项目中,我使用了TIM4来完成这一步骤。具体配置情况请详见下面的配置函数。(注意!!!第二段代码是TIM4的定时器中断函数,请放在使用TIM4的地方即可)

  1. void Timer4_Init(void)
  2. {
  3. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
  4. TIM_InternalClockConfig(TIM4);/*使用内部时钟TIM4*/
  5. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  6. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
  7. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  8. TIM_TimeBaseInitStructure.TIM_Period = 1000 - 1;
  9. TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;
  10. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  11. TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
  12. TIM_ClearFlag(TIM4,TIM_FLAG_Update);
  13. /*打开中断*/
  14. TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
  15. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC优先级分组2
  16. NVIC_InitTypeDef NVIC_InitStructure;
  17. NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  18. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  21. NVIC_Init(&NVIC_InitStructure);
  22. TIM_Cmd(TIM4,ENABLE);/*使能定时器4*/
  23. }
  24. uint16_t Timer4_GetCounter(void) //获取定时器4的值
  25. {
  26. return TIM_GetCounter(TIM4);
  27. }
  1. void TIM4_IRQHandler(void) //定时器4中断
  2. {
  3. if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
  4. {
  5. TIM_ClearITPendingBit(TIM4,TIM_IT_Update);//更新中断
  6. count++;
  7. }
  8. }
  1. 超声波模块

超声波的配置也是相对简单,先给TRIG引脚一个高电平,在使用延时函数延时20微秒,打开TIM4计时,再检测ECHO是否接收,若接收,获取TIM4的计时结果,若为接收,则等待接收。具体请详见下面代码。

  1. /*记录定时器溢出次数*/
  2. uint16_t count = 0;
  3. void Ultrasonic_GPIO_Init(void)
  4. {
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. /*TRIG触发信号*/
  8. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  9. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  10. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
  11. GPIO_Init(GPIOB, &GPIO_InitStructure);
  12. /*ECOH回响信号*/
  13. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
  14. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
  15. GPIO_Init(GPIOB, & GPIO_InitStructure);
  16. }
  17. float Get_Ultrasonic_distance(void)
  18. {
  19. Timer4_Init();
  20. Ultrasonic_GPIO_Init();
  21. float distance = 0 ,sum = 0;
  22. uint32_t tim = 0 ;
  23. uint8_t i = 0;
  24. while(i!=5)        //连续测5次,求平均值,使结果准确
  25. {
  26. GPIO_ResetBits(GPIOB, GPIO_Pin_15);//先置为低电平
  27. GPIO_SetBits(GPIOB, GPIO_Pin_15); //拉高信号,作为触发信号
  28. Delay_us(20); //高电平信号超过10us
  29. GPIO_ResetBits(GPIOB, GPIO_Pin_15);
  30. /*等待回响信号,回响信号到来*/
  31. while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == RESET);
  32. TIM_SetCounter(TIM4,0); //将TIM4计数寄存器的计数值清零
  33. TIM_Cmd(TIM4,ENABLE);//开启定时器计数
  34. i+=1;
  35. /*回响信号消失*/
  36. while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == SET);
  37. TIM_Cmd(TIM4, DISABLE);//关闭定时器
  38. tim = Timer4_GetCounter();//获取计TIM4数寄存器中的计数值,以便计算回响信号时间
  39. distance = ((float)(tim + count * 1000)) / 58.0;//通过回响信号计算距离
  40. sum += distance;
  41. //TIM4->CNT = 0;
  42. TIM_SetCounter(TIM4,0); //将TIM4计数寄存器的计数值清零
  43. count = 0; //中断溢出次数清零
  44. Delay_ms(20);
  45. }
  46. distance = (sum / 5.0); //单位cm
  47. return distance; //距离作为函数返回值
  48. }
  49. void TIM4_IRQHandler(void) //定时器4中断
  50. {
  51. if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
  52. {
  53. TIM_ClearITPendingBit(TIM4,TIM_IT_Update);//更新中断
  54. count++;
  55. }
  56. }//这里使用了中断函数,就放这里
  1. 逻辑函数

逻辑函数就是小车收到数据该如何转向的问题,请详见下面的代码,注释很详细的。

  1. void Ultrasonic_car(void)
  2. {
  3. int8_t Speed = 50; /*速度范围为[0,100]*/
  4. float left_distance = 0.0;
  5. float right_distance = 0.0;
  6. float front_distance = 0.0;
  7. Delay_ms(500);
  8. SG90_Front();//舵机面向前方
  9. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  10. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  11. SG90_Left();//舵机转向左边
  12. Delay_ms(500);
  13. left_distance = Get_Ultrasonic_distance();//测出左边的距离
  14. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  15. SG90_Right();//舵机转向右边
  16. Delay_ms(500);
  17. right_distance = Get_Ultrasonic_distance();//测出右边的距离
  18. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  19. SG90_Front();//舵机面向前方
  20. if((front_distance>left_distance)&&(front_distance>right_distance))
  21. {
  22. Car_Forword(Speed);
  23. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  24. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  25. }
  26. else if((left_distance>front_distance)&&(left_distance>right_distance))
  27. {
  28. Car_Left(Speed);
  29. Delay_ms(500);
  30. Car_Forword(Speed);
  31. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  32. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  33. }
  34. else if((right_distance>front_distance)&&(right_distance>left_distance))
  35. {
  36. Car_Right(Speed);
  37. Delay_ms(500);
  38. Car_Forword(Speed);
  39. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  40. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  41. }
  42. else Car_Stop();
  43. while(1)
  44. {
  45. OLED_ShowNum(2,7,Speed,3);
  46. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  47. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  48. if(front_distance<=50)
  49. {
  50. Car_Stop();
  51. SG90_Left();//舵机转向左边
  52. Delay_ms(800);
  53. left_distance = Get_Ultrasonic_distance();//测出左边的距离
  54. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  55. SG90_Right();//舵机转向y边
  56. Delay_ms(800);
  57. right_distance = Get_Ultrasonic_distance();//测出y边的距离
  58. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  59. SG90_Front();//舵机面向前方
  60. front_distance = Get_Ultrasonic_distance();//测出前方的距离
  61. OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
  62. if(left_distance>right_distance)
  63. {
  64. Car_Left(Speed);
  65. Delay_ms(500);
  66. Car_Stop();
  67. Delay_s(1);
  68. Car_Forword(Speed);
  69. }
  70. else if((left_distance<right_distance))
  71. {
  72. Car_Right(Speed);
  73. Delay_ms(500);
  74. Car_Stop();
  75. Delay_s(1);
  76. Car_Forword(Speed);
  77. }
  78. else
  79. {
  80. Car_Back(Speed);
  81. Delay_ms(800);
  82. Car_Left(Speed);
  83. Delay_ms(800);
  84. Car_Forword(Speed);
  85. }
  86. }
  87. }
  88. }

写到这里,超声波小车就结束了,重点很少,主要是得理解思路。上面函数中出现的其他函数的调用,会在下面(五、通用功能的函数文件)给出C文件的。

三、蓝牙小车(具体用到的GPIO请见上面的思维导图)(注意!!!5V供电)

用到的模块是HC-05蓝牙模块,蓝牙模块上的TXD引脚和RXD引脚分别接stm32的USART串口上的RXD和TXD对应的引脚。(我使用的是STM32F103C8T6,它的USART1的RXD和TXD分别是GPIOA9和GPIOA10),具体请见下面的代码。

  1. 串口初始化模块

  1. void Serial_Init(void)
  2. {
  3. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  5. GPIO_InitTypeDef GPIO_InitStructure;
  6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9. GPIO_Init(GPIOA, &GPIO_InitStructure);
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOA, &GPIO_InitStructure);
  14. USART_InitTypeDef USART_InitStructure;
  15. USART_InitStructure.USART_BaudRate = 9600;
  16. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  17. USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  18. USART_InitStructure.USART_Parity = USART_Parity_No;
  19. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  20. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  21. USART_Init(USART1, &USART_InitStructure);
  22. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  23. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  24. NVIC_InitTypeDef NVIC_InitStructure;
  25. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  26. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  27. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  28. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  29. NVIC_Init(&NVIC_InitStructure);
  30. USART_Cmd(USART1, ENABLE);
  31. }
  1. 蓝牙模块(发送和接收数据的函数)

  1. uint8_t Serial_RxData;
  2. uint8_t Serial_RxFlag;
  3. void Serial_SendByte(uint8_t Byte)    //发送一个字节
  4. {
  5. USART_SendData(USART1, Byte);
  6. while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  7. }
  8. void Serial_SendArray(uint8_t *Array, uint16_t Length) //发送一个数组
  9. {
  10. uint16_t i;
  11. for (i = 0; i < Length; i ++)
  12. {
  13. Serial_SendByte(Array[i]);
  14. }
  15. }
  16. void Serial_SendString(char *String) //发送一个字符串
  17. {
  18. uint8_t i;
  19. for (i = 0; String[i] != '\0'; i ++)
  20. {
  21. Serial_SendByte(String[i]);
  22. }
  23. }
  24. void Serial_SendNumber(uint32_t Number, uint8_t Length) //发送一串数字
  25. {
  26. uint8_t i;
  27. for (i = 0; i < Length; i ++)
  28. {
  29. Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
  30. }
  31. }
  32. uint8_t Serial_GetRxFlag(void) //获取中断标志位
  33. {
  34. if (Serial_RxFlag == 1)
  35. {
  36. Serial_RxFlag = 0;
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. uint8_t Serial_GetRxData(void) //获取收到的数据
  42. {
  43. return Serial_RxData;
  44. }
  45. void USART1_IRQHandler(void) //串口USART1的中断
  46. {
  47. if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  48. {
  49. Serial_RxData = USART_ReceiveData(USART1);
  50. Serial_RxFlag = 1;
  51. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  52. }
  53. }
  1. 逻辑函数(手机连接蓝牙模块,发送指令使小车完成相应的动作)

  1. uint8_t RxData=0;
  2. void Hc05_car(void)
  3. {
  4. while(1)
  5. {
  6. if(Serial_GetRxFlag() == 1)
  7. {
  8. RxData=Serial_GetRxData();
  9. //OLED_ShowHexNum(3,1,RxData,2);
  10. }
  11. switch(RxData)
  12. {
  13. case 1:Car_Forword(100);break;
  14. case 2:Car_Stop();break;
  15. case 3:Car_Back(100);break;
  16. case 4:Car_Stop();break;
  17. case 5:Car_Left(100);break;
  18. case 6:Car_Stop();break;
  19. case 7:Car_Right(100);break;
  20. case 8:Car_Stop();break;
  21. case 9:Buzzer_ON1();break;
  22. case 10:Buzzer_OFF();break;
  23. case 11:LED_ON();break;
  24. case 12:LED_OFF();break;
  25. case 13:LED_Flash_ON();break;
  26. case 14:LED_Flash_OFF();break;
  27. }
  28. }
  29. }

四、其他模块和功能

  1. OLED显示屏(四引脚)(温馨提示!!!关于这个模块掌握的还不够好,但是我可以分享出大佬代码,供大家参考。。。共有三个文件,大家直接复制粘贴用即可)

OLED.c

  1. #include "stm32f10x.h"
  2. #include "OLED_Font.h"
  3. /*引脚配置*/
  4. #define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
  5. #define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
  6. /*引脚初始化*/
  7. void OLED_I2C_Init(void)
  8. {
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  14. GPIO_Init(GPIOB, &GPIO_InitStructure);
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  16. GPIO_Init(GPIOB, &GPIO_InitStructure);
  17. OLED_W_SCL(1);
  18. OLED_W_SDA(1);
  19. }
  20. /**
  21. * @brief I2C开始
  22. * @param 无
  23. * @retval 无
  24. */
  25. void OLED_I2C_Start(void)
  26. {
  27. OLED_W_SDA(1);
  28. OLED_W_SCL(1);
  29. OLED_W_SDA(0);
  30. OLED_W_SCL(0);
  31. }
  32. /**
  33. * @brief I2C停止
  34. * @param 无
  35. * @retval 无
  36. */
  37. void OLED_I2C_Stop(void)
  38. {
  39. OLED_W_SDA(0);
  40. OLED_W_SCL(1);
  41. OLED_W_SDA(1);
  42. }
  43. /**
  44. * @brief I2C发送一个字节
  45. * @param Byte 要发送的一个字节
  46. * @retval 无
  47. */
  48. void OLED_I2C_SendByte(uint8_t Byte)
  49. {
  50. uint8_t i;
  51. for (i = 0; i < 8; i++)
  52. {
  53. OLED_W_SDA(Byte & (0x80 >> i));
  54. OLED_W_SCL(1);
  55. OLED_W_SCL(0);
  56. }
  57. OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
  58. OLED_W_SCL(0);
  59. }
  60. /**
  61. * @brief OLED写命令
  62. * @param Command 要写入的命令
  63. * @retval 无
  64. */
  65. void OLED_WriteCommand(uint8_t Command)
  66. {
  67. OLED_I2C_Start();
  68. OLED_I2C_SendByte(0x78); //从机地址
  69. OLED_I2C_SendByte(0x00); //写命令
  70. OLED_I2C_SendByte(Command);
  71. OLED_I2C_Stop();
  72. }
  73. /**
  74. * @brief OLED写数据
  75. * @param Data 要写入的数据
  76. * @retval 无
  77. */
  78. void OLED_WriteData(uint8_t Data)
  79. {
  80. OLED_I2C_Start();
  81. OLED_I2C_SendByte(0x78); //从机地址
  82. OLED_I2C_SendByte(0x40); //写数据
  83. OLED_I2C_SendByte(Data);
  84. OLED_I2C_Stop();
  85. }
  86. /**
  87. * @brief OLED设置光标位置
  88. * @param Y 以左上角为原点,向下方向的坐标,范围:0~7
  89. * @param X 以左上角为原点,向右方向的坐标,范围:0~127
  90. * @retval 无
  91. */
  92. void OLED_SetCursor(uint8_t Y, uint8_t X)
  93. {
  94. OLED_WriteCommand(0xB0 | Y); //设置Y位置
  95. OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置低4位
  96. OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置高4位
  97. }
  98. /**
  99. * @brief OLED清屏
  100. * @param 无
  101. * @retval 无
  102. */
  103. void OLED_Clear(void)
  104. {
  105. uint8_t i, j;
  106. for (j = 0; j < 8; j++)
  107. {
  108. OLED_SetCursor(j, 0);
  109. for(i = 0; i < 128; i++)
  110. {
  111. OLED_WriteData(0x00);
  112. }
  113. }
  114. }
  115. /**
  116. * @brief OLED显示一个字符
  117. * @param Line 行位置,范围:1~4
  118. * @param Column 列位置,范围:1~16
  119. * @param Char 要显示的一个字符,范围:ASCII可见字符
  120. * @retval 无
  121. */
  122. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
  123. {
  124. uint8_t i;
  125. OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
  126. for (i = 0; i < 8; i++)
  127. {
  128. OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
  129. }
  130. OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
  131. for (i = 0; i < 8; i++)
  132. {
  133. OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
  134. }
  135. }
  136. /**
  137. * @brief OLED显示字符串
  138. * @param Line 起始行位置,范围:1~4
  139. * @param Column 起始列位置,范围:1~16
  140. * @param String 要显示的字符串,范围:ASCII可见字符
  141. * @retval 无
  142. */
  143. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
  144. {
  145. uint8_t i;
  146. for (i = 0; String[i] != '\0'; i++)
  147. {
  148. OLED_ShowChar(Line, Column + i, String[i]);
  149. }
  150. }
  151. /**
  152. * @brief OLED次方函数
  153. * @retval 返回值等于X的Y次方
  154. */
  155. uint32_t OLED_Pow(uint32_t X, uint32_t Y)
  156. {
  157. uint32_t Result = 1;
  158. while (Y--)
  159. {
  160. Result *= X;
  161. }
  162. return Result;
  163. }
  164. /**
  165. * @brief OLED显示数字(十进制,正数)
  166. * @param Line 起始行位置,范围:1~4
  167. * @param Column 起始列位置,范围:1~16
  168. * @param Number 要显示的数字,范围:0~4294967295
  169. * @param Length 要显示数字的长度,范围:1~10
  170. * @retval 无
  171. */
  172. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  173. {
  174. uint8_t i;
  175. for (i = 0; i < Length; i++)
  176. {
  177. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
  178. }
  179. }
  180. /**
  181. * @brief OLED显示数字(十进制,带符号数)
  182. * @param Line 起始行位置,范围:1~4
  183. * @param Column 起始列位置,范围:1~16
  184. * @param Number 要显示的数字,范围:-2147483648~2147483647
  185. * @param Length 要显示数字的长度,范围:1~10
  186. * @retval 无
  187. */
  188. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
  189. {
  190. uint8_t i;
  191. uint32_t Number1;
  192. if (Number >= 0)
  193. {
  194. OLED_ShowChar(Line, Column, '+');
  195. Number1 = Number;
  196. }
  197. else
  198. {
  199. OLED_ShowChar(Line, Column, '-');
  200. Number1 = -Number;
  201. }
  202. for (i = 0; i < Length; i++)
  203. {
  204. OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
  205. }
  206. }
  207. /**
  208. * @brief OLED显示数字(十六进制,正数)
  209. * @param Line 起始行位置,范围:1~4
  210. * @param Column 起始列位置,范围:1~16
  211. * @param Number 要显示的数字,范围:0~0xFFFFFFFF
  212. * @param Length 要显示数字的长度,范围:1~8
  213. * @retval 无
  214. */
  215. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  216. {
  217. uint8_t i, SingleNumber;
  218. for (i = 0; i < Length; i++)
  219. {
  220. SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
  221. if (SingleNumber < 10)
  222. {
  223. OLED_ShowChar(Line, Column + i, SingleNumber + '0');
  224. }
  225. else
  226. {
  227. OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
  228. }
  229. }
  230. }
  231. /**
  232. * @brief OLED显示数字(二进制,正数)
  233. * @param Line 起始行位置,范围:1~4
  234. * @param Column 起始列位置,范围:1~16
  235. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
  236. * @param Length 要显示数字的长度,范围:1~16
  237. * @retval 无
  238. */
  239. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  240. {
  241. uint8_t i;
  242. for (i = 0; i < Length; i++)
  243. {
  244. OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
  245. }
  246. }
  247. /**
  248. * @brief OLED初始化
  249. * @param 无
  250. * @retval 无
  251. */
  252. void OLED_Init(void)
  253. {
  254. uint32_t i, j;
  255. for (i = 0; i < 1000; i++) //上电延时
  256. {
  257. for (j = 0; j < 1000; j++);
  258. }
  259. OLED_I2C_Init(); //端口初始化
  260. OLED_WriteCommand(0xAE); //关闭显示
  261. OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
  262. OLED_WriteCommand(0x80);
  263. OLED_WriteCommand(0xA8); //设置多路复用率
  264. OLED_WriteCommand(0x3F);
  265. OLED_WriteCommand(0xD3); //设置显示偏移
  266. OLED_WriteCommand(0x00);
  267. OLED_WriteCommand(0x40); //设置显示开始行
  268. OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
  269. OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
  270. OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
  271. OLED_WriteCommand(0x12);
  272. OLED_WriteCommand(0x81); //设置对比度控制
  273. OLED_WriteCommand(0xCF);
  274. OLED_WriteCommand(0xD9); //设置预充电周期
  275. OLED_WriteCommand(0xF1);
  276. OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
  277. OLED_WriteCommand(0x30);
  278. OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
  279. OLED_WriteCommand(0xA6); //设置正常/倒转显示
  280. OLED_WriteCommand(0x8D); //设置充电泵
  281. OLED_WriteCommand(0x14);
  282. OLED_WriteCommand(0xAF); //开启显示
  283. OLED_Clear(); //OLED清屏
  284. }

OLED.h

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. void OLED_Init(void);
  4. void OLED_Clear(void);
  5. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  6. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  7. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  8. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  9. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  10. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  11. #endif

OLED_Font.h

  1. #ifndef __OLED_FONT_H
  2. #define __OLED_FONT_H
  3. /*OLED字模库,宽8像素,高16像素*/
  4. const uint8_t OLED_F8x16[][16]=
  5. {
  6. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  8. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
  9. 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  10. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
  11. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  12. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
  13. 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  14. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
  15. 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  16. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
  17. 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  18. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
  19. 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  20. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
  21. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  22. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
  23. 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  24. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
  25. 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  26. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
  27. 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  28. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
  29. 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  36. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
  37. 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  38. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  39. 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  40. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
  41. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  42. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
  43. 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  44. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
  45. 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  46. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
  47. 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  48. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
  49. 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  50. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
  51. 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  52. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
  53. 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  54. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
  55. 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  56. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  57. 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  58. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
  59. 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  60. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  62. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
  63. 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  64. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
  65. 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  66. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
  67. 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  68. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
  69. 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  70. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
  71. 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  72. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
  73. 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  74. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
  75. 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  76. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
  77. 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  78. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
  79. 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  80. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  81. 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  82. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  83. 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  84. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
  85. 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  86. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  87. 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  88. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
  89. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  90. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
  91. 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  92. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
  93. 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  94. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
  95. 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  96. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
  97. 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  98. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
  99. 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  100. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  101. 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  102. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
  103. 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  104. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  105. 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  106. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
  107. 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  108. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
  109. 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  110. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
  111. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  112. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  113. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  114. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
  115. 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  116. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
  117. 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  118. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
  119. 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  120. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
  121. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  122. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
  123. 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  124. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
  125. 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  126. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
  127. 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  128. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
  129. 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  130. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
  131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  133. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  134. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
  135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  136. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  137. 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  138. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
  139. 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  140. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
  141. 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  142. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
  143. 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  144. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  145. 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  146. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
  147. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  148. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  149. 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  150. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
  151. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  152. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
  153. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  154. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
  155. 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  156. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
  157. 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  158. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
  159. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  160. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  161. 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  162. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
  163. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  164. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  165. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  166. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
  167. 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  168. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
  169. 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  170. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  171. 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  172. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  173. 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  174. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
  175. 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  176. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
  177. 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  178. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  179. 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  180. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
  181. 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  182. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  183. 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  184. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  185. 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  186. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  187. 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  188. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
  189. 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  190. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  191. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  192. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
  193. 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  194. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
  195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  196. };
  197. #endif
  1. 舵机(注意5V供电!!!橙色接PWM,红色接5V,黑色接GND)

我使用的舵机模块是SG90,并使用stm32的TIM3的通道1输出PWM信号来控制旋转角度和方向,具体请详见代码和注释。

  1. /*这里用TIM3的通道1来产生PWM信号调速舵机的旋转角度*/
  2. void PWM_TIM3_Init(void)/*这个是TIM3的配置,和上面的说明一样*/
  3. {
  4. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  10. GPIO_Init(GPIOA,&GPIO_InitStructure);
  11. TIM_InternalClockConfig(TIM3);
  12. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  13. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  14. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  15. TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
  16. TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
  17. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  18. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
  19. TIM_OCInitTypeDef TIM_OCInitStructure;
  20. TIM_OCStructInit(&TIM_OCInitStructure);
  21. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  22. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
  23. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  24. TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
  25. TIM_OC1Init(TIM3,&TIM_OCInitStructure);
  26. TIM_Cmd(TIM3,ENABLE);
  27. }
  28. /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
  29. void PWM_TIM3_SetCompare1(uint16_t Compare) //TIM3 的通道1
  30. {
  31. TIM_SetCompare1(TIM3,Compare);
  32. }
  1. L298N(我在关于PWM的讲解有介绍)

这里我放上小车的两路电机(4个电机,因为我采用并联方法,使一OUT1/OUT2,OUT3/OUT4分别驱动两个电机)所用到的PWM调速的文件。其中使用定时器TIM2的通道1和通道2

  1. void PWM_TIM2_Init(void)/*配置TIM用来输出PWM波形时,要参考使用手册,因为它的GPIO口是官方确定的*/
  2. {
  3. /*打开TIM和GPIO的时钟*/
  4. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. /*配置GPIO的输出模式、GPIO口、频率*/
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/*复用推挽输出*/
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOA,&GPIO_InitStructure);/*GPIO初始化*/
  12. /**/
  13. TIM_InternalClockConfig(TIM2);
  14. /*这里通过ARR,PSC,CCR的值来计算计时频率和占空比的值*/
  15. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  16. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  17. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  18. TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
  19. TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
  20. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;/*确定占空比*/
  21. TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
  22. /*这里配置输出比较单元*/
  23. TIM_OCInitTypeDef TIM_OCInitStructure;
  24. TIM_OCStructInit(&TIM_OCInitStructure);
  25. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  26. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
  27. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  28. TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
  29. /*输出PWM波形的GPIO口的初始化*/
  30. TIM_OC1Init(TIM2,&TIM_OCInitStructure); /*非常重要且容易忘记,配置好PWM的gpio口后一定要记得初始化*/
  31. TIM_OC2Init(TIM2,&TIM_OCInitStructure);
  32. /*使能TIM2*/
  33. TIM_Cmd(TIM2,ENABLE);
  34. }
  35. /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
  36. void PWM_TIM2_SetCompare1(uint16_t Compare) //TIM2 的通道1
  37. {
  38. TIM_SetCompare1(TIM2,Compare);/*很重要,改变占空比参数就靠这个函数*/
  39. }
  40. void PWM_TIM2_SetCompare2(uint16_t Compare) //TIM2 的通道2
  41. {
  42. TIM_SetCompare2(TIM2,Compare);
  43. }
  1. 电源(12V锂电池组,可充电)

  1. 多路输出的电源模块(稳压模块)

由于电源是12V,但是STM32最小系统板供电是3.3V,而且舵机和超声波是5V供电,所以我觉得这个模块是必不可少的东西。

五、通用功能的函数文件

  1. 延时函数

  1. #include "stm32f10x.h"
  2. /**
  3. * @brief 微秒级延时
  4. * @param xus 延时时长,范围:0~233015
  5. * @retval 无
  6. */
  7. void Delay_us(uint32_t xus)
  8. {
  9. SysTick->LOAD = 72 * xus; //设置定时器重装值
  10. SysTick->VAL = 0x00; //清空当前计数值
  11. SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
  12. while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
  13. SysTick->CTRL = 0x00000004; //关闭定时器
  14. }
  15. /**
  16. * @brief 毫秒级延时
  17. * @param xms 延时时长,范围:0~4294967295
  18. * @retval 无
  19. */
  20. void Delay_ms(uint32_t xms)
  21. {
  22. while(xms--)
  23. {
  24. Delay_us(1000);
  25. }
  26. }
  27. /**
  28. * @brief 秒级延时
  29. * @param xs 延时时长,范围:0~4294967295
  30. * @retval 无
  31. */
  32. void Delay_s(uint32_t xs)
  33. {
  34. while(xs--)
  35. {
  36. Delay_ms(1000);
  37. }
  38. }
  1. 直流电机函数

  1. #include "stm32f10x.h" // Device header
  2. #include "Motor.h"
  3. #include "PWM.h"
  4. void Motor_R_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOA,&GPIO_InitStructure);
  12. PWM_TIM2_Init();
  13. }
  14. void Motor_L_Init(void)
  15. {
  16. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  19. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
  20. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  21. GPIO_Init(GPIOA,&GPIO_InitStructure);
  22. PWM_TIM2_Init();
  23. }
  24. void Motor_R_SetSpeed(int8_t Speed)
  25. {
  26. if(Speed>=0)
  27. {
  28. GPIO_SetBits(GPIOA,GPIO_Pin_2);
  29. GPIO_ResetBits(GPIOA,GPIO_Pin_3);
  30. PWM_TIM2_SetCompare1(Speed);
  31. }
  32. else
  33. {
  34. GPIO_SetBits(GPIOA,GPIO_Pin_3);
  35. GPIO_ResetBits(GPIOA,GPIO_Pin_2);
  36. PWM_TIM2_SetCompare1(-Speed);
  37. }
  38. }
  39. void Motor_L_SetSpeed(int8_t Speed)
  40. {
  41. if(Speed>=0)
  42. {
  43. GPIO_SetBits(GPIOA,GPIO_Pin_4);
  44. GPIO_ResetBits(GPIOA,GPIO_Pin_5);
  45. PWM_TIM2_SetCompare2(Speed);
  46. }
  47. else
  48. {
  49. GPIO_SetBits(GPIOA,GPIO_Pin_5);
  50. GPIO_ResetBits(GPIOA,GPIO_Pin_4);
  51. PWM_TIM2_SetCompare2(-Speed);
  52. }
  53. }
  1. 小车运动函数

  1. #include "stm32f10x.h" // Device header
  2. #include "Car.h"
  3. #include "Motor.h"
  4. void Car_Init(void)
  5. {
  6. Motor_R_Init();
  7. Motor_L_Init();
  8. }
  9. void Car_Forword(uint8_t Speed)
  10. {
  11. Motor_R_SetSpeed(Speed);
  12. Motor_L_SetSpeed(Speed);
  13. }
  14. void Car_Back(uint8_t Speed)
  15. {
  16. Motor_R_SetSpeed(-Speed);
  17. Motor_L_SetSpeed(-Speed);
  18. }
  19. void Car_Left(uint8_t Speed)
  20. {
  21. Motor_R_SetSpeed(Speed);
  22. Motor_L_SetSpeed(-Speed);
  23. }
  24. void Car_Right(uint8_t Speed)
  25. {
  26. Motor_R_SetSpeed(-Speed);
  27. Motor_L_SetSpeed(Speed);
  28. }
  29. void Car_Stop(void)
  30. {
  31. Motor_R_SetSpeed(0);
  32. Motor_L_SetSpeed(0);
  33. }
  1. PWM调速(我有对PWM专门的讲解文章,不懂的话可以去看看关于PWM的讲解

  1. #include "stm32f10x.h" // Device header
  2. #include "PWM.h"
  3. void PWM_TIM2_Init(void)/*配置TIM用来输出PWM波形时,要参考使用手册,因为它的GPIO口是官方确定的*/
  4. {
  5. /*打开TIM和GPIO的时钟*/
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  8. /*配置GPIO的输出模式、GPIO口、频率*/
  9. GPIO_InitTypeDef GPIO_InitStructure;
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/*复用推挽输出*/
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOA,&GPIO_InitStructure);/*GPIO初始化*/
  14. /**/
  15. TIM_InternalClockConfig(TIM2);
  16. /*这里通过ARR,PSC,CCR的值来计算计时频率和占空比的值*/
  17. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  18. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  19. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  20. TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
  21. TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
  22. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;/*确定占空比*/
  23. TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
  24. /*这里配置输出比较单元*/
  25. TIM_OCInitTypeDef TIM_OCInitStructure;
  26. TIM_OCStructInit(&TIM_OCInitStructure);
  27. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  28. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
  29. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  30. TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
  31. /*输出PWM波形的GPIO口的初始化*/
  32. TIM_OC1Init(TIM2,&TIM_OCInitStructure); /*非常重要且容易忘记,配置好PWM的gpio口后一定要记得初始化*/
  33. TIM_OC2Init(TIM2,&TIM_OCInitStructure);
  34. /*使能TIM2*/
  35. TIM_Cmd(TIM2,ENABLE);
  36. }
  37. /*这里用TIM3的通道1来产生PWM信号调速舵机的旋转角度*/
  38. void PWM_TIM3_Init(void)/*这个是TIM3的配置,和上面的说明一样*/
  39. {
  40. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  41. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  42. GPIO_InitTypeDef GPIO_InitStructure;
  43. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  44. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  45. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  46. GPIO_Init(GPIOA,&GPIO_InitStructure);
  47. TIM_InternalClockConfig(TIM3);
  48. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  49. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  50. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  51. TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
  52. TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
  53. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  54. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
  55. TIM_OCInitTypeDef TIM_OCInitStructure;
  56. TIM_OCStructInit(&TIM_OCInitStructure);
  57. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  58. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
  59. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  60. TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
  61. TIM_OC1Init(TIM3,&TIM_OCInitStructure);
  62. TIM_Cmd(TIM3,ENABLE);
  63. }
  64. /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
  65. void PWM_TIM2_SetCompare1(uint16_t Compare) //TIM2 的通道1
  66. {
  67. TIM_SetCompare1(TIM2,Compare);/*很重要,改变占空比参数就靠这个函数*/
  68. }
  69. void PWM_TIM2_SetCompare2(uint16_t Compare) //TIM2 的通道2
  70. {
  71. TIM_SetCompare2(TIM2,Compare);
  72. }
  73. void PWM_TIM3_SetCompare1(uint16_t Compare) //TIM3 的通道1
  74. {
  75. TIM_SetCompare1(TIM3,Compare);
  76. }
  1. 按键函数

  1. #include "stm32f10x.h" // Device header
  2. #include "Key.h"
  3. #include "Delay.h"
  4. void Key_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_InitStructure);
  12. }
  13. uint8_t Key_GetNum(void)
  14. {
  15. uint8_t KeyNum = 0;
  16. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3) == 0)
  17. {
  18. Delay_ms(20);
  19. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3) == 0);
  20. Delay_ms(20);
  21. KeyNum=1;
  22. }
  23. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5) == 0)
  24. {
  25. Delay_ms(20);
  26. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5) == 0);
  27. Delay_ms(20);
  28. KeyNum=2;
  29. }
  30. return KeyNum;
  31. }
  1. 蜂鸣器

  1. #include "stm32f10x.h" // Device header
  2. #include "Buzzer.h"
  3. #include "Delay.h"
  4. void Buzzer_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_InitStructure);
  12. }
  13. void Buzzer_OFF(void) //关闭蜂鸣器
  14. {
  15. Buzzer_Init();
  16. GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
  17. }
  18. void Buzzer_ON1(void)
  19. {
  20. Buzzer_Init();
  21. GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为d电平
  22. }
  23. void Buzzer_ON2(void) //打开蜂鸣器
  24. {
  25. Buzzer_Init();
  26. GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为低电平
  27. Delay_ms(100);
  28. GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
  29. Delay_ms(100);
  30. GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为低电平
  31. Delay_ms(100);
  32. GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
  33. Delay_ms(700);
  34. }
  1. 提示灯

  1. #include "stm32f10x.h" // Device header
  2. #include "LED.h"
  3. #include "Delay.h"
  4. void LED_Init(void)
  5. {
  6. //第一步,使能GPIO的时钟
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  8. //第二步,定义GPIO的初始化结构体类型
  9. GPIO_InitTypeDef GPIO_InitStructure;
  10. //第三步,配置GPIO的输出类型为推挽输出
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  12. //第四步,配置GPIO的引脚
  13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_1;
  14. //第五步,配置GPIO的输出速度
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16. //第六步,初始化GPIOx的寄存器
  17. GPIO_Init(GPIOB,&GPIO_InitStructure);
  18. // GPIO_ResetBits(GPIOA,GPIO_Pin_0); //GPIOA设置为低电平,LED点亮
  19. // GPIO_SetBits(GPIOA,GPIO_Pin_0); //GPIOA设置为高电平,LED灭
  20. // GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET); //LED亮
  21. // GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET); //LED灭
  22. }
  23. void LED_ON(void)
  24. {
  25. LED_Init();
  26. GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_RESET); //LED亮
  27. }
  28. void LED_OFF(void)
  29. {
  30. LED_Init();
  31. GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET); //LED灭
  32. Delay_ms(500);
  33. }
  34. void LED_Flash_ON(void)
  35. {
  36. LED_Init();
  37. GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET); //LED亮
  38. Delay_ms(500);
  39. GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_SET); //LED灭
  40. Delay_ms(500);
  41. }
  42. void LED_Flash_OFF(void)
  43. {
  44. LED_Init();
  45. GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET); //LED灭
  46. }

六、主函数

  1. main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "Delay.h"
  3. #include "OLED.h"
  4. #include "Motor.h"
  5. #include "PWM.h"
  6. #include "LED.h"
  7. #include "Car.h"
  8. #include "SG90.h"
  9. #include "Ultrasonic.h"
  10. #include "Timer.h"
  11. #include "Buzzer.h"
  12. #include "Key.h"
  13. #include "Serial.h"
  14. #include "Hc05.h"
  15. uint8_t Speed = 50; /*速度范围为[0,100]*/
  16. uint8_t KeyNum;
  17. uint8_t Mode=0;
  18. //extern uint8_t RxData;
  19. int main(void)
  20. {
  21. OLED_Init();
  22. Car_Init();
  23. Key_Init();
  24. Serial_Init();
  25. SG90_Servo_Init();
  26. Ultrasonic_GPIO_Init();
  27. OLED_ShowString(1,1,"Dist:");
  28. OLED_ShowString(2,1,"Speed:");
  29. OLED_ShowString(3,1,"MOde:");
  30. while(1)
  31. {
  32. Mode = Key_GetNum();
  33. OLED_ShowNum(3,6,Mode,3);
  34. if(Mode)
  35. {
  36. switch(Mode)
  37. {
  38. case 1: Hc05_car();break;
  39. case 2: Ultrasonic_car();break;
  40. }
  41. }
  42. }
  43. }

七、电路连接表述

电路连接的时候一定要细心,防止出现短路的情况。接下来,我会描述一下我的电路连接思路,供大家参考一下。

首先从电源出发,电源正负极分别接到L298N的12V和GND位置上,这样就可以给L298N供电,使其驱动4个直流电机。再把12V的电源正负极接到稳压模块上,把电压降到5V和3.3V,这样就可以用3.3V来给stm32最小系统板供电,5V来给舵机和超声波以及蓝牙模块供电。

接4个电机时采用并联的方法,这里不太好描述,上个图片一目了然!!

像这样,应该很容易理解吧!

最后就是操作步骤了:连接好电路,打开开关,此时OLED显示屏上的第三行内容是Mode:000,这时如果按下B5引脚上的按键,Mode;002,这时候就开始了超声波避障模式。如果按下B3引脚上的按键,Mode;001,这时候就是蓝牙控制模式,打开手机连接蓝牙,操作自己设置的键盘就可以控制小车了。(补充;OLED显示屏第一行内容是Dist: ,它显示的是超声波模式下探测到的距离的实时值。第二行内容是Speed: ,它显示的是小车的速度值)

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

闽ICP备14008679号