Title: Lecture 14: GPIO Outputs
1Lecture 14 GPIO Outputs
- Lecturers
- Professor John Devlin
- Mr Robert Ross
2Overview
- Driving GPIO Outputs
- Driving Higher loads
- Further Reading
- MSP430x2xx Family Users Guide
3Configuring GPIO as Outputs
- The previous lecture focused on using GPIO pins
as inputs - This lecture will discuss using GPIO pins as
outputs - Prior to use, pins must be configured as inputs
or outputs (given a direction) - As with inputs, write to the direction register
PxDIR the value 1 for each output pin - BIS.b 00101101b, P1DIR Sets Pins 0, 2, 3 and
5 of P1 as outputs
4Outputting data
- PxOUT registers are used to output data
- Writing a 1 will output a high to the pin,
writing a 0 will output a low to the pin. - Examples
- BIS.b 00000010b, P1OUT Sets Pin 1 to high
- BIC.b 00000001b, P1OUT Sets Pin 0 to low
5?????aµµat????ta? ??f?a??? e??d??? (se assembly)
- ?? ??????µe st?? p??ta p??? ?a????s??µe ?t? e??a?
???d?? ta ded?µ??a ap????e???ta? se buffer t??
p??ta? ?a? ?ta? ?a????s?e? ?t? e??a? ???d?? t?te
eµfa?????ta? ?? ?atast?se?? st??? a???d??te?!! - ??t? e??a? s?µa?t???!! ?? p??ta ?a?????aµe t??
?ate????s? sa? ???d? t?te ta ded?µ??a p?? ?ta?
?d? ap????e?µ??a ?a ep???a?a? t?? e??d???
?d????ta? se ap??ß?epte? ?atast?se??.
6?????aµµat????ta? ??f?a??? e??d??? (se C (Davies
p.71))
- Listing 4.2 Program ledson.c in C to light LEDs
with a constant pattern. - // ledson.c - simple program to light LEDs
- // Sets pins to output , lights pattern of LEDs ,
then loops forever - // Olimex 1121 STK board with LEDs active low on
P2.3,4 - // J H Davies , 2006 -05 -17 IAR Kickstart
version 3.41A - // -----------------------------------------------
----------------------- - include ltmsp430x11x1.hgt // Specific device
- void main (void)
-
- WDTCTL WDTPW WDTHOLD // Stop watchdog timer
- P2DIR 0x18 // Set pins with LEDs to output ,
0b00011000 ( - P2OUT 0x08 // LED2 (P2.4) on , LED1 (P2.3) off
(active low!) St?? C de? //?p???e? p??ß??µa ?a
?a????s??µe p??ta t?? p??ta sa? ???d?!! - for () // Loop forever ...
- // ... doing nothing
7?????aµµat????ta? ??f?a??? e??d??? (absolute
assembly)
- include ltmsp430x11x1.hgt Header file for this
device - ORG 0xF000 Start of 4KB flash memory
- Reset Execution starts here
- mov.w WDTPWWDTHOLD , WDTCTL Stop watchdog
timer - mov.b 00001000b, P2OUT
- LED2 (P2.4) on , LED1 (P2.3) off (active low!)
- mov.b 00011000b, P2DIR Set pins with LEDs to
output - InfLoop Loop forever ...
- jmp InfLoop ... doing nothing
- -------------------------------------------------
---------------------- - ORG 0xFFFE Address of MSP430 RESET Vector
- DW Reset
8?????aµµat????ta? ??f?a??? e??d??? (absolute
assembly)
- ?e t?? ?d???a ORG 0xF000 Start of 4KB flash
memory - ?a???????µe t?? ??????s? t?? µ??µ??
(ORGORGANIZATION) ??a t?? ap????e?s? t?? ??d??a
????????ta? ?t? ? ????? d?e????se?? t?? flash
µ??µ?? ?e???? ap? 0xF000
9?????aµµat????ta? ??f?a??? e??d??? (absolute
assembly)
- G?a ?a ?e????se? ? e?t??es? t?? ??d??a
???s?µ?p????µe ta e??? - ??????µe µ?a et???ta Reset st?? a??? t?? ??d??a
- ?e t?? ?d???a ORG 0xFFFE ?a? DW Reset ?a???????µe
?t? st? RESET Vector ?a ap????e??e? ? d?e????s?
t?? p??t?? e?t???? t?? ??d??a. ?ts? ?e???? ?
e?t??es? p?????µµat?? st?? MSP430
10?????aµµat????ta? ??f?a??? e??d??? (relocatable
assembly)
- include ltmsp430x11x1.hgt Header file for this
device - RSEG CODE Program goes in code memory
- Reset Execution starts here
- mov.w WDTPWWDTHOLD , WDTCTL Stop watchdog
timer - mov.b 00001000b, P2OUT
- LED2 (P2.4) on , LED1 (P2.3) off (active low!)
- mov.b 00011000b, P2DIR Set pins with LEDs to
output - InfLoop Loop forever ...
- jmp InfLoop ... doing nothing
- -------------------------------------------------
---------------------- - RSEG RESET Segment for reset vector
- DW Reset Address to start execution
- END
11?????aµµat????ta? ??f?a??? e??d??? (relocatable
assembly)
- The basic action of the linker is to group parts
of the program that use the same type of memory
into segments and allocate these to appropriate
addresses of the MCU. Here we use only two types
of memory, the executable code and the reset
vector, whose segments have obvious names. - The directive RSEG CODE tells the assembler that
the following instructions should be put in the
CODE segment, which the linker then puts at the
correct address in flash memory. RSEG stands for
relocatable segment, meaning that the address
is assigned by the linker (the alternative is
ASEG for absolute segment, in which case we
must provide the address). The relation between
segments and addresses is defined in the linker
control script, lnk430F1121A.xcl for this device.
12Example Square Wave
- Create a square wave on P1.0 (Port1, Bit 0) with
a duty cycle of 50 - MOV.b 00000001b, P1DIR Set P1.0 as output
- BIS.b 00000001b, P1OUT Set output as 1
- invert
- XOR.b 00000001b, P1OUT Inverts bit 0
- MOV 0, R4 Reset R4 (used as a counter)
- loop
- INC R4 Increment R4
- CMP 100, R4 Does R4 100
- JNE loop If R4 ! 100 keep counting
- JMP invert If R4 100, invert bit
NOTE Comments improve readability
13Example Square WaveMore efficient 1 less
instruction!
- Create a square wave on P1.0 (Port1, Bit 0) with
a duty cycle of 50 - MOV.b 00000001b, P1DIR Set P1.0 as output
- BIS.b 00000001b, P1OUT Set output as 1
- invert
- XOR.b 00000001b, P1OUT Inverts bit 0
- MOV 100, R4 Reset R4 (used as a counter)
- loop
- DEC R4 Decrement R4
- JNZ loop If R4 ! 0 keep counting
- JMP invert If R4 0, invert bit
NOTE Comments improve readability
14Combining Input and Output
- A switch is connected to P1.7
- A LED is connected to P1.2
- While switch is pressed turn LED on, when switch
is not pressed turn LED off
15Solution
- MOV.b 01111111b, P1DIR Set P1.7 as input
- P1.0- P1.6 as outputs
- loop
- BIT.b 10000000b, P1IN
- JNZ led_off If R4 1, button not pressed
- BIC.b 00000100b, P1OUT Outputs Low, LED on
- JMP loop
- led_off
- BIS.b 00000100b, P1OUT Outputs High, LED off
- JMP loop
16Alternate Solution
- MOV.b 01111111b, P1DIR Set P1.7 as input
- P1.0 - P1.6 as outputs
- loop
- MOV.b P1IN, R4
- MOV.b P1OUT, R5
- AND 10000000b, R4 Mask out unwanted bits
- RRA R4 Move bit 7 to bit 2
- RRA R4
- RRA R4
- RRA R4
- RRA R4
- BIS R4, R5 If 1 will turn off LED
- BIS 11111011b, R4 Setup all other bits
- AND R4, R5 If 0 will turn on LED
- MOV.b R5, P1OUT Move to output
- JMP loop
17Circuit of an Input/Output Pin(Davies p.212)
18Circuit of an Input/Output Pin(Davies p.212)
- The input protection diodes can cause a puzzling
side effect. Suppose that a logical high - input is applied to a circuit whose power supply
is not connected. Current flows through - the protection diode from the input to VCC, from
where it supplies the rest of the circuit. - Thus the circuit works almost normally, despite
having no apparent source of power.
19Circuit of an Input/Output Pin(Davies p.212)
20Output hardware
- Microprocessors typically only have small amount
of current they can sink (take in) or source
(supply) on the GPIO outputs - MSP430 12mA Source, 48mA Sink (across port)
- Low power LEDs can used in a sink arrangement
- Higher Power Loads
- Relays
- FETs
21Driving LEDs
- R is a current limiting resistor
- VOL(MAX) (Max low voltage output from uP) -
Typically 0.5V - VF (LED forward voltage) Typically 1.7-2.2V
- IF (LED forward current) Typically 10-30mA
22Driving LEDs
- Eg.
- VCC 5V
- VOL(MAX) 0.2V
- VF 1.7V
- IF 15mA
- R 206O
- Can use either 180O or 220O
23Relays
- Relays are electromagnetic switches
- They require higher current than microprocessors
can typically source and therefore need to be
driven with a transistor - Diode protects transistor from back EMF
24FETs
- Relays are slow, noisy acoustically and
sometimes electrically and are mechanical so
eventually wear out - FETs (Field Effect Transistors) have low on
resistance and can be used like switches
Microcontroller
25H-Bridges
- A useful application of FETs is in H-Bridges
- H-Bridges used for bi-directional motor control
(forward/reverse) - ROV motor controller
26Summary
- GPIO Pins can be configured as outputs by writing
a 1 to their PxDIR register - Outputs are controlled via the PxOUT registers
- Higher loads can be driven using Transistors,
FETs and Relays