stm32 TIM输出比较(OC) PWM

stm32 P6-4  05:00   [6-4] PWM驱动LED呼吸灯&PWM驱动舵机&PWM驱动直流电机_哔哩哔哩_bilibili

1.stm32f10x.tim.h 部分头文件讲解

void TIM_DeInit(TIM_TypeDef* TIMx);
​​​​​​​void TIM_TimeBaseInit(TIM_TypeDef*TIMx,TIM_TimeBaseInitTypeDef*TIM_TimeBaseInitStruct);

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_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct);          //输出比较结构体赋一个默认值

 

void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState);  //仅高级定时器使用,使用高级定时器输出pwm时,调用该函数,使能主输出,否则pwm不能正常输出

 

void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);        //配置强制输出模式
void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction); 
void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction); 
void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);

 

void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);  //配置CCR寄存器预装功能,预装功能:影子寄存器,更新事件才会生效
void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload); 
void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload); 
void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);  

 

void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);      //配置快速使能
void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast); 
void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast); 
void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast); 


void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);        //外部事件时清除REF信号
void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);


void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);    //单独设置输出比较极性,带N就是高级定时器互补通道的配置,OC4没有互补通道   
void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);     
void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);       
void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);

void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx);      //单独修改输出使能参数
void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN);    //单独修改输出使能参数
void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode);  //选择输出比较模式,单独更改输出比较模式



void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);          //单独更改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.开起RCC时钟,把要用的TIM外设和GPIO外设的时钟打开;

 2.配置时机单元;

3.配置时机比较单元(CCR的值,输出比较模式,极性选择,输出使能);

 4.配置GPIO,初始化为复用推挽输出;

 5.运行控制,启动计数器,使能输出PWM; 

  •  PWM频率:    Freq = CK_PSC / (PSC + 1) / (ARR + 1)
  • PWM占空比: Duty = CCR / (ARR + 1)
  • PWM分辨率: Reso = 1 / (ARR + 1)

 rcc开启时钟:

#include "stm32f10x.h" // Device header

void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);               //开启TIM2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //开启gpio端口时钟

// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
// GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

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);

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
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1;                   //PSC
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);              通道已初始化;借用了GPIOA PA0口(见stm32f103c8t6引脚定义),关注一下重映射

TIM_Cmd(TIM2, ENABLE);              //启动定时器

}

 void PWM_SetCompare1(uint16_t Compare)

{
  TIM_SetCompare1(TIM2, Compare);          //在运行时候单独更改通道1的CCR
}

posted @ 2024-03-12 11:01  NevMore  阅读(339)  评论(0编辑  收藏  举报