单片机采用STM32驱动板采用两个L298N 电源采用12V 驱动四个电机使用PWM波先加速再匀速的完整代码包括配置PWM波 L298N接线应该怎样接
时间: 2024-06-08 22:08:40 浏览: 159
以下是基于STM32驱动L298N控制电机的代码和连接方式,其中使用了TIM2和TIM3作为PWM输出,电机连接方式如下图所示:
![L298N Connection Diagram](https://cdn.learnku.com/uploads/images/202108/03/41677/5Gk1P3dmVz.png!large)
代码如下:
```c
#include "stm32f10x.h"
#define PWM_PERIOD 1000
#define PWM_PRESCALER 72
void L298N_GPIO_Config(void);
void PWM_Config(void);
void PWM_output(uint16_t pwm_duty);
int main(void)
{
L298N_GPIO_Config();
PWM_Config();
while(1)
{
//加速
for(int i=0;i<=100;i++)
{
uint16_t pwm_duty = i*PWM_PERIOD/100;
PWM_output(pwm_duty);
Delay_ms(10);
}
//匀速
PWM_output(PWM_PERIOD/2);
Delay_ms(2000);
}
}
void L298N_GPIO_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
//电机1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//电机2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void PWM_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM2
TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD - 1;
TIM_TimeBaseStructure.TIM_Prescaler = PWM_PRESCALER - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
//TIM3
TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD - 1;
TIM_TimeBaseStructure.TIM_Prescaler = PWM_PRESCALER - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
void PWM_output(uint16_t pwm_duty)
{
TIM_SetCompare1(TIM2, pwm_duty);
TIM_SetCompare2(TIM2, 0);
TIM_SetCompare1(TIM3, pwm_duty);
TIM_SetCompare2(TIM3, 0);
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOB, GPIO_Pin_6);
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
}
void Delay_ms(uint32_t nCount)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
uint32_t ticks = RCC_Clocks.HCLK_Frequency/10000;
while(nCount--)
{
volatile uint32_t i;
for(i=0; i<ticks; i++);
}
}
```
其中,`L298N_GPIO_Config`函数用于配置L298N接口的GPIO口,包括四个INx引脚。`PWM_Config`函数用于配置PWM输出的TIM2和TIM3,`PWM_output`函数用于设置PWM输出的占空比和L298N的INx引脚状态。`Delay_ms`函数用于延时,单位为毫秒。在`main`函数中,首先调用`L298N_GPIO_Config`和`PWM_Config`函数对GPIO口和PWM进行配置,然后使用`PWM_output`函数逐渐加速电机,最后以匀速运行。
阅读全文