本帖最后由 lengxiaohua 于 2016-8-23 11:43 编辑
写了一个两轮自平衡车电机驱动代码,但仿真运行时两个PWM 都没有输出,看了很长时间也不知道为什么,不知道是不是配置的问题,希望高手帮忙看看 谢谢拉
主函数:main.c
#include "STM32f10x.h"
#include "MotorDriver.h"
int main(void)
{
MotorDriver_Init();
while(1);
}
电机驱动函数:MotorDriver.c
PC0-3控制左右轮的正反转,PC6-tiM8_CH1 PC7-TIM8_CH2输出PWM波
#include "stm32f10x.h"
//#include "GlobalVars.h"
#include "MotorDriver.h"
//左轮:PC0、PC1 右轮:PC2、PC3
#define MOTOR_L_IN1_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_0))
#define MOTOR_L_IN1_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_0))
#define MOTOR_L_IN2_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_1))
#define MOTOR_L_IN2_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_1))
#define MOTOR_R_IN1_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_2))
#define MOTOR_R_IN1_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_2))
#define MOTOR_R_IN2_LOW (GPIO_ResetBits(GPIOC, GPIO_Pin_3))
#define MOTOR_R_IN2_HIGH (GPIO_SetBits(GPIOC, GPIO_Pin_3))
//电机驱动左轮配置
void MotorDriver_L_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure ;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//使能GPIOC、AFIO、TIM8时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM8,ENABLE);
//PC0、PC1初始化
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PC6初始化为AF_PP模式(TIM8_CH1)
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//TIM8_CH1配置
TIM_BaseInitStructure.TIM_Prescaler=3-1; //时钟预分频数3,TIM8的计数时钟频率为24MHz
TIM_BaseInitStructure.TIM_Period=1000-1; //自动重装载寄存器数值,PWM2频率为24MHz/1000=24KHz
TIM_BaseInitStructure.TIM_ClockDivision=0; //采样分频
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_BaseInitStructure.TIM_RepetitionCounter=0; //重复计数器
TIM_TimeBaseInit(TIM8,&TIM_BaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure); //把TIM_OCInitStruct 中的每一个参数按缺省值填入
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //PWM1输出模式
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Enable;//使能该通道
TIM_OC1Init(TIM8,&TIM_OCInitStructure); //按指定参数初始化
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIM8在ARR上的预装载寄存器
TIM_Cmd(TIM8, ENABLE); //打开TIM8
TIM_CtrlPWMOutputs(TIM8, ENABLE); //PWM输出使能
}
//电机驱动右轮配置
void MotorDriver_R_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure ;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//使能GPIOC、AFIO、TIM8时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM8,ENABLE);
//PC2、PC3初始化
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PC7初始化为AF_PP模式(TIM8_CH2)
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//TIM8_CH2配置
TIM_BaseInitStructure.TIM_Prescaler=3-1; //时钟预分频数3,TIM8的计数时钟频率为24MHz
TIM_BaseInitStructure.TIM_Period=1000-1; //自动重装载寄存器数值,PWM2频率为24MHz/1000=24KHz
TIM_BaseInitStructure.TIM_ClockDivision=0; //采样分频
TIM_BaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_BaseInitStructure.TIM_RepetitionCounter=0; //重复计数器
TIM_TimeBaseInit(TIM8,&TIM_BaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure); //把TIM_OCInitStruct 中的每一个参数按缺省值填入
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //PWM1输出模式
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputState_Enable;//使能该通道
TIM_OC2Init(TIM8,&TIM_OCInitStructure); //按指定参数初始化
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIM8在ARR上的预装载寄存器
TIM_Cmd(TIM8, ENABLE); //打开TIM8
TIM_CtrlPWMOutputs(TIM8, ENABLE); //PWM输出使能
}
//左轮电机正转
void MotorDriver_L_Turn_Forward(void)
{
MOTOR_L_IN1_LOW;
MOTOR_L_IN2_HIGH;
}
//左轮电机反转
void MotorDriver_L_Turn_Reverse(void)
{
MOTOR_L_IN1_HIGH;
MOTOR_L_IN2_LOW;
}
//右轮电机正转
void MotorDriver_R_Turn_Forward(void)
{
MOTOR_R_IN1_LOW;
MOTOR_R_IN2_HIGH;
}
//右轮电机反转
void MotorDriver_R_Turn_Reverse(void)
{
MOTOR_R_IN1_HIGH;
MOTOR_R_IN2_LOW;
}
//电机驱动外设初始化
void MotorDriver_Init(void)
{
MotorDriver_L_Config();
MotorDriver_R_Config();
MotorDriver_L_Turn_Forward();
MotorDriver_R_Turn_Forward();
}
驱动头文件:MotorDriver.h
#ifndef _MOTORDRIVER_H_
#define _MOTORDRIVER_H_
void MotorDriver_L_Config(void);
void MotorDriver_R_Config(void);
void MotorDriver_L_Turn_Forward(void); //左轮电机正转
void MotorDriver_L_Turn_Reverse(void); //左轮电机反转
void MotorDriver_R_Turn_Forward(void); //右轮电机正转
void MotorDriver_R_Turn_Reverse(void); //右轮电机反转
void MotorDriver_Init(void); //电机驱动外设初始化
#endif
恩 在仔细看看吧 谢谢你
一周热门 更多>