Home > other >  Stm32f103 to control the car move
Stm32f103 to control the car move

Time:04-14

Motor PWM initialization code:


//motor PWM mouth initialization
//the left motor arr: automatically reinstall value PSC: clock preassigned frequency right motor arr2: automatic reinstall value psc2: clock preassigned frequency

Void Motor_PWM_Init (under-16 arr, under-16 PSC, under-16 arr2, under-16 psc2)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

//PWMA PB7
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);//can make 3 clock timer

//set the pin for multiplexing output function, output TIM4 PWM pulse waveform of CH1 GPIOB. 7
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_7;//TIM_CH2
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//multiplexing push-pull output
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//initialize the GPIO

//initializes the TIM4
TIM_TimeBaseStructure.TIM_Period=arr;//set the next update events into activity cycle of heavy load automatically register values
TIM_TimeBaseStructure. TIM_Prescaler=PSC;//set used as TIMx clock frequency preassigned frequency value of the divisor
TIM_TimeBaseStructure. TIM_ClockDivision=TIM_CKD_DIV1;//set the clock division: TDTS=Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//TIM counting up mode
TIM_TimeBaseInit (TIM4, & amp; TIM_TimeBaseStructure);//according to the specified parameters in TIM_TimeBaseInitStruct initialization TIMx time base unit

//initializes the TIM4 Channel1 PWM mode
TIM_OCInitStructure. TIM_OCMode=TIM_OCMode_PWM1;//select timer mode: TIM pulse width modulation mode 2
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable;//more output can make
TIM_OCInitStructure. TIM_OCPolarity=TIM_OCPolarity_High;//output polarity: TIM high output polarity comparison
TIM_OCInitStructure. TIM_Pulse=0;
TIM_OC2Init (TIM4, & amp; TIM_OCInitStructure);//according to T set outside of initial parameters between the specified TIM4 OC2
TIM_OC2PreloadConfig (TIM4, TIM_OCPreload_Enable);//can make TIM4 preloaded on CCR2 register
TIM_Cmd (TIM4, ENABLE);//can make TIM3

//PWMB PB6
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);//can make 3 clock timer
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//can make GPIO peripherals and AFIO multiplexing function module clock

//set the pin for multiplexing output function, output TIM4 PWM pulse waveform of CH1 GPIOB. 6
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_6;//TIM4_CH2
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//multiplexing push-pull output
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//initialize the GPIO

//initializes the TIM4
TIM_TimeBaseStructure. TIM_Period=arr2;//set the next update events into activity cycle of heavy load automatically register values
TIM_TimeBaseStructure. TIM_Prescaler=psc2;//set used as TIMx clock frequency preassigned frequency value of the divisor
TIM_TimeBaseStructure. TIM_ClockDivision=TIM_CKD_DIV1;//set the clock division: TDTS=Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//TIM counting up mode
TIM_TimeBaseInit (TIM4, & amp; TIM_TimeBaseStructure);//according to the specified parameters in TIM_TimeBaseInitStruct initialization TIMx time base unit

//initialize the TIM3 Channel2 PWM mode
TIM_OCInitStructure. TIM_OCMode=TIM_OCMode_PWM1;//select timer mode: TIM pulse width modulation mode 2
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable;//more output can make
TIM_OCInitStructure. TIM_OCPolarity=TIM_OCPolarity_High;//output polarity: TIM high output polarity comparison
TIM_OCInitStructure. TIM_Pulse=0;
TIM_OC1Init (TIM4, & amp; TIM_OCInitStructure);//set according to the T specify the parameters of the initial non-greeks TIM3 OC2
TIM_OC1PreloadConfig (TIM4, TIM_OCPreload_Enable);//can make TIM4 preloaded on CCR2 register
TIM_Cmd (TIM4, ENABLE);//can make TIM3
}

Motor GPIO initialization:


# include "stm32f10x. H"
# include "bsp_gpio. H"
# include "bsp_motor. H"



/* *
* Function MOTOR_GPIO_Config
* @ author liusen
* @ date 2015.01.03
* @ brief motor GPIO port initialization
* @ param [in] void
* @ param [out] void
* @ retval void
* @ par History without
*/

Void MOTOR_GPIO_Init (void)
{
/* define the type of a GPIO_InitTypeDef structure */
GPIO_InitTypeDef GPIO_InitStructure;

/* open GPIOB peripheral clock */
RCC_APB2PeriphClockCmd (Motor_RCC, ENABLE);

/* * choose to control GPIOB pin/
GPIO_InitStructure. GPIO_Pin=Left_MotoA_Pin | Left_MotoB_Pin | Right_MotoA_Pin | Right_MotoB_Pin;

/* set the pin mode to general push-pull output */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

Rate of 50 MHZ/* set pin */
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

/* call library functions, the initialization GPIOB */
GPIO_Init (Motor_Port, & amp; GPIO_InitStructure);

Low level *//*
GPIO_ResetBits (Motor_Port, Left_MotoA_Pin | Left_MotoB_Pin | Right_MotoA_Pin | Right_MotoB_Pin);


//PB4 closed the JTAG
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//can make GPIO peripherals and AFIO multiplexing function module clock
GPIO_PinRemapConfig (GPIO_Remap_SWJ_JTAGDisable, ENABLE);//can ban the jtag





}

Car movement function:

//car movement control function
# include "app_motor. H"
# include "sys. H"
# include "bsp_motor. H"

# define LeftMotor_Go () {GPIO_SetBits (Motor_Port Left_MotoA_Pin); GPIO_ResetBits (Motor_Port Left_MotoB_Pin); }
# define LeftMotor_Back () {GPIO_ResetBits (Motor_Port Left_MotoA_Pin); GPIO_SetBits (Motor_Port Left_MotoB_Pin); }
# define LeftMotor_Stop () {GPIO_ResetBits (Motor_Port Left_MotoA_Pin); GPIO_ResetBits (Motor_Port Left_MotoB_Pin); }

# define RightMotor_Go () {GPIO_SetBits (Motor_Port Right_MotoA_Pin); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related