stm32f103按键控制电机转速实验内容
时间: 2023-07-29 16:11:48 浏览: 244
基于STM32F103的直流电机调速系统_基于STM32F103_调速电机_直流电机的_
5星 · 资源好评率100%
实验目的:使用STM32F103单片机控制直流电机转速,并通过按键控制电机的转速。
实验器材:STM32F103C8T6开发板、直流电源、直流电机、按键开关、杜邦线等。
实验原理:通过PWM输出控制电机的转速,按键控制占空比的变化,从而改变电机的转速。
实验步骤:
1. 将STM32F103C8T6开发板与直流电机连接,电源接入开发板的5V和GND端口,电机的正极接入开发板的PA6口,电机的负极接入开发板的GND端口。
2. 将按键连接到开发板的PC13口,其中一个端口接入PC13口,另一个端口接入GND端口。
3. 配置开发板的PWM输出,将PA6口配置为TIM3_CH1输出,使用TIM3作为PWM输出的定时器。具体配置方法可以参考STM32的官方文档和开发板的手册。
4. 配置开发板的中断服务函数,当按键按下时,改变PWM的占空比,从而改变电机的转速。具体代码实现可以参考下面的示例代码。
5. 编写主函数,初始化开发板的各个模块,并进入循环中等待按键的触发。
示例代码:
```
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#define MOTOR_PWM_TIM TIM3
#define MOTOR_PWM_PIN GPIO_Pin_6
#define MOTOR_PWM_GPIO GPIOA
#define MOTOR_PWM_TIM_CLK RCC_APB1Periph_TIM3
#define MOTOR_PWM_GPIO_CLK RCC_APB2Periph_GPIOA
#define BUTTON_GPIO GPIOC
#define BUTTON_PIN GPIO_Pin_13
#define BUTTON_GPIO_CLK RCC_APB2Periph_GPIOC
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(MOTOR_PWM_TIM_CLK, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(MOTOR_PWM_GPIO_CLK, ENABLE);
/* GPIOA Configuration: PA6 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = MOTOR_PWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MOTOR_PWM_GPIO, &GPIO_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(MOTOR_PWM_TIM, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(MOTOR_PWM_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(MOTOR_PWM_TIM, TIM_OCPreload_Enable);
/* TIM3 enable counter */
TIM_Cmd(MOTOR_PWM_TIM, ENABLE);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOC clock enable */
RCC_APB2PeriphClockCmd(BUTTON_GPIO_CLK, ENABLE);
/* Configure Button pin as input floating */
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(BUTTON_GPIO, &GPIO_InitStructure);
/* Connect Button EXTI Line to Button GPIO Pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
TIM_Configuration();
EXTI_Configuration();
while (1)
{
/* Do nothing */
}
}
void EXTI15_10_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line13) != RESET)
{
static uint16_t pwm_value = 0;
static uint8_t increase_pwm = 1;
if (increase_pwm)
{
pwm_value += 100;
if (pwm_value >= 1000)
{
increase_pwm = 0;
}
}
else
{
pwm_value -= 100;
if (pwm_value <= 0)
{
increase_pwm = 1;
}
}
TIM_SetCompare1(MOTOR_PWM_TIM, pwm_value);
EXTI_ClearITPendingBit(EXTI_Line13);
}
}
```
实验注意事项:
1. 实验中要注意电机的正负极的接线,保证电机能够正常工作。
2. 在按键的中断服务函数中,要注意占空比的变化范围,避免电机转速变化过快或过慢。
3. 在编写代码时,要注意各个模块的初始化顺序和参数设置,保证程序能够正常运行。
阅读全文