Sie sind auf Seite 1von 55

7-Segment Display Board

and Arrays

ECET 209 – Lecture 22


Introduction to Microcontrollers
Arrays

• Arrays are:
– Closely related values that share the same name
and are indexed by a number
– Can be stored in RAM, Flash or EEPROM

– Can be related to cells inside of Excel

ECET 209 Purdue University 2


Arrays

Name of the Array

Array Element
ECET 209 Purdue University 3
Arrays

ECET 209 Purdue University 4


Arrays
• Arrays are similar to variables
– Must be declared
– Values can be written to
– Values can be read
• Common uses:
– Collections of data
– Serial Queues
– Initialization values
– Etc.
ECET 209 Purdue University 5
Arrays

• Accessing Arrays
– Arrays are accessed by their name and an index
– The index is held inside of square brackets
– The index starts at zero
– The final element is then the length of the array
minus 1

ECET 209 Purdue University 6


Accessing Arrays

ECET 209 Purdue University 7


Arrays
• Arrays must be declared before they can be
used ( like normal variables )
– Use meaningful names
– Also have a specified length
– May be initialized with a value if desired

ECET 209 Purdue University 8


Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University 9


Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

Array Name
ECET 209 Purdue University 10
Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

Data Type
ECET 209 Purdue University 11
Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

Number of
Elements
ECET 209 Purdue University 12
Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University Initial Values13


Example Array with ASCII
Characters

Assignment Operator

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University Initial Values14


Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

Braces

ECET 209 Purdue University Initial Values15


Example Array with ASCII
Characters

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University Initial Values16


Example Array with ASCII
Characters
Separated by Commas

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University Initial Values17


Example Array with ASCII
Characters Terminated
w/ Semicolon

unsigned char ch_array [ 3 ] = { 0x32, 0x39, 0x41 };

ECET 209 Purdue University Initial Values18


Graphically

ECET 209 Purdue University 19


Array Usage

value = ch_array [ 0 ]; // read 1st element

ECET 209 Purdue University 20


Array Usage

value = ch_array [ 0 ]; // read 1st element

so value is now equal to 0x32


ECET 209 Purdue University 21
Word of Caution

• C will let you shoot yourself in the FOOT!


• Writing to arrays can cause problems if not
done “correctly”
– Remember, arrays are sequential memory
locations
– What happens if I write outside of my array???

ECET 209 Purdue University 22


Boundary Checking

ch_array[ 3 ] = 0xAA; // place info into the array

ECET 209 Purdue University 23


Arrays

• Use arrays in Lab for 7-Segment Display

ECET 209 Purdue University 24


7-segment display
VCC

2
2
0
Ω
ATmega16

ECET 209 Purdue University 25


7-segment display
VCC

2
2
0
Ω
ATmega16 VCC
2
2
0
Ω
ECET 209 Purdue University 26
Arrays with 7-segment display
VCC

2
2
0
Ω
ATmega16

ECET 209 Purdue University 27


Arrays with 7-Segments

unsigned char digit_control [ ]={

};

ECET 209 Purdue University 28


7-segment display

VCC
BCD
8 2
4 2
2 7447 0
1 Ω

ECET 209 Purdue University 29


7-segment display

ECET 209 Purdue University 30


7-segment display

BCD
8 2
4 2
2 7447 0
1 Ω

+5

Select digit

ECET 209 Purdue University 31


7-segment display

ECET 209 Purdue University 32


Multiplexed 7-segment display

BCD 4 3 2 1
8 2
4 2
2 7447 0
1 Ω

+5
4
3 +5
2 +5
1
+5
ECET 209 Purdue University 33
Select digit
Multiplexing Details

• Scan the Digit Selects


– 4 mSec
– 4 mSec * 4 Displays = 16mSec
– time = 1 / frequency
– 16.67mSec = 1 / 60Hz
– ¼ duty cycle => 25%
– LEDs are ¼ or 25% as bright as normal
– Common to drive at 4 to 8 times I rated
ECET 209 Purdue University 34
To display 1957

ECET 209 Purdue University 35


To display 1957

BCD 4 3 2 1
0 8 2
0 4 2
0 2 7447 0
1 1 Ω

+5
0 4
1 3 +5
1 2 +5
1 1
+5
ECET 209 Purdue University 36
Select digit
To display 1957

BCD 4 3 2 1
1 8 2
0 4 2
0 2 7447 0
1 1 Ω

+5
1 4
0 3 +5
1 2 +5
1 1
+5
ECET 209 Purdue University 37
Select digit
To display 1957

BCD 4 3 2 1
0 8 2
1 4 2
0 2 7447 0
1 1 Ω

+5
1 4
1 3 +5
0 2 +5
1 1
+5
ECET 209 Purdue University 38
Select digit
To display 1957

BCD 4 3 2 1
0 8 2
1 4 2
1 2 7447 0
1 1 Ω

+5
1 4
1 3 +5
1 2 +5
0 1
+5
ECET 209 Purdue University 39
Select digit
To display 1957

BCD 4 3 2 1
0 8 2
0 4 2
0 2 7447 0
1 1 Ω

+5
0 4
1 3 +5
1 2 +5
1 1
+5
ECET 209 Purdue University 40
Select digit
ECET 209 Purdue University 41
7-Segment Display Board

ECET 209 Purdue University 42


Multiplexed 7-segment display

ECET 209 Purdue University 43


I/O Hookup

Digit 4 PORTA.7
Digit 3 PORTA.6
Digit 2 PORTA.5
Digit 1 PORTA.4

BCD 8 PORTA.3
BCD 4 PORTA.2
BCD 2 PORTA.1
BCD 1 PORTA.0
ECET 209 Purdue University 44
ECET 209 Purdue University 45
Array Usage
• What if …

unsigned char BCD_data [ 4 ] = { 1, 9, 5, 7};

ECET 209 Purdue University 46


Array Usage
• What if …

unsigned char BCD_data [ 4 ] = { 1, 9, 5, 7};

BCD_data

1
9
5
7

ECET 209 Purdue University 47


Array Usage
• What if …

unsigned char BCD_data [ 4 ] = { 1, 9, 5, 7};

BCD_data

1 index 0
9 index 1
5 index 2
7 index 3

ECET 209 Purdue University 48


Array Usage
• What if …

unsigned char Digit_Select [ 4 ] = {0x70,0xB0,0xD0,0xE0};

Digit_Select

0x70 index 0
0xB0 index 1
0xD0 index 2
0xE0 index 3

ECET 209 Purdue University 49


7 Segment Code

while (1)
{
for ( index = 0 ; index < 4; index++)
{
PORTA = Digit_Select[index] | BCD_data [index ];
delay( 4 );
}
}

ECET 209 Purdue University 50


To display 1957

BCD 4 3 2 1
0 8 2
0 4 2
0 2 7447 0
1 1 Ω

+5
0 4
1 3 +5
1 2 +5
1 1
+5
ECET 209 Purdue University 51
Select digit
To display 1957

BCD 4 3 2 1
1 8 2
0 4 2
0 2 7447 0
1 1 Ω

+5
1 4
0 3 +5
1 2 +5
1 1
+5
ECET 209 Purdue University 52
Select digit
To display 1957

BCD 4 3 2 1
0 8 2
1 4 2
0 2 7447 0
1 1 Ω

+5
1 4
1 3 +5
0 2 +5
1 1
+5
ECET 209 Purdue University 53
Select digit
To display 1957

BCD 4 3 2 1
0 8 2
1 4 2
1 2 7447 0
1 1 Ω

+5
1 4
1 3 +5
1 2 +5
0 1
+5
ECET 209 Purdue University 54
Select digit
Questions?

ECET 209 Purdue University 55

Das könnte Ihnen auch gefallen