linux控制pwm输出个数,STM32F407 精确控制PWM 脉冲 输出个数

  • Post author:
  • Post category:linux


本帖最后由 pilihuo81 于 2012-12-1 11:00 编辑

说明:

使用定时器TIM2 控制定时器TIM1 1kHz 精确控制输出脉冲个数

通过TIM1 输出占空比调整脉冲个数 本人控制是 1—9999个

//******************************************************************************

TIM_DeInit(TIM2); //时钟为:84MHz

/* TIM2 Base configuration */

TIM_TimeBaseStructure.TIM_Period=20000-1;                 //

TIM_TimeBaseStructure.TIM_Prescaler=42000-1;

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//TIM_CKD_DIV1; //采样分频

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ARRPreloadConfig(TIM2, DISABLE);//禁止ARR预装载缓冲器

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;                         //

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//Disable;//Enable;

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;

TIM_OCInitStructure.TIM_Pulse =20000-(welding_time*2);//0;//TimerPeriod/2;//0;//Channel1Pulse;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//High;//Low;

TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;//;//Set;

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref); //选择触发输出为OC1

/* TIM Interrupts enable */

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//TIM_IT_CC1,ENABLE);

TIM_Cmd(TIM2, ENABLE);        //开启时钟

/****TIM1  For PWM output *********************************/

TIM_DeInit(TIM1); //时钟为:168MHz

/* TIM1 Base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 42-1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;

TIM_TimeBaseStructure.TIM_Period = 2000-1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

/* Channel_1 Configuration in PWM mode */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;

TIM_OCInitStructure.TIM_Pulse = 20*(100-duty_cycle);

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//High;//  Low;

TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);

TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active);

TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Disable);

/* Channel_2 Configuration in PWM mode */

//TIM_OCInitStructure.TIM_Pulse = 20*(duty_cycle);

//TIM_OC2Init(TIM1, &TIM_OCInitStructure);

/* Channel_3 Configuration in PWM mode */

TIM_OCInitStructure.TIM_Pulse = 20*(duty_cycle);

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//High;

TIM_OC3Init(TIM1, &TIM_OCInitStructure);

TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive);

TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Disable);

TIM1->CCMR1=0x60;

TIM1->CCMR2=0x60;

/TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Enable);

/TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Enable);

TIM_ITRxExternalClockConfig(TIM1, TIM_TS_ITR1);//外部时钟模式1,触发源为TIMER2

TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Gated);//配置为门控模式,时钟自动切换为内部时钟

/* TIM1 counter enable */

TIM_Cmd(TIM1, ENABLE);

/* Main Output Enable */

TIM_CtrlPWMOutputs(TIM1, DISABLE);

//************************************************************************

//******************************************************************************

TIM_DeInit(TIM7); //时钟为:84MHz

/* TIM7 Time base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 82340;//42000;//82340;//42000-1;//82340;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period =5-1;;// 25-1;//2000-1;//5-1;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_RepetitionCounter  =0;

TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);

TIM_UpdateRequestConfig(TIM7,TIM_UpdateSource_Regular);

/* Clear TIM7 Updata_Interrupts Bit */

TIM_ClearITPendingBit(TIM7, TIM_IT_Update);

/* TIM7 Updata_Interrupts enable */

//TIM_ITConfig(TIM7,TIM_IT_Update, ENABLE);

TIM_ITConfig(TIM7,TIM_IT_Update, ENABLE);

/* TIM7 enable counter */

TIM_Cmd(TIM7, ENABLE);

使能控制

void EXTI0_IRQHandler(void)

{

TIM_Cmd(TIM7, ENABLE);

//tim7delay_ms(50);

///TIM_ITConfig(TIM7,TIM_IT_Update, ENABLE);

if(EXTI_GetITStatus(EXTI_Line0) != RESET)

{

/* Clear the EXTI line 0 pending bit */

//TIM_Cmd(TIM7, ENABLE);

//EXTI_ClearITPendingBit(EXTI_Line0);

tim7delay_ms(30);

if(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0))

{

if(weld_Enable==1)

{

TIM1->CCMR1=0x60;

TIM1->CCMR2=0x60;

TIM1->CNT=20*(100-duty_cycle)-100;//100;//1800;      20*(100-duty_cycle)-100

TIM2->CNT=19999-(welding_time)*2;

//TIM_Cmd(TIM3, ENABLE);        //开启时钟

//TIM_Cmd(TIM2, ENABLE);        //开启时钟

TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Enable);

TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Enable);

/* Main Output Enable */

TIM_CtrlPWMOutputs(TIM1, ENABLE);

TIM_CCxCmd(TIM2,TIM_Channel_1,TIM_CCx_Enable);

//TIM_ITConfig(TIM7,TIM_IT_Update, ENABLE);

TIM_Cmd(TIM7, ENABLE);

if(welding_time==9999)

{

welding_time7=welding_time-2;

}

else if(welding_time<9999)

{

welding_time7=welding_time;

}

tim7delay_ms(welding_time7);

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

weld_Enable=0;

if(flag1==0)

{

GPIO_SetBits(GPIOD, GPIO_Pin_12);

GPIO_ResetBits(GPIOD, GPIO_Pin_13);

}

else

{

GPIO_ResetBits(GPIOD, GPIO_Pin_12);

GPIO_SetBits(GPIOD, GPIO_Pin_13);

}

if(flag1==0)

{

flag1=1;

}

else if(flag1==1)

{

flag1=0;

}

}

}

/* Clear the EXTI line 0 pending bit */

EXTI_ClearITPendingBit(EXTI_Line0);

}

}

结束控制

void TIM2_IRQHandler(void)

{

if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

{

TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE);

/* Main Output Disable */

TIM_CtrlPWMOutputs(TIM1, DISABLE);

TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active);

TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive);

TIM_CCxCmd(TIM1,TIM_Channel_1,TIM_CCx_Disable);

TIM_CCxCmd(TIM1,TIM_Channel_3,TIM_CCx_Disable);

TIM_CCxCmd(TIM2,TIM_Channel_1,TIM_CCx_Disable);

TIM_ClearITPendingBit(TIM2,TIM_IT_Update);

weld_Enable=1;

}

}