0% found this document useful (0 votes)
36 views28 pages

Interfacing Between The Psoc and The World

The document discusses interfacing the PSoC microcontroller with external hardware using general purpose input/output (GPIO). GPIO allows the PSoC to get digital and analog data from outside sources, output data to displays and devices, and control outputs like LEDs. The simplest method of interfacing is through GPIO pins, which can be configured as inputs or outputs under program control to read from or write to external devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views28 pages

Interfacing Between The Psoc and The World

The document discusses interfacing the PSoC microcontroller with external hardware using general purpose input/output (GPIO). GPIO allows the PSoC to get digital and analog data from outside sources, output data to displays and devices, and control outputs like LEDs. The simplest method of interfacing is through GPIO pins, which can be configured as inputs or outputs under program control to read from or write to external devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Interfacing 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

Seattle Pacific University

GPIO - 1
Microcontroller System Design

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
Pause
Set port2[0] high
Pause
Repeat

Seattle Pacific University

GPIO - 2
Microcontroller System Design

GPIO 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

Seattle Pacific University

GPIO - 3
Microcontroller System Design

29466 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

Seattle Pacific University

GPIO - 4
Microcontroller System Design

GPIO 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.

Seattle Pacific University

GPIO - 5
Microcontroller System Design

I/O Configuration using PSoC Designer


Device Editor Pinout Window (Lower Left)

Default is High-Z
Analog (input)
StdCPU means port
read/written by CPU

Can rename port


for ease of use
Select Drive Mode
for each port pin
Strong: Output
Hi-Z: Input
Details on other drive modes,
connections, and interrupts later

Seattle Pacific University

GPIO - 6
Microcontroller System Design

Basic 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 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

Seattle Pacific University

GPIO - 7
Microcontroller System Design

Basic 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 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
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.

Seattle Pacific University

GPIO - 8
Microcontroller System Design

A 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
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
MOV REG[PRT1DM2], %11011011
Macro M8C_SetBank0
M8C_SetBank1 // set Bank 1
Select Reg Bank 1: OR F, %00010000 MOV REG[PRT1DM1], %11111011
MOV REG[PRT1DM0], %00000100
Macro M8C_SetBank1
M8C_SetBank0 // set Bank 0
Convention Normally in Bank 0
If change to Bank 1, must change back to Bank 0 when done

Seattle Pacific University

GPIO - 9
Microcontroller System Design

Register References
All registers are described in TRM section C-13
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
Browse around this file a bit to find useful macros

Seattle Pacific University

GPIO - 10
Microcontroller System Design

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

Example: Set Port1[5] to Hi-Z (input)


and Port1[2] to Strong Drive (output)
5
0
1
0

4
1
1
0

3
1
1
0

1
1
1
0

0
1
1
0
=H
i-Z

i-Z

g
alo
An

g
alo
An

fau
( de
lt)

lt)

t)

lt)

lt)

lt)

lt)

tpu

fau
( de

(ou

fau
( de

rive

g
alo
An

t)

fau
( de

pu

fau
( de

fau
( de

l in

g
alo
An

a
git
(di

g
alo
An

g
alo
An

D
ng

i-Z

i-Z

i-Z

i-Z

i-Z

tro

=H

=S

=H

=H

=H

=H

=H

Seattle Pacific University

2
0
0
1

6
DM

6
DM

6
DM

2
DM

6
DM

6
DM

Bank1

6
1
1
0

6
DM

7
PRT1DM2 1
PRT1DM1 1
PRT1DM0 0

1
DM

Bank 0

M8C_SetBank0
MOV REG[PRT1DM2], %11011011
M8C_SetBank1
MOV REG[PRT1DM1], %11111011
MOV REG[PRT1DM0], %00000100
M8C_SetBank0

GPIO - 11
Microcontroller System Design

Bi-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 Port3Input
M8C_SetBank0
MOV REG[PRT3DM2], %00000000
M8C_SetBank1
MOV REG[PRT3DM1], %11111111
MOV REG[PRT3DM0], %00000000
M8C_SetBank0
ENDM

MACRO Port3Output
M8C_SetBank0
MOV REG[PRT3DM2], %00000000
M8C_SetBank1
MOV REG[PRT3DM1], %00000000
MOV REG[PRT3DM0], %11111111
M8C_SetBank0
ENDM

Can simply use Port3Input or Port3Output in source code now

Seattle Pacific University

GPIO - 12
Microcontroller System Design

Port Modes: Strong and Slow Strong


VCC

VCC
To PSoC
Input Logic

To PSoC
Input Logic

Port Pin

Port Pin
From
PSoC
Core

From
PSoC
Core
GND

GND
Slow Strong Drive Mode

Strong Drive Mode


Normal output mode.
Works like an inverter.
Rise/Fall times 3ns-18ns

Seattle Pacific University

Controlled rise/fall times.


Rise/Fall times 10-25ns.
Saves power

GPIO - 13
Microcontroller System Design

Port Modes: Hi-Z and Analog Hi-Z


VCC

VCC
To PSoC
Digital Input Logic

To PSoC
Analog Input Logic

Port Pin

GND
Hi-Z Mode
Digital Input Mode

Seattle Pacific University

Port Pin

GND
Analog Hi-Z Mode
Analog Input Mode.
Select when port is unused
since it consumes no power.

GPIO - 14
Microcontroller System Design

Port Modes: Resistive Pull-Up and Pull-Down


VCC

5.6K

VCC
To PSoC
Input Logic

To PSoC
Input Logic

Port Pin

Port Pin

From
PSoC
Core

5.6K

GND

From
PSoC
Core

Resistive Pull-Up

GND
Resistive Pull-Down
Effective Circuit if written with 0

Effective Circuit if written with 1

VCC
5.6K

To PSoC
Input Logic
Port Pin

Seattle Pacific University

WARNING:
If you are using this
mode as input-only,
make sure to write
correct value to port!

To PSoC
Input Logic
Port Pin
5.6K

GPIO - 15
Microcontroller System Design

Switches and Pushbutton Inputs


VCC

SPST Button:
Produces either Vcc
or Floating input.

Button

VCC

To PSoC
Input Logic

Button

Port Pin

5.6K

Adding a pull-down
resistor fixes it.

5.6K

PSoC port in
Pull-Down mode
Dont Forget:
Write 0 to port

Seattle Pacific University

GPIO - 16
Microcontroller System Design

Monitoring 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

Seattle Pacific University

GPIO - 17
Microcontroller System Design

Detecting 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 Port1[3]


MOV REG[PRT1DR],0 // make sure input pin is pulled
down
MOV [COUNT],0
LOOP:
1

TST REG[PRT1DR], %00001000 // check if port1[3] is


JZ LOOP

// not 1, keep checking (polling)


Will increment multiple

INC [COUNT] // button pressed act on it


times for each button press
// output COUNT to LCD
JMP LOOP
GPIO - 18
Seattle Pacific University
Microcontroller System Design

Multiple Counts per Press


time
Pressed
Released

Inc
COUNT

Display
COUNT

Poll
for 1

Displays 1

Inc
COUNT

Display
COUNT

Poll
for 1

Displays 2

Solution: After acting on button, eat up 1s


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 1s
JMP LOOP

Seattle Pacific University

GPIO - 19
Microcontroller System Design

Bouncing Switches
VCC

time

Button

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

Seattle Pacific University

GPIO - 20
Microcontroller System Design

5.6K

De-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

Seattle Pacific University

GPIO - 21
Microcontroller System Design

Hardware De-Bounce with R-S Latch


Prior
De-Bounced

Pushbutton
Single-Pole Double-Throw (SPDT) Switch

Input
+5

GND

GND

Seattle Pacific University

De-Bounced
Output
S

GPIO - 22
Microcontroller System Design

Hardware De-Bounce with RC Circuit


Prior

De-Bounced

VCC

Button

Use an RC circuit to slow down the


5.6K

Seattle Pacific University

rise time
Can also view as filtering out highfrequency response
Requires a carefully-sized capacitor,
matched with the resistor ($)
Slows down response time

GPIO - 23
Microcontroller System Design

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 1s
CALL DELAY_25MS // ignore switch for 25ms
JMP LOOP

25ms

Seattle Pacific University

25ms

GPIO - 24
Microcontroller System Design

Design 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
Seattle Pacific University

GPIO - 25
Microcontroller System Design

How to make a time delay


First solution make a long loop
DELAY:

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

Seattle Pacific University

GPIO - 26
Microcontroller System Design

How 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 to:
1 Hz 1 sec/tick
8 Hz 125 ms/tick
64 Hz 15.6 ms/tick
512 Hz 1.95 ms/tick

SleepTimer_TickWait(ticks) delays for a number of


ticks
Seattle Pacific University

GPIO - 27
Microcontroller System Design

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:
M8C_EnableGInt
call SleepTimer_Start
mov A, SleepTimer_512_HZ
call SleepTimer_SetInterval
call SleepTimer_EnableInt

; Turn on interrupts
; Start SleepTimer
; Use 512 Hz interrupt rate

mov A,13
lcall SleepTimer_TickWait
; Put main loop user code here
jmp Loop

; Set tick delay to 13 ticks


; Delay for 13 ticks (25 ms)

; Enable SleepTimer Interrupt

Loop:

Seattle Pacific University

GPIO - 28
Microcontroller System Design

You might also like