Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- volatile int i;
- volatile int value[20];
- volatile bool ready = false;
- ISR(TIMER0_COMPA_vect) {
- //interrupt commands for TIMER 0 here
- if(!ready){
- value[i] = analogRead(A0);
- i++;
- }
- if(i==20){
- ready = true;
- i = 0;
- }
- }
- void setup(){
- // TIMER 0 for interrupt frequency 1000 Hz:
- cli(); // stop interrupts
- TCCR0A = 0; // set entire TCCR0A register to 0
- TCCR0B = 0; // same for TCCR0B
- TCNT0 = 0; // initialize counter value to 0
- // set compare match register for 1000 Hz increments
- OCR0A = 249; // = 16000000 / (64 * 1000) - 1 (must be <256)
- // turn on CTC mode
- TCCR0B |= (1 << WGM01);
- // Set CS02, CS01 and CS00 bits for 64 prescaler
- TCCR0B |= (0 << CS02) | (1 << CS01) | (1 << CS00);
- // enable timer compare interrupt
- TIMSK0 |= (1 << OCIE0A);
- sei(); // allow interrupts
- Serial.begin(9600);
- }
- void loop(){
- if(ready){
- noInterrupts();
- int total = 0;
- for (int i = 0; i < 20; i++){
- total += value[i];
- }
- total = total * 5 / 1023;
- total = total / 20;
- Serial.println("Vrms = " + String(total));
- ready = false;
- interrupts();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement