|
@@ -1,120 +1,42 @@
|
|
-#include "queue.h"
|
|
|
|
|
|
|
|
-boolean disableRx = true;
|
|
|
|
|
|
+// Define which pins to use for different tasks
|
|
|
|
+const int rxPinA = 7; // 433MHz RX, Connected to pin 7
|
|
|
|
+const int ledPin = 17; // The RX LED has a defined Arduino pin
|
|
|
|
|
|
-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;
|
|
|
|
|
|
+// This code is only run once at startup
|
|
|
|
+void setup()
|
|
|
|
+{
|
|
|
|
+ // Setup mode of each pin that we will use
|
|
|
|
+ pinMode(ledPin, OUTPUT); // Set LED-pin as an output
|
|
|
|
+ pinMode(rxPinA, INPUT);
|
|
|
|
+
|
|
|
|
+ // Call the function that configures the interrupts
|
|
|
|
+ cli(); // Clear interrupt
|
|
|
|
+ setup_timer_interrupt();
|
|
|
|
+ sei(); // Enable interrupt
|
|
|
|
+
|
|
|
|
+ // Setup the different serial communication channels
|
|
|
|
+ Serial.begin(9600); // Serial monitor (Over USB, when debugging with Arduino)
|
|
|
|
+ Serial1.begin(9600); // HW-UART (To Raspberry)
|
|
}
|
|
}
|
|
|
|
|
|
-//********************************************************************
|
|
|
|
-// INTERRUPT FUNCTIONS
|
|
|
|
-//********************************************************************
|
|
|
|
-
|
|
|
|
-void setup_timer_interrupt()
|
|
|
|
|
|
+// This code is called over and oven again
|
|
|
|
+void loop()
|
|
{
|
|
{
|
|
- //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);
|
|
|
|
|
|
+ static uint16_t width; // Stores the length of the received pulse
|
|
|
|
+ static unsigned long totalRx = 0; // The total number of received pulses
|
|
|
|
|
|
- // Set CS12:CS10 to 010 = /8 prescaler
|
|
|
|
- TCCR1B |= (1 << CS11);
|
|
|
|
-
|
|
|
|
- // enable timer compare interrupt
|
|
|
|
- TIMSK1 |= (1 << OCIE1A);
|
|
|
|
-}
|
|
|
|
|
|
+ if ( rcvDeQueue(&width) ) {
|
|
|
|
+
|
|
|
|
+ totalRx++; // Increase the total received pulses
|
|
|
|
|
|
-// 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);
|
|
|
|
|
|
+ // Check if the received pulse is within limits for a start-pulse
|
|
|
|
+ if ( 3400 <= width && width <= 3800 ) {
|
|
|
|
+ Serial.print("Received one ! Total pulses: ");
|
|
|
|
+ Serial.println(totalRx);
|
|
|
|
+ }
|
|
|
|
|
|
- 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) ) {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|