Title: Other pins in port are ignored since they are Hi-Z (input
1Interfacing between the PSoC and the World
- Although the PSoC has lots of great stuff in it,
it doesnt 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
2GPIO
- 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 port20
- Set port20 low
- Pause
- Set port20 high
- Pause
- Repeat
3GPIO Ports
- The PSoC has up to eight eight-bit ports
- 29466 has three ports
- Port0 Port2
- 29566 has five ports
- Port0 Port4
- 29666 has six ports
- Port0 Port5
- 29866 has eight ports
- Port0 Port7
429466 Ports
- Three ports
- All can be used for digital I/O
- Ports 0 and 2 have some capacity for analog I/O
- Many pins have constraints for special-purpose
use - See Part- Datasheet for details
5GPIO Configuration
- The first step is to configure the port pins
- 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 PSoC Designer
- Easy, static (doesnt change once set)
- Using ASM Language commands
- Slightly harder
- Can change mode any time under program control
- For ALL the details, see TRM B-6.
6I/O Configuration using PSoC Designer
Device Editor Pinout Window (Lower Left)
Default is High-ZAnalog (input)
StdCPU means portread/written by CPU
Can rename portfor ease of use
Select Drive Modefor each port pin Strong
Output Hi-Z Input
Details on other drive modes, connections, and
interrupts later
7Basic Output using GPIO
- First, configure port pin to an output 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 Port14
- Assume Port 1 has been configured so that pin4
is Strong Drive and all others are Hi-Z - MOV REGPRT1DR, 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
pins3,2,1,0 are Strong Drive and others are
Hi-Z - MOV REGPRT1DR, 00000101
8Basic Input using GPIO
- First, configure port pin to an input mode
- Hi-Z Digital is the basic input mode
- Read PRTxDR
- Values of all port pins are copied to destination
- Example Read Port12
- Assume Port 1 has been configured so that pin2
is Hi-Z - MOV A, REGPRT1DR // 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 Port12 is low
- LOOP TST REGPRT1DR,00000100 JZ
LOOP
Warning Hi-Z digital works well for inputs that
produce nice 0s and 1s. Pushbuttons dont do
this and require a different mode. See slides on
Port Modes below.
9A word or two about Register Banks
- PSoCs have 512 registers
- Addressed by an 8-bit address (!)
- Registers are arranged in two banks, each with
256 registers - REG0 refers to two different registers,
depending on which bank is selected - REG0 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
- Select Reg Bank 1 OR F, 00010000
- Macro M8C_SetBank1
- Convention Normally in Bank 0
- If change to Bank 1, must change back to Bank 0
when done
MOV REGPRT1DM2, 11011011M8C_SetBank1 // set
Bank 1MOV REGPRT1DM1, 11111011MOV
REGPRT1DM0, 00000100M8C_SetBank0 // set Bank 0
10Register References
- All registers are described in TRM section C-13
- Bank zero register addresses listed as 0,xxh
- 0,01h Bank 0, REG01 (PRT0IE)
- Bank one registers addresses listed as 1,xxh
- 1,03h Bank 1, REG03 (PRT0IC1)
- Also, look at m8c.inc in External Headers of
Application Editor in PSoC Designer - Browse around this file a bit to find useful
macros
11I/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
- Example Set Port15 to Hi-Z (input) and
Port12 to Strong Drive (output)
Bank 0
1
1
0
1
1
0
1
1
1
1
1
1
1
0
1
1
0
0
0
0
0
1
0
0
Bank1
M8C_SetBank0MOV REGPRT1DM2, 11011011M8C_SetBa
nk1MOV REGPRT1DM1, 11111011MOV REGPRT1DM0,
00000100M8C_SetBank0
DM2 Hi-Z (digital input)
DM6 Hi-Z Analog (default)
DM6 Hi-Z Analog (default)
DM6 Hi-Z Analog (default)
DM6 Hi-Z Analog (default)
DM6 Hi-Z Analog (default)
DM6 Hi-Z Analog (default)
DM1 Strong Drive (output)
12Bi-Directional Ports
- Some devices use bi-directional pins
- Memory data busses, for example
- PSoC port pins can be dynamically reconfigured as
inputs or outputs
- Useful to setup Macros to change the direction of
a port on the fly
MACRO Port3InputM8C_SetBank0 MOV REGPRT3DM2,
00000000M8C_SetBank1MOV REGPRT3DM1,
11111111MOV REGPRT3DM0, 00000000M8C_SetBank0
ENDM
MACRO Port3OutputM8C_SetBank0 MOV REGPRT3DM2,
00000000M8C_SetBank1MOV REGPRT3DM1,
00000000MOV REGPRT3DM0, 11111111M8C_SetBank0
ENDM
- Can simply use Port3Input or Port3Output in
source code now
13Port Modes Strong and Slow Strong
Slow Strong Drive Mode
Strong Drive Mode
Controlled rise/fall times.Rise/Fall times
10-25ns. Saves power
Normal output mode. Works like an
inverter. Rise/Fall times 3ns-18ns
14Port Modes Hi-Z and Analog Hi-Z
Hi-Z Mode
Analog Hi-Z Mode
Digital Input Mode
Analog Input Mode. Select when port is
unusedsince it consumes no power.
15Port Modes Resistive Pull-Up and Pull-Down
Resistive Pull-Down
Resistive Pull-Up
Effective Circuit if written with 0
Effective Circuit if written with 1
WARNINGIf you are using this mode as
input-only, make sure to write correct value to
port!
16Switches and Pushbutton Inputs
SPST ButtonProduces either Vccor Floating
input.
Adding a pull-downresistor fixes it.
PSoC port inPull-Down mode
Dont ForgetWrite 0 to port
17Monitoring External Events - Polling
- How does the CPU know if an external event has
happened? - Button pushed down
- Incoming signal on a line
- Alarm signal
- Etc.
- How do people do this? Polling repeated
checking. - Sentries on watch
- Waiting for someone
- Waiting for class to end
18Detecting a Pushbutton Input
- 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 Port13
- MOV REGPRT1DR,0 // make sure input pin is
pulled down MOV COUNT,0LOOP TST
REGPRT1DR, 00001000 // check if port13 is 1
JZ LOOP // not 1, keep
checking (polling) INC COUNT // button
pressed act on it // output COUNT to
LCD JMP LOOP
Will increment multipletimes for each button
press
19Multiple Counts per Press
Displays 1
Displays 2
- Solution After acting on button, eat up 1s
- MOV REGPRT1DR,0 // make sure input pin is
pulled down MOV COUNT,0ZeroLOOP TST
REGPRT1DR, 00001000 // check if port13 is 1
JZ LOOP // not 1, keep
checking (polling) INC COUNT // button
pressed act on it // output COUNT to
LCDOneLOOP TST REGPRT1DR, 00001000 // check
if port13 is 1 JNZ OneLOOP // poll to eat
up excess 1s JMP LOOP
20Bouncing Switches
time
Pressed
Released
- Switches are mechanical objects
- Contacts will bounce up and down when moved
- Produces a series of short pulses
- A bouncy switch may cause multiple events to be
triggered each time it is pressed
21De-bouncing
- De-bouncing switches is critical to most systems
- Solutions may involve hardware changes or
software changes - Hardware
- Build hardware that gets rid of the bounce
- Works well, but costs , space and power
- Software
- Write software to ignore bounces
- Cheap, but harder to fine-tune
22Hardware De-Bounce with R-S Latch
Prior
De-Bounced
23Hardware De-Bounce with RC Circuit
Prior
De-Bounced
- 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
24Software 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
REGPRT1DR,0 // make sure input pin is pulled
down MOV COUNT,0ZeroLOOP TST REGPRT1DR,
00001000 // check if port13 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
25msOneLOOP TST REGPRT1DR, 00001000 //
check if port13 is 1 JNZ OneSLOOP // poll to
eat up excess 1s CALL DELAY_25MS //
ignore switch for 25ms JMP LOOP
25Design Issues with Software De-Bounce
- If the de-bounce delay is too long
- Response time may be impaired
- May miss some real inputs
- User will be unhappy
- If the de-bounce delay is too short
- Some inputs will be counted twice
- User will be unhappy
- Always make de-bounce delay as long as is
tolerable - Switches may become bouncier as they age
- Manufacturer may replace switches with a bouncier
type
26How to make a time delay
- First solution make a long loopDELAY MOV A,
255 NOP NOP DEC A JNZ DELAY RET - Problems
- Time delay depends on clock speed may change
- Time delay depends on CPI may change
- Icky to compute just how long delay is
27How to make a time delay
- Second solution use a built-in timer
- 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 to1 Hz 1
sec/tick8 Hz 125 ms/tick64 Hz 15.6
ms/tick512 Hz 1.95 ms/tick - SleepTimer_TickWait(ticks) delays for a number of
ticks
28Sleep 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
M8C_EnableGInt Turn on interrupts call
SleepTimer_Start Start SleepTimer mov A,
SleepTimer_512_HZ Use 512 Hz interrupt rate
call SleepTimer_SetInterval call
SleepTimer_EnableInt Enable SleepTimer
Interrupt Loop mov A,13 Set tick delay
to 13 ticks lcall SleepTimer_TickWait Delay
for 13 ticks (25 ms) Put main loop user code
here jmp Loop