0 - Choopan Rattanapoka

advertisement
INPUT INTERFACE
353156 – Microprocessor
Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat
Review : Control Register on GPIO Port

There are 4 abstract registers to control GPIO port

IODIR (GPIO Port Direction Control Register)
To control either GPIO port is an input port (0) or output port(1)
 We use IODIR0 for Port0, and IODIR1 for Port1


IOPIN (GPIO Pin Value Register)
To read current port status
 We use IOPIN0 for Port0, and IOPIN1 for Port1


IOSET (GPIO Port Output Set Register)
To write data “1” to the specific port to make output logic to “1”
 IOSET0 for Port0, and IOSET1 for Port1


IOCLR (GPIO Port Output Clear Register)
To write data “1” to the specific port to make output logic to “0”
 IOCLR0 for Port0, and IOCLR1 for Port1

Setting a port as an INPUT port

Suppose that you want to set a port P0.0 as an
Input port.
 IODIR0



= 0x0;
//ALL Port 0.0 will be input port.
Normally an INPUT port without anything connect to
it will return a logic “1” (high)
So If you want to take an input “1” to
microcontroller, you need to supply a logic “0” (low)
We can check an INPUT port status by using IOPIN
register
How to connect a push button to
microcontroller


While you do not push a switch, P0.0 has a logic “1”
When you push a switch, P0.0 has a logic “0”
How to check whether switch is push or
not in a program
#include “LPC23xx.h”
int main(void) {
unsigned int P0_0 = 0x1;
IODIR0 = 0x0;
while(1) {
Switch is not pushed
31
30
. . . 8
7
6
5
4
3
2
1
0
IOPIN0
1
1
.
.
.
1
1
1
1
1
1
1
1
1
P0_0
0
0
.
.
.
0
0
0
0
0
0
0
0
1
0
0
.
.
.
0
0
0
0
0
0
0
0
1
Bit
IOPIN0 & P0_0
if((IOPIN0 & P0_0) == 0)
{
Switch is pushed
//switch is pushed
} else {
//switch isn’t pushed
}
}
}
31
30
. . . 8
7
6
5
4
3
2
1
0
IOPIN0
1
1
.
.
.
1
1
1
1
1
1
1
1
0
P0_0
0
0
.
.
.
0
0
0
0
0
0
0
0
1
0
0
.
.
.
0
0
0
0
0
0
0
0
0
Bit
IOPIN0 & P0_0
Example : 1 Push Button + 1 LED

We want to use
 P0.0
as an input connecting to a push button switch
 P0.1 as an output connecting to a LED
 When a push button is pressed, LED will be on.
 When a push button is not pressed, LED will be off.
Example 1: Flow Chart
Example 1 : Write a Program
#include “LPC23XX.h”
int main(void) {
unsigned int P0_0 = 0x1;
unsigned int P0_1 = 0x2;
IODIR0 = P0_1;
IOCLR0 = P0_1;
while(1) {
if((IOPIN0 & P0_0) == 0)
IOSET0 = P0_1;
else
IOCLR0 = P0_1;
}
}
Example 2 : 2 Switches + 2 LEDs

We want to use
 P0.0,
P0.1 as input ports connecting to push button
switches
 P0.4, P0.5 as an output connecting to a LED
 When a push button P0.0 is pressed, LED connecting to
P0.4 will be on.
 When a push button P0.1 is pressed, LED connecting to
P0.5 will be on.
Example 2: Flow Chart
P0.0
P0.4
P0.1
P0.5
Example 2: Write a Program (1)
#include “LPC23XX.h”
int main(void) {
unsigned int P0_0 = (1 << 0);
unsigned int P0_1 = (1 << 1);
unsigned int P0_4 = (1 << 4);
unsigned int P0_5 = (1 << 5);
IODIR0 = P0_4 | P0_5;
IOCLR0 = P0_4 | P0_5;
//Code on the next slide
}
Example 2: Write a Program (2)
while (1) {
if((IOPIN0 & P0_0) == 0) {
IOSET0 = P0_4;
} else {
IOCLR0 = P0_4;
}
if((IOPIN0 & P0_1) == 0) {
IOSET0 = P0_5;
} else {
IOCLR0 = P0_5;
}
}
Example 3: Code Complete
#include “LPC23XX.h”
while (1) {
int main(void) {
if((IOPIN0 & P0_0) == 0) {
unsigned int P0_0 = (1 << 0);
IOSET0 = P0_4;
unsigned int P0_1 = (1 << 1);
} else {
unsigned int P0_4 = (1 << 4);
IOCLR0 = P0_4;
unsigned int P0_5 = (1 << 5);
}
if((IOPIN0 & P0_1) == 0) {
IODIR0 = P0_4 | P0_5;
IOSET0 = P0_5;
IOCLR0 = P0_4 | P0_5;
} else {
IOCLR0 = P0_5;
}
}
}
Life is not Easy 


When you use switch as input to your microcontroller to
do more complex task than just turn on and off LED, it
can have a “Contact bounce and De-bouncing”
problem.
Contact bounce and De-bouncing
Push-button switches, toggle switches, and electromechanical relays all have one thing in common: contacts.
 It's the metal contacts that make and break the circuit and
carry the current in switches and relays.
 Because they are metal, contacts have mass. And since at
least one of the contacts is on a movable strip of metal, it
has springiness.

Contact Bounce and De-Bouncing

If you use switch as input to your counter program.


Let’s say, if you push a switch one time, counter will be increase
the value by 1.
Suppose counter value is now 0

From the figure above, what is the counter value after you press
and release the switch ?
Fixed Contact Bounce by Code (1)

Suppose, P0.0 is used as input port connecting to a
push button switch
Fixed Contact Bounce by Code (2)
if ((IOPIN0 & P0_0) == 0) {
Counter++;
}
if ((IOPIN0 & P0_0) == 0) {
for(i = 0; i < 2000; i++);
if ((IOPIN0 & P0_0) == 0) {
Counter++;
}
}
Example3 : 7-Segment + 1 switch

We will use
 P0.0
as a input port connecting to a push button
 P1.24 – P1.31 for output to drive 7-segment
 7-segment displays “0” at the initial state
 After a user presses the switch, the 7-segment will
display next number of its state
 If the number of 7-segments display is “9”, next time
user presses the button the 7-segment will display “0”
Example 3: 7-Segment
Display
P1.31
(a)
P1.30
(b)
P1.29
(c)
P1.28
(d)
P1.27
(e)
P1.26
(f)
P1.25
(g)
P1.24
(.)
Hex Value
0
1
1
1
1
1
1
0
0
0xFC000000
1
0
1
1
0
0
0
0
0
0x60000000
2
1
1
0
1
1
0
1
0
0xDA000000
3
1
1
1
1
0
0
1
0
0xF2000000
4
0
1
1
0
0
1
1
0
0x66000000
5
1
0
1
1
0
1
1
0
0xB6000000
6
1
0
1
1
1
1
1
0
0xBE000000
7
1
1
1
0
0
0
0
0
0xE0000000
8
1
1
1
1
1
1
1
0
0xFE000000
9
1
1
1
1
0
1
1
0
0xF6000000
.
0
0
0
0
0
0
0
1
0x01000000
Example 3 : Write a Program
#include “LPC23XX.h”
int main(void) {
switch(Counter) {
case 0 : IOSET1 = 0xFC000000; break;
unsigned int P1_24TO31 = 0xFF000000;
case 1 : IOSET1 = 0x60000000; break;
unsigned int P0_0 = 0x1; int Counter = 0, d;
case 2 : IOSET1 = 0xDA000000; break;
IODIR1 = P1_24TO31;
case 3 : IOSET1 = 0xF2000000; break;
IOCLR1 = P1_24TO31;
case 4 : IOSET1 = 0x66000000; break;
while(1) {
case 5 : IOSET1 = 0xB6000000; break;
if((IOPIN0 & P0_0) == 0) {
for(d = 0; d <= 1000; d++);
case 7 : IOSET1 = 0xE0000000; break;
if((IOPIN0 & P0_0) == 0) {
case 8 : IOSET1 = 0xFE000000; break;
Counter++;
if(Counter > 9) Counter = 0;
}
IOCLR1 = P1_24TO31;
}
case 6 : IOSET1 = 0xBE000000; break;
case 9 : IOSET1 = 0xF6000000; break;
} //end switch
} //end while
} //end main
Download