#include "queue.h" boolean disableRx = true; const int rxPinA = 7; // RX, Connected to pin 7 //******************************************************************** // SETUP //******************************************************************** void setup() { disableRx = true; pinMode(rxPinA, INPUT); Serial.begin(9600); // USB Serial rcvInitQueue(); cli(); // Clear interrupt setup_timer_interrupt(); sei(); // Enable interrupt disableRx = false; } //******************************************************************** // INTERRUPT FUNCTIONS //******************************************************************** void setup_timer_interrupt() { //set timer1 interrupt at 20Hz TCCR1A = 0;// set entire TCCR1A register to 0 TCCR1B = 0;// same for TCCR1B TCNT1 = 0;//initialize counter value to 0 // set compare match register for 1hz increments // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts OCR1A = 40; // turn on CTC mode TCCR1B |= (1 << WGM12); // Set CS12:CS10 to 010 = /8 prescaler TCCR1B |= (1 << CS11); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); } // 20uS timer interrupt function ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) { static unsigned char currValue = 0; static unsigned short samples = 0; static unsigned short newSamples = 0; // Sample the pin value unsigned char value = digitalRead(rxPinA); if( value == currValue ) { samples++; samples+=newSamples; newSamples=0; } else { newSamples++; } if( newSamples == 3 ) { uint16_t queSamples = (uint16_t)((samples*20 > 60000)? 60000 : samples*20); rcvEnQueue(queSamples); // Max 46000 samples = newSamples; newSamples = 0; currValue = value; } } void stopInterrupts() { EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register } //******************************************************************** // LOOP // //******************************************************************** #define SER_RX_MODE_WAIT 0 #define SER_RX_MODE_RCV 1 #define SER_RX_MODE_END 2 extern unsigned long x_data; // Receive data from Nexa extern unsigned long temp1_x_data; // Receive data from temp1 void loop() { static boolean firstRun = true; static uint16_t width; unsigned long now = millis(); // Get the current time // Display version information 10 sec after startup if( firstRun == true && now > 30000 ) { Serial.println("433MHz Temp sensor receiver (Sparkfun Pro Micro) Ver: 180204 09:35"); firstRun = false; } if( rcvDeQueue(&width) ) { } }