xalexchen 发表于 2009-8-23 16:39:05

[求助]请教傻孩子AVR的PWM与ICP共用问题

我们老师说AVR的(比如M128)的Timer1 同时使用PWM输出和ICP捕获。我用信号发生器测试。不打开PWM时,测出来的数据是正确的。
同时打开就有一段是正确,有一段又不正确。我使用时钟8分频,晶振7.3728Mhz。大概就1us TCNT1增加一次。

ICP捕捉代码

#pragma vector = TIMER1_CAPT_vect
__interrupt void Timer1_ICP(void)
{
    T1_ICP.Icr1L_Value=ICR1L;
    T1_ICP.Icr1H_Value=ICR1H;
   
    /* calculate Now Value */
    T1_ICP.Cnt_New=T1_ICP.Icr1L_Value+(256*T1_ICP.Icr1H_Value);
    /* compare Value */
    if(T1_ICP.Cnt_New>=T1_ICP.Cnt_Old)                           
    {                                                            
         T1_ICP.Cnt_Delta=T1_ICP.Cnt_New-T1_ICP.Cnt_Old;
    }
    else               
    {
         T1_ICP.Cnt_Delta=0xffff-T1_ICP.Cnt_Old+T1_ICP.Cnt_New;
    }
   
    T1_ICP.Cnt_Old=T1_ICP.Cnt_New;
   
    /* calculate Period using fclk/8 */
    T1_ICP.Period=(uint16)(T1_ICP.Cnt_Delta*1.085);
   
    T1_ICP.Counter++;
   
    if(T1_ICP.Counter%2000==0)
    {      
          T1_ICP.Counter = 0;
          T1_ICP.sendflag = 1;   //置位用于显示      
    }
}

我绝对PWM和ICP可以共用,但是就是不知道那里可能配置出错。

xalexchen 发表于 2009-8-23 16:46:04

http://cache.amobbs.com/bbs_upload782111/files_17/ourdev_473925.jpg
(原文件名:QQ截图未命名.jpg)

http://cache.amobbs.com/bbs_upload782111/files_17/ourdev_473923.jpg
(原文件名:未命名3.jpg)



串口输出值部分有效,也就是163微秒周期。我用示波器看着的。方波50%占空比
页: [1]
查看完整版本: [求助]请教傻孩子AVR的PWM与ICP共用问题