Sie sind auf Seite 1von 49

Functions

ECET 209 – Lecture 14


Introduction to Microcontrollers
Overview

• Functions
• Keypad Functions
• Top Down Design

ECET 209 Purdue University 2


Functions

• Functions are “building blocks”


– Perform complicated processes ( > 1 line of C )
– Eliminate redundant code
– Make code easier to read
– Allow for “libraries”
– Allow software to be modularized

ECET 209 Purdue University 3


Functions

• Functions have the following traits


– Allow parameters to be passed to them
• Ultimately become variables

– Allow a single value to be passed back


• Use assignment operator to place value in a variable
• Can you value directly for control

– May neither receive or return any values


ECET 209 Purdue University 4
Anatomy of a Function

• Functions have three components


– The function name
– The return value from the function
– The parameters passed to the function

return_data_type function_name ( passed_data_size(s) )

ECET 209 Purdue University 5


Anatomy of a Function

• Functions have three components


– The function name
– The return value from the function
– The parameters passed to the function

return_data_type function_name ( passed_data_size(s) )

ECET 209 Purdue University 6


ECET 209 Purdue University 7
Press Release Pressing “5”
DAV
DATA 0101

74922
KEYPAD
ENCODER
C D E F 1
DAV
8 9 A B
4 5 6 7 D3 0
D2 1
0 1 2 3 D1 0
D0 1
ECET 209 Purdue University 8
Keypad Flowchart

Wait for Dav


while ( (PINA & 0x10) == 0)
{
}
Read the Key
key = PINA & 0x0F;
while ( (PINA & 0x10) != 0)
{
Wait for
Not Dav }

ECET 209 Purdue University 9


Keypad Function

• What about the keypad software


– Does this function receive anything?
– This function sends data back!

unsigned char read_key(void);

ECET 209 Purdue University 10


Keypad Function
unsigned char read_key(void)
{
unsigned char key;
while ( (PINA & 0x10) == 0)
{
}
key = PINA & 0x0F;
while ( (PINA & 0x10) != 0)
{
}
return ( key );
}
ECET 209 Purdue University 11
Keypad Usage
unsigned char read_key(void);
void main(void)
{
// missing initialization code
while ( 1 )
{
PORTC = ~read_key(); // get and display
}
}
unsigned char read_key(void)
{
// missing code from previous slide
}ECET 209 Purdue University 12
ECET 209 Purdue University 13
Example

• Get a BCD number from the keypad.

• How do we accomplish this task?

ECET 209 Purdue University 14


Example Functions
unsigned int get_BCD ( void );

Usage:

value = get_BCD( );

ECET 209 Purdue University 15


Get a BCD Digit
Main

Get BCD

ECET 209 Purdue University 16


Get a BCD Digit
Main

Get BCD

Read Keypress

ECET 209 Purdue University 17


Get a BCD Digit
Main

Get BCD

Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 18
Get a BCD Digit
Main

Get BCD

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 19
Get a BCD Digit
Main

0 to 9
Get BCD

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 20
Questions?
Main

0 to 9
Get BCD

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 21
Example

• Get a 3 digit decimal number from the


keypad.

• How do we accomplish this task?

– Get the numbers 1, 5 and 6 combine them to


produce the number 156.

ECET 209 Purdue University 22


Example Functions
unsigned int get_Decimal ( void );

Usage:

value = get_Decimal( );

ECET 209 Purdue University 23


Get a 3-Digit Decimal Number
Main

Get Decimal

ECET 209 Purdue University 24


Get a 3-Digit Decimal Number
Main

Get Decimal

Get BCD

ECET 209 Purdue University 25


Get a 3-Digit Decimal Number
Main

Get Decimal

Get BCD
0 to F
Read Keypress
D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 26
Get a 3-Digit Decimal Number
Main

Get Decimal

0 to 9
Get BCD
0 to F
Read Keypress
D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 27
Get a 3-Digit Decimal Number
Main

Get Decimal

0 to 9
Get BCD
0 to F
Read Keypress
Do this 3 times D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 28
Get a 3-Digit Decimal Number
Main

Get Decimal

0 to 9
Get BCD
How do I build
this number ??? 0 to F
Read Keypress
D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 29
What are the weights of the
numbers?

156

100’s 1’s
10’s

ECET 209 Purdue University 30


Example

• Take the numbers, multiply them by the


appropriate power of ten, and add them
together
– 1 * 100
– 5 * 10
– 6*1
– ( 100 + 50 + 6 ) = 156

ECET 209 Purdue University 31


Get a 3-Digit Decimal Number
Main

000 to 999
Get Decimal

0 to 9
Get BCD
0 to F
Read Keypress
D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 32
Get a 3-Digit Decimal Number
Main

000 to 999
Get Decimal

0 to 9
Get BCD
0 to F
Read Keypress
D0
D1 DAV
D2
D3

Keypad
ECET 209 Purdue University Hardware 33
Example Keypad Functions

• Create a function that reads a two digit


hexadecimal value from the keypad

ECET 209 Purdue University 34


Example Functions
unsigned char get_HEX ( void );

Usage:

value = get_HEX( );

ECET 209 Purdue University 35


Creating a Hex Value

• Hexadecimal values have two places

Upper Digit Lower Digit

0 to F 0 to F

ECET 209 Purdue University 36


Creating a Hex Value
Main Upper Digit Lower Digit

Get Hex

ECET 209 Purdue University 37


Creating a Hex Value
Main Upper Digit Lower Digit

Get Hex

Read Keypress

ECET 209 Purdue University 38


Creating a Hex Value
Main Upper Digit Lower Digit

Get Hex

Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 39
Creating a Hex Value
Main Upper Digit Lower Digit

Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 40
Creating a Hex Value
Main ?? Lower Digit

Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 41
Creating a Hex Value
Main ?? Lower Digit

Get Hex

Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 42
Creating a Hex Value
Main ?? Lower Digit

Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 43
Creating a Hex Value
Main ?? ??

Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 44
Creating a Hex Value
Main ?? ??

Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 45
Creating a Hex Value
Main Upper Digit Lower Digit

0x00 to 0xFF
Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 46
Get a Hex Digit
Main

0x00 to 0xFF
Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 47
Questions??
Main Upper Digit Lower Digit

0x00 to 0xFF
Get Hex

0 to F
Read Keypress

D0
D1
DAV
D2
D3

Keypad
Hardware
ECET 209 Purdue University 48
ECET 209 Purdue University 49

Das könnte Ihnen auch gefallen