大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-7-12
|
发表于 2016-8-1 09:13:35
|
显示全部楼层
- #include "TIM1_pwm_output.h"
- void TIM1_GPIO_Config(void) //Advanced TIM
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE); //ê1Äü¶¨ê±Æ÷1μÄê±Öó
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE); //ê1Äü¶¨ê±Æ÷
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüPA¶Ë¿úê±Öó
-
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1 //óë·äÃùÆ÷PA812óÃ,ÔYê±Æá±Î
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //TIM_CH2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //TIM_CH3
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //TIM_CH4
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //TIM_CH1N
- 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_14; //TIM_CH2N
- 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_15; //TIM_CH3N
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- }
- static void TIM1_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
-
- u16 CCR1_Val1 = 500;
- u16 CCR2_Val1 = 375;
- u16 CCR3_Val1 = 250;
- u16 CCR4_Val1 = 125;
-
- TIM_TimeBaseStructure.TIM_Period = 999; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ 80K
- TIM_TimeBaseStructure.TIM_Prescaler =0; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 2»·ÖÆμ
- TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val1; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //ê1ÄüTIMxÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val1; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC2Init(TIM1, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //ê1ÄüTIMxÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val1; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC3Init(TIM1, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //ê1ÄüTIMxÔúCCR3éÏμÄԤװÔؼÄ′æÆ÷
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val1; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC4Init(TIM1, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //ê1ÄüTIM1ÔúCCR4éÏμÄԤװÔؼÄ′æÆ÷
-
- TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
- TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
- TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
- /* assert_param(IS_TIM_OSSR_STATE(TIM_BDTRInitStruct->TIM_OSSRState));
- assert_param(IS_TIM_OSSI_STATE(TIM_BDTRInitStruct->TIM_OSSIState));
- assert_param(IS_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->TIM_LOCKLevel));
- assert_param(IS_TIM_BREAK_STATE(TIM_BDTRInitStruct->TIM_Break));
- assert_param(IS_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->TIM_BreakPolarity));
- assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->TIM_AutomaticOutput));
- */
- TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
- TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIMxíaéè
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwmêä3öê1Äü£¬ò»¶¨òa¼ÇμÃ′ò¿a
- }
- void TIM1_PWM_Init(void)
- {
- TIM1_GPIO_Config();
- TIM1_Mode_Config ();
- }
复制代码
|
|