Bluetooth control servo program based on mini board

  1. module introduction

The stm32mini board controls the steering gear through the Bluetooth of the mobile phone

This article introduces the program of controlling the steering gear based on the Bluetooth of the stm32mini board


1. HC-05 schematic diagram and working principle

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201020203303973.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1OTE5MDMy,size_16,color_FFFFFF,t_70#pic_center)

2. Use steps

1. Initialize the Bluetooth serial port

code show as below:

u8 HC05_Init(void)
{
    
    
	u8 retry=10,t;	  		 
	u8 temp=1;
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);	//开启时钟
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				 // 端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //上拉输入
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //设置传输速率
	GPIO_Init(GPIOA, &GPIO_InitStructure);					 //调用函数初始化
	 
 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				 // 端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //设置为推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //设置速率为50Mhz
	GPIO_Init(GPIOC, &GPIO_InitStructure);					 //初始化

	GPIO_SetBits(GPIOC,GPIO_Pin_4);
 	
	USART2_Init(9600);	//设置波特率为9600
	
}	 

2. Set pwm to control the steering gear

code show as below:

void TIM1_PWM_Init(u16 arr,u16 psc)
{
    
      
	 GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);// 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);  //使能GPIO外设时钟
	                                                                     	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	
	TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频系数
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化

 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //初始化

  TIM_CtrlPWMOutputs(TIM1,ENABLE);	//MOE 主输出使能	

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1预装载使能
	
	TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
	TIM_Cmd(TIM1, ENABLE);  //使能TIM1
}


3. Enter the main function to write the program

code show as below:

int main(void)
 {
    
    
	u8 a;
	u8 b; 
	u8 reclen=0;  
	delay_init();	    	  
	TIM1_PWM_Init(199,7199);//不分频,pwm频率===(7200*200)/72000000=0.02=20ms
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	uart_init(9600); 
	HC05_Init();  
 	while(1) 
	{
    
    		
		if(USART2_RX_STA&0X8000)		
		{
    
    
			for(a=0;a<3;a++)
			{
    
    
				pwm[a]=USART2_RX_BUF[a];  //设置一个数组存放接收到的数字
			}
 			reclen=USART2_RX_STA&0X7FFF;	//得到数据长度
		  USART2_RX_BUF[reclen]=0;	 	//加入结束符
			angle=(pwm[0]-'0')*100+(pwm[1]-'0')*10+pwm[2]-'0';  //将接收到的数据转换为角度
			u2_printf(" angle:%d\r\n",angle);  //发送旋转角度
 			USART2_RX_STA=0;	   //标志位清零
		}	
		b=195-(angle/45)*5;   
		TIM_SetCompare1(TIM1,b);
	}	
}	

Summarize

The hardware is connected to the 5v pin on the mini board, and the pwm pin is connected to PA8.
This is my first blog, if there is any mistake, please forgive me


Guess you like

Origin blog.csdn.net/qq_45919032/article/details/109188561