【问题标题】:measuring a generated wave frequency using ICU - Atmega32使用 ICU - Atmega32 测量生成的波频率
【发布时间】:2020-07-16 02:00:17
【问题描述】:

我正在尝试以下列方式使用 ATMega32 输入捕捉单元:

我有一个使用 TIMER0(通过 OC0 引脚)生成的波(XTAL=8MHz,预分频器 =1024,CTC 模式,OCR0=244,给出 ~32Hz)。然后我尝试使用 TIMER1(通过 ICP 引脚)捕获此波(XTAL=8MHz,预分频器=1024,正常模式,产生 ~7812.5 Hz)

代码 sn-p 提供了更多详细信息。

我的结果只有 0 的问题。

**

已修改。

** 非常感谢@clifford, 根据下面的代码,它可以工作,但是:

1- 有一点错误(结果不准确)。

2- 当我改变我的方程式时

来自

        count=(capture2+(captOVf*65536))-capture1;

           to   
   count=capture2-capture1; 

我的 LCD 上显示垃圾。

非常抱歉问题格式不好。

typedef enum{
    CAPTURE1,
    CAPTURE2,
    WAIT
}timer_states_t;

volatile timer_states_t flag= WAIT;
volatile u8 x,y,captOVf,TOVfs=0;
volatile u16 capture1,capture2;

void setup_pins(){

    SET_BIT(DDRB,3); //SET OC0 pin as output
    CLEAR_BIT(DDRD,6); //set ICP pin as input
    SET_BIT(PORTD,6);  //set ICP pin pullup resistance

}

//Timer0 Configurations / wave generator .
void timer0_init(){
    OCR0=244;
    TCCR0=0x1D; //ctc , 1024 , toggle OC0 pin on compare match ;
}

//Timer1 Configurations / capturer .
void timer1_init(){
    TCCR1A=0x00; // Timer1 , normal mode, no on compare output ,
    TCCR1B=0x45; // prescaler 1024, Input capture edge as rising .
    SET_BIT(TIMSK,TICIE1); // enable input capture interrupt .
    SET_BIT(TIMSK,TOIE1); //enable timer1 overflow interrupt .
    CLEAR_BIT(ACSR,ACIC);  //disable Analog comparator .
    SET_BIT(SREG,7); //GLOOBAL ITERRUPT

}

ISR(TIMER1_OVF_vect){
    TOVfs++;

}

ISR(TIMER1_CAPT_vect){
    switch(flag){
    case CAPTURE1:
        //capture1=ICR1;
        x=ICR1L;
        y=ICR1H;
        capture1=x | (y<<8);
        TOVfs=0;
        flag=CAPTURE2;
        break;

    case CAPTURE2:
        //capture2=ICR1;
        x=ICR1L;
        y=ICR1H;
        capture2=x | (y<<8);
        flag=WAIT;
        captOVf=TOVfs;
        break;
    }
}
int main(){

setup_pins();
timer0_init();
timer1_init();
LCD_init();

unsigned long count  ;
u8* countString ;

while(1){
    flag=CAPTURE1;

    while(flag != WAIT);

        count=(capture2+(captOVf*65536))-capture1;
        itoa(count,countString,10);
        LCD_send_string("count:");
        LCD_send_string(countString);
        _delay_ms(500);
        LCD_send_cmd(lcd_Clear);

    }
}

【问题讨论】:

  • 你说结果不准确,没有透露实际结果。你得到了什么,你期望什么,为什么 - 展示你的工作。

标签: timer embedded atmega32


【解决方案1】:

没有任何东西会修改capture1capture2,它们本身就是单元化的。

capture1 = x | (y<<8);
...
capture2 = x | (y<<8);

也许? temp1temp2 似乎毫无用处;在 ISR 中设置,未在其他地方引用。

考虑到 TIMER2 计数对于单个 32Hz 周期约为 244,溢出计数器是否不必要?只需在 CAPTURE1 处重置 TIMER1 计数并在 CAPTURE2 处获取计数,删除算术和 capture 变量之一。

所有共享变量都需要声明volatile,所有非共享变量不需要是全局变量。

【讨论】:

  • 请再次检查修改后的问题。
  • @AliK。现在它不是同一个问题,理想情况下你会发布一个新问题。特别是因为这个答案已被接受。您不应修改问题以包含来自答案的建议,因为它会使现有答案变得毫无意义。 SO 是问答而非讨论论坛。如果这对上下文有用,您可以包含指向此问题的链接。有一个更简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
相关资源
最近更新 更多