Download Slide 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Pulse-width modulation wikipedia , lookup

Rotary encoder wikipedia , lookup

Power inverter wikipedia , lookup

Control system wikipedia , lookup

Rectifier wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Power electronics wikipedia , lookup

Two-port network wikipedia , lookup

Buck converter wikipedia , lookup

Flip-flop (electronics) wikipedia , lookup

Wien bridge oscillator wikipedia , lookup

Schmitt trigger wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Surge protector wikipedia , lookup

Current mirror wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Lecture 14: GPIO Outputs
Lecturers:
Professor John Devlin
Mr Robert Ross
Overview
• Driving GPIO Outputs
• Driving Higher loads
Further Reading:
• MSP430x2xx Family User’s Guide
Configuring 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
Outputting 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
Προγραμματίζοντας ψηφιακές
εξόδους (σε assembly)
• Αν γράψουμε στην πόρτα πριν καθορίσουμε ότι
είναι έξοδος τα δεδομένα αποθηκεύονται σε
buffer της πόρτας και όταν καθορισθεί ότι είναι
έξοδος τότε εμφανίζονται οι καταστάσεις στους
ακροδέκτες!!
• Αυτό είναι σημαντικό!! Αν πρώτα καθορίζαμε την
κατεύθυνση σαν έξοδο τότε τα δεδομένα που
ήταν ήδη αποθηκευμένα θα επηρέαζαν τις
εξόδους οδηγώντας σε απρόβλεπτες
καταστάσεις.
Προγραμματίζοντας ψηφιακές εξόδους
(σε 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 <msp430x11x1.h> // 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!) Στην C δεν
//υπάρχει πρόβλημα να καθορίσουμε πρώτα την πόρτα σαν έξοδο!!
for (;;) { // Loop forever ...
} // ... doing nothing
}
Προγραμματίζοντας ψηφιακές εξόδους
(absolute assembly)
#include <msp430x11x1.h> ; Header file for this device
ORG 0xF000 ; Start of 4KB flash memory
Reset: ; Execution starts here
mov.w #WDTPW|WDTHOLD ,& 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
Προγραμματίζοντας ψηφιακές εξόδους
(absolute assembly)
• Με την οδηγία ORG 0xF000 ; Start of
4KB flash memory
καθορίζουμε την οργάνωση της μνήμης
(ORG=ORGANIZATION) για την
αποθήκευση του κώδικα γνωρίζοντας
ότι ο χώρος διευθύνσεων της flash
μνήμης ξεκινά από 0xF000
Προγραμματίζοντας ψηφιακές εξόδους
(absolute assembly)
Για να ξεκινήσει η εκτέλεση του κώδικα
χρησιμοποιούμε τα εξής:
• Ορίζουμε μια ετικέτα Reset στην αρχή του
κώδικα
• Με την οδηγία ORG 0xFFFE και DW
Reset καθορίζουμε ότι στο RESET Vector
θα αποθηκευθεί η διεύθυνση της πρώτης
εντολής του κώδικα. Έτσι ξεκινά η
εκτέλεση προγράμματος στον MSP430
Προγραμματίζοντας ψηφιακές εξόδους
(relocatable assembly)
#include <msp430x11x1.h> ; Header file for this device
RSEG CODE ; Program goes in code memory
Reset: ; Execution starts here
mov.w #WDTPW|WDTHOLD ,& 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
Προγραμματίζοντας ψηφιακές εξόδους
(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.
Example – Square Wave
• Create a square wave on P1.0 (Port1, Bit 0) with
a duty cycle of 50%
MOV.b #00000001b, &P1DIR
BIS.b #00000001b, &P1OUT
invert
XOR.b #00000001b, &P1OUT
MOV #0, R4
loop
INC R4
CMP #100, R4
JNE loop
JMP invert
NOTE: Comments
improve readability
; Set P1.0 as output
; Set output as 1
; Inverts bit 0
; Reset R4 (used as a counter)
; Increment R4
; Does R4 = 100
; If R4 != 100 keep counting
; If R4 = 100, invert bit
Example – Square Wave
More efficient – 1 less instruction!
• Create a square wave on P1.0 (Port1, Bit 0) with
a duty cycle of 50%
MOV.b #00000001b, &P1DIR
BIS.b #00000001b, &P1OUT
invert
XOR.b #00000001b, &P1OUT
MOV #100, R4
loop
DEC R4
JNZ loop
JMP invert
NOTE: Comments
improve readability
; Set P1.0 as output
; Set output as 1
; Inverts bit 0
; Reset R4 (used as a counter)
; Decrement R4
; If R4 != 0 keep counting
; If R4 = 0, invert bit
Combining 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
Solution
MOV.b #01111111b, &P1DIR
; Set P1.7 as input
; P1.0- P1.6 as outputs
loop
BIT.b #10000000b, &P1IN
JNZ led_off
BIC.b #00000100b, &P1OUT
JMP loop
led_off
BIS.b #00000100b, &P1OUT
JMP loop
; If R4 = 1, button not pressed
; Outputs Low, LED on
; Outputs High, LED off
Alternate 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
RRA R4
RRA R4
RRA R4
RRA R4
RRA R4
BIS R4, R5
BIS #11111011b, R4
AND R4, R5
MOV.b R5, &P1OUT
JMP loop
; Mask out unwanted bits
; Move bit 7 to bit 2
; If 1 will turn off LED
; Setup all other bits
; If 0 will turn on LED
; Move to output
Circuit of an Input/Output Pin
(Davies p.212)
Circuit 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.
Circuit of an Input/Output Pin
(Davies p.212)
Output 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 LED’s can used in a sink
arrangement
• Higher Power Loads
– Relays
– FET’s
Driving LED’s
Vcc
• R is a current limiting
resistor
• VOL(MAX) (Max low
voltage output from uP)
- Typically 0.5V
• VF (LED forward
voltage) – Typically 1.72.2V
• IF (LED forward current)
– Typically 10-30mA
R
MCU
R
(VCC  VOL( MAX ) )  VF
IF
Driving LED’s
Vcc
• Eg.
–
–
–
–
R
VCC = 5V
VOL(MAX) =0.2V
VF = 1.7V
IF =15mA
• R = 206Ω
• Can use either 180Ω
or 220Ω
MCU
R
( VCC  VOL( MAX ) )  VF
IF
Relays
• 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
12V
MCU
R1
R2
FET’s
• 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
H-Bridges
• A useful application of
FETs is in H-Bridges
• H-Bridges used for bidirectional motor control
(forward/reverse)
• ROV motor controller
Summary
• 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, FET’s and Relays