Loading ...
Sorry, an error occurred while loading the content.

measuring an un known signal frequency by avr atmega8

Expand Messages
  • danial_1000
    hi, I actually wana read an ac signal frequency by using the icp pin of atmega8 but i having the following problemes . 1. how we calculate time stamps by
    Message 1 of 1 , Jul 7, 2010
    • 0 Attachment
      hi,

      I actually wana read an ac signal frequency by using the icp pin of atmega8 but i having the following problemes .

      1. how we calculate time stamps by using the ICR1 register,when assign timer input capture interrupt for the rising edge we do "rising=ICR1"so at that time how we know that what should be the value of ICR1.plz some one explain using simple language for example i
      say i start walking from my hostal at 0930hrs and reached cafeteria at 1000hrs so i actually took 30 minutes to reach there.here how i say the time stamps because here we are we dealing with clock pulses.

      2. we do the math here and change the interrupt priority,

      ISR(TIMER1_CAPT_vect){

      /*This subroutine checks was it start of pulse (rising edge)

      or was it end (fallingedge)and performs required operations*/

      if (ICP) //if high level

      {

      //save start time

      rising=ICR1;/*******************plz explain************************/

      //set to trigger on falling edge

      TCCR1B=TCCR1B&0xBF;

      //reset overflow counter

      ov_counter=0;

      }

      else

      {

      //save falling time

      falling=ICR1;

      //rising edge triggers next

      TCCR1B=TCCR1B|0x40;

      counts=(uint32_t)falling-(uint32_t)rising+(uint32_t)ov_counter*65535;

      /***************is this math calculating the total number of counts the one complete cyle been taken?*******************/

      }

      }

      The complete code is here.if i apply 50 hz single to the icp then the time period should be 20 millisecond but i m getting the garabage result.the clock of the cpu is 1MHZ.



      #include <avr\io.h>

      #include <avr\interrupt.h>

      #include <avr\iom8.h>

      #define ICP PINB0

      //define ovrflow counter

      uint16_t ov_counter;

      //define times for start and end of signal

      uint16_t rising, falling;

      //define overall counts

      uint32_t counts;

      uint32_t millisecond;

      //overflow counter interrupts service routine

      ISR(TIMER1_OVF_vect){

      ov_counter++;

      }

      //Timer1 capture interrupt service subroutine

      ISR(TIMER1_CAPT_vect){

      /*This subroutine checks was it start of pulse (rising edge)

      or was it end (fallingedge)and performs required operations*/

      if (ICP) //if high level

      {

      //save start time

      rising=ICR1;

      //set to trigger on falling edge

      TCCR1B=TCCR1B&0xBF;

      //reset overflow counter

      ov_counter=0;

      }

      else

      {

      //save falling time

      falling=ICR1;

      //rising edge triggers next

      TCCR1B=TCCR1B|0x40;

      counts=(uint32_t)falling-(uint32_t)rising+(uint32_t)ov_counter*65535;

      millisecond=counts/1000;//conversion from counts to milliseconds
      //actually the counts are the pulses in microseconds so to convert it to
      //to millisecond i divide it by 1000.


      }

      }

      int main(void)

      {

      DDRD=0XFF;

      //enable overflow and input capture interrupts

      TIMSK=0x24;

      /*Noise canceller, without prescaler, rising edge*/

      TCCR1B=0xC1;

      sei();

      while(1)

      {

      if( millisecond>18 && millisecond<21)

      {

      PORTD=0b10101010;

      {

      else

      {



      PORTD=0;

      }



      }

      }
    Your message has been successfully submitted and would be delivered to recipients shortly.