- 项目: stm32 PWM控制舵机
- 代码
- Servo.c
#include "stm32f10x.h" #include "PWM.h" //舵机初始化 void Servo_Init(void) { PWM_Init(); } //舵机角度设定 void Servo_SetAngle(float Angle) { PWM_SetCompare1(Angle / 180 * 2000 + 500); //Angle=0, PWM_SetCompare1(500)对应舵机0°角 //Angle=180,PWM_SetCompare1(2500)对应舵机180°角 }
- Servo.h
#ifndef __SERVO_H #define __SERVO_H void Servo_Init(void); void Servo_SetAngle(float Angle); #endif
- main.c
#include "stm32f10x.h" #include "Delay.h" #include "OLED.h" #include "Servo.h" #include "Key.h" uint8_t KeyNum; float Angle; int main(void) { OLED_Init(); Servo_Init(); Key_Init(); OLED_ShowString(1, 1, "Angle:"); while(1) { KeyNum = Key_GetNum(); if(KeyNum == 1) { Angle += 30; if(Angle > 180) { Angle = 0; } } Servo_SetAngle(Angle); OLED_ShowNum(1, 7, Angle, 3); } }
- PWM.c
#include "stm32f10x.h" void PWM_Init(void) { //PB0默认TIM3_CH3通道 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //配置PB0为复用推挽输出 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出,中断控制来自片上外设(TIM3_CH3),而非输出数据寄存器 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //设置TIM3定时器内部时钟源 TIM_InternalClockConfig(TIM3); //频率为1kHz, 占空比为50%,分辨率为1% TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //清除更新标志位 TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //TIM3中断配置使能 TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初始值 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //输出比较模式 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性选择 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能 TIM_OCInitStructure.TIM_Pulse = 0; //CCR TIM_OC1Init(TIM3, &TIM_OCInitStructure); //打开TIM3定时器外设 TIM_Cmd(TIM3, ENABLE); } //PWM占空比的函数 void PWM_SetCompare1(uint16_t Compare1) { TIM_SetCompare1(TIM3, Compare1); }
- PWM.h
#ifndef __PWM_H #define __PWM_H void PWM_Init(void); void PWM_SetCompare1(uint16_t Compare1); #endif
- Key.c
#include "stm32f10x.h" #include "delay.h" void Key_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } uint8_t Key_GetNum(void) { uint8_t KeyNum = 0; if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1) //判断GPIOA Pin_0口高低 { Delay_ms(20); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1); Delay_ms(20); KeyNum =1; } return KeyNum; }
- Key.h
#ifndef __KEY_H #define __KEY_H void Key_Init(void); uint8_t Key_GetNum(void); #endif
- Delay.c
#include "stm32f10x.h" /** * @brief 微秒级延时 * @param xus 延时时长,范围:0~233015 * @retval 无 */ void Delay_us(uint32_t xus) { SysTick->LOAD = 72 * xus; //设置定时器重装值 SysTick->VAL = 0x00; //清空当前计数值 SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器 while(!(SysTick->CTRL & 0x00010000)); //等待计数到0 SysTick->CTRL = 0x00000004; //关闭定时器 } /** * @brief 毫秒级延时 * @param xms 延时时长,范围:0~4294967295 * @retval 无 */ void Delay_ms(uint32_t xms) { while(xms--) { Delay_us(1000); } } /** * @brief 秒级延时 * @param xs 延时时长,范围:0~4294967295 * @retval 无 */ void Delay_s(uint32_t xs) { while(xs--) { Delay_ms(1000); } }
- Delay.h
#ifndef __DELAY_H #define __DELAY_H void Delay_us(uint32_t us); void Delay_ms(uint32_t ms); void Delay_s(uint32_t s); #endif
- Servo.c
- 参考资料