资源简介

利用STM32定时器的同步,可以精准控制PWM脉冲数量(库函数版)

资源截图

代码片段和文件信息

#include “TIM_Master_Slave.h“

void TIM_Master_Slave(void)    //定义TIM2主   TIM4从
{
//定义
TIM_TimebaseInitTypeDef TIM_TimebaseInitStruct;
TIM_OCInitTypeDef  TIM_OCInitStruct;
GPIO_InitTypeDef  GPIO_InitStruct;

NVIC_InitTypeDef  NVIC_InitStruct;
//时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOAENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOBENABLE);

  // TIM2定时器PWM配置
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA &GPIO_InitStruct);

TIM_TimebaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimebaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimebaseInitStruct.TIM_Period = 999 ;
TIM_TimebaseInitStruct.TIM_Prescaler = 71;
TIM_TimebaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimebaseInit(TIM2&TIM_TimebaseInitStruct);

TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_Pulse = 499 ;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable ;
TIM_OC1Init(TIM2 &TIM_OCInitStruct);

//主定时器主模式控制配置
TIM_SelectOutputTrigger(TIM2TIM_TRGOSource_Update);
TIM_SelectMasterSlaveMode(TIM2 TIM_MasterSlaveMode_Enable);


  // TIM4定时器PWM配置
GPIO_InitStruct.

评论

共有 条评论