/*变量定义*/
//extern ledKeyState led_key0 ;
uint16_t tiM3per = 500;
int main(void)
{
vRCC_Config(); //初始化系统,使得系统频率为72M
Init_Delay(72);
TIM_MasterSlaveModeInit(12,TIM3per,TIM3per>>1);
// TIM_Cmd(TIM2,ENABLE);
// TIM_Cmd(TIM3,ENABLE);
while(1)
{
// TIM3per = TIM3per - 2;
// if(TIM3per < 50)
// TIM3per = 100;
// TIM_SetAutoreload(TIM3, TIM3per);//设置TIM3的重装值
// TIM_SetCompare1(TIM3,TIM3per>>1);//设置占空比为50%
// TIM_Cmd(TIM3,ENABLE);//使能TIM3;
}
}
/********************************************************************************
* 函数名:TIM_MasterSlaveModeInit
* 功能 :设置定时器主从模式
* 输入 : TIM2per 定时器TIM2重载值
* TIM3per 定时器TIM3重载值
* TIM3Compare1 定时器TIM3比较预装载值
* 输出 :无
********************************************************************************/
void TIM_MasterSlaveModeInit(uint16_t TIM2per, uint16_t TIM3per, uint16_t TIM3Compare1)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
/*使能外设时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_6; //PA0
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
/*TIM2工作在单脉冲下*/
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //没有时钟分割
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStruct.TIM_Prescaler = 719; //预分频值,每100us计数一次
TIM_TimeBaseInitStruct.TIM_Period = TIM2per; //重装值
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
// TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single); //设置TIM2在单脉冲模式
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //OC1输出使能
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高
TIM_OCInitStruct.TIM_Pulse = 7; //预装载值
TIM_OC1Init(TIM2,&TIM_OCInitStruct);
/*TIM2中断设置*/
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*********************/
// TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);//使能TIM2的主从模式
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1); //TIM2为上升沿为触发时钟
TIM_SelectInputTrigger(TIM2,TIM_TS_ITR2); //内部触发,从TIM3触发
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_Cmd(TIM2,ENABLE);//先不使能能TIM2
//TIM3工作在从模式的门控模式下的PWM输出模式
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //没有时钟分割
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStruct.TIM_Prescaler = 719; //预分频值,1MHz
TIM_TimeBaseInitStruct.TIM_Period = TIM3per; //重装值
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_Pulse = TIM3Compare1;
TIM_OC1Init(TIM3,&TIM_OCInitStruct);
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); //使能TIM3的主从模式
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC1Ref); //输出内部触发
// TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/*使能定时器TIM3主输出*/
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd(TIM2,ENABLE); //使能TIM2
TIM_Cmd(TIM3,ENABLE); //使能TIM3
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) //检查溢出信号
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除溢出标志
TIM_Cmd(TIM3,DISABLE);
}
}
复制代码
实现功能:利用定时器主从模式,通过定时器TIM3 内部输出TIM_TRGOSource_OC1Ref时钟触发TIM2计数,TIM2计数中断关闭TIM3 ,实现TIM3输出PWM脉冲数可控
遇到的问题:TIM3 与 TIM2 开启不同步,TIM3先开启工作一段时间后才输出脉冲触发TIM2计数,原因是什么?求解答 |
|