lecture 13: gpio inputs lecturers: professor john devlin mr robert ross

32
Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Upload: dayna-jones

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Lecture 13: GPIO Inputs

Lecturers:Professor John Devlin

Mr Robert Ross

Page 2: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Overview

• Introduction to GPIO

• Interfacing inputs as GPIO

• Input ringing

Page 3: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Further Reading

• “A guide to Debouncing” http://www.ganssle.com/debouncing.pdf

• MSP430x2xx Family User’s Guide

Page 4: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 5: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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)

Page 6: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 7: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Page 8: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Page 9: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Page 10: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Page 11: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 12: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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 forgetthis step because the inputs floats!!!!!;

Pull-up / pull-down resistors

Page 13: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

A program can respond to inputs in two ways. For a simple analogy, suppose I am waiting for my daughter to come home while writing this book. She has forgotten her key so I need to unlock the door for her.• I could go regularly to the door and look out to see whether she has returned, after each paragraph for instance. This is polling.• I could carry on writing continuously until I receive an interrupt from the doorbell. I then fininish the sentence to leave the work in a well-defined state and go to the door.

Input readingpolling/interrupts

Page 14: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross
Page 15: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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 BIT1void main (void){WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timerP2OUT |= LED1; // Preload LED1 off (active low!)P2DIR = LED1; // Set pin with LED1 to outputfor (;;) { // Loop foreverif ((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!)}}}

Page 16: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 17: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 18: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 19: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Σύνδεση με πληκτρολόγιο 3x4

Page 20: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Σύνδεση με πληκτρολόγιο 3x4

Page 21: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Σύνδεση με πληκτρολόγιο 3x4

Page 22: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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, &P1INJNZ pin_1_high ; Jump if P1.0 was 1

– Check if Pin 4 on P1 is ‘0’:BIT #00010000b, &P1INJZ pin_4_low ; Jump if P1.4 was 0

Page 23: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Register Table

Page 24: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

Connecting inputs (Hardware)

• 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

MCU10k

Vcc

MSP430

Vcc

Page 25: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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?

Switch Bounce

0

1

2

3

4

5

6

0 100 200 300 400 500 600 700 800 900 1000

Time (us)

Vo

lta

ge

(V

)

Page 26: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 27: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Capacitor Switch Curve

0

1

2

3

4

5

6

0 100 200 300 400 500 600 700

Time (us)

Vo

ltag

e (V

)

MCU10k

Vcc

10nF

Page 28: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

MCU10k

Vcc

10nF

Debounce with Schmitt Trigger

0

1

2

3

4

5

6

0 100 200 300 400 500 600 700

Time (us)

Vo

lta

ge

(V

)

Input Voltage

Schmitt Trigger

Page 29: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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.

Page 30: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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

Page 31: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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)

Page 32: Lecture 13: GPIO Inputs Lecturers: Professor John Devlin Mr Robert Ross

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