微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 手机设计讨论 > MTK手机平台交流 > 利用定时器PWM模式输出一定数量脉冲 ---- 一个定时器作为另一个定时器的与分频器方式

利用定时器PWM模式输出一定数量脉冲 ---- 一个定时器作为另一个定时器的与分频器方式

时间:10-02 整理:3721RD 点击:
利用定时器PWM模式输出一定数量脉冲  
方式:定时器的同步,以一个定时器作为另一个定时器的与分频器方式。
具体代码实现:  
void Tim4_Slave_Init(void)
{   TIM_DeInit(TIM4);
//主要用于复位TIM4定时器,使之进入初始状态   TIM_TimeBaseStructure.TIM_Period=pulnum;
//自动重装 载寄存器的值  TIM_TimeBaseStructure.TIM_Prescaler=0;
//时钟预分频数  TIM_TimeBaseStructure.TIM_ClockDivision=0;
//采样分频   TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
//向上溢出  TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM4,TIM_TS_ITR0);
TIM_InternalClockConfig(TIM4);
TIM4->SMCR =  0X07;
//设定从模式控制寄存器     //TIM_UpdateDisableConfig(TIM4,ENABLE);
TIM_ARRPreloADConfig(TIM4, ENABLE);
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
//清除溢出中断标志   TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
// TIM_Cmd(TIM4,ENABLE);
//是否开启时钟(开启后每发送一个脉冲,定时器加一)  
}
void Tim1_PWM_Init(void)
{         
CCR1_Val = (timer1_period+1)/2;
CCR2_Val = (timer1_period+1)/2;
CCR3_Val = (timer1_period+1)/2;
CCR4_Val = (timer1_period+1)/2;
GPIO_PinRemapConfig(
GPIO_FullRemap_TIM1, ENABLE);
/* -----------------------------------------------------------------------      
TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles:     
TIM1CLK = 36 MHz, Prescaler = 0x0, TIM1 counter clock = 36 MHz      
TIM1 ARR Register = 999 => TIM1 Frequency = TIM1 counter clock/(ARR + 1)     
TIM1 Frequency = 36 KHz.      
TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 50%     
TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR)* 100 = 50%     
TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR)* 100 = 50%     
TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR)* 100 = 50%   
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = timer1_period;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM1->CR1 &= ((u16)0x03FD);
// UDIS enable     
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);//apre   
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top