浏览代码

Basic 433 Rx is working with interrupts and queue.

Thomas Chef 7 年之前
父节点
当前提交
21908b8380
共有 4 个文件被更改,包括 86 次插入127 次删除
  1. 53 0
      interrupt.ino
  2. 0 18
      queue.h
  3. 3 1
      queue.ino
  4. 30 108
      wireless_temp_sensor_receiver.ino

+ 53 - 0
interrupt.ino

@@ -0,0 +1,53 @@
+
+// This function does low-lovel setups to achieve a call to an interrupts function every 20uS
+void setup_timer_interrupt()
+{
+    //set timer1 interrupt at 10Hz
+    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
+// 48810 int/sec = 20.487605 uS
+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++;
+    }
+
+    // We wait until we have sampled 3 equal states before actually accepting that we have received a new pulse
+    if ( newSamples == 3 ) {
+        uint16_t queSamples = (uint16_t)((samples * 20 > 60000) ? 60000 : samples * 20); // Calc the time in uS
+        rcvEnQueue(queSamples); // Send the received pulse to the queue. Max 46000
+        samples = newSamples;
+        newSamples = 0;
+        currValue = value;
+    }
+}

+ 0 - 18
queue.h

@@ -1,18 +0,0 @@
-#ifndef ____queue__
-#define ____queue__
-
-
-
-#define RX_QUEUE_SIZE 500	// Queue size for pulses
-
-
-unsigned char rcvDeQueue( unsigned int *value );
-unsigned char rcvEnQueue( uint16_t value );
-unsigned char rcvRollbackQueue();
-unsigned int getQueueUsed();
-void  rcvInitQueue(  );
-
-
-
-#endif
-

+ 3 - 1
queue.ino

@@ -1,4 +1,6 @@
-#include "queue.h"
+// This file constructs a FIFO-queue that can be filled with data from one function and drained from somewhere else without data curruption
+
+#define RX_QUEUE_SIZE 500    // Queue size for pulses
 
 static int read_d;				/*!< Read index points to the place in the queue that has already been read */
 static int write_d;				/*!< The write index points to the place where the next write in the queue will take place. */

+ 30 - 108
wireless_temp_sensor_receiver.ino

@@ -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) ) {
-    
-        
-  }
-  
-}
-
-
-
-