320 likes | 372 Vues
This lecture provides an introduction to GPIO inputs and how to interface them. It covers topics such as input ringing, debouncing, and configuring the GPIO pins. Lecture conducted by Professor John Devlin and Mr. Robert Ross.
E N D
Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Overview • Introduction to GPIO • Interfacing inputs as GPIO • Input ringing
Further Reading • “A guide to Debouncing” http://www.ganssle.com/debouncing.pdf • MSP430x2xx Family User’s Guide
Introduction to GPIO • GPIO stands for General Purpose Input/Output, and relates to the pins which can be configured as digital input or output pins • TI refer to GPIO as Digital I/O (Chapter 6 of the MSP430x2xx Family Users Guide) • The MSP430-2013 has 2 GPIO ports (P1 and P2) • P1 has 8 GPIO Pins • P2 has 2 GPIO Pins
Introduction to GPIO • All the pins can be individually configured as either Inputs or Outputs • All the pins can be configured as triggers for interrupts • All P1 pins share one interrupt vector (Flag: P1IFG), both P2 pins share a second interrupt vector (Flag: P2IFG) • Each pin can be individually configured with internal pull-up or pull-down resistors • Separate input and output registers (PxIN and PxOUT)
Configuring GPIO • This lecture focuses on using GPIO pins for input, next lecture will discuss using GPIO pins for output • Prior to use, pins must be configured as input or output – this is referred to as giving the pins a direction
Setting Direction • To set direction: • Write into PxDIR register the direction of all the pins • x will be 1 for Port1, and 2 for Port2 (ie. P1DIR and P2DIR) • ‘0’ = Input • ‘1’ = Output • Examples: • BIS.b #001h, &P1DIR ; Sets Pin 0 of P1 as an output • BIC.b #002h, &P2DIR ; Sets Pin 1 of P2 as an input • BIS.b #00101101b, &P1DIR ; Sets Pins 0, 2, 3 and 5 of P1 as outputs • BIC.b #11010010b, &P1DIR ; Sets Pins 1, 4, 6 and 7 of P1 as inputs • MOV.b #10000100b, &P1DIR; Sets Pins 7 and 2 as outputs, the rest as inputs
Pull-up / pull-down resistors Pull-up or pull-down resistors can be activated by setting bits in the PxREN registers, provided that the pin is con.gured as an input. The MCU behaves randomly if you forget this step because the inputs floats!!!!!;
Input readingpolling/interrupts A program can respond to inputs in two ways. For a simple analogy, suppose I am waitingfor my daughter to come home while writing this book. She has forgotten her key so Ineed to unlock the door for her. • I could go regularly to the door and look out to see whether she has returned, aftereach paragraph for instance. This is polling. • I could carry on writing continuously until I receive an interruptfrom the doorbell.I then fininish the sentence to leave the work in a well-defined state and go to thedoor.
Listing 4.5: Program butled1.c in C to light LED1 when button B1 is pressed. This version has a single loop containing a decision statement. // butled1.c - press button to light LED // Single loop with "if" // Olimex 1121 STK board , LED1 active low on P2.3, // button B1 active low on P2.1 // J H Davies , 2006 -06 -01; IAR Kickstart version 3.41A // ---------------------------------------------------------------------- #include <msp430x11x1.h> // Specific device // Pins for LED and button on port 2 #define LED1 BIT3 #define B1 BIT1 void main (void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P2OUT |= LED1; // Preload LED1 off (active low!) P2DIR = LED1; // Set pin with LED1 to output for (;;) { // Loop forever if ((P2IN & B1) == 0) { // Is button down? (active low) P2OUT &= LED1; // Yes: Turn LED1 on (active low!) } else { P2OUT |= LED1; // No: Turn LED1 off (active low!) } } }
Internal Pull Up/Down Resistor • To enable internal resistor • Write to PxREN register • ‘0’ = Disabled • ‘1’ = Enabled • Examples: • BIS.b #00101101b, &P1REN ; Enables internal resistor on pins 0, 2, 3 and 5 of P1 • BIC.b #11010010b, &P1REN ; Disables internal resistor on pins 1, 4, 6 and 7 of P1
Input function select • I/O pins are often multiplexed with special function peripherals (eg. ADC, DAC, PWM generator) • The user needs to specify if the pins are simply digital I/O or if the special function is required: • Write to PxSEL registers • ‘0’ = Digital I/O • ‘1’ = Special Function
Unused PINs • Unused pins should not be left floating – this can waste power and cause strange behaviour • TI recommends two different things can be done with unused pins: • Configure as Digital I/O, Output and don’t connect to anything • Configure as Digital I/O, Input, and enable the internal pull-up/down resistor
Reading Input Pins • The current state of input pins (0 or 1) can be read from the PxIN Registers • This is a read-only register, TI notes that attempting to write to this register will result in increased power consumption for no useful result • Remember ‘BIT’ Command is used to perform ‘AND’ updating the flags without writing to the dst register • Examples: • Check if Pin 0 on P1 is ‘1’: BIT #00000001b, &P1IN JNZ pin_1_high ; Jump if P1.0 was 1 • Check if Pin 4 on P1 is ‘0’: BIT #00010000b, &P1IN JZ pin_4_low ; Jump if P1.4 was 0
Switches - most common digital input to a microcontroller Switches should be connected to a pull-up resistor (so the input doesn’t float) The MSP430 has internal pull-up resistors which can be used instead Vcc MCU 10k Connecting inputs (Hardware) Vcc MSP 430
Switch Problems - Ringing • Switches are mechanical devices – a piece of metal is used to close a circuit • Unfortunately switches rarely close a circuit cleanly - the metal bounces, causing “ringing” • Can anyone see a problem with this?
Solutions to ringing • Hardware: Add additional hardware to ensure that the microprocessor only receives clean edges • Software: Develop an algorithm which detects transitions but discards ringing
10nF Vcc MCU 10k Debouncing - Hardware • A capacitor can be used to remove bouncing • Output is now more ramp-like (RC e-t/RC Charge/Discharge curves) • Digital devices prefer sharp transitions – not a slow curve
Vcc 10k 10nF Debouncing - Hardware • A Schmitt trigger can be used to convert the curve to have sharper edges • A Schmitt trigger will output a high when input is over a high threshold and a low when under a low threshold – in between they keep the same value • Schmitt triggers therefore exhibit hysteresis MCU
Debouncing - Software • Software needs to detect discrete switch presses, without detecting bounces • When switch is pressed, it should discriminate between a bounce and a valid button press.
Debouncing - Software • Algorithm: • When switched pressed start counting once per millisecond • If switch value = 0, reset counter • When counter = 20 (or so) the switch has been debounced
Do we always need to debounce? • Not necessarily • What does the system need to know – the time when a switch is first pressed, or the number of times a switch is pressed • Switches that might not need debouncing: • Game show buzzer • Ejector seat • TV remote control (sends same code until switch is not pressed)
Summary • GPIO Pins are General Purpose Input and Output pins • Before they are used they should be configured: • Direction (Input or Output) (PxDIR) • GPIO or Special Function (PxSEL) • Resistor enable (PxREN) • If pins are used to drive interrupts, several more registers need to be setup • Switches need to be debounced if the number of switch presses is important