L22_7Segments and Arrays

L22_7Segments and Arrays - 7-Segment Display Board and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 ]; ECET 209 Purdue University // read 1st element 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 4 2 1 ECET 209 7447 2 2 0 Ω Purdue University 29 7-segment display ECET 209 Purdue University 30 7-segment display BCD 8 4 2 1 7447 2 2 0 Ω +5 Select digit ECET 209 Purdue University 31 7-segment display ECET 209 Purdue University 32 Multiplexed 7-segment display BCD 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 4 3 2 1 ECET 209 Select digit +5 +5 Purdue University +5 33 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 0 0 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 0 1 1 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 36 To display 1957 BCD 1 0 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 0 1 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 37 To display 1957 BCD 0 1 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 1 0 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 38 To display 1957 BCD 0 1 1 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 1 1 0 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 39 To display 1957 BCD 0 0 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 0 1 1 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 40 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 Digit 3 Digit 2 Digit 1 BCD 8 BCD 4 BCD 2 BCD 1 ECET 209 PORTA.7 PORTA.6 PORTA.5 PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0 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 9 5 7 ECET 209 Purdue University index index index index 0 1 2 3 48 Array Usage • What if … unsigned char Digit_Select [ 4 ] = {0x70,0xB0,0xD0,0xE0}; Digit_Select 0x70 0xB0 0xD0 0xE0 ECET 209 Purdue University index index index index 0 1 2 3 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 0 0 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 0 1 1 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 51 To display 1957 BCD 1 0 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 0 1 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 52 To display 1957 BCD 0 1 0 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 1 0 1 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 53 To display 1957 BCD 0 1 1 1 8 4 2 1 4 7447 3 2 1 2 2 0 Ω +5 1 1 1 0 4 3 2 1 +5 +5 ECET 209 Select digit Purdue University +5 54 Questions? ECET 209 Purdue University 55 ...
View Full Document

Ask a homework question - tutors are online