• 大小: 3.28MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-29
  • 语言: 其他
  • 标签: STM32  

资源简介

STM32定时器利用主从方式的门控模式输出可控个数的PWM脉冲

资源截图

代码片段和文件信息

#include “stm32f10x.h“
#include “timer.h“

//==============================
//
// 配置中断向量表及优先级
//
//==============================
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef  VECT_TAB_RAM
  NVIC_SetVectorTable(NVIC_VectTab_RAM 0x0);
#else  
  NVIC_SetVectorTable(NVIC_VectTab_FLASH 0x0);
#endif


NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

}

void GPIO_Config(void)

    GPIO_InitTypeDef GPIO_InitStructure; 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 ENABLE); 

    /* Timer2 Channel 1 PA0 */ 
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_Init(GPIOA &GPIO_InitStructure); 




//================================================================================
void TIM2_Master__TIM3_Slave_Configuration(u32 PulseFrequency) 

TIM_TimebaseInitTypeDef TIM_TimebaseStructure; 
TIM_OCInitTypeDef TIM_OCInitStructure; 

u16 nPDTemp ; 

TIM_Cmd(TIM2 DISABLE); 
nPDTemp = 72000UL/PulseFrequency;     // 更改频率时,注意处理72000UL及TIM_Prescaler,使TIM_Period不要超过0xFFFF

// 时基配置:配置PWM输出定时器——TIM2 
/* Time base configuration */ 
TIM_TimebaseStructure.TIM_Period = nPDTemp-1; 
TIM_TimebaseStructure.TIM_Prescaler = 999; 
TIM_TimebaseStructure.TIM_ClockDivision = 0; 
TIM_TimebaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimebaseStructure.TIM_RepetitionCounter = 0; 
TIM_TimebaseInit(TIM2 &TIM_TimebaseStructure); 

// 输出配置:配置PWM输出定时器——TIM2 
/* PWM1 Mode configuration: Channel1 */    
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 = nPDTemp>>1;//50% 
TIM_OC1Init(TIM2 &TIM_OCInitStructure); 
TIM_OC1PreloadConfig(TIM2 TIM_OCPreload_Enable); 
TIM_ARRPreloadConfig(TIM2 ENABLE); 


// 时基配置:配置脉冲计数寄存器——TIM3 
TIM_TimebaseStructure.TIM_Period = 0xFFFF;       
TIM_TimebaseStructure.TIM_Prescaler = 0; 
TIM_TimebaseStructure.TIM_ClockDivision = 0; 
TIM_TimebaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimebaseStructure.TIM_RepetitionCounter = 0; 
TIM_TimebaseInit(TIM3 &TIM_TimebaseStructure); 

// Output Compare Active Mode configuration: Channel1 
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; 
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // 这里的配置值意义不大   
TIM_OC1Init(TIM3 &TIM_OCInitStructure); 


// 配置TIM2为主定

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     312692  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.axf

     文件      19377  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.hex

     文件      54816  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.htm

     文件      16384  2012-05-23 16:05  定时器主从方式输出可控个数的PWM脉冲\AllInOne.IAB

     文件        488  2012-05-23 16:05  定时器主从方式输出可控个数的PWM脉冲\AllInOne.IAD

     文件       8192  2012-05-23 16:05  定时器主从方式输出可控个数的PWM脉冲\AllInOne.IMB

     文件        408  2012-05-23 16:05  定时器主从方式输出可控个数的PWM脉冲\AllInOne.IMD

     文件        392  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.lnp

     文件      40682  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.map

     文件         92  2012-05-23 16:08  定时器主从方式输出可控个数的PWM脉冲\AllInOne.PFI

     文件       1757  2012-11-03 09:36  定时器主从方式输出可控个数的PWM脉冲\AllInOne.plg

     文件        776  2012-05-23 16:08  定时器主从方式输出可控个数的PWM脉冲\AllInOne.PO

     文件       8936  2012-05-23 16:02  定时器主从方式输出可控个数的PWM脉冲\AllInOne.PR

     文件      47064  2012-05-23 16:05  定时器主从方式输出可控个数的PWM脉冲\AllInOne.PRI

     文件      47640  2012-05-23 16:25  定时器主从方式输出可控个数的PWM脉冲\AllInOne.PS

     文件        479  2016-05-17 21:37  定时器主从方式输出可控个数的PWM脉冲\AllInOne.sct

     文件       1381  2016-05-17 21:40  定时器主从方式输出可控个数的PWM脉冲\AllInOne.tra

     文件      71224  2015-06-25 21:55  定时器主从方式输出可控个数的PWM脉冲\AllInOne.uvgui.Administrator

    .......    140662  2016-11-17 16:44  定时器主从方式输出可控个数的PWM脉冲\AllInOne.uvgui.lxy

    .......    140659  2016-10-14 14:07  定时器主从方式输出可控个数的PWM脉冲\AllInOne.uvgui_lxy.bak

    .......     13780  2016-11-17 16:44  定时器主从方式输出可控个数的PWM脉冲\AllInOne.uvopt

    .......     16975  2016-05-17 21:57  定时器主从方式输出可控个数的PWM脉冲\AllInOne.uvproj

     文件      11574  2012-05-23 16:25  定时器主从方式输出可控个数的PWM脉冲\AllInOne.WK3

     文件      11991  2016-11-17 16:44  定时器主从方式输出可控个数的PWM脉冲\AllInOne_AllInOne.dep

    .......       479  2012-05-05 17:26  定时器主从方式输出可控个数的PWM脉冲\AllInOne_sct.Bak

     文件        268  2012-05-04 16:27  定时器主从方式输出可控个数的PWM脉冲\AllInOne_Target 1.dep

    .......     13779  2016-10-14 14:07  定时器主从方式输出可控个数的PWM脉冲\AllInOne_uvopt.bak

    .......     16906  2015-06-25 21:55  定时器主从方式输出可控个数的PWM脉冲\AllInOne_uvproj.bak

     文件       3603  2016-05-17 17:24  定时器主从方式输出可控个数的PWM脉冲\core_cm3.crf

     文件        112  2016-05-17 17:24  定时器主从方式输出可控个数的PWM脉冲\core_cm3.d

............此处省略206个文件信息

评论

共有 条评论