I seem to have completely forgotten this post, it's been draft for more than a year now. Sorry about that
Part 1 here .
First, why didn't I use dedicated IRDA peripheral on chip? Because I had absolutely no idea of the parameters involved, have never used it - and most importantly, it would have been really difficult to wire on my board since existing design didn't have correct IO exposed. And I really just needed to have few select messages to send, so copying signal timing was good enough.
Over time I pondered on my spare time how I could make receiver better, less sensitive to signal timing. Previous attempt used IO interrupts,which caused system to occasionally miss one due to timings, so that was no go. Eventually I got a rouch draft of an idea working;
- Set up a timer peripheral to run at twice or clock rate of IR signal (this I could measure with scope, 76 kHz in this case as I wanted both "up" and "down" parts; actual modulation was running at 38 kHz). As listened this does nothing but increase a counter on every tick. This makes period of 13us per tick.
- All other interrupts are disabled
- Main loop does nothing but polls IO input line and on change, it first takes counter increased by timer above and stores its value (Tcurr).
- Now we take timer difference from last change (Td = Tcurr-Tlast). If the value is less than 3, this is modulation and we can ignore it; this is stored as "last activity" count (Tlast = Tcurr). Very first change triggers storing TmodLast.
If difference between from previous "last activity" (Tlast) count is larger (more than 200uS or so), we know that this was "modulation off" period which actually carries bits; now next modulation period was started.
Now compare difference from previous Tlast and TmodLast to get
previous "modulation on" length. First is preamble (3600us or so) which
resets receiver. All shorter (400us) periods were here for timing; more sophisticated system might use them for data too.
For this sequence, I treated 400us "modulation off" period as "0" and 1200us period as "1".
- After longer off period, system automatically ends receiving.
This above is the end result, original idea had some flaws I found out when writing the code (debugging it was a bit difficult due to very strict timing requirements! Lot of debugging in the end was done by triggering few IO lines and monitoring IR data and said lines on oscilloscope that those happened when expected). In the end I had receiver working.
Appendix on 2024: I later had to re-record signals as device I was controlling changed, and this time I got lazy. I just demodulate signal (using commercial demodulating receiver) and store on-off periods, without bothering to actually decode it.
Replay is done with timer interrupt by toggling IR LED state when signal is "on"; main replay function merely keeps track on state and timing, and sets "IR modulating" signal for interrupt. Much simpler, but at same time far less satisfying...