Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB 2: Basic IO Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.

Similar presentations


Presentation on theme: "LAB 2: Basic IO Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems."— Presentation transcript:

1 LAB 2: Basic IO Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems

2 Introduction In this lab, we will learn the basic IO of MSP430 LanuchPad – Configuring the I/O port of LanuchPad for output – Configuring the I/O port of LanuchPad for input

3 Part and Socket Crystal Pads Power Connector Reset Button P1.3 Button Chip Pinouts USB Emulator Connection LEDs and Jumpers P1.0 & P1.6 6-pin eZ430 Connector Embedded Emulation LaunchPad Development Board

4 4 Recall: Sample Code for Output #include void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog P1DIR |= 0x41; // set P1.0 & 6 to outputs for(;;) { volatile unsigned int i; P1OUT ^= 0x41; // Toggle P1.0 & 6 i = 50000; // Delay do (i--); while (i != 0); }

5 5 Recall: Memory-Mapped IO P1REN … P1IN P1OUT sw r3,P1

6 Registers (Mem Addr) FunctionsDescriptions P1IN (0x0020) Port 1 inputThis is a read-only register that reflects the current state of the port's pins. P1OUT (0x0021) Port 1 outputThe values written to this read/write register are driven out to corresponding pins when they are configured to output. P1DIR (0x0022) Port 1 data direction Bits written as 1 (0) configure the corresponding pins for output (input). P1SEL (0x0026) Port 1 function select Bits written as 1 (0) configure corresponding pins for use by the specialized peripheral (for general-purpose I/O). P1REN (0x0027) Port 1 resistor enable Bits set in this register enable pull-up/down resistors on the corresponding I/O pins. Configuring the I/O Ports

7 Problem with Input Using a Switch When the switch is down (closed), MSP430 will detect a 0 from the pin. When the switch is up (open), what will MSP430 detect? Ans.: random value  Floating

8 Typical Way of Connecting a Button The pull-up resistor R pull holds input at logic 1 (voltage V CC ) while button is up and logic 0 or V SS when button is down  active low – A wasted current flows through R pull to ground when button is pressed – This is reduced by making R pull large enough, typically 33 k 

9 Typical Way of Connecting a Button MSP430 offers internal pull-up/down. PxREN register selects whether this resistor is used (1 to enable, 0 to disable) – When enabled, the corresponding bit of PxOUT register selects whether the resistor pulls the input up to V CC (1) or down to V SS (0).

10 Sample Code 1 for Input #include // Pins for LED and button on port 1 #define LED1 BIT0 //P1.0 to red LED #define B1 BIT3 //P1.3 to button void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer P1OUT |= LED1; //Preload LED1 P1DIR = LED1; //Set pin with LED1 to output for(;;){ //Loop forever if((P1IN & B1) ==0){ //Is button down or not P1OUT &= ~LED1; } else{ P1OUT |= LED1; } } P1OUT is not initialized and must be written before configuring the pin for output.

11 Sample Code 1 for Input (MSP430g2553) #include #define LED1 BIT0 //P1.0 to red LED #define B1 BIT3 //P1.3 to button void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer P1OUT |= LED1 + B1; P1DIR = LED1; //Set pin with LED1 to output P1REN = B1; //Set pin to use pull-up resistor for(;;){ //Loop forever if((P1IN & B1) ==0){ //Is button down or not P1OUT &= ~LED1; } else{ P1OUT |= LED1; } }

12 Sample Code 2 for Input #include #define LED1 BIT0 //P1.0 to red LED #define B1 BIT3 //P1.3 to button volatile unsigned int i,j; void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer P1OUT |= LED1; //Preload LED1 P1DIR = LED1; //Set pin with LED1 to output for(;;){ while((P1IN & B1) != 0){ //Loop on button up i = P1IN;j = P1OUT; } P1OUT &= ~LED1; // Turn LED1 off while((P1IN & B1) == 0){ //Loop on button down i = P1IN;j = P1OUT;} P1OUT |= LED1; // Turn LED1 on }

13 Sample Code 2 for Input (for MSP430g2553) #include #define LED1 BIT0 //P1.0 to red LED #define B1 BIT3 //P1.3 to button volatile unsigned int i, j; void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer P1OUT |= LED1 + B1; P1DIR = LED1; //Set pin with LED1 to output P1REN = B1; //Set pin to use pull-up resistor for(;;){ while((P1IN & B1) != 0){ //Loop on button up i = P1IN;j = P1OUT;} P1OUT &= ~LED1; // Turn LED1 off while((P1IN & B1) == 0){ //Loop on button down i = P1IN;j = P1OUT;} P1OUT |= LED1; // Turn LED1 on }

14 Lab 1 Basic 1: – Upload the sample input program 2 to the MSP430 LaunchPad and run it Basic 2: – Change the red LED to the green LED Basic 3: – Run the debugger to show the values of P1IN after each while() statement

15 Lab 1 Advanced (Bonus): – Write a program to toggle the LED each time the button is pressed. Turn it on the first time, off the second, on the third, and so on.

16 Debug How to know the process is working correctly? 1.In the code line containing: i = P1IN; j = P1OUT; double-click on the variable i,j to select it. 2.Then right-click on it and select Add Watch Expression. If needed, click on the Watch tab near the upper right of the CCS screen to see the variable added to the watch window. 3.Right-click on the appropriate line of code and select Toggle Breakpoint. 4.When we run the code, it will hit the breakpoint and stop, allowing the variable to be read and updated in the watch window. 5.Run the code 6.Do this a few times, observing the value.


Download ppt "LAB 2: Basic IO Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems."

Similar presentations


Ads by Google