研究生
最后登录1970-1-1
在线时间 小时
注册时间2014-4-17
|
/** ****************************************************************************** * @file bsp_pwm_output.c * @author STMicroelectronics * @version V1.0 * @date 2013-xx-xx * @brief tim3êä3öËÄ·pwm bsp ****************************************************************************** * @attention * * êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å * ÂÛì3 :http://www.firebbs.cn * ìÔ±| :http://firestm32.taobao.com * ****************************************************************************** */ #include "bsp_breathing.h"
/* LEDáá¶èμ輶 PWM±í */uint8_t indexWave[] = {1,1,2,2,3,4,6,8,10,14,19,25,33,44,59,80, 107,143,191,255,255,191,143,107,80,59,44,33,25,19,14,10,8,6,4,3,2,2,1,1};
/** * @brief ÅäÖÃTIM3¸′óÃêä3öWMê±óÃμ½μÄI/O * @param ÎT * @retval ÎT */static void TIM3_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOB clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* ÅäÖÃoôÎüμÆóÃμ½μÄB0òy½Å */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure);}
/** * @brief ÅäÖÃǶì×Ïòá¿ÖD¶Ï¿ØÖÆÆ÷NVIC * @param ÎT * @retval ÎT */static void NVIC_Config_PWM(void){ NVIC_InitTypeDef NVIC_InitStructure; /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* ÅäÖÃTIM3_IRQÖD¶ÏÎaÖD¶ÏÔ′ */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}
/** * @brief ÅäÖÃTIM3êä3öμÄWMDÅoÅμÄģ꽣¬èçÖüÆú¡¢¼«DÔ * @param ÎT * @retval ÎT */
/* * TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> ÖD¶Ï & TIMxCNT ÖØD¼Æêy * TIMx_CCR(μçƽ·¢éú±ä»ˉ) * * DÅoÅÖüÆú=(TIMx_ARR +1 ) * ê±ÖóÖüÆú * */
/* _______ ______ _____ ____ ___ __ _ * |_| |__| |___| |____| |_____| |______| |_______| |________| */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_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1Äüêä3ö TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC2Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüԤװÔØ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1Äüêä3ö TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC1Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //ê1ÄüԤװÔØ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ê1Äüêä3ö TIM_OCInitStructure.TIM_Pulse = 0; TIM_OC4Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3 TIM_OC4PreloadConfig(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¶ÏóÅÏ輶 }
/** * @brief TIM3 oôÎüμÆ3õê¼»ˉ * ÅäÖÃWMÄ£ê½oíGPIO * @param ÎT * @retval ÎT */void TIM3_Breathing_Init(void){ TIM3_GPIO_Config(); TIM3_Mode_Config(); }
/*********************************************END OF FILE**********************/
中断服务程序
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¸Ä¶¨ê±Æ÷μıè½Ï¼Ä′æÆ÷Öμ
TIM3->CCR1 = indexWave[pwm_index]; //¸ù¾YPWM±íDT¸Ä¶¨ê±Æ÷μıè½Ï¼Ä′æÆ÷Öμ
TIM3->CCR2 = indexWave[pwm_index]; //¸ù¾YPWM±íDT¸Ä¶¨ê±Æ÷μıè½Ï¼Ä′æÆ÷Öμ
TIM3->CCR4 = 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¶Ï±ê־λ
}
|
|