CS 4101 General Purpose IO Prof ChungTa King

  • Slides: 15
Download presentation
CS 4101 嵌入式系統概論 General Purpose IO Prof. Chung-Ta King Department of Computer Science National

CS 4101 嵌入式系統概論 General Purpose IO Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan Materials from MSP 430 Microcontroller Basics, John H. Davies, Newnes, 2008 National Tsing Hua University

Launch. Pad Development Board USB Emulator Connection Embedded Emulation 6 -pin e. Z 430

Launch. Pad Development Board USB Emulator Connection Embedded Emulation 6 -pin e. Z 430 Connector Crystal Pads Chip Pinouts P 1. 3 Button LEDs P 1. 0 & P 1. 6 National Tsing Hua University Part and Socket Power Connector Reset Button 1

Recall: Sample Code for Output #include <msp 430. h> void main(void) { WDTCTL =

Recall: Sample Code for Output #include <msp 430. h> void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog P 1 DIR |= 0 x 41; // set P 1. 0 & 6 to outputs for(; ; ) { volatile unsigned int i; P 1 OUT ^= 0 x 41; // Toggle P 1. 0 & P 1. 6 i = 50000; // Delay do (i--); while (i != 0); } } National Tsing Hua University 2

Recall: Memory-Mapped I/O LED P 1 OUT P 1 IN … P 1 REN

Recall: Memory-Mapped I/O LED P 1 OUT P 1 IN … P 1 REN MOV. W #0 x 80, P 1 OUT National Tsing Hua University X No P 3 for 20 -pin devices 3

Configuring the I/O Ports Registers Functions Descriptions P 1 IN (0 x 0020) Port

Configuring the I/O Ports Registers Functions Descriptions P 1 IN (0 x 0020) Port 1 input This is a read-only register that reflects the current state of the port's pins. P 1 OUT (0 x 0021) Port 1 output The values written to this read/write register are driven out to corresponding pins when they are configured to output. P 1 DIR (0 x 0022) Port 1 data direction Bits written as 1 (0) configure the corresponding pins for output (input). P 1 SEL (0 x 0026) Port 1 function select Bits written as 1 (0) configure corresponding pins for use by the specialized peripheral (for general-purpose I/O). P 1 REN (0 x 0027) Port 1 resistor enable Bits set in this register enable pull-up/down resistors on the corresponding I/O pins. (Mem Addr) National Tsing Hua University 4

MSP 430 GPIO • GPIO = General Purpose Bit Input/Output • 8 -bit I/O

MSP 430 GPIO • GPIO = General Purpose Bit Input/Output • 8 -bit I/O ports • Each pin is individually controllable • Controlled by memory-mapped registers R 1 IN, R 1 OUT, … I/O Port 1 P 1. 7 P 1. 6 P 1. 5 P 1. 4 P 1. 3 P 1. 2 P 1. 1 P 1. 0 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 National Tsing Hua University 5

Px. DIR (Pin Direction): Input or Output P 1 IN. 7 P 1 OUT.

Px. DIR (Pin Direction): Input or Output P 1 IN. 7 P 1 OUT. 7 P 1 DIR. 7 7 “ 1” 6 5 4 3 2 1 0 P 1 IN P 1 OUT P 1 DIR 1 1 • Px. DIR. y: 0 = input 1 = output • Register example: P 1 DIR &= 0 x 81; National Tsing Hua University 6

GPIO Output P 1 IN. 7 P 1 OUT. 7 P 1 DIR. 7

GPIO Output P 1 IN. 7 P 1 OUT. 7 P 1 DIR. 7 7 P 1 IN X P 1 OUT 1 P 1 DIR 1 “ 1” 6 5 4 3 2 1 0 • Px. OUT. y: 0 = low 1 = high • Register example: P 1 OUT &= 0 x 80; National Tsing Hua University 7

GPIO Input P 1 IN. 7 P 1 OUT. 7 “ 1” P 1

GPIO Input P 1 IN. 7 P 1 OUT. 7 “ 1” P 1 DIR. 7 7 P 1 IN 1 P 1 OUT X P 1 DIR 0 “ 1” “ 0” 6 5 4 3 2 1 0 • Problem occurs if P 1. 7 is connected to a button! National Tsing Hua University 8

Problem with Input from a Button • When the button is pressed down (closed),

Problem with Input from a Button • When the button is pressed down (closed), MSP 430 will detect a 0 from the pin • When the button is up (open), what will MSP 430 detect? Ans. : random value Floating National Tsing Hua University 9

Typical Way of Connecting a Button • The pull-up resistor Rpull holds input at

Typical Way of Connecting a Button • The pull-up resistor Rpull holds input at logic 1 (voltage VCC) while button is up and logic 0 or VSS when button is down active low - A wasted current flows through Rpull to ground when button is down - This is reduced by making Rpull large enough, typically 33 k National Tsing Hua University 10

Typical Way of Connecting a Button • MSP 430 offers internal pull-up/down resistor •

Typical Way of Connecting a Button • MSP 430 offers internal pull-up/down resistor • Px. REN register selects whether this resistor is used (1 to enable, 0 to disable) - When enabled, the corresponding bit of Px. OUT register selects whether the resistor pulls the input up to VCC (1) or down to VSS (0) National Tsing Hua University 11

GPIO Input P 1 IN. 7 P 1 OUT. 7 wn up/do P 1

GPIO Input P 1 IN. 7 P 1 OUT. 7 wn up/do P 1 DIR. 7 P 1 REN. 7 Enable resistor 7 l P 1 IN x P 1 OUT 1 l P 1 DIR 0 l P 1 REN 1 l l Input pins are held in high-impedance (Hi-Z) state, so 6 5 4 3 2 1 0 they can react to 0 or 1 When not driven, Hi-Z inputs may float up/down … Prevent this with pull-up/pull-down resistors Px. REN enables pull-up/pull-down resistors Px. OUT selects pull-up (1) or -down (0) Lower cost devices may not provide up/down resistors for all ports National Tsing Hua University 12

Sample Code for Input #include <msp 430. h> #define LED 1 BIT 0 //P

Sample Code for Input #include <msp 430. h> #define LED 1 BIT 0 //P 1. 0 to red LED #define B 1 BIT 3 //P 1. 3 to button void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer P 1 OUT |= LED 1 + B 1; P 1 DIR = LED 1; //Set pin with LED 1 to output P 1 REN = B 1; //Set pin to use pull-up resistor for(; ; ){ //Loop forever if((P 1 IN & B 1) ==0){ //Is button down? P 1 OUT &= ~LED 1; // Yes, turn LED 1 off } else{ P 1 OUT |= LED 1; // No, turn LED 1 on } } } National Tsing Hua University 13

Sample Code 2 for Input #include <msp 430. h> #define LED 1 BIT 6

Sample Code 2 for Input #include <msp 430. h> #define LED 1 BIT 6 // P 1. 0 to green LED #define B 1 BIT 3 // P 1. 3 to button volatile unsigned int i, j; void main(void){ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P 1 OUT |= LED 1 + B 1; P 1 DIR = LED 1; // Set pin with LED 1 to output P 1 REN = B 1; // Set pin to use pull-up resistor for(; ; ){ while((P 1 IN & B 1) != 0){ // Loop on button up i = P 1 IN; j = P 1 OUT; } P 1 OUT &= ~LED 1; // Turn LED 1 off while((P 1 IN & B 1) == 0){ // Loop on button down i = P 1 IN; j = P 1 OUT; } P 1 OUT |= LED 1; // Turn LED 1 on } } National Tsing Hua University 14