打印
[STM32F1]

定时器1 PWM输出为什么会存在延迟

[复制链接]
1170|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
grace75|  楼主 | 2014-8-31 18:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
PC13是发光二极管

在STM32F103C8T6上运行的时候,C13的电平变化和PWM输出不是同时的 总是存在延迟
用其他通用定时器测试过就没有类似的问题
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);        
        //²»·ÖƵ¡£PWMƵÂÊ=72000000/900=80Khz
        /* ---------------------------------------------------------------
        TIM3CLK ¼´PCLK1=36MHz
        TIM3 Configuration: generate 1 PWM signals :
    TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
    TIM3 ARR Register = 900 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Frequency = 36 KHz.
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100
        TIM3CLK = 36 MHz, Prescaler = 0, TIM3 counter clock = 36MHz
        --------------------------------------------------------------- */
        /* Time base configuration */
        TIM_TimeBaseStructure.TIM_Period = 20000-1; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ         50Hz
        TIM_TimeBaseStructure.TIM_Prescaler =72-1; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ  ²»·ÖƵ
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
        
        /* Output Compare Active Mode configuration: Channel1 */
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //±È½ÏÊä³öʹÄÜ
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0; //ÉèÖôý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;     //??????  
         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;     //??????  

        TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //ʹÄÜTIMxÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
        TIM_OC2Init(TIM1, &TIM_OCInitStructure);  //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
        TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);  //ʹÄÜTIMxÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
        TIM_OC3Init(TIM1, &TIM_OCInitStructure);  //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
        TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);  //ʹÄÜTIMxÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
        
        
        TIM_ARRPreloadConfig(TIM1, ENABLE); //ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæÆ÷
        TIM_CtrlPWMOutputs(TIM1,ENABLE);
        /* TIM3 enable counter */
        TIM_Cmd(TIM1, ENABLE);  //ʹÄÜTIMxÍâÉè
int main(void)
{
  //u16 k=0;
        RCC_Configuration();
  GPIO_Configuration();
  PWM_Configuration();         
  delay_init(72);
        //USART2_Config();           //³õʼ»¯ ÅäÖÃUSART2 £¨ÓÃÓÚ½ÓÊÕMPU6050Êý¾Ý£©         
        //nRF24L01_Configuration();
        
        
#ifdef OLED
        OLED_Init();
#endif
        /*   Æô¶¯Ç°ÏµÍ³²âÊÔ     */               
  while(1)
        {
        delay_ms(1000);
        
        GPIO_WriteBit(GPIOC, GPIO_Pin_13, 1);
        TIM_SetCompare3(TIM1,1500);
        
        delay_ms(1000);
        
        
        GPIO_WriteBit(GPIOC, GPIO_Pin_13, 0);
        TIM_SetCompare3(TIM1,2800);

        }
}


发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

68

帖子

0

粉丝