I'm currently working on a project, where I have an external device (POCSAG pager - POCSAG protocol) that is giving out a digital (5V - 0V) square-wave signal. I have an ATmega2560 (Arduino) microcontroller and I want to read the signal so I can decode it to text.
I already finished this project where the external device works in perfect "lab conditions", which means that one bit is transmitted in exactly 833 microseconds (1200 Hz). I read all the bits correctly with a simple timer and decode them afterwards.
The problem is, when I try it on a real-world pager, the bit-rate varies a lot! From 750 microseconds to 870 microseconds, and each bit is somewhere in between this which means that I get a lot of errors when reading the signal since one message is up to 5000 bits long. Let's not forget that there is always noise on the data line when the real signal is not present, when there are no messages being transmitted.
I managed to make it work in "lab conditions" by measuring 3 bits, and if all bits match 833 microseconds I attached an interrupt that would trigger a timer with a period of 833 microseconds that samples the signal and saves it into a character array (yes, I know, bad solution). As I said, this works perfectly as long as the frequency is stable.
I tried measuring 10 - 20 bits which were between 750 - 860 microseconds and calculating the average bit-rate and sampling the signal with that rate afterwards... Didn't work either.
The digital signal always has a preamble of 576 alternating bits (0101010101...) so you can detect when an actual message is being transmitted.
Any ideas on how I should tackle this?
I'm sorry if I didn't explain it thoroughly enough... If you have any further questions, let me know!
EDIT: This is how the signal is encoded. In short, one bit is transmitted in 833 microseconds (supposedly) and the message starts bits 576 alternating bits, called the preamble (010101010101...). After that a 32-bit long Frame Sync codeword is transmitted (the FS codeword is always the same) that signals the beginning of the first batch of data. There are 8 frames (each frame holds 2 codewords, 2 x 32 bits) which hold 16 codewords of data. After those 8 frames, if the message continues, another FS codeword is transmitted and so on and on...