用stm32f103c8t6写一个用PWM波控制电机速度的程序
时间: 2023-12-18 13:05:25 浏览: 66
以下是基于STM32F103C8T6芯片写的PWM波控制电机速度的程序:
首先,需要在代码中定义相关的引脚和变量:
```
#include "stm32f10x.h"
#define MOTOR_PIN GPIO_Pin_0
#define MOTOR_PORT GPIOA
#define MOTOR_RCC RCC_APB2Periph_GPIOA
#define PWM_PIN GPIO_Pin_1
#define PWM_PORT GPIOA
#define PWM_RCC RCC_APB2Periph_GPIOA
#define TIM3_RCC RCC_APB1Periph_TIM3
#define PWM_TIM TIM3
```
在主函数中初始化GPIO和定时器:
```
int main(void)
{
//初始化系统时钟
SystemInit();
//使能GPIO和定时器时钟
RCC_APB2PeriphClockCmd(MOTOR_RCC | PWM_RCC, ENABLE);
RCC_APB1PeriphClockCmd(TIM3_RCC, ENABLE);
//初始化MOTOR引脚
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOTOR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MOTOR_PORT, &GPIO_InitStructure);
//初始化PWM引脚
GPIO_InitStructure.GPIO_Pin = PWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_PORT, &GPIO_InitStructure);
//初始化定时器
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 5999; //设置PWM频率为10kHz
TIM_TimeBaseStructure.TIM_Prescaler = 71; //设置分频系数为72
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(PWM_TIM, &TIM_TimeBaseStructure);
//初始化PWM输出通道
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //设置初始占空比为0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(PWM_TIM, &TIM_OCInitStructure);
//启动定时器
TIM_Cmd(PWM_TIM, ENABLE);
}
```
然后,在主程序中可以使用以下代码改变PWM输出的占空比来控制电机的速度:
```
//设置PWM输出的占空比
TIM_SetCompare1(PWM_TIM, pulse_width);
```
其中,pulse_width为一个0到5999之间的值,代表PWM输出的高电平时间长度,可以通过改变pulse_width的值来改变PWM波的占空比,从而达到控制电机速度的目的。
完整代码如下:
```
#include "stm32f10x.h"
#define MOTOR_PIN GPIO_Pin_0
#define MOTOR_PORT GPIOA
#define MOTOR_RCC RCC_APB2Periph_GPIOA
#define PWM_PIN GPIO_Pin_1
#define PWM_PORT GPIOA
#define PWM_RCC RCC_APB2Periph_GPIOA
#define TIM3_RCC RCC_APB1Periph_TIM3
#define PWM_TIM TIM3
int main(void)
{
//初始化系统时钟
SystemInit();
//使能GPIO和定时器时钟
RCC_APB2PeriphClockCmd(MOTOR_RCC | PWM_RCC, ENABLE);
RCC_APB1PeriphClockCmd(TIM3_RCC, ENABLE);
//初始化MOTOR引脚
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOTOR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MOTOR_PORT, &GPIO_InitStructure);
//初始化PWM引脚
GPIO_InitStructure.GPIO_Pin = PWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_PORT, &GPIO_InitStructure);
//初始化定时器
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 5999; //设置PWM频率为10kHz
TIM_TimeBaseStructure.TIM_Prescaler = 71; //设置分频系数为72
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(PWM_TIM, &TIM_TimeBaseStructure);
//初始化PWM输出通道
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //设置初始占空比为0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(PWM_TIM, &TIM_OCInitStructure);
//启动定时器
TIM_Cmd(PWM_TIM, ENABLE);
while(1)
{
int pulse_width = 3000; //PWM输出的占空比,范围为0-5999
TIM_SetCompare1(PWM_TIM, pulse_width); //设置PWM输出的占空比
GPIO_SetBits(MOTOR_PORT, MOTOR_PIN); //打开电机
delay_ms(5000);
pulse_width = 0;
TIM_SetCompare1(PWM_TIM, pulse_width);
GPIO_ResetBits(MOTOR_PORT, MOTOR_PIN); //关闭电机
delay_ms(5000);
}
}
//延时函数
void delay_ms(uint32_t ms)
{
uint32_t i;
for(i=0; i<ms*4000; i++);
}
```
阅读全文