初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-3-17
|
楼主 |
发表于 2016-3-31 10:46:51
|
显示全部楼层
这个是定时器配置
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* éèÖÃTIM3CLK ê±ÖóÎa72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê1ÄüTIM3ê±Öó
/* »ù±¾¶¨ê±Æ÷ÅäÖà */
TIM_TimeBaseStructure.TIM_Period = 255; //챶¨ê±Æ÷′ó0¼Æêyμ½255£¬¼′Îa266′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructure.TIM_Prescaler = 1999; //éèÖÃÔ¤·ÖÆμ£o
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWMÄ£ê½ÅäÖà */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃÎaPWMÄ£ê½1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1Äüêä3ö
TIM_OCInitStructure.TIM_Pulse = 0; //éèÖÃ3õê¼WMÂö3å¿í¶èÎa0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îaμíμçƽ
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüԤװÔØ
TIM_ARRPreloadConfig(TIM3, ENABLE); //ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE); //ê1ÄüupdateÖD¶Ï
NVIC_Config_PWM(); //ÅäÖÃÖD¶ÏóÅÏ輶
}
呼吸灯中断服务函数
void TIM3_IRQHandler(void)
{
static uint8_t pwm_index = 0; //óÃóúPWM2é±í
static uint8_t period_cnt = 0; //óÃóú¼ÆËãÖüÆúêy
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //TIM_IT_Update
{
period_cnt++;
if(period_cnt >= 10) //èôêä3öμÄÖüÆúêy′óóú10£¬êä3öÏÂò»ÖÖÂö3å¿íμÄWM2¨
{
TIM3->CCR3 = indexWave[pwm_index]; //¸ù¾YPWM±íDT¸Ä¶¨ê±Æ÷μıè½Ï¼Ä′æÆ÷Öμ
pwm_index++; //±êÖ¾WM±íμÄÏÂò»¸öÔaËØ
if( pwm_index >= 40) //èôWMÂö3å±íòѾ-êä3öíê3éò»±é£¬ÖØÖÃWM2é±í±êÖ¾
{
pwm_index=0;
}
period_cnt=0; //ÖØÖÃÖüÆú¼Æêy±êÖ¾
}
TIM_ClearITPendingBit (TIM3, TIM_IT_Update); //±ØDëòaÇå3yÖD¶Ï±ê־λ
}
}
|
|