基于stm32循迹小车运动实例
#include “motor.h”
#include “Math.h”
#include “delay.h”
#include “stm32f10x.h” // Device header
signed short sPWMR,sPWML,dPWM;
//GPIOÅäÖú¯Êý
/*void MotorGPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_GO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LEFT_MOTOR_GO_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_PWM;
GPIO_Init(LEFT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_PWM;
GPIO_Init(RIGHT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_GO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(RIGHT_MOTOR_GPIO, &GPIO_InitStructure);
}
void run() //Ç°½ø
{
RIGHT_MOTOR_GO_SET;
RIGHT_MOTOR_PWM_RESET;//PB9
LEFT_MOTOR_GO_SET;
LEFT_MOTOR_PWM_RESET;//PB8
}
*/
void TIM4_PWM_Init(unsigned short arr,unsigned short psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_GO; ×óµç»ú·½Ïò¿ØÖÆ PB7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LEFT_MOTOR_GO_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LEFT_MOTOR_PWM; //×óµç»úPWM¿ØÖÆ PB8
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(LEFT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_GO; //ÓÒµç»ú·½Ïò¿ØÖÆ PA4
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(RIGHT_MOTOR_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RIGHT_MOTOR_PWM; //ÓÒµç»úPWM¿ØÖÆ PB9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(RIGHT_MOTOR_PWM_GPIO, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ ²»·ÖƵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_Pulse = 0; //ÉèÖôý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
TIM_CtrlPWMOutputs(TIM4,ENABLE); //MOE Ö÷Êä³öʹÄÜ
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //CH1ԤװÔØʹÄÜ
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //CH1ԤװÔØʹÄÜ
TIM_ARRPreloadConfig(TIM4, ENABLE); //ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM4, ENABLE); //ʹÄÜTIM1
}
void SetMotorSpeed(unsigned char ucChannel,signed char cSpeed)
{
// static short sMotorSpeed = 0;
short sPWM;
// float fDir = 1;
if (cSpeed>=100) cSpeed = 100;
if (cSpeed<=-100) cSpeed = -100;
sPWM = 7201 - fabs(cSpeed)*72;
switch(ucChannel)
{
case 0://ÓÒÂÖ
TIM_SetCompare3(TIM4,sPWM);
if (cSpeed>0)
RIGHT_MOTOR_GO_RESET;
else if(cSpeed<0)
RIGHT_MOTOR_GO_SET;
break;
case 1://×óÂÖ
TIM_SetCompare4(TIM4,sPWM);
if (cSpeed>0)
LEFT_MOTOR_GO_SET;
else if (cSpeed<0)
LEFT_MOTOR_GO_RESET;
break;
}
}
//----------------------------------Ô˶¯º¯Êý--------------------------------
void ZYSTM32_run(signed char speed,int time) //Ç°½øº¯Êý
{
signed char f_speed = - speed;
SetMotorSpeed(1,f_speed);//×óÂÖ //Ϊ¸ºÊý
SetMotorSpeed(0,speed);//ÓÒÂÖ //ΪÕýÊý
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_brake(int time) //ɲ³µº¯Êý
{
SetMotorSpeed(1,0);//×óÂÖ //Ϊ0
SetMotorSpeed(0,0);//ÓÒÂÖ //Ϊ0
RIGHT_MOTOR_GO_RESET;
LEFT_MOTOR_GO_RESET;
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_Left(signed char speed,int time) //×óתº¯Êý
{
SetMotorSpeed(1,0);//×óÂÖ //×óÂÖ²»¶¯
SetMotorSpeed(0,speed); //ÓÒÂÖΪÕý
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_Spin_Left(signed char speed,int time) //×óÐýתº¯Êý
{
SetMotorSpeed(1,speed);//×óÂÖ //×óÂÖΪÕý
SetMotorSpeed(0,speed); //ÓÒÂÖΪÕý
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_Right(signed char speed,int time) //ÓÒתº¯Êý
{
signed char f_speed = - speed;
SetMotorSpeed(1,f_speed);//×óÂÖ //×óÂÖΪ¸º
SetMotorSpeed(0,0); //ÓÒÂÖΪ0
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_Spin_Right(signed char speed,int time) //ÓÒÐýתº¯Êý
{
signed char f_speed = - speed;
SetMotorSpeed(1,f_speed);//×óÂÖ //×óÂÖΪ¸º
SetMotorSpeed(0,f_speed); //ÓÒÂÖΪ¸º
delay_ms(time); //ʱ¼äΪºÁÃë
}
void ZYSTM32_back(signed char speed,int time) //ºóÍ˺¯Êý
{
signed char f_speed = - speed;
SetMotorSpeed(1,speed);//×óÂÖ //ΪÕýÊý
SetMotorSpeed(0,f_speed);//ÓÒÂÖ //Ϊ¸ºÊý
delay_ms(time); //ʱ¼äΪºÁÃë
}