Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - Multiplex: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: AVR Embedded Tutorial - Multiplex

OldNewLine
1+# Charlieplexing (Multiplexing) LEDs
2+
3+Charlieplexing is a technique for driving a multiplexed display in which relatively few I/O pins of a microcontroller are used to drive an array of LEDs or LED segments.
4+
5+## Port access
6+
7+For how the shift register commands access the ports, see:
8+
9+- [[AVR Embedded Tutorial - Simple GPIO on and off output|GPIO]]
10+- [[AVR Embedded Tutorial - Shiftregister|Shift registers]]
11+
12+## Example
13+
14+Each time the timer is run through, a different LED segment is activated. So the timer has to run 4 times until a frame is updated. For a constant repetition frequency, the timer is best suited because it is called up regularly. A multiplex could also be made in the main loop, but if there is a short pause there for other functions, the display will start to flicker.
15+
16+The output takes place via two shift registers (74HC595). If the AVR has enough GPIO pins, you could also do this without the shift registers.
17+
18+## Digits constant for 7-segment display
19+
20+The LED combinations of the digits are stored in the Digits\[\] constant. HEX characters are also stored in this constant.
21+
22+``` pascal
23+ const
24+ Digits : packed array[0..15] of byte = (
25+ %00111111, // = 0
26+ %00000110, // = 1
27+ %01011011, // = 2
28+ %01001111, // = 3
29+ %01100110, // = 4
30+ %01101101, // = 5
31+ %01111101, // = 6
32+ %00000111, // = 7
33+ %01111111, // = 8
34+ %01100111, // = 9
35+ %01110111, // = a - for HEX
36+ %01111100, // = b
37+ %00111001, // = c
38+ %01011110, // = d
39+ %01111001, // = e
40+ %01110001 // = f
41+ );
42+```
43+
44+## Values ​​of the individual segments
45+
46+As an example, a 4-digit 7-segment display is used. If you want a larger multiplex, you have to increase the number of digits in the display.
47+
48+``` pascal
49+ const
50+ digitCount = 4;
51+ var
52+ Number : array[0..digitCount - 1] of byte;
53+```
54+
55+## Multiplex
56+
57+That is the heart of the multiplex. Each time the timer is run through, the segment is changed. If you want to see this, you have to reduce the timer frequency so that you can see it. For this example, **Timer0** is used.
58+
59+``` pascal
60+procedure Timer0_Interrupt; public name 'TIM0_COMPA_ISR'; interrupt;
61+const
62+ segPos : byte = 0;
63+begin
64+ segPos := segPos + 1;
65+
66+ if(segPos > digitCount - 1) then
67+ begin
68+ segPos := 0;
69+ end;
70+
71+ WritePortA(latchPin, False);
72+ WritePortA(latchPin, True);
73+
74+ shiftOut595(Digits[Number[segPos]]); // Go to the current segment in the number
75+ shiftOut595(1 shl segPos); // Activate current segment
76+end;
77+```
78+
79+## Delay
80+
81+A simple delay. This is required when changing the number in the main loop.
82+
83+``` pascal
84+procedure delay;
85+var
86+ i : integer;
87+begin
88+ for i := 0 to 200 do
89+ asm
90+ nop // waste cycles
91+ end;
92+end;
93+```
94+
95+## Initialization and main loop
96+
97+Timer0 for the mutiplex is initialized here and the required ports are switched to output. This example alternates between 1234 and 5678.
98+
99+For more about timers, see: [[AVR Embedded Tutorial - Timer, Counter|Timers]].
100+
101+``` pascal
102+ begin
103+ // Disable interrupts
104+ asm
105+ cli
106+ end;
107+
108+ // Switch ports to output
109+ DDRA := (1 shl dataOutPin) or (1 shl latchPin) or (1 shl clockPin);
110+
111+ // Initialize the Timer0
112+ TCCR0A := (1 shl 1);
113+ TCCR0B := TCCR0B or %101;
114+ TIMSK0 := TIMSK0 or (1 shl OCIE0A);
115+
116+ // Enable interrupts
117+ asm
118+ sei
119+ end;
120+
121+ // Main loop
122+ repeat
123+ Number[0] := 1;
124+ Number[1] := 2;
125+ Number[2] := 3;
126+ Number[3] := 4;
127+ delay;
128+
129+ Number[0] := 5;
130+ Number[1] := 6;
131+ Number[2] := 7;
132+ Number[3] := 8;
133+ delay ;
134+ until 1 = 2;
135+ end;
136+```
137+
138+# See also
139+
140+- [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview
141+
142+---
143+*From the Free Pascal wiki, page "AVR Embedded Tutorial - Multiplex", as it stood on 2020-01-26 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
144+Category: AVR
145+Category: Arduino
146+Category: Embedded
147+Category: Tutorials

Back to history