wireless_temp_sensor_receiver.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "queue.h"
  2. boolean disableRx = true;
  3. const int rxPinA = 7; // RX, Connected to pin 7
  4. //********************************************************************
  5. // SETUP
  6. //********************************************************************
  7. void setup() {
  8. disableRx = true;
  9. pinMode(rxPinA, INPUT);
  10. Serial.begin(9600); // USB Serial
  11. rcvInitQueue();
  12. cli(); // Clear interrupt
  13. setup_timer_interrupt();
  14. sei(); // Enable interrupt
  15. disableRx = false;
  16. }
  17. //********************************************************************
  18. // INTERRUPT FUNCTIONS
  19. //********************************************************************
  20. void setup_timer_interrupt()
  21. {
  22. //set timer1 interrupt at 20Hz
  23. TCCR1A = 0;// set entire TCCR1A register to 0
  24. TCCR1B = 0;// same for TCCR1B
  25. TCNT1 = 0;//initialize counter value to 0
  26. // set compare match register for 1hz increments
  27. // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
  28. OCR1A = 40;
  29. // turn on CTC mode
  30. TCCR1B |= (1 << WGM12);
  31. // Set CS12:CS10 to 010 = /8 prescaler
  32. TCCR1B |= (1 << CS11);
  33. // enable timer compare interrupt
  34. TIMSK1 |= (1 << OCIE1A);
  35. }
  36. // 20uS timer interrupt function
  37. ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
  38. static unsigned char currValue = 0;
  39. static unsigned short samples = 0;
  40. static unsigned short newSamples = 0;
  41. // Sample the pin value
  42. unsigned char value = digitalRead(rxPinA);
  43. if( value == currValue ) {
  44. samples++;
  45. samples+=newSamples;
  46. newSamples=0;
  47. }
  48. else {
  49. newSamples++;
  50. }
  51. if( newSamples == 3 ) {
  52. uint16_t queSamples = (uint16_t)((samples*20 > 60000)? 60000 : samples*20);
  53. rcvEnQueue(queSamples); // Max 46000
  54. samples = newSamples;
  55. newSamples = 0;
  56. currValue = value;
  57. }
  58. }
  59. void stopInterrupts() {
  60. EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register
  61. }
  62. //********************************************************************
  63. // LOOP
  64. //
  65. //********************************************************************
  66. #define SER_RX_MODE_WAIT 0
  67. #define SER_RX_MODE_RCV 1
  68. #define SER_RX_MODE_END 2
  69. extern unsigned long x_data; // Receive data from Nexa
  70. extern unsigned long temp1_x_data; // Receive data from temp1
  71. void loop() {
  72. static boolean firstRun = true;
  73. static uint16_t width;
  74. unsigned long now = millis(); // Get the current time
  75. // Display version information 10 sec after startup
  76. if( firstRun == true && now > 30000 ) {
  77. Serial.println("433MHz Temp sensor receiver (Sparkfun Pro Micro) Ver: 180204 09:35");
  78. firstRun = false;
  79. }
  80. if( rcvDeQueue(&width) ) {
  81. }
  82. }