電機PWM初始化代碼:
//電機PWM口初始化
//左電機 arr:自動重裝值 psc:時鐘預分頻數 右電機 arr2:自動重裝值 psc2:時鐘預分頻數
void Motor_PWM_Init(u16 arr, u16 psc, u16 arr2, u16 psc2 )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//PWMA PB7
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定時器3時鐘
//設定該引腳為復用輸出功能,輸出TIM4 CH1的PWM脈沖波形 GPIOB.7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //設定在下一個更新事件裝入活動的自動重裝載暫存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //設定用來作為TIMx時鐘頻率除數的預分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位
//初始化TIM4 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //根據T指定的引數初始化外設TIM4 OC2
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的預裝載暫存器
TIM_Cmd(TIM4, ENABLE); //使能TIM3
//PWMB PB6
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定時器3時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外設和AFIO復用功能模塊時鐘
//設定該引腳為復用輸出功能,輸出TIM4 CH1的PWM脈沖波形 GPIOB.6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //TIM4_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM4
TIM_TimeBaseStructure.TIM_Period = arr2; //設定在下一個更新事件裝入活動的自動重裝載暫存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc2; //設定用來作為TIMx時鐘頻率除數的預分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根據T指定的引數初始化外設TIM3 OC2
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR2上的預裝載暫存器
TIM_Cmd(TIM4, ENABLE); //使能TIM3
}
電機GPIO初始化:
#include "stm32f10x.h"
#include "bsp_gpio.h"
#include "bsp_motor.h"
/**
* Function MOTOR_GPIO_Config
* @author liusen
* @date 2015.01.03
* @brief 電機GPIO口初始化
* @param[in] void
* @param[out] void
* @retval void
* @par History 無
*/
void MOTOR_GPIO_Init(void)
{
/*定義一個GPIO_InitTypeDef型別的結構體*/
GPIO_InitTypeDef GPIO_InitStructure;
/*開啟GPIOB的外設時鐘*/
RCC_APB2PeriphClockCmd(Motor_RCC, ENABLE);
/*選擇要控制的GPIOB引腳*/
GPIO_InitStructure.GPIO_Pin = Left_MotoA_Pin | Left_MotoB_Pin | Right_MotoA_Pin | Right_MotoB_Pin;
/*設定引腳模式為通用推挽輸出*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/*設定引腳速率為50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*呼叫庫函式,初始化GPIOB*/
GPIO_Init(Motor_Port, &GPIO_InitStructure);
/* 低電平 */
GPIO_ResetBits(Motor_Port, Left_MotoA_Pin | Left_MotoB_Pin | Right_MotoA_Pin | Right_MotoB_Pin);
//PB4關閉JTAG
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外設和AFIO復用功能模塊時鐘
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //禁能jtag
}
小車運動函式:
//小車運動控制函式
#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); GPIO_ResetBits(Motor_Port, Right_MotoB_Pin);}
#define RightMotor_Back() {GPIO_ResetBits(Motor_Port, Right_MotoA_Pin); GPIO_SetBits(Motor_Port, Right_MotoB_Pin);}
#define RightMotor_Stop() {GPIO_ResetBits(Motor_Port, Right_MotoA_Pin); GPIO_ResetBits(Motor_Port, Right_MotoB_Pin);}
#define LeftMotorPWM(Speed) TIM_SetCompare2(TIM4, Speed);
#define RightMotorPWM(Speed) TIM_SetCompare1(TIM4, Speed);
//小車前進
void Car_Run(int Speed)
{
LeftMotor_Go();
RightMotor_Go();
LeftMotorPWM(Speed);
RightMotorPWM(Speed);
}
//小車后退
void Car_Back(int Speed)
{
LeftMotor_Back();
RightMotor_Back();
LeftMotorPWM(Speed);
RightMotorPWM(Speed);
}
//小車左轉
void Car_Left(int Speed)
{
LeftMotor_Stop();
RightMotor_Go();
LeftMotorPWM(0);
RightMotorPWM(Speed);
}
//小車右轉
void Car_Right(int Speed)
{
LeftMotor_Go();
RightMotor_Stop();
LeftMotorPWM(Speed);
RightMotorPWM(0);
}
//小車剎車
void Car_Stop(void)
{
LeftMotor_Stop();
RightMotor_Stop();
LeftMotorPWM(0);
RightMotorPWM(0);
}
初始化:
void bsp_init(void)
{
delay_init();
MOTOR_GPIO_Init(); /*電機GPIO初始化*/
Motor_PWM_Init(7200,0, 7200, 0); /*不分頻。PWM頻率 72000000/7200=10khz */
}
主函式:
int main(void)
{
bsp_init();
while (1)
{
Car_Run(3600);
delay_ms(1000);
Car_Back(3600);
delay_ms(1000);
Car_Left(3600);
delay_ms(1000);
Car_Right(3600);
delay_ms(1000);
Car_SpinLeft(3600, 3600);
delay_ms(1000);
Car_SpinRight(3600, 3600);
delay_ms(1000);
Car_Stop();
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276008.html
標籤:硬件使用
上一篇:求助!DSP28335的SCIA接MAX3488芯片之后,Rx引腳一直為低電平,哪位大神知道原因,請賜教!
下一篇:學生小白跪求解哪位大神能幫幫我
