Project 6: Timer 1 generates PWM to control the steering gear

1. Working principle of steering gear

    The control signal enters the signal modulation chip from the channel of the receiver to obtain a DC bias voltage. It has a reference circuit inside, which generates a reference signal with a period of 20ms and a width of 1.5ms, and compares the obtained DC bias voltage with the voltage of the potentiometer to obtain a voltage difference output. Finally, the positive and negative of the voltage difference are output to the motor driver chip to determine the positive and negative rotation of the motor. When the speed of the motor is constant, the potentiometer is driven to rotate through the cascade deceleration wheel, so that the voltage difference is 0, and the motor stops rotating. Steering gear control: Generally, a time base pulse of about 20ms is required, and the high level part of the pulse is generally the angle control pulse part in the range of 0.5ms-2.5ms. The steering gear used in this experiment is a 180-degree servo, and the control relationship is as follows:

 2. Servo circuit diagram

3. Advanced timer generates PWM wave

Compared with the initialization of ordinary timer 1, two more sentences need to be added:

//重复计数器的值
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
//主输出使能
 TIM_CtrlPWMOutputs(TIM1, ENABLE);

4. Code

main.c

#include "motor.h"
#include <stdio.h>
#include "delay.h"
#include "stm32f10x.h"
#include "followline.h"
#include "ultrasonic.h"
#include "steeringengine.h"

u8 UART3_data,UART1_data;
u8 UART3_rcv[20],UART3_rcv_count;
u8 UART1_rcv[50],UART1_rcv_count,Uart1_finish;

int main(void)
{
//      motor_pwm_TIME4_init(71,999);//电机PWM初始化
        delay_init();//延迟初始化
//      motor_GPIO_init();//电机管脚初始化、寻迹管脚初始化。
//        uart_init1(9600);
//	    hwbz_gpio_init();
	      steer_gpio_init();
        steering_pwm_TIME1_init(7199,199);
	//int right_2=0,right_1=0 ,left_2=0,left_1=0;
	
	while(1)
	{
			

       TIM_SetCompare4(TIM1,jd45);
       delay_ms(800);
		   TIM_SetCompare4(TIM1,jd90);
       delay_ms(800);
       TIM_SetCompare4(TIM1,jd180);
       delay_ms(800);
		
	}
}  

steeringengine.c 

#include "steeringengine.h"

void  steer_gpio_init(void )
{
		GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA, &GPIO_InitStruct);

}
void steering_pwm_TIME1_init(int presc,int arr)
{
	 //定义定时器的结构体
	 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	 //定义定时器PWM输出通道的结构体
	 TIM_OCInitTypeDef TIM_OCInitStruct;
	 //打开TIME1的时钟
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	 //配置TIME1结构体的参数
	 TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;
	 TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	 TIM_TimeBaseInitStruct.TIM_Prescaler = presc;
	 TIM_TimeBaseInitStruct.TIM_Period =arr;
	
	//重复计数器的值
   TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
 
	 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
	
	 //配置TIME1通道结构体的参数
	 TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
	 TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
	 TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable ;
	 TIM_OCInitStruct.TIM_Pulse =0;
	 
	  //TIME4通道4初始化
   TIM_OC4Init(TIM1, &TIM_OCInitStruct);
	 
	 //使能TIME4通道1的预存寄存器
	 TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
	 // 主输出使能,当使用的是通用定时器时,这句不需要
   TIM_CtrlPWMOutputs(TIM1, ENABLE);
	 
//	 TIM_ARRPreloadConfig(TIM1, ENABLE);

   //使能TIME4
	 TIM_Cmd(TIM1, ENABLE);
  
}

steeringengine.h

#ifndef __STEERINGENGINE_H__
#define __STEERINGENGINE_H__

#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"

#define jd0   5
#define jd45  10
#define jd90  15
#define jd135 20
#define jd180 25


void  steer_gpio_init(void );
void steering_pwm_TIME1_init(int presc,int arr);

#endif

 

Guess you like

Origin blog.csdn.net/qq_47541315/article/details/120142019