赞
踩
最近制作了一款基于stm32的有两个功能的小车,自我感觉还算不错,于是想分享出来,如果能帮到你那我很开心。如果你感觉哪里有错误和需要改进的地方,欢迎发表意见,我会真诚接受并虚心学习!谢谢哦!
超声波的使用需要配置一个TIM定时器,是用来计算超声波模块的TRIG引脚发射脉冲信号到ECHO引脚接收到返回的脉冲信号这一区间段的时间(单位换算成秒),在我的这个项目中,我使用了TIM4来完成这一步骤。具体配置情况请详见下面的配置函数。(注意!!!第二段代码是TIM4的定时器中断函数,请放在使用TIM4的地方即可)
- void Timer4_Init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
-
- TIM_InternalClockConfig(TIM4);/*使用内部时钟TIM4*/
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//不分频
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 1000 - 1;
- TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
-
- TIM_ClearFlag(TIM4,TIM_FLAG_Update);
- /*打开中断*/
- TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC优先级分组2
-
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_Init(&NVIC_InitStructure);
-
- TIM_Cmd(TIM4,ENABLE);/*使能定时器4*/
- }
-
- uint16_t Timer4_GetCounter(void) //获取定时器4的值
- {
- return TIM_GetCounter(TIM4);
- }
- void TIM4_IRQHandler(void) //定时器4中断
- {
- if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
- {
- TIM_ClearITPendingBit(TIM4,TIM_IT_Update);//更新中断
- count++;
- }
- }
超声波的配置也是相对简单,先给TRIG引脚一个高电平,在使用延时函数延时20微秒,打开TIM4计时,再检测ECHO是否接收,若接收,获取TIM4的计时结果,若为接收,则等待接收。具体请详见下面代码。
- /*记录定时器溢出次数*/
- uint16_t count = 0;
-
-
- void Ultrasonic_GPIO_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /*TRIG触发信号*/
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*ECOH回响信号*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
- GPIO_Init(GPIOB, & GPIO_InitStructure);
- }
-
-
- float Get_Ultrasonic_distance(void)
- {
- Timer4_Init();
- Ultrasonic_GPIO_Init();
-
- float distance = 0 ,sum = 0;
- uint32_t tim = 0 ;
- uint8_t i = 0;
-
- while(i!=5) //连续测5次,求平均值,使结果准确
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_15);//先置为低电平
- GPIO_SetBits(GPIOB, GPIO_Pin_15); //拉高信号,作为触发信号
- Delay_us(20); //高电平信号超过10us
- GPIO_ResetBits(GPIOB, GPIO_Pin_15);
-
- /*等待回响信号,回响信号到来*/
- while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == RESET);
- TIM_SetCounter(TIM4,0); //将TIM4计数寄存器的计数值清零
- TIM_Cmd(TIM4,ENABLE);//开启定时器计数
-
- i+=1;
-
- /*回响信号消失*/
- while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == SET);
- TIM_Cmd(TIM4, DISABLE);//关闭定时器
-
- tim = Timer4_GetCounter();//获取计TIM4数寄存器中的计数值,以便计算回响信号时间
- distance = ((float)(tim + count * 1000)) / 58.0;//通过回响信号计算距离
- sum += distance;
- //TIM4->CNT = 0;
- TIM_SetCounter(TIM4,0); //将TIM4计数寄存器的计数值清零
- count = 0; //中断溢出次数清零
- Delay_ms(20);
- }
-
- distance = (sum / 5.0); //单位cm
- return distance; //距离作为函数返回值
- }
-
- void TIM4_IRQHandler(void) //定时器4中断
- {
- if(TIM_GetITStatus(TIM4,TIM_IT_Update) == SET)
- {
- TIM_ClearITPendingBit(TIM4,TIM_IT_Update);//更新中断
- count++;
- }
- }//这里使用了中断函数,就放这里
逻辑函数就是小车收到数据该如何转向的问题,请详见下面的代码,注释很详细的。
- void Ultrasonic_car(void)
- {
- int8_t Speed = 50; /*速度范围为[0,100]*/
- float left_distance = 0.0;
- float right_distance = 0.0;
- float front_distance = 0.0;
-
- Delay_ms(500);
- SG90_Front();//舵机面向前方
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
-
- SG90_Left();//舵机转向左边
- Delay_ms(500);
- left_distance = Get_Ultrasonic_distance();//测出左边的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
-
- SG90_Right();//舵机转向右边
- Delay_ms(500);
- right_distance = Get_Ultrasonic_distance();//测出右边的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- SG90_Front();//舵机面向前方
-
- if((front_distance>left_distance)&&(front_distance>right_distance))
- {
- Car_Forword(Speed);
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- }
- else if((left_distance>front_distance)&&(left_distance>right_distance))
- {
- Car_Left(Speed);
- Delay_ms(500);
- Car_Forword(Speed);
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- }
- else if((right_distance>front_distance)&&(right_distance>left_distance))
- {
- Car_Right(Speed);
- Delay_ms(500);
- Car_Forword(Speed);
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- }
- else Car_Stop();
-
- while(1)
- {
- OLED_ShowNum(2,7,Speed,3);
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- if(front_distance<=50)
- {
- Car_Stop();
- SG90_Left();//舵机转向左边
- Delay_ms(800);
- left_distance = Get_Ultrasonic_distance();//测出左边的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
-
- SG90_Right();//舵机转向y边
- Delay_ms(800);
- right_distance = Get_Ultrasonic_distance();//测出y边的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
- SG90_Front();//舵机面向前方
- front_distance = Get_Ultrasonic_distance();//测出前方的距离
- OLED_ShowNum(1,6,Get_Ultrasonic_distance(),5);
-
- if(left_distance>right_distance)
- {
- Car_Left(Speed);
- Delay_ms(500);
- Car_Stop();
- Delay_s(1);
- Car_Forword(Speed);
- }
- else if((left_distance<right_distance))
- {
- Car_Right(Speed);
- Delay_ms(500);
- Car_Stop();
- Delay_s(1);
- Car_Forword(Speed);
- }
- else
- {
- Car_Back(Speed);
- Delay_ms(800);
- Car_Left(Speed);
- Delay_ms(800);
- Car_Forword(Speed);
- }
- }
-
- }
- }
写到这里,超声波小车就结束了,重点很少,主要是得理解思路。上面函数中出现的其他函数的调用,会在下面(五、通用功能的函数文件)给出C文件的。
用到的模块是HC-05蓝牙模块,蓝牙模块上的TXD引脚和RXD引脚分别接stm32的USART串口上的RXD和TXD对应的引脚。(我使用的是STM32F103C8T6,它的USART1的RXD和TXD分别是GPIOA9和GPIOA10),具体请见下面的代码。
- void Serial_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_Init(USART1, &USART_InitStructure);
-
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_Cmd(USART1, ENABLE);
- }
- uint8_t Serial_RxData;
- uint8_t Serial_RxFlag;
-
- void Serial_SendByte(uint8_t Byte) //发送一个字节
- {
- USART_SendData(USART1, Byte);
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- }
-
- void Serial_SendArray(uint8_t *Array, uint16_t Length) //发送一个数组
- {
- uint16_t i;
- for (i = 0; i < Length; i ++)
- {
- Serial_SendByte(Array[i]);
- }
- }
-
- void Serial_SendString(char *String) //发送一个字符串
- {
- uint8_t i;
- for (i = 0; String[i] != '\0'; i ++)
- {
- Serial_SendByte(String[i]);
- }
- }
-
- void Serial_SendNumber(uint32_t Number, uint8_t Length) //发送一串数字
- {
- uint8_t i;
- for (i = 0; i < Length; i ++)
- {
- Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
- }
- }
-
- uint8_t Serial_GetRxFlag(void) //获取中断标志位
- {
- if (Serial_RxFlag == 1)
- {
- Serial_RxFlag = 0;
- return 1;
- }
- return 0;
- }
-
- uint8_t Serial_GetRxData(void) //获取收到的数据
- {
- return Serial_RxData;
- }
-
- void USART1_IRQHandler(void) //串口USART1的中断
- {
- if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
- {
- Serial_RxData = USART_ReceiveData(USART1);
- Serial_RxFlag = 1;
- USART_ClearITPendingBit(USART1, USART_IT_RXNE);
- }
- }
- uint8_t RxData=0;
-
- void Hc05_car(void)
- {
- while(1)
- {
- if(Serial_GetRxFlag() == 1)
- {
- RxData=Serial_GetRxData();
- //OLED_ShowHexNum(3,1,RxData,2);
- }
-
- switch(RxData)
- {
- case 1:Car_Forword(100);break;
- case 2:Car_Stop();break;
- case 3:Car_Back(100);break;
- case 4:Car_Stop();break;
- case 5:Car_Left(100);break;
- case 6:Car_Stop();break;
- case 7:Car_Right(100);break;
- case 8:Car_Stop();break;
- case 9:Buzzer_ON1();break;
- case 10:Buzzer_OFF();break;
- case 11:LED_ON();break;
- case 12:LED_OFF();break;
- case 13:LED_Flash_ON();break;
- case 14:LED_Flash_OFF();break;
- }
- }
- }
- #include "stm32f10x.h"
- #include "OLED_Font.h"
-
- /*引脚配置*/
- #define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
- #define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
-
- /*引脚初始化*/
- void OLED_I2C_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- OLED_W_SCL(1);
- OLED_W_SDA(1);
- }
-
- /**
- * @brief I2C开始
- * @param 无
- * @retval 无
- */
- void OLED_I2C_Start(void)
- {
- OLED_W_SDA(1);
- OLED_W_SCL(1);
- OLED_W_SDA(0);
- OLED_W_SCL(0);
- }
-
- /**
- * @brief I2C停止
- * @param 无
- * @retval 无
- */
- void OLED_I2C_Stop(void)
- {
- OLED_W_SDA(0);
- OLED_W_SCL(1);
- OLED_W_SDA(1);
- }
-
- /**
- * @brief I2C发送一个字节
- * @param Byte 要发送的一个字节
- * @retval 无
- */
- void OLED_I2C_SendByte(uint8_t Byte)
- {
- uint8_t i;
- for (i = 0; i < 8; i++)
- {
- OLED_W_SDA(Byte & (0x80 >> i));
- OLED_W_SCL(1);
- OLED_W_SCL(0);
- }
- OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
- OLED_W_SCL(0);
- }
-
- /**
- * @brief OLED写命令
- * @param Command 要写入的命令
- * @retval 无
- */
- void OLED_WriteCommand(uint8_t Command)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //从机地址
- OLED_I2C_SendByte(0x00); //写命令
- OLED_I2C_SendByte(Command);
- OLED_I2C_Stop();
- }
-
- /**
- * @brief OLED写数据
- * @param Data 要写入的数据
- * @retval 无
- */
- void OLED_WriteData(uint8_t Data)
- {
- OLED_I2C_Start();
- OLED_I2C_SendByte(0x78); //从机地址
- OLED_I2C_SendByte(0x40); //写数据
- OLED_I2C_SendByte(Data);
- OLED_I2C_Stop();
- }
-
- /**
- * @brief OLED设置光标位置
- * @param Y 以左上角为原点,向下方向的坐标,范围:0~7
- * @param X 以左上角为原点,向右方向的坐标,范围:0~127
- * @retval 无
- */
- void OLED_SetCursor(uint8_t Y, uint8_t X)
- {
- OLED_WriteCommand(0xB0 | Y); //设置Y位置
- OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置低4位
- OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置高4位
- }
-
- /**
- * @brief OLED清屏
- * @param 无
- * @retval 无
- */
- void OLED_Clear(void)
- {
- uint8_t i, j;
- for (j = 0; j < 8; j++)
- {
- OLED_SetCursor(j, 0);
- for(i = 0; i < 128; i++)
- {
- OLED_WriteData(0x00);
- }
- }
- }
-
- /**
- * @brief OLED显示一个字符
- * @param Line 行位置,范围:1~4
- * @param Column 列位置,范围:1~16
- * @param Char 要显示的一个字符,范围:ASCII可见字符
- * @retval 无
- */
- void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
- {
- uint8_t i;
- OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
- for (i = 0; i < 8; i++)
- {
- OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
- }
- OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
- for (i = 0; i < 8; i++)
- {
- OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
- }
- }
-
- /**
- * @brief OLED显示字符串
- * @param Line 起始行位置,范围:1~4
- * @param Column 起始列位置,范围:1~16
- * @param String 要显示的字符串,范围:ASCII可见字符
- * @retval 无
- */
- void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
- {
- uint8_t i;
- for (i = 0; String[i] != '\0'; i++)
- {
- OLED_ShowChar(Line, Column + i, String[i]);
- }
- }
-
- /**
- * @brief OLED次方函数
- * @retval 返回值等于X的Y次方
- */
- uint32_t OLED_Pow(uint32_t X, uint32_t Y)
- {
- uint32_t Result = 1;
- while (Y--)
- {
- Result *= X;
- }
- return Result;
- }
-
- /**
- * @brief OLED显示数字(十进制,正数)
- * @param Line 起始行位置,范围:1~4
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~4294967295
- * @param Length 要显示数字的长度,范围:1~10
- * @retval 无
- */
- void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
- {
- uint8_t i;
- for (i = 0; i < Length; i++)
- {
- OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
- }
- }
-
- /**
- * @brief OLED显示数字(十进制,带符号数)
- * @param Line 起始行位置,范围:1~4
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:-2147483648~2147483647
- * @param Length 要显示数字的长度,范围:1~10
- * @retval 无
- */
- void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
- {
- uint8_t i;
- uint32_t Number1;
- if (Number >= 0)
- {
- OLED_ShowChar(Line, Column, '+');
- Number1 = Number;
- }
- else
- {
- OLED_ShowChar(Line, Column, '-');
- Number1 = -Number;
- }
- for (i = 0; i < Length; i++)
- {
- OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
- }
- }
-
- /**
- * @brief OLED显示数字(十六进制,正数)
- * @param Line 起始行位置,范围:1~4
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~0xFFFFFFFF
- * @param Length 要显示数字的长度,范围:1~8
- * @retval 无
- */
- void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
- {
- uint8_t i, SingleNumber;
- for (i = 0; i < Length; i++)
- {
- SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
- if (SingleNumber < 10)
- {
- OLED_ShowChar(Line, Column + i, SingleNumber + '0');
- }
- else
- {
- OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
- }
- }
- }
-
- /**
- * @brief OLED显示数字(二进制,正数)
- * @param Line 起始行位置,范围:1~4
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
- * @param Length 要显示数字的长度,范围:1~16
- * @retval 无
- */
- void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
- {
- uint8_t i;
- for (i = 0; i < Length; i++)
- {
- OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
- }
- }
-
- /**
- * @brief OLED初始化
- * @param 无
- * @retval 无
- */
- void OLED_Init(void)
- {
- uint32_t i, j;
-
- for (i = 0; i < 1000; i++) //上电延时
- {
- for (j = 0; j < 1000; j++);
- }
-
- OLED_I2C_Init(); //端口初始化
-
- OLED_WriteCommand(0xAE); //关闭显示
-
- OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
- OLED_WriteCommand(0x80);
-
- OLED_WriteCommand(0xA8); //设置多路复用率
- OLED_WriteCommand(0x3F);
-
- OLED_WriteCommand(0xD3); //设置显示偏移
- OLED_WriteCommand(0x00);
-
- OLED_WriteCommand(0x40); //设置显示开始行
-
- OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
-
- OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
-
- OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
- OLED_WriteCommand(0x12);
-
- OLED_WriteCommand(0x81); //设置对比度控制
- OLED_WriteCommand(0xCF);
-
- OLED_WriteCommand(0xD9); //设置预充电周期
- OLED_WriteCommand(0xF1);
-
- OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
- OLED_WriteCommand(0x30);
-
- OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
-
- OLED_WriteCommand(0xA6); //设置正常/倒转显示
-
- OLED_WriteCommand(0x8D); //设置充电泵
- OLED_WriteCommand(0x14);
-
- OLED_WriteCommand(0xAF); //开启显示
-
- OLED_Clear(); //OLED清屏
- }
- #ifndef __OLED_H
- #define __OLED_H
-
- void OLED_Init(void);
- void OLED_Clear(void);
- void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
- void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
- void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
- void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
- void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
- void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
-
- #endif
- #ifndef __OLED_FONT_H
- #define __OLED_FONT_H
-
- /*OLED字模库,宽8像素,高16像素*/
- const uint8_t OLED_F8x16[][16]=
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
-
- 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
-
- 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
-
- 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
- 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
-
- 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
- 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
-
- 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
- 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
-
- 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
- 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
-
- 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
-
- 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
- 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
-
- 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
- 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
-
- 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
- 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
-
- 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
- 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
-
- 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
- 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
-
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
- 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
-
- 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
-
- 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
- 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
-
- 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
- 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
-
- 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
- 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
-
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
- 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
-
- 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
- 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
-
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
- 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
-
- 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
- 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
-
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
- 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
-
- 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
-
- 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
-
- 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
- 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
-
- 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
- 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
-
- 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
- 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
-
- 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
- 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
-
- 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
- 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
-
- 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
- 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
-
- 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
-
- 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
- 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
-
- 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
-
- 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
- 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
-
- 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
- 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
-
- 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
- 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
-
- 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
- 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
-
- 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
-
- 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
- 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
-
- 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
- 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
-
- 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
- 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
-
- 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
- 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
-
- 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
- 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
-
- 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
-
- 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
- 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
-
- 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
-
- 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
- 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
-
- 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
- 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
-
- 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
- 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
-
- 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
- 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
-
- 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
- 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
-
- 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
- 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
-
- 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
- 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
-
- 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
- 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
-
- 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
- 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
-
- 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
- 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
-
- 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
-
- 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
- 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
-
- 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
-
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
-
- 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
-
- 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
- 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
-
- 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
-
- 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
-
- 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
-
- 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
-
- 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
-
- 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
- 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
-
- 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
- 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
-
- 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
-
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
-
- 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
- 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
-
- 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
- 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
-
- 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
-
- 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
- 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
-
- 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
-
- 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
- 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
-
- 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
- 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
-
- 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
- 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
-
- 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
- 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
-
- 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
- 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
-
- 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
- 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
-
- 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
- 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
-
- 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
- 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
-
- 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
-
- 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
- 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
-
- 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
- };
-
- #endif
我使用的舵机模块是SG90,并使用stm32的TIM3的通道1输出PWM信号来控制旋转角度和方向,具体请详见代码和注释。
- /*这里用TIM3的通道1来产生PWM信号调速舵机的旋转角度*/
- void PWM_TIM3_Init(void)/*这个是TIM3的配置,和上面的说明一样*/
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- TIM_InternalClockConfig(TIM3);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
- TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
-
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
-
- TIM_OC1Init(TIM3,&TIM_OCInitStructure);
-
- TIM_Cmd(TIM3,ENABLE);
- }
-
- /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
- void PWM_TIM3_SetCompare1(uint16_t Compare) //TIM3 的通道1
- {
- TIM_SetCompare1(TIM3,Compare);
- }
这里我放上小车的两路电机(4个电机,因为我采用并联方法,使一OUT1/OUT2,OUT3/OUT4分别驱动两个电机)所用到的PWM调速的文件。其中使用定时器TIM2的通道1和通道2
- void PWM_TIM2_Init(void)/*配置TIM用来输出PWM波形时,要参考使用手册,因为它的GPIO口是官方确定的*/
- {
- /*打开TIM和GPIO的时钟*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- /*配置GPIO的输出模式、GPIO口、频率*/
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/*复用推挽输出*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);/*GPIO初始化*/
- /**/
- TIM_InternalClockConfig(TIM2);
- /*这里通过ARR,PSC,CCR的值来计算计时频率和占空比的值*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
- TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;/*确定占空比*/
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
- /*这里配置输出比较单元*/
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
- /*输出PWM波形的GPIO口的初始化*/
- TIM_OC1Init(TIM2,&TIM_OCInitStructure); /*非常重要且容易忘记,配置好PWM的gpio口后一定要记得初始化*/
- TIM_OC2Init(TIM2,&TIM_OCInitStructure);
- /*使能TIM2*/
- TIM_Cmd(TIM2,ENABLE);
- }
-
- /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
- void PWM_TIM2_SetCompare1(uint16_t Compare) //TIM2 的通道1
- {
- TIM_SetCompare1(TIM2,Compare);/*很重要,改变占空比参数就靠这个函数*/
- }
-
- void PWM_TIM2_SetCompare2(uint16_t Compare) //TIM2 的通道2
- {
- TIM_SetCompare2(TIM2,Compare);
- }
由于电源是12V,但是STM32最小系统板供电是3.3V,而且舵机和超声波是5V供电,所以我觉得这个模块是必不可少的东西。
- #include "stm32f10x.h"
-
- /**
- * @brief 微秒级延时
- * @param xus 延时时长,范围:0~233015
- * @retval 无
- */
- void Delay_us(uint32_t xus)
- {
- SysTick->LOAD = 72 * xus; //设置定时器重装值
- SysTick->VAL = 0x00; //清空当前计数值
- SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
- while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
- SysTick->CTRL = 0x00000004; //关闭定时器
- }
-
- /**
- * @brief 毫秒级延时
- * @param xms 延时时长,范围:0~4294967295
- * @retval 无
- */
- void Delay_ms(uint32_t xms)
- {
- while(xms--)
- {
- Delay_us(1000);
- }
- }
-
- /**
- * @brief 秒级延时
- * @param xs 延时时长,范围:0~4294967295
- * @retval 无
- */
- void Delay_s(uint32_t xs)
- {
- while(xs--)
- {
- Delay_ms(1000);
- }
- }
- #include "stm32f10x.h" // Device header
- #include "Motor.h"
- #include "PWM.h"
-
-
- void Motor_R_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- PWM_TIM2_Init();
- }
-
- void Motor_L_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- PWM_TIM2_Init();
- }
-
- void Motor_R_SetSpeed(int8_t Speed)
- {
- if(Speed>=0)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_2);
- GPIO_ResetBits(GPIOA,GPIO_Pin_3);
- PWM_TIM2_SetCompare1(Speed);
- }
- else
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_3);
- GPIO_ResetBits(GPIOA,GPIO_Pin_2);
- PWM_TIM2_SetCompare1(-Speed);
- }
- }
- void Motor_L_SetSpeed(int8_t Speed)
- {
- if(Speed>=0)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- GPIO_ResetBits(GPIOA,GPIO_Pin_5);
- PWM_TIM2_SetCompare2(Speed);
- }
- else
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_5);
- GPIO_ResetBits(GPIOA,GPIO_Pin_4);
- PWM_TIM2_SetCompare2(-Speed);
- }
- }
- #include "stm32f10x.h" // Device header
- #include "Car.h"
- #include "Motor.h"
-
- void Car_Init(void)
- {
- Motor_R_Init();
- Motor_L_Init();
- }
-
- void Car_Forword(uint8_t Speed)
- {
- Motor_R_SetSpeed(Speed);
- Motor_L_SetSpeed(Speed);
- }
-
- void Car_Back(uint8_t Speed)
- {
- Motor_R_SetSpeed(-Speed);
- Motor_L_SetSpeed(-Speed);
- }
-
- void Car_Left(uint8_t Speed)
- {
- Motor_R_SetSpeed(Speed);
- Motor_L_SetSpeed(-Speed);
- }
-
- void Car_Right(uint8_t Speed)
- {
- Motor_R_SetSpeed(-Speed);
- Motor_L_SetSpeed(Speed);
- }
-
- void Car_Stop(void)
- {
- Motor_R_SetSpeed(0);
- Motor_L_SetSpeed(0);
- }
- #include "stm32f10x.h" // Device header
- #include "PWM.h"
-
- void PWM_TIM2_Init(void)/*配置TIM用来输出PWM波形时,要参考使用手册,因为它的GPIO口是官方确定的*/
- {
- /*打开TIM和GPIO的时钟*/
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- /*配置GPIO的输出模式、GPIO口、频率*/
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/*复用推挽输出*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);/*GPIO初始化*/
- /**/
- TIM_InternalClockConfig(TIM2);
- /*这里通过ARR,PSC,CCR的值来计算计时频率和占空比的值*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
- TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;/*确定占空比*/
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
- /*这里配置输出比较单元*/
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
- /*输出PWM波形的GPIO口的初始化*/
- TIM_OC1Init(TIM2,&TIM_OCInitStructure); /*非常重要且容易忘记,配置好PWM的gpio口后一定要记得初始化*/
- TIM_OC2Init(TIM2,&TIM_OCInitStructure);
- /*使能TIM2*/
- TIM_Cmd(TIM2,ENABLE);
- }
-
-
- /*这里用TIM3的通道1来产生PWM信号调速舵机的旋转角度*/
- void PWM_TIM3_Init(void)/*这个是TIM3的配置,和上面的说明一样*/
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- TIM_InternalClockConfig(TIM3);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
- TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
-
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0; //CCR的值
-
- TIM_OC1Init(TIM3,&TIM_OCInitStructure);
-
- TIM_Cmd(TIM3,ENABLE);
- }
-
- /*封装TIM的通道的调用函数,不封装也可以,看自己情况*/
- void PWM_TIM2_SetCompare1(uint16_t Compare) //TIM2 的通道1
- {
- TIM_SetCompare1(TIM2,Compare);/*很重要,改变占空比参数就靠这个函数*/
- }
-
- void PWM_TIM2_SetCompare2(uint16_t Compare) //TIM2 的通道2
- {
- TIM_SetCompare2(TIM2,Compare);
- }
-
- void PWM_TIM3_SetCompare1(uint16_t Compare) //TIM3 的通道1
- {
- TIM_SetCompare1(TIM3,Compare);
- }
- #include "stm32f10x.h" // Device header
- #include "Key.h"
- #include "Delay.h"
-
-
- void Key_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- }
-
- uint8_t Key_GetNum(void)
- {
- uint8_t KeyNum = 0;
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3) == 0)
- {
- Delay_ms(20);
- while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3) == 0);
- Delay_ms(20);
- KeyNum=1;
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5) == 0)
- {
- Delay_ms(20);
- while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5) == 0);
- Delay_ms(20);
- KeyNum=2;
- }
- return KeyNum;
- }
- #include "stm32f10x.h" // Device header
- #include "Buzzer.h"
- #include "Delay.h"
-
- void Buzzer_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- }
-
- void Buzzer_OFF(void) //关闭蜂鸣器
- {
- Buzzer_Init();
- GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
- }
-
- void Buzzer_ON1(void)
- {
- Buzzer_Init();
- GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为d电平
- }
-
- void Buzzer_ON2(void) //打开蜂鸣器
- {
- Buzzer_Init();
- GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为低电平
- Delay_ms(100);
- GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
- Delay_ms(100);
-
- GPIO_ResetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为低电平
- Delay_ms(100);
- GPIO_SetBits(GPIOB,GPIO_Pin_11); //GPIOB设置为高电平
- Delay_ms(700);
-
- }
- #include "stm32f10x.h" // Device header
- #include "LED.h"
- #include "Delay.h"
-
- void LED_Init(void)
- {
- //第一步,使能GPIO的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- //第二步,定义GPIO的初始化结构体类型
- GPIO_InitTypeDef GPIO_InitStructure;
- //第三步,配置GPIO的输出类型为推挽输出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- //第四步,配置GPIO的引脚
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_1;
- //第五步,配置GPIO的输出速度
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- //第六步,初始化GPIOx的寄存器
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- // GPIO_ResetBits(GPIOA,GPIO_Pin_0); //GPIOA设置为低电平,LED点亮
- // GPIO_SetBits(GPIOA,GPIO_Pin_0); //GPIOA设置为高电平,LED灭
- // GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET); //LED亮
- // GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET); //LED灭
- }
-
- void LED_ON(void)
- {
- LED_Init();
- GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_RESET); //LED亮
- }
-
- void LED_OFF(void)
- {
- LED_Init();
- GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET); //LED灭
- Delay_ms(500);
- }
-
- void LED_Flash_ON(void)
- {
- LED_Init();
- GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET); //LED亮
- Delay_ms(500);
- GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_SET); //LED灭
- Delay_ms(500);
- }
-
- void LED_Flash_OFF(void)
- {
- LED_Init();
- GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET); //LED灭
- }
- #include "stm32f10x.h" // Device header
- #include "Delay.h"
- #include "OLED.h"
- #include "Motor.h"
- #include "PWM.h"
- #include "LED.h"
- #include "Car.h"
- #include "SG90.h"
- #include "Ultrasonic.h"
- #include "Timer.h"
- #include "Buzzer.h"
- #include "Key.h"
- #include "Serial.h"
- #include "Hc05.h"
-
- uint8_t Speed = 50; /*速度范围为[0,100]*/
- uint8_t KeyNum;
- uint8_t Mode=0;
- //extern uint8_t RxData;
-
- int main(void)
- {
- OLED_Init();
- Car_Init();
- Key_Init();
- Serial_Init();
- SG90_Servo_Init();
- Ultrasonic_GPIO_Init();
-
- OLED_ShowString(1,1,"Dist:");
- OLED_ShowString(2,1,"Speed:");
- OLED_ShowString(3,1,"MOde:");
-
- while(1)
- {
- Mode = Key_GetNum();
- OLED_ShowNum(3,6,Mode,3);
- if(Mode)
- {
- switch(Mode)
- {
- case 1: Hc05_car();break;
- case 2: Ultrasonic_car();break;
- }
- }
- }
- }
电路连接的时候一定要细心,防止出现短路的情况。接下来,我会描述一下我的电路连接思路,供大家参考一下。
首先从电源出发,电源正负极分别接到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: ,它显示的是小车的速度值)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。