Advertisement
Guest User

Decoder

a guest
Mar 15th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #define POCSAG_RECV 2 // receiver pin
  2. #define SYNC_BITS 50 // number of bit periods to measure for calculating the average sample rate
  3. #define MAX_MSG_BITS 1500 // when to stop reading the pin
  4.  
  5. volatile bool sync_start = false;
  6. volatile bool start_sampler = false;
  7. bool sample_rate_calculated = false;
  8.  
  9. int sample_rate = 0; // sampling rate
  10.  
  11. int measured_periods[SYNC_BITS]; // array of multiple bit periods for syncing the bit-rate
  12.  
  13. int bits_read = 0; // to count against MAX_MSG_BITS
  14.  
  15. String data; // where our data is stored
  16.  
  17. /// function prototype ///
  18. int calculate_sample_rate();
  19. /// function prototype ///
  20.  
  21. void setup() {
  22.  
  23.   pinMode(POCSAG_RECV, INPUT_PULLUP); // set pin 2 as receiver input
  24.  
  25.   Serial.begin(9600);
  26.  
  27.   attachInterrupt(digitalPinToInterrupt(POCSAG_RECV), start_sync_ISR, RISING);
  28.  
  29. }
  30.  
  31. void loop() {
  32.  
  33.   if (sync_start && !sample_rate_calculated) { // start the bit-rate syncing sequence
  34.  
  35.       for (int i = 0; i < SYNC_BITS; i++) { // save multiple bit-rates into an array
  36.      
  37.         measured_periods[i] = pulseIn(POCSAG_RECV, HIGH);
  38.      
  39.       }
  40.  
  41.       sample_rate = calculate_sample_rate(); // calculate the average
  42.  
  43.       sample_rate_calculated = true; // set this variable to true in order for the loop to skip next time
  44.      
  45.       for (int i = 0; i < SYNC_BITS; i++) { // unnecessary -> print all bit-periods to see
  46.        
  47.           Serial.println(measured_periods[i]);
  48.        
  49.         }
  50.  
  51.       Serial.println("--------");
  52.       Serial.print("Sample rate: ");
  53.       Serial.println(sample_rate); // print the average sample rate...
  54.       Serial.println("--------");
  55.  
  56.       sync_start = false; // turn this variable to false in order to skip this loop next time
  57.  
  58.      
  59.       // here we attach the interrupt so we can start reading bits with synced bit-rate
  60.       attachInterrupt(digitalPinToInterrupt(POCSAG_RECV), start_sampler_ISR, RISING);
  61.    
  62.     }
  63.  
  64.   if(start_sampler) { // run after bit-rate syncing is complete...
  65.    
  66.       delayMicroseconds(sample_rate + sample_rate / 2); // delay one bit period and a half, so we start in the middle of the next bit
  67.  
  68.       while (bits_read < MAX_MSG_BITS) { // read data from receiver pin until data is full (MAX_MSG_BITS number)
  69.        
  70.           data += !digitalRead(POCSAG_RECV); // invert due to internal pull-up resistor enabled
  71.           delayMicroseconds(sample_rate); // delay one bit-period in order to sample again near the middle of the next bit
  72.           bits_read++; // increase counter to reach MAX_MSG_BITS
  73.        
  74.         }
  75.  
  76.         start_sampler = false; // disable the data reading loop...
  77.  
  78.         Serial.println(data); // print acquired data...
  79.    
  80.     }
  81.  
  82. }
  83.  
  84. void start_sync_ISR() { // ISR for starting the sync process when first bit reaches the receiver pin
  85.  
  86.     detachInterrupt(0); // remove interrupt in order to perform calculations
  87.  
  88.     sync_start = true; // start syncing in the main loop
  89.  
  90.   }
  91.  
  92. void start_sampler_ISR() { // ISR for starting the sampling process in main loop
  93.  
  94.     detachInterrupt(0);
  95.  
  96.     start_sampler = true;
  97.  
  98.   }
  99.  
  100. int calculate_sample_rate() { // function for calculating the average bit-rate for sampling.
  101.  
  102.     long measured_sum;
  103.  
  104.     for (int i = 0; i < SYNC_BITS; i++) {
  105.      
  106.         measured_sum += measured_periods[i];
  107.      
  108.       }
  109.  
  110.     return measured_sum / SYNC_BITS;
  111.  
  112.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement