高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-11-29
|
楼主 |
发表于 2015-1-16 18:17:31
|
显示全部楼层
- static void TIM3_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // /* éèÖÃTIM3CLK Îa 72MHZ */
- // RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- // GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
- /* GPIOA and GPIOB clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO|RCC_APB2Periph_TIM1, ENABLE);
- // GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
- GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);
-
- /*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11|GPIO_Pin_13 | GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- // /*GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- // GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- /**
- * @brief ÅäÖÃTIM3êä3öμÄPWMDÅoÅμÄģ꽣¬èçÖüÆú¡¢¼«DÔ¡¢Õ¼¿Õ±è
- * @param ÎT
- * @retval ÎT
- */
- /*
- * TIMxCLK/CK_PSC --> TIMxCNT --> TIMx_ARR --> TIMxCNT ÖØD¼Æêy
- * TIMx_CCR(μçƽ·¢éú±ä»ˉ)
- * DÅoÅÖüÆú=(TIMx_ARR +1 ) * ê±ÖóÖüÆú
- * Õ¼¿Õ±è=TIMx_CCR/(TIMx_ARR +1)
- */
- static void TIM3_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* PWMDÅoÅμçƽìø±äÖμ */
- u16 CCR1_Val = 500;
- u16 CCR2_Val = 375;
- u16 CCR3_Val = 250;
- u16 CCR4_Val = 125;
- /* -----------------------------------------------------------------------
- TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR+1)* 100% = 50%
- TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR+1)* 100% = 37.5%
- TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR+1)* 100% = 25%
- TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR+1)* 100% = 12.5%
- ----------------------------------------------------------------------- */
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 999; //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃÎaPWMÄ£ê½1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçƽ·¢éúìø±ä
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
- TIM_OC1Init(TIM1, &TIM_OCInitStructure); //ê1Äüí¨μà1
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //éèÖÃí¨μà2μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄPWM
- TIM_OC2Init(TIM1, &TIM_OCInitStructure); //ê1Äüí¨μà2
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄPWM
- TIM_OC3Init(TIM1, &TIM_OCInitStructure); //ê1Äüí¨μà3
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel4 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄPWM
- TIM_OC4Init(TIM1, &TIM_OCInitStructure); //ê1Äüí¨μà4
- TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM1, ENABLE); // ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR
- //GPIO_PinConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
- /* TIM3 enable counter */
- TIM_Cmd(TIM1, ENABLE); //ê1Äü¶¨ê±Æ÷3
- }
- /**
- * @brief TIM3 êä3öPWMDÅoÅ3õê¼»ˉ£¬Ö»òaμ÷óÃÕa¸öoˉêy
- * TIM3μÄËĸöí¨μà¾í»áóDPWMDÅoÅêä3ö
- * @param ÎT
- * @retval ÎT
- */
- void TIM3_PWM_Init(void)
- {
- TIM3_GPIO_Config();
- TIM3_Mode_Config();
- }
复制代码 |
|