话不多说直接上代码,都是一些很基础的东西
以上模块的代码就不放了,都是没有区别的,网上随便找找
===============================================================
以下是电机驱动部分代码,我分为了三部分
PWM -> 控制转速
Motor -> 控制正反转
Encoder -> 获取编码器值
只有代码是不太明朗的,所以
上原理图
/****************************** PWM.c ********************************************/
我使用的是定时器1,是高级定时器,注意!!!!!!
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_8;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InternalClockConfig(TIM1); //内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_Period = 7200- 1; //ARR(0~65535) 1KHz
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC(0~65535) 定时频率=72M/(PSC+1)/(ARR+1)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器(高级计数器特有)
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure); //结构体赋初始值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //CCR 占空比=CCR/(ARR+1),分辨率=1/(ARR+1)
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //初始化通道
TIM_OC4Init(TIM1,&TIM_OCInitStructure); //初始化通道
//控制波形是立即生效还是定时器发生下一次更新事件时被更新的
//Enable:下一次更新事件时被更新
//Disable:立即生效
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1,ENABLE); //使能TIM1的外设
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能,高级定时器必须开启这个
}
/****************************** PWM.h ********************************************/
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
#endif
/****************************** Motor.c *******************************************/
#include "stm32f10x.h" // Device header
#include "PWM.h"
/*电机初始化函数*/
void Motor_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_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
PWM_Init();
}
/*赋值函数*/
void Load(int16_t moto1,int16_t moto2)
{
if(moto1 >= 0)
{
GPIO_SetBits(GPIOB,GPIO_Pin_14);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
TIM_SetCompare1(TIM1,moto1); //运行过程更改CCR
}
else
{
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
GPIO_SetBits(GPIOB,GPIO_Pin_15);
TIM_SetCompare1(TIM1,-moto1);//moto1是负数,PWM_SetCompare3函数需要传入正数,所以加负号。
}
if(moto2 >= 0)
{
GPIO_SetBits(GPIOB,GPIO_Pin_13);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
TIM_SetCompare4(TIM1,moto2); //运行过程更改CCR
}
else
{
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
TIM_SetCompare4(TIM1,-moto2);
}
}
/*限幅函数*/
extern int PWM_MAX,PWM_MIN;
void Limit(int *motoA,int *motoB)
{
if(*motoA>PWM_MAX)*motoA=PWM_MAX;
if(*motoA<PWM_MIN)*motoA=PWM_MIN;
if(*motoB>PWM_MAX)*motoB=PWM_MAX;
if(*motoB<PWM_MIN)*motoB=PWM_MIN;
}
/****************************** Motor.h *******************************************/
#ifndef __MOTOR_H
#define __MOTOR_H
void Motor_Init(void);
void Load(int16_t moto1,int16_t moto2);/*赋值函数*/
void Limit(int *motoA,int *motoB);/*限幅函数*/
void Motor_SetSpeed(int16_t Speed);
#endif
/****************************** Encoder.c *******************************************/
#include "stm32f10x.h" // Device header
void Encoder_TIM3_Init(void) //定时器3编码器初始化
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_Period = 65535; //ARR(0~65535) 1s
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //PSC(0~65535) 定时频率=72M/(PSC+1)/(ARR+1)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器(高级计数器特有)
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//配置编码器模式
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);//初始化输入捕获
TIM_ICInitStructure.TIM_ICFilter=10;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//配置溢出更新中断标志位
TIM_SetCounter(TIM3,0);//清零定时器计数值
TIM_Cmd(TIM3,ENABLE);
}
void Encoder_TIM4_Init(void) //定时器4编码器初始化
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStructure.TIM_Period = 65535; //ARR(0~65535) 1s
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //PSC(0~65535) 定时频率=72M/(PSC+1)/(ARR+1)
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器(高级计数器特有)
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//配置编码器模式
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);//初始化输入捕获
TIM_ICInitStructure.TIM_ICFilter=10;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//配置溢出更新中断标志位
TIM_SetCounter(TIM4,0);//清零定时器计数值
TIM_Cmd(TIM4,ENABLE);
}
/**********************
编码器
速度读取函数
入口参数:定时器
**********************/
int Read_Speed(int TIMx)
{
int value_1;
switch(TIMx)
{
/* 1.采集编码器的计数值并保存 2.将定时器的计数值清零。*/
case 3:value_1=(short)TIM_GetCounter(TIM3);TIM_SetCounter(TIM3,0);break;
case 4:value_1=(short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4,0);break;
default:value_1=0;
}
return value_1;
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
/****************************** Encoder.h *******************************************/
#ifndef __ENCODER_H
#define __ENCODER_H
void Encoder_TIM3_Init(void);
void Encoder_TIM4_Init(void);
int Read_Speed(int TIMx);
#endif
代码配原理图,这样就好多了!!!
下一篇继续