Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define POCSAG_RECV 2 // receiver pin
- #define SYNC_BITS 50 // number of bit periods to measure for calculating the average sample rate
- #define MAX_MSG_BITS 1500 // when to stop reading the pin
- volatile bool sync_start = false;
- volatile bool start_sampler = false;
- bool sample_rate_calculated = false;
- int sample_rate = 0; // sampling rate
- int measured_periods[SYNC_BITS]; // array of multiple bit periods for syncing the bit-rate
- int bits_read = 0; // to count against MAX_MSG_BITS
- String data; // where our data is stored
- /// function prototype ///
- int calculate_sample_rate();
- /// function prototype ///
- void setup() {
- pinMode(POCSAG_RECV, INPUT_PULLUP); // set pin 2 as receiver input
- Serial.begin(9600);
- attachInterrupt(digitalPinToInterrupt(POCSAG_RECV), start_sync_ISR, RISING);
- }
- void loop() {
- if (sync_start && !sample_rate_calculated) { // start the bit-rate syncing sequence
- for (int i = 0; i < SYNC_BITS; i++) { // save multiple bit-rates into an array
- measured_periods[i] = pulseIn(POCSAG_RECV, HIGH);
- }
- sample_rate = calculate_sample_rate(); // calculate the average
- sample_rate_calculated = true; // set this variable to true in order for the loop to skip next time
- for (int i = 0; i < SYNC_BITS; i++) { // unnecessary -> print all bit-periods to see
- Serial.println(measured_periods[i]);
- }
- Serial.println("--------");
- Serial.print("Sample rate: ");
- Serial.println(sample_rate); // print the average sample rate...
- Serial.println("--------");
- sync_start = false; // turn this variable to false in order to skip this loop next time
- // here we attach the interrupt so we can start reading bits with synced bit-rate
- attachInterrupt(digitalPinToInterrupt(POCSAG_RECV), start_sampler_ISR, RISING);
- }
- if(start_sampler) { // run after bit-rate syncing is complete...
- delayMicroseconds(sample_rate + sample_rate / 2); // delay one bit period and a half, so we start in the middle of the next bit
- while (bits_read < MAX_MSG_BITS) { // read data from receiver pin until data is full (MAX_MSG_BITS number)
- data += !digitalRead(POCSAG_RECV); // invert due to internal pull-up resistor enabled
- delayMicroseconds(sample_rate); // delay one bit-period in order to sample again near the middle of the next bit
- bits_read++; // increase counter to reach MAX_MSG_BITS
- }
- start_sampler = false; // disable the data reading loop...
- Serial.println(data); // print acquired data...
- }
- }
- void start_sync_ISR() { // ISR for starting the sync process when first bit reaches the receiver pin
- detachInterrupt(0); // remove interrupt in order to perform calculations
- sync_start = true; // start syncing in the main loop
- }
- void start_sampler_ISR() { // ISR for starting the sampling process in main loop
- detachInterrupt(0);
- start_sampler = true;
- }
- int calculate_sample_rate() { // function for calculating the average bit-rate for sampling.
- long measured_sum;
- for (int i = 0; i < SYNC_BITS; i++) {
- measured_sum += measured_periods[i];
- }
- return measured_sum / SYNC_BITS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement