28
Microcontroller System Design GPIO - 1 Seattle Pacific University Interfacing between the PSoC and the World Although the PSoC has lots of great stuff in it, it doesn’t do much by itself Interfacing with outside hardware allows the PSoC to: Get digital data from outside sources Query the state of pushbuttons and toggle switches Get analog data from outside sources Output digital data to displays and other devices Turn LEDs on and off Output analog data There are many methods of interfacing with outside hardware, but the simplest is General Purpose I/O

06-GPIO

Embed Size (px)

DESCRIPTION

gpio

Citation preview

EE 3270 chapter 1Interfacing between the PSoC and the World
Although the PSoC has lots of great stuff in it, it doesn’t do much by itself
Interfacing with outside hardware allows the PSoC to:
Get digital data from outside sources
Query the state of pushbuttons and toggle switches
Get analog data from outside sources
Output digital data to displays and other devices
Turn LEDs on and off
Output analog data
*
GPIO
General Purpose I/O refers to using individual port pins under program control
Output – CPU can set a pin high or low at any time
Input – CPU can read the value on a pin at any time
Example: To output a square wave on port2[0]
Set port2[0] low
29466 has three ports
Ports 0 and 2 have some capacity for analog I/O
Many pins have constraints for special-purpose use
See Part-# Datasheet for details
*
Basic output mode called “Strong Drive”
Basic input mode called “Hi-Z”
Port pins may be configured individually, with mixed inputs and outputs in the same port
Two methods:
Using ASM Language commands
For ALL the details, see TRM B-6.
*
Device Editor Pinout Window (Lower Left)
Select Drive Mode
*
Strong drive is the basic output mode
Write ‘0’ or ‘1’ to appropriate bit in PRTxDR
Value will be output until changed
Example: Write a ‘1’ to Port1[4]
Assume Port 1 has been configured so that pin[4] is Strong Drive and all others are Hi-Z
MOV REG[PRT1DR], %00010000
Other pins in port are ignored since they are Hi-Z (input)
Example: Write pattern “0101” to four low order bits of Port1
Assume Port 1 has been configured so that pins[3,2,1,0] are Strong Drive and others are Hi-Z
MOV REG[PRT1DR], %00000101
Hi-Z Digital is the basic input mode
Read PRTxDR
Example: Read Port1[2]
Assume Port 1 has been configured so that pin[2] is Hi-Z
MOV A, REG[PRT1DR] // read all pins of port 1
Other input pins will get the values currently on the pins, even if garbage
Output pins will get the value last written to them
Example: Loop as long as Port1[2] is low
LOOP: TST REG[PRT1DR],%00000100
JZ LOOP
*
PSoCs have 512 registers
Registers are arranged in two banks, each with 256 registers
REG[0] refers to two different registers, depending on which bank is selected
REG[0] = PRT0DR in bank 0, PRT0DM0 in bank 1
Bank selection is made with the XIO bit in the FLAG register
Bit 4 of Flag Register = XIO = Current Register Bank
Select Reg Bank 0: AND F, %11101111
Macro M8C_SetBank0
Macro M8C_SetBank1
Convention – Normally in Bank 0
If change to Bank 1, must change back to Bank 0 when done
MOV REG[PRT1DM2], %11011011
M8C_SetBank1 // set Bank 1
MOV REG[PRT1DM1], %11111011
MOV REG[PRT1DM0], %00000100
M8C_SetBank0 // set Bank 0
Bank zero register addresses listed as 0,xxh
0,01h – Bank 0, REG[01] (PRT0IE)
Bank one registers addresses listed as 1,xxh
1,03h – Bank 1, REG[03] (PRT0IC1)
Also, look at m8c.inc in “External Headers” of Application Editor in PSoC Designer
*
I/O Configuration using ASM Language
Drive mode registers select one of seven possible drive modes for each bit of each port
PRTxDM2 (bank 0), PRTxDM1 (bank 1), PRTxDM0 (bank 1) configure the bits for port x
1
1
0
1
1
0
0
1
0
1
1
0
1
1
0
0
0
1
1
1
0
1
1
0
Bank 0
Some devices use bi-directional pins
Memory data busses, for example
PSoC port pins can be dynamically reconfigured as inputs or outputs
MACRO Port3Input
M8C_SetBank0
ENDM
Useful to setup Macros to change the direction of a port on the fly
*
Strong Drive Mode
Hi-Z Mode
GND
VCC
Resistive Pull-Up
Resistive Pull-Down
WARNING:
If you are using this mode as input-only, make sure to write correct value to port!
VCC
Monitoring External Events - Polling
How does the CPU know if an external event has happened?
Button pushed down
Alarm signal
Sentries on watch
Waiting for someone
*
Connect a pushbutton to an input port
Make sure it is hooked up so it produces a ‘1’ when pressed. Use pull-down mode if needed.
Write a loop that checks if the port pin is ‘1’
Example: Button connected to Port1[3]
MOV REG[PRT1DR],0 // make sure input pin is pulled down
MOV [COUNT],0
LOOP: TST REG[PRT1DR], %00001000 // check if port1[3] is 1
JZ LOOP // not 1, keep checking (polling)
INC [COUNT] // button pressed – act on it
// output COUNT to LCD
*
Solution: After acting on button, “eat up” 1’s
MOV REG[PRT1DR],0 // make sure input pin is pulled down
MOV [COUNT],0
ZeroLOOP: TST REG[PRT1DR], %00001000 // check if port1[3] is 1
JZ LOOP // not 1, keep checking (polling)
INC [COUNT] // button pressed – act on it
// output COUNT to LCD
OneLOOP: TST REG[PRT1DR], %00001000 // check if port1[3] is 1
JNZ OneLOOP // poll to “eat up” excess 1’s
JMP LOOP
Produces a series of short pulses
A bouncy switch may cause multiple events to be triggered each time it is pressed
5.6K
VCC
Button
Solutions may involve hardware changes or software changes
Hardware:
Works well, but costs $$$, space and power
Software:
*
Prior
De-Bounced
Pushbutton
Use an RC circuit to slow down the rise time
Can also view as filtering out high-frequency response
Requires a carefully-sized capacitor, matched with the resistor ($)
Slows down response time
Software De-Bounce
Whenever a change in the input value is detected, ignore the input for some period of time afterward
Example: Delay 25ms after input changes
MOV REG[PRT1DR],0 // make sure input pin is pulled down
MOV [COUNT],0
ZeroLOOP: TST REG[PRT1DR], %00001000 // check if port1[3] is 1
JZ LOOP // not 1, keep checking (polling)
INC [COUNT] // button pressed – act on it
// output COUNT to LCD
CALL DELAY_25MS // ignore switch for 25ms
OneLOOP: TST REG[PRT1DR], %00001000 // check if port1[3] is 1
JNZ OneSLOOP // poll to “eat up” excess 1’s
CALL DELAY_25MS // ignore switch for 25ms
JMP LOOP
If the de-bounce delay is too long…
Response time may be impaired
May miss some real inputs
User will be unhappy
Some inputs will be counted twice
User will be unhappy
Switches may become bouncier as they age
Manufacturer may replace switches with a bouncier type
*
DELAY: MOV A, 255
Time delay depends on CPI – may change
Icky to compute just how long delay is
*
PSoC has several timers – one is the Sleep Timer
Sleep Timer designed as an alarm clock to wake up the system periodically. May be used as a general “alarm clock”
Sleep Timer counts “ticks” of its input clock
System calls allow delaying for a number of ticks
Input clock can be selected to:
1 Hz – 1 sec/tick
8 Hz – 125 ms/tick
64 Hz – 15.6 ms/tick
512 Hz – 1.95 ms/tick
*
ãSeattle Pacific University
Sleep Timer API
The sleep timer relies on interrupts for it operation (more on them later)
Code must enable interrupts both Globally (for the PSoC) and specifically for the Sleep Timer
Example code to delay approximately 25 ms
(25 ms / 1.95 ms is around 13 ticks)
_main:
call SleepTimer_SetInterval
Loop:
; Put main loop user code here
jmp Loop