logo elektroda
logo elektroda
X
logo elektroda

Code discussion for power measurement system using interrupts in Arduino

cdtdsilva 309 0
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20651495
    cdtdsilva
    Level 10  
    Here is the code if anyone would like to play with a similar system.

    
    long Htime;               //integer for storing high time
    long Ltime;               //integer for storing low time
    long Ttime;               //integer for storing total time of a cycle
    long frequency;           //storing frequency
    
    ISR(TIMER1_OVF_vect) // interrupt service routine
    {
      TCNT1 = 3035;               // Interrupt runs once a second @ 16MHz
      Htime = pulseIn(8, HIGH);   // read high time
      Ltime = pulseIn(8, LOW);    // read low time
      Ttime = (Htime + Ltime);
      if (Ttime > 300)            // Limit maximun power @ 10KW
      {
        frequency = 3000000 / Ttime;
      }
      else
      {
        frequency = 0;
      }
      Ttime = 0;
    
      // Power is flowing out. Add (-) and turn off consumptiopn LED
      if (PINC & (1 << PC1))      
      {
        Serial.print("-");
        PORTC &= ~(1 << PORTC0);  // low
      }
      else
      {
        PORTC = (1 << PC0);       //high
      }
      Serial.println(frequency);
    }
    
    void setup()
    {
      Serial.begin(9600);
      DDRC &= ~(1 << PC1);        // INPUT for power direction (A1)
      DDRC = (1 << PC0);          // OUTPUT light (power consumption) (A0)
      TCCR1A = 0;
      TCCR1B = 0;
    
      TCCR1B |= (1 << CS12); // 256 prescaler
      TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
    }
    
    void loop()
    {
    
    }
    
    
  • ADVERTISEMENT
ADVERTISEMENT