由于tb6612缺货,价太猛。后面需要改造一下电路。用双PWM进行控制一个电机。 PWM口分配 P1–定时器1的两个通道CH1-E9 --CH2-E11 P2–定时器1的两个通道CH3-E13 -CH4-E14
P3–定时器9的两个通道CH1 E5 --CH2-E6 P4 定时器12的两个通道CH1(PB14)CH2-PB15
舵机 定时器8 (舵机1)CH1-C6 (舵机2)CH2-C7 (舵机3)CH3-C8 CH4-C9(注意,
这里分配给了MPU6050的中断) (舵机4)定时器10–PB8 (舵机5)定时器11–PB9 (舵机6)定时器13–PA6 (舵机7)定时器14–PA7
定时器2、定时器3、定时器4、定时5都用作了编码器
定时器6作为心跳
定时器7作为另外一个心跳。
这么一来,可以适配多数的驱动器。后续自己也会制做一个小尺寸的驱动器。更新一下实验室的学习板子。
更新的电机函数是 dcmotor2.h
#ifndef __DCMOTOR2_H
#define __DCMOTOR2_H
#include "sys.h"
#define HTimer1Clk RCC_APB2Periph_TIM1
#define HTimer1CH1Port GPIOE
#define HTimer1CH1Pin GPIO_Pin_9
#define HTimer1CH1PinSource GPIO_PinSource9
#define HTimer1CH1GPIOClk RCC_AHB1Periph_GPIOE
#define HTimer1CH2Port GPIOE
#define HTimer1CH2Pin GPIO_Pin_11
#define HTimer1CH2PinSource GPIO_PinSource11
#define HTimer1CH2GPIOClk RCC_AHB1Periph_GPIOE
#define HTimer1CH3Port GPIOE
#define HTimer1CH3Pin GPIO_Pin_13
#define HTimer1CH3PinSource GPIO_PinSource13
#define HTimer1CH3GPIOClk RCC_AHB1Periph_GPIOE
#define HTimer1CH4Port GPIOE
#define HTimer1CH4Pin GPIO_Pin_14
#define HTimer1CH4PinSource GPIO_PinSource14
#define HTimer1CH4GPIOClk RCC_AHB1Periph_GPIOE
#define HTim1AddTimeTick 1
#define HTim1_Prescaler (168-1)
void HTim1_Init(u32 arr,u32 psc);
void TIM9_PWM_Init(u32 arr,u32 psc);
void TIM12_PWM_Init(u32 arr,u32 psc);
void Motor1Pwm(int DutyTemp);
void Motor2Pwm(int DutyTemp);
void Motor3Pwm(int DutyTemp);
void Motor4Pwm(int DutyTemp);
#endif
demotor2.c
#include "dcmotor2.h"
void HTim1_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(HTimer1CH1GPIOClk, ENABLE);
RCC_AHB1PeriphClockCmd(HTimer1CH2GPIOClk, ENABLE);
RCC_AHB1PeriphClockCmd(HTimer1CH3GPIOClk, ENABLE);
RCC_AHB1PeriphClockCmd(HTimer1CH4GPIOClk, ENABLE);
GPIO_PinAFConfig(HTimer1CH1Port,HTimer1CH1PinSource,GPIO_AF_TIM1);
GPIO_PinAFConfig(HTimer1CH2Port,HTimer1CH2PinSource,GPIO_AF_TIM1);
GPIO_PinAFConfig(HTimer1CH3Port,HTimer1CH3PinSource,GPIO_AF_TIM1);
GPIO_PinAFConfig(HTimer1CH4Port,HTimer1CH4PinSource,GPIO_AF_TIM1);
GPIO_InitStructure.GPIO_Pin = HTimer1CH1Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(HTimer1CH1Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = HTimer1CH2Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(HTimer1CH2Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = HTimer1CH3Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(HTimer1CH3Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = HTimer1CH4Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(HTimer1CH4Port, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, DISABLE);
}
void TIM9_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource5,GPIO_AF_TIM9);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource6,GPIO_AF_TIM9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM9, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM9, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM9, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM9,ENABLE);
TIM_Cmd(TIM9, DISABLE);
}
void TIM12_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_TIM12);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_TIM12);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM12,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM12, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM12, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM12, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM12, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM12, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM12,ENABLE);
TIM_Cmd(TIM12, DISABLE);
}
void Motor1Pwm(int DutyTemp)
{
if(DutyTemp==0)
{
TIM_SetCompare1(TIM1,0);
TIM_SetCompare2(TIM1,0);
return;
}
if(DutyTemp>4000) DutyTemp = 4000;
if(DutyTemp<-4000)DutyTemp = -4000;
if(DutyTemp>0)
{
TIM_SetCompare1(TIM1,DutyTemp);
TIM_SetCompare2(TIM1,0);
}
else
{
TIM_SetCompare1(TIM1,0);
TIM_SetCompare2(TIM1,-DutyTemp);
}
}
void Motor2Pwm(int DutyTemp)
{
if(DutyTemp==0)
{
TIM_SetCompare3(TIM1,0);
TIM_SetCompare4(TIM1,0);
return;
}
if(DutyTemp>4000) DutyTemp = 4000;
if(DutyTemp<-4000)DutyTemp = -4000;
if(DutyTemp>0)
{
TIM_SetCompare3(TIM1,DutyTemp);
TIM_SetCompare4(TIM1,0);
}
else
{
TIM_SetCompare3(TIM1,0);
TIM_SetCompare4(TIM1,-DutyTemp);
}
}
void Motor3Pwm(int DutyTemp)
{
if(DutyTemp==0)
{
TIM_SetCompare1(TIM9,0);
TIM_SetCompare2(TIM9,0);
return;
}
if(DutyTemp>4000) DutyTemp = 4000;
if(DutyTemp<-4000)DutyTemp = -4000;
if(DutyTemp>0)
{
TIM_SetCompare1(TIM9,DutyTemp);
TIM_SetCompare2(TIM9,0);
}
else
{
TIM_SetCompare1(TIM9,0);
TIM_SetCompare2(TIM9,-DutyTemp);
}
}
void Motor4Pwm(int DutyTemp)
{
if(DutyTemp==0)
{
TIM_SetCompare1(TIM12,0);
TIM_SetCompare2(TIM12,0);
return;
}
if(DutyTemp>4000) DutyTemp = 4000;
if(DutyTemp<-4000)DutyTemp = -4000;
if(DutyTemp>0)
{
TIM_SetCompare1(TIM12,DutyTemp);
TIM_SetCompare2(TIM12,0);
}
else
{
TIM_SetCompare1(TIM12,0);
TIM_SetCompare2(TIM12,-DutyTemp);
}
}
main.c
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "oled.h"
#include "extizd.h"
#include "BaseTimX.h"
#include "htimx.h"
#include "dcmotor2.h"
#include "IOI2C.h"
#include "MPU6050.h"
int int1=10;
int int2=12;
float float1=1.5;
float float2=10.25;
void oledPage0()
{
OLED_ShowChar(0,0,'Z');
OLED_ShowChar(1*8,0,'P');
OLED_ShowChar(2*8,0,'0');
OLED_ShowChar(4*8,0,'F');
OLED_ShowChar(5*8,0,'P');
OLED_ShowChar(6*8,0,'0');
OLED_ShowChar(0*8,1,'Z');
OLED_ShowChar(1*8,1,'S');
OLED_ShowChar(2*8,1,'1');
OLED_ShowChar(3*8,1,'0');
OLED_ShowChar(4*8,1,'0');
OLED_ShowChar(5*8,1,'0');
OLED_ShowChar(6*8,1,'F');
OLED_ShowChar(7*8,1,'S');
OLED_ShowChar(8*8,1,'0');
OLED_ShowChar(9*8,1,'.');
OLED_ShowChar(10*8,1,'0');
OLED_ShowChar(11*8,1,'1');
OLED_ShowChar(0,2,'k');
OLED_ShowChar(1*8,2,'m');
OLED_ShowChar(2*8,2,'0');
OLED_ShowChar(4*8,2,'a');
OLED_ShowChar(5*8,2,'s');
OLED_ShowChar(6*8,2,'0');
OLED_ShowChar(0*8,3,'z');
OLED_ShowChar(1*8,3,'s');
OLED_ShowChar(2*8,3,'0');
OLED_ShowChar(3*8,3,'=');
OLED_ShowChar(0*8,4,'x');
OLED_ShowChar(1*8,4,'s');
OLED_ShowChar(2*8,4,'0');
OLED_ShowChar(3*8,4,'=');
}
int main(void)
{
char sendcmd[50]={0};
u8 KeyVal=0;
float a=123.456;
delay_init(168);
LED_Init();
KeyModeFlag=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
HTim1_Init(4166-1,4-1);
TIM_Cmd(TIM1, ENABLE);
TIM9_PWM_Init(4166-1,4-1);
TIM_Cmd(TIM9, ENABLE);
TIM12_PWM_Init(4166-1,2-1);
TIM_Cmd(TIM12, ENABLE);
while(1)
{
LED0=0;
Motor1Pwm(0.5*4166);
Motor2Pwm(0.5*4166);
Motor3Pwm(0.5*4166);
Motor4Pwm(0.5*4166);
delay_ms(2000);
Motor1Pwm(0);
Motor2Pwm(0);
Motor3Pwm(0);
Motor4Pwm(0);
delay_ms(2000);
LED0=1;
Motor1Pwm(-0.5*4166);
Motor2Pwm(-0.5*4166);
Motor3Pwm(-0.5*4166);
Motor4Pwm(-0.5*4166);
delay_ms(2000);
Motor1Pwm(0);
Motor2Pwm(0);
Motor3Pwm(0);
Motor4Pwm(0);
delay_ms(2000);
LED0=0;
Motor1Pwm(0.7*4166);
Motor2Pwm(0.7*4166);
Motor3Pwm(0.7*4166);
Motor4Pwm(0.7*4166);
delay_ms(2000);
Motor1Pwm(0);
Motor2Pwm(0);
Motor3Pwm(0);
Motor4Pwm(0);
delay_ms(2000);
LED0=1;
Motor1Pwm(-0.7*4166);
Motor2Pwm(-0.7*4166);
Motor3Pwm(-0.7*4166);
Motor4Pwm(-0.7*4166);
delay_ms(2000);
Motor1Pwm(0);
Motor2Pwm(0);
Motor3Pwm(0);
Motor4Pwm(0);
delay_ms(2000);
}
}
|