stm32F103pwm控制tb6612代码
时间: 2023-08-13 22:46:42 浏览: 97
以下是使用STM32F103和TB6612驱动板实现PWM控制的示例代码:
```c
#include "stm32f10x.h"
//定义PWM输出引脚
#define AIN1_H GPIO_SetBits(GPIOA,GPIO_Pin_0)
#define AIN1_L GPIO_ResetBits(GPIOA,GPIO_Pin_0)
#define AIN2_H GPIO_SetBits(GPIOA,GPIO_Pin_1)
#define AIN2_L GPIO_ResetBits(GPIOA,GPIO_Pin_1)
#define BIN1_H GPIO_SetBits(GPIOA,GPIO_Pin_2)
#define BIN1_L GPIO_ResetBits(GPIOA,GPIO_Pin_2)
#define BIN2_H GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define BIN2_L GPIO_ResetBits(GPIOA,GPIO_Pin_3)
//定义PWM输出频率
#define PWM_FREQ 1000
//定义PWM输出占空比
#define DUTY_CYCLE 50
//定义PWM输出计数值
#define PWM_CNT (72000000/PWM_FREQ/100)
void TIM3_PWM_Init(void)
{
//定义结构体变量
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//使能GPIO和TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = PWM_CNT - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//初始化PWM输出
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ((PWM_CNT - 1)*DUTY_CYCLE)/100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ((PWM_CNT - 1)*DUTY_CYCLE)/100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ((PWM_CNT - 1)*DUTY_CYCLE)/100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ((PWM_CNT - 1)*DUTY_CYCLE)/100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
//启动TIM3
TIM_Cmd(TIM3, ENABLE);
}
int main(void)
{
//初始化PWM输出
TIM3_PWM_Init();
while(1)
{
//控制电机正转
AIN1_H;
AIN2_L;
BIN1_H;
BIN2_L;
//延时一段时间
for(int i = 0;i < 1000000;i++);
}
}
```
这是一个简单的示例代码,可以通过修改AIN1_H等宏定义来控制输出的PWM信号。在本例中,TIM3被用作PWM输出,GPIOA的0、1、2、3引脚被配置为TIM3的通道1、2、3、4输出。在main函数中,控制AIN1、AIN2、BIN1、BIN2引脚的电平来控制电机的转动方向。
阅读全文