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 # b, &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 # b, &P1OUT ; Sets Pin 1 to high BIC.b # b, &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, ; IAR Kickstart version 3.41A // #include // Specific device void main (void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P2DIR = 0x18; // Set pins with LEDs to output, 0b ( P2OUT = 0x08; // LED2 (P2.4) on, LED1 (P2.3) off (active low!) Στην C δεν //υπάρχει πρόβλημα να καθορίσουμε πρώτα την πόρτα σαν έξοδο!! for (;;) { // Loop forever... } //... doing nothing }
Προγραμματίζοντας ψηφιακές εξόδους (absolute assembly) #include ; 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 # b,& P2OUT ; LED2 (P2.4) on, LED1 (P2.3) off (active low!) mov.b # b,& 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 ; 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 # b,& P2OUT ; LED2 (P2.4) on, LED1 (P2.3) off (active low!) mov.b # b,& 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 # b, &P1DIR ; Set P1.0 as output BIS.b # b, &P1OUT ; Set output as 1 invert XOR.b # b, &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
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 # b, &P1DIR ; Set P1.0 as output BIS.b # b, &P1OUT ; Set output as 1 invert XOR.b # b, &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
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 # b, &P1DIR; Set P1.7 as input ; P1.0- P1.6 as outputs loop BIT.b # b, &P1IN JNZ led_off; If R4 = 1, button not pressed BIC.b # b, &P1OUT; Outputs Low, LED on JMP loop led_off BIS.b # b, &P1OUT; Outputs High, LED off JMP loop
Alternate Solution MOV.b # b, &P1DIR; Set P1.7 as input ; P1.0 - P1.6 as outputs loop MOV.b &P1IN, R4 MOV.b &P1OUT, R5 AND # b, R4; Mask out unwanted bits RRA R4; Move bit 7 to bit 2 RRA R4 BIS R4, R5; If 1 will turn off LED BIS # b, R4; Setup all other bits AND R4, R5; If 0 will turn on LED MOV.b R5, &P1OUT; Move to output JMP loop
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 R is a current limiting resistor V OL(MAX) (Max low voltage output from uP) - Typically 0.5V V F (LED forward voltage) – Typically V I F (LED forward current) – Typically 10-30mA MCU Vcc R
Driving LED’s Eg. –V CC = 5V –V OL(MAX) =0.2V –V F = 1.7V –I F =15mA R = 206Ω Can use either 180Ω or 220Ω MCU Vcc R
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 MCU R1 R2 12V
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 bi- directional 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