Path: AVR_EN => Applications => Infrared => IR transmitter variable frequency    (Diese Seite in Deutsch: Flag DE)
IR-tranmsmitter variable frequency Applications of
AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega
Infrared transmitter with variable frequency

Infrared transmitter with variable frequency

For all those that need something like that: a frequency variable crystal controlled Infrared transmitter, which can transmit between 30 and 60 kHz. What ís that good for? Here you find two variations: one with an 8-Bit-Counter in an ATtiny25 and one with a 16-Bit-Counter in an ATtiny24.

Some hints on how the software works are given in the section on Software-hints.

The schematics of this project are available as LibreOffic-Draw file here, the calculations are in the Calc file here.

Top 8-Bit-TC 16-Bit-TC Software

1 The transmitter with an ATtiny25

1.1 Schematic of the transmitter with an ATtiny25

The schematic of the IR transmitter with an ATtiny25 The IR-transmitter with an ATtiny25 This is the IR transmitter with an ATtiny25.

The device measures the position of the potentiometer on its ADC1 input pin. Depending from the result the device provides a rectangle between 30 and 60 kHz on its OC1A output pin.

The OC1A output drives a transistorized constant-current source, that switches the Infrared LED on with 100 mA and off. The red LED, that provides the base voltage for the transistor, turns on if the output pin is high and if the IR LED is attached correctly. A frequency counter can be attached to the OC1A output pin to measure the frequency.

The LED can be turned on and off by the state of the the PCINT0 input pin.

The ATtiny25 is clocked with a crystal of 15 MHz, so that the generated frequency is exact and offers a maximum of frequency resolution.

Also included is a 6-pin-ISP interface, by which the AVR can be programmed within the system. The two resistors of 1k decouple the device during programming.

Top 8-Bit-TC 16-Bit-TC Software

1.2 The software for the ATtiny25

The software is available as Assembler source code for download here. After assembling and burning the hexcode, the fuses of the ATtiny25 have to be altered, otherwise the IR transmitter is by 15-fold too slow.

Original fuses of the ATtiny25 Fuses for the 15MHz crystal on the ATtiny25 To the left the original fuses can be seen, to the right the fuses for the 15 MHz crystal.


Top 8-Bit-TC 16-Bit-TC Software

2 The transmitter with an ATtiny24

2.1 Schematic of the IR transmitter with an ATtiny24

Schematic of the IR transmitter with an ATtiny24 IR transmitter with an ATtiny24 This is the same device but equipped with an ATtiny24. The tn24 has a 16-Bit-Timer. A 20-pin ATtiny2313 can also be used, which similarly provides a 16-Bit-Timer and an INT0. But please note that the software needs some changes to work with an tn2313.

The device measures the voltage of the potentiometer on its ADC0 input pin. Depending from the result the OC1A output pin toggles with 30 to 60 kHz.

Attached to the OC1A pin is a tranistorized constant current source that drives the IR LED with approximately 100 mA current. To the OC1A output, a frequency counter can measure the frequency.

On the INT0 input pin, the transmitter can be switched on and off.

The ATtiny24 is clocked with a crystal of 20 MHz, so that the generated frequency is exact and stable.

Added is also a ISP6 plug, by which the ATtiny24 can be programmed within the system.

2.2 The software for the ATtiny24

The Software is available for download as Assembler source code here. After assembling and burning the hexcode into the flash menory, the fuses have to be changed, otherwise the IR transmitter runs by 20-fold too slow.

Original fuses of the ATtiny24A Fuses for the 20MHz crystal on the ATtiny24A To the left the original fuses can be seen, to the right the fuses for the 20 MHz crystal.


Top 8-Bit-TC 16-Bit-TC Software

3 Hints for the software

3.1 The timer

The two timers for generating the IR frequency In the ATtiny25 version the 8-bit-timer TC1 works, in the ATtiny24 the 16-bit timer TC1 works to generate the IR signals. Both timers work in CTC mode, with Compare A as reset source.

Note that for a complete rectangle two CTC cycles are necessary, so that the frequencies in calculations are doubled.

The reason why the ATtiny25 works with a 15 MHz crystal can be seen if you change the cell B3 in the spreadsheet to a higher value: the prescaler then changes to 2 (which the TC1 in an ATtiny25 can do). Otherwise the 8-Bit-Timer would overflow.



The potentiometer alters the frequency of the ATtiny25 At 15 MHz the CTC divider has to divide the clock frequency by 250 to transmit 30 kHz IR frequency, at 60 kHz by 125. At 20 MHz clock the divider in a 16-Bit-TC1 those are 333 and 167. The Compare-A values are by one lower than those divider values.

The span, over which the potentiometer's voltage modifies the compare values is at 15 MHz 125 and slightly smaller than for 20 MHz (166), which means a smaller resolution.

The potentiometer alters the frequency of the ATtiny24 The two diagrams show, that the frequency rises linearly with the potentiometer's angle. This is achieved by a table, by which each of the potential 256 results of the ADC measurements have an associated compare-A value.

The spreadsheet calculation offers in the cell areas G15 to G145 (8 bit) and in J15 to J145 (16 bit) a table that can be copied to the source code. In the lower parts of those tables more than one compare value (8 bit: four, 16 bit: three) are the same.

The timers run in free-running mode, that means they do'nt need any interrupts. Varied by the ADC measurements are the compare values only. The INT0 interrupt determines whether the counter is toggling the OC1A output (INT0 input low) or clearing the OC1A (INT0 high).

Top 8-Bit-TC 16-Bit-TC Software

3.2 The ADC measurements

Because the potentiometer is a regular 270° device and not a 10-turn, the ADC is working as a left-adjusted 8-bit ADC: only the upper 8 bits of the result are used.

256 measurements of the ADC are summed up. The MSB of the sum is either used directly (8-bit table) or after doubling (16-bit table) to read the next compare A value from the table by using the LPM instruction. When reading the 8-bit table, the compare register is OCR1C. When reading from the 16-bit table, the MSB of the compare value has to be written first to OCR1AH, the LSB has to follow next. Therefore the LSB has to be stored.

The ADC timing is calculated in the spreadsheet table "adc-timing" for both clock frequencies. As the ADC is in free-running mode, the next conversion starts immediately after reading the high byte of the result. As this happens in an interrupt service routine, the next conversion needs 13 plus 7 (for the interrupt and the vector jump) plus one clock cycle for the read instruction, altogether 21 clock cycles per measurement. With a clock prescaler of 128 and with 256 measurements for the sum each 46 resp. 34 milli-seconds a result is written to the compare port register.

Because the TC1 clock needs 20 µs at 30 kHz IR frequency for each CTC run, the new value is written after 2,753 CTC runs, which is relatively seldom. That also means that in the rare cases, where the next compare value is smaller than the previous value, and the timer has already counted to beyond the next value, it would miss the compare match. That means that the 8-bit counter counts to 255 and then to the new compare match value, before the next compare match toggles the OC1A output. The 16-bit counter counts to 65.535 and then to the next compare value. As this happens really seldom, I did not synchronize the compare port register write with the compare match.

As the next ADC result already appears 46 ms later, the differences of the compare values are rather small, reducing the probability of such a compare match miss further. And this only happens if the potentiometer is on its way downwards.

If you want to synchronize the compare write with the compare match, just add a compare match interrupt service routine that writes the new value just after a compare match has occurred.

3.3 The external interrupt

If the switch on the PCINT0 pin (ATtiny25) or on the INT0 pin (ATtiny24) opens or closes, the respective interrupt is executed.

On both edges the switch input is read and either the COM1A1 bit is set to clear the OC1A pin (if the switch input is high) or to toggle the OC1A pin (if the switch input is low).

Top 8-Bit-TC 16-Bit-TC Software


©2023 by http://www.avr-asm-tutorial.net