1.PWM驱动LED呼吸灯
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct); //结构体初始化输出比较单元
void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState); //仅高级定时器使用,在使用高级定时器输出PWM函数时调用。否则PWM不能正常输出
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);
//单独更改通道1的CCR值
void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
void TIM_SetCompare3(TIM_TypeDef* TIMx, uint16_t Compare3);
void TIM_SetCompare4(TIM_TypeDef* TIMx, uint16_t Compare4);
(1)main.c
#include "Device/Include/stm32f10x.h" // Device header
#include "pwm.h"
#include "delay.h"
#include "OLED.h"
uint8_t i;
int main(void)
{OLED_Init();PWM_Init();while(1){for(i=0;i<=100;i++){PWM_setcompare1(i); //通过单独改变通道1(PA0)的CCR值,PWM的占空比、分辨率、频率随之改变Delay_ms(10);}for(i=0;i<=100;i++){PWM_setcompare1(100-i);Delay_ms(10);}}
}
(2) pwm.c
#include "Device/Include/stm32f10x.h" // Device header
void PWM_Init(void)
{
//根据PWM基本结构框图,时基单元初始化、输出比较单元选择(TIM1通道初始化)RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //打开时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出(输出控制权转移给片上外设)GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);TIM_InternalClockConfig(TIM2); //选择内部时钟TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure; //初始化时基单元TIM_TimeBaseInitstructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInitstructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitstructure.TIM_Period=100-1;//定时器周期,实际就是ARR的值。计10000个数TIM_TimeBaseInitstructure.TIM_Prescaler=720-1;//7200-1;//PSC.72MHZ下7200分频TIM_TimeBaseInitstructure.TIM_RepetitionCounter=0;//重复计数器,高级控制寄存器专用寄存器位,这里不用设置TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstructure);//初始化定时器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(TIM2,&TIM_OCInitstructure); //使用PA0口,对应第一个通道,使用OC1Init函数//TIM1通道初始化TIM_Cmd(TIM2,ENABLE);//计数器使能,启动定时器
}
void PWM_setcompare1(uint16_t compare)
{TIM_SetCompare1(TIM2,compare);
}
(3)pwm.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_setcompare1(uint16_t compare);
#endif
2.PWM驱动舵机
(1)main.c
#include "Device/Include/stm32f10x.h" // Device header
#include "servo.h"
#include "delay.h"
#include "OLED.h"
#include "key.h"
uint8_t i;
uint8_t KeyNum;
float Angle;
int main(void)
{OLED_Init();servo_Init();Key_Init();OLED_ShowString(1,1,"Angle:");while(1){KeyNum=Key_GetNumber();if(KeyNum==1){Angle+=30;if(Angle>180){Angle=0;}}servo_SetAngle(Angle);OLED_ShowNum(1,7,Angle,3);}
}
(2)pwm.c
#include "Device/Include/stm32f10x.h" // Device header
void PWM_Init(void)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //打开时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出(输出控制权转移给片上外设)GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1; //用PA1口通道2GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);TIM_InternalClockConfig(TIM2); //选择内部时钟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的值。计10000个数TIM_TimeBaseInitstructure.TIM_Prescaler=72-1;//7200-1;//PSC.72MHZ下7200分频TIM_TimeBaseInitstructure.TIM_RepetitionCounter=0;//重复计数器,高级控制寄存器专用寄存器位,这里不用设置TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstructure);//初始化定时器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_OC2Init(TIM2,&TIM_OCInitstructure); //使用PA0口,对应第一个通道,使用OC1Init函数TIM_Cmd(TIM2,ENABLE);//计数器使能,启动定时器
}
void PWM_setcompare2(uint16_t compare)
{TIM_SetCompare2(TIM2,compare);
}
(3)pwm.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_setcompare2(uint16_t compare);
#endif
(2)servo.c
#include "Device/Include/stm32f10x.h" // Device header
#include "pwm.h"
void servo_Init(void)
{PWM_Init();
}
void servo_SetAngle(float Angle)
{PWM_setcompare2(Angle/180*2000+500);
}
(3)servo.h
#ifndef __SERVO_H
#define __SERVO_Hvoid servo_Init(void);
void servo_SetAngle(float Angle);#endif
3.PWM驱动直流电机
(1)main.c
#include "Device/Include/stm32f10x.h" // Device header
#include "motor.h"
#include "delay.h"
#include "OLED.h"
#include "key.h"
uint8_t KeyNum;
uint8_t i;
int8_t speed;
int main(void)
{OLED_Init();motor_Init();Key_Init();OLED_ShowString(1,1,"speed:");while(1){KeyNum=Key_GetNumber();if(KeyNum==1){speed+=20;if(speed>100){speed=-100;}}Motor_SetSpeed(speed);OLED_ShowSignedNum(1,7,speed,3);}
}
(2)motor.c
#include "Device/Include/stm32f10x.h" // Device header
#include "PWM.h"
void motor_Init(void)
{PWM_Init();RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//????????????????GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void Motor_SetSpeed(int8_t speed) //正转反转有符号
{if(speed>=0){GPIO_SetBits(GPIOA,GPIO_Pin_4);GPIO_ResetBits(GPIOA,GPIO_Pin_5);PWM_setcompare3(speed);}else{GPIO_ResetBits(GPIOA,GPIO_Pin_4);GPIO_SetBits(GPIOA,GPIO_Pin_5);PWM_setcompare3(-speed);}
}
(3)motor.h
#ifndef __MOTOR_H
#define __MOTOR_Hvoid motor_Init(void);
void Motor_SetSpeed(int8_t speed);#endif