DN_IR.txt
30apr25 ndp


Reference Design Notes for a IR Sensor/Receiver driver. 

ref1: tsop38238.pdf - TSOP38238 data sheet.
ref2: nec_infrared_transmission_protocol-281113-1713-47344.pdf - IR signal protocol

Target: ATtiny AVR SIOC-14 devices (ATtiny814, ATtiny1614, etc.) 5v @ 20MHz.

Files: ir.c, ir.h

The tsop38238 demodulates a standard 38 kHz IR remote signal use by many home electronic devices such as TVs, DVDs, and audio system. It is a three terminal device with power, ground and output. It uses 2.5v to 5.5v for power with its output equal to (Vcc - 0.3v) and provides and Iout of 5ma.
The output is LOW when a signal is being received and HIGH when there is no signal.

The NEC IR Protocol Message consist of a 9ms SYNC burst followed by a 4ms gap, an 8-bit Address, its inverse, 
an 8-bit Command, and then its inverse.

A logical '0' is a 562.5us pulse followed by a 562.5us gap.
A logical '1' is a 562.5us pulse followed by a 1.6875ms gap.

An Interrupt-On-Change interrupt service routine (ISR) will be used to dectect '1's and '0' and the SYNC 
conditions using a simple state machine. It will set a BitReady flag to TRUE and set a BitData register 
to 0 or 1 based on the bit detected. Upon reading the BitReady flag, the flag will be set to FALSE.

A higher level service routine will poll the ISR, at a rate faster than 500us, and collect results to 
decode the IR message.

Use millis() to measure gaps (LOWs). >4ms = SYNC, >1ms = 1, else 0. Crude, but should work.
State machine to find SYNC, Address, Inverted Address, Command, and Inverted Command bytes.

3.333 MHz clock.  TCB can use DIV2 -> 1.666 MHz (0.6 us).

// part of ir_init()
TCB.CCMP = 0xFFFF
TCB.CTRLA = DIV2 | ENABLE

uint16_t newCnt;
static uint`6_t lastCnt = 0;

uint16_t getDuration() {
newCnt = TCB.CNT
if lastCnt < newCnt		// overflowed?
	newCnt += ~lastCnt +1;
else
	newCnt -= lastCnt;
}
Put timing inside ISR. hmm.. repeatable, almost right. Compare with waveform.
BUG: Not windowing the gap. Fixed. Working.
TODO:Add compare normal w/ inverted for validation check. Done.
Test: SONY remote uses different format. Blah.






