研究生
最后登录1970-1-1
在线时间 小时
注册时间2013-12-12
|
本帖最后由 thy110 于 2014-7-2 23:01 编辑
[code=c]
[/code]- /*
- * º¯ÊýÃû£ºTIM3_Mode_Config
- * ÃèÊö £ºÅäÖÃTIM3Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
- * ÊäÈë £ºÎÞ
- * Êä³ö £ºÎÞ
- * µ÷Óà £ºÄÚ²¿µ÷ÓÃ
- */
- static void TIM3_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* PWMÐźŵçƽÌø±äÖµ */
- u16 CCR1_Val = 500;
- u16 CCR2_Val = 375;
- u16 CCR3_Val = 250;
- u16 CCR4_Val = 125;
- /* -----------------------------------------------------------------------
- TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
- TIM3CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
- TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
- TIM3 Frequency = 72 KHz.
- TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
- TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
- TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
- TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
- ----------------------------------------------------------------------- */
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 999; //µ±¶¨Ê±Æ÷´Ó0¼ÆÊýµ½999£¬¼´Îª1000´Î£¬ÎªÒ»¸ö¶¨Ê±ÖÜÆÚ
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖÃÔ¤·ÖƵ£º²»Ô¤·ÖƵ£¬¼´Îª72MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃΪPWMģʽ1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //ÉèÖÃͨµÀ2µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC2Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ2
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //ÉèÖÃͨµÀ3µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC3Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ3
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel4 */
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //ÉèÖÃͨµÀ4µÄµçƽÌø±äÖµ£¬Êä³öÁíÍâÒ»¸öÕ¼¿Õ±ÈµÄPWM
- TIM_OC4Init(TIM3, &TIM_OCInitStructure); //ʹÄÜͨµÀ4
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM3, ENABLE); // ʹÄÜTIM3ÖØÔؼĴæÆ÷ARR
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷3
- }
复制代码 |
|