常用函数简单说明
①用来配置输出比较模块 TIM_OC1Init()
(初始化) TIM_OC2Init()
TIM_OC3Init()
TIM_OC4Init()
②TIM_OCStructInit();用来给输出比较结构体赋一个默认值
③用来配置强制输出模式 TIM_ForcedOC1Config()
TIM_ForcedOC2Config()
TIM_ForcedOC3Config()
TIM_ForcedOC4Config()
④用来配置CCR寄存器的预装功能
TIM_OC1PreloadConfig()
TIM_OC2PreloadConfig()
TIM_OC3PreloadConfig()
TIM_OC4PreloadConfig()
⑤用来配置快速使能的 TIM_OC1FastConfig() TIM_OC2FastConfig()
TIM_OC3FastConfig()
TIM_OC4FastConfig()
⑥用来单独设置输出比较的极性 TIM_OC1PolarityConfig() TIM_OC1NPolarityConfig() TIM_OC2PolarityConfig()
TIM_OC2NPolarityConfig()
TIM_OC3PolarityConfig() TIM_OC3NPolarityConfig()
TIM_OC4PolarityConfig()
⑦用来氮素修改输出使能参数 TIM_CCxCmd()
TIM_CCxNCmd()
⑧TIM_SelectOCxM();选择输出比较模式
⑨用来单独更改CCR寄存器值 TIM_SetCompare1()
TIM_SetCompare2()
TIM_SetCompare3()
TIM_SetCompare4()
⑩TIM_CtrlPWMOutputs();仅高级定时器使用,在使用高级定时器输出PWM时,需要调用这个函数使能主输出,否则PWM将不能正常输出
/*
编写步骤
1.RCC开启时钟(TIM、GPIO)
2.配置时基单元
3.配置输出比较单元
4.配置GPIO
5.运行控制
*/
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "pwm_led.h"
//初始化函数
void PWM_Init(void)
{
/*开启时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //开启TIM2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟
/*GPIO初始化*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA0引脚初始化为复用推挽输出
//受外设控制的引脚,均需要配置为复用模式
/*配置时钟源*/
TIM_InternalClockConfig(TIM2); //选择TIM2为内部时钟,若不调用此函数,TIM默认也为内部时钟
/*时基单元初始化*/
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的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //预分频器,即PSC的值
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器,高级定时器才会用到
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); //将结构体变量交给TIM_TimeBaseInit,配置TIM2的时基单元
/*输出比较初始化*/
TIM_OCInitTypeDef TIM_OCInitStructure; //定义结构体变量
TIM_OCStructInit(&TIM_OCInitStructure); //结构体初始化,若结构体没有完整赋值,则最好执行此函数,给结构体所有成员都赋一个默认值,避免结构体初值不确定的问题
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //输出比较模式,选择PWM模式1
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); //将结构体变量交给TIM_OC1Init,配置TIM2的输出比较通道1
/*TIM使能*/
TIM_Cmd(TIM2, ENABLE); //使能TIM2,定时器开始运行
}
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM2, Compare); //设置CCR1的值
}
#ifndef _PWM_LED_H
#define _PWM_LED_H
#include "stdint.h"
void PWM_Init(void);
void PWM_SetCompare1(uint16_t Compare);
#endif
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "delay.h"
#include "OLED.h"
#include "pwm_led.h"
uint8_t i; //定义for循环的变量
int main (void)
{
/*模块初始化*/
OLED_Init();
PWM_Init();
while(1)
{
for (i = 0; i <= 100; i++)
{
PWM_SetCompare1(i); //依次将定时器的CCR寄存器设置为0~100,PWM占空比逐渐增大,LED逐渐变亮
delay_ms(10); //延时10ms
}
for (i = 0; i <= 100; i++)
{
PWM_SetCompare1(100 - i); //依次将定时器的CCR寄存器设置为100~0,PWM占空比逐渐减小,LED逐渐变暗
delay_ms(10); //延时10ms
}
}
}