Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. volatile int i;
  2. volatile int value[20];
  3. volatile bool ready = false;
  4.  
  5. ISR(TIMER0_COMPA_vect) {
  6.     //interrupt commands for TIMER 0 here
  7.     if(!ready){
  8.         value[i] = analogRead(A0);
  9.         i++;
  10.     }
  11.  
  12.     if(i==20){
  13.         ready = true;
  14.         i = 0;
  15.     }
  16. }
  17.  
  18. void setup(){
  19.     // TIMER 0 for interrupt frequency 1000 Hz:
  20.     cli();       // stop interrupts
  21.     TCCR0A = 0;  // set entire TCCR0A register to 0
  22.     TCCR0B = 0;  // same for TCCR0B
  23.     TCNT0 = 0;   // initialize counter value to 0
  24.     // set compare match register for 1000 Hz increments
  25.     OCR0A = 249;  // = 16000000 / (64 * 1000) - 1 (must be <256)
  26.     // turn on CTC mode
  27.     TCCR0B |= (1 << WGM01);
  28.     // Set CS02, CS01 and CS00 bits for 64 prescaler
  29.     TCCR0B |= (0 << CS02) | (1 << CS01) | (1 << CS00);
  30.     // enable timer compare interrupt
  31.     TIMSK0 |= (1 << OCIE0A);
  32.     sei();  // allow interrupts
  33.  
  34.     Serial.begin(9600);
  35. }
  36.  
  37. void loop(){
  38.  
  39.     if(ready){
  40.         noInterrupts();
  41.         int total = 0;
  42.         for (int i = 0; i < 20; i++){
  43.             total += value[i];
  44.         }
  45.         total = total * 5 / 1023;
  46.         total = total / 20;
  47.  
  48.         Serial.println("Vrms = " + String(total));
  49.         ready = false;
  50.         interrupts();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement