Presentation is loading. Please wait.

Presentation is loading. Please wait.

Blinking an LED Using MSP430ware to Control GPIO

Similar presentations


Presentation on theme: "Blinking an LED Using MSP430ware to Control GPIO"— Presentation transcript:

1 Blinking an LED Using MSP430ware to Control GPIO
Chapter 3 MSP430 Design Workshop

2 Objectives List 3 components of MSP430ware
Describe (and name) the GPIO control registers Implement the steps needed to use MSP430ware DriverLib in a CCS project Show how to disable the watchdog timer Lab – Use MSP430ware to blink and LED and read a button on the MSP430 Launchpad

3 MSP430ware Libraries Examples Software Tools DriverLib* Graphics
USB Stack CapTouch MathLib IEC60730 Examples All device generations Development boards Software Tools Grace ULP Optimization Advisor * Other tools/libraries covered in later workshop chapters DriverLib...

4 Driver Library vs Traditional C Coding (PWM example)
Driver Library offers easy-to-understand functions No more cryptic registers to configure Functional coding of peripherals rather than bitwise programming High-level API makes it easy to port code between most MSP430 devices Minimal overhead

5 MSP430ware DriverLib Modules
Software modules tend to match 1-to-1 with hardware peripherals Some of the module names above have been abbreviated Not all devices have all hardware (and thus, software) modules DriverLib is not currently available for MSP430 ValueLine devices Looking at GPIO...

6 MSP430 GPIO Ports I/O Port 1 GPIO = General Purpose Bit Input/Output
8-bit I/O ports 1 to 12 ports, depending on family and pin-count Each pin is individually controllable Input pins can generate interrupts (Chapter 5) Controlled by memory- mapped registers: IN OUT DIR REN SEL P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 I/O Port 1 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 In or Out?

7 PxDIR (Pin Direction): Input or Output
P1OUT.7 P1DIR.7 “1” 7 6 5 4 3 2 1 P1IN P1OUT P1DIR PxDIR.y: 0 = input  Register example: 1 = output P1DIR |= 0x81; MSP430ware example: #include <driverlib.h> GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN0 + GPIO_PIN7 ); Set output value...

8 GPIO Output “1” P1IN.7 P1OUT.7 P1DIR.7 “1” P1IN “1” P1OUT
6 5 4 3 2 1 P1IN x P1OUT P1DIR PxOUT.y: 0 = low  Register example: 1 = high P1OUT |= 0x80; MSP430ware example: GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN7 ); Input…

9 GPIO Input (Resistors)
P1IN.7 P1OUT.7 P1DIR.7 P1REN.7 up/down Enable resistor Input pins are held in high-impedance (Hi-Z) state, so they can react to 0 or 1 When not driven, Hi-Z inputs may float up/down … prevent this with pullup/pulldown resistors PxREN enables resistors PxOUT selects pull-up (1) or -down (0) Lower cost devices may not provide up/down resistors for all ports 7 6 5 4 3 2 1 P1IN x P1OUT P1DIR P1REN unsigned short usiButton = 0; GPIO_setAsInputPinWithPullUpResistor( GPIO_PORT_P1, GPIO_PIN7 ); usiButton = GPIO_getInputPinValue( GPIO_PORT_P1, GPIO_PIN7 );

10 Controlling GPIO Ports
Most pins on MCU’s are multiplexed to provide you with greater flexibility – which peripherals do you want to use in your system

11 Pin Flexibility P1IN P1OUT
7 6 5 4 3 2 1 P1IN P1OUT P1DIR P1REN P1DS P1SEL Most pins on MCU’s are multiplexed to provide you with greater flexibility Often, two (or more) digital peripherals are connected to the pin – in this case, some families use PxDIR to select between them, while others have multiple PxSEL registers P1SEL.1 “PxSEL = 0” IN / OUT Peripheral (e.g. Timer) “PxSEL = 1” GPIO_setAsPeripheralModuleFunctionOutputPin( port, pin ); GPIO_setAsPeripheralModuleFunctionInputPin( port, pin );

12 GPIO Summary: F5529 vs FR5969 vs G2553
PA PB PC PD PJ* (4-bit ) Reset Value (PUC) P1† P2 P3 P4 P5 P6 P7 P8 PxIN  All Four Devices support Ports 1 and 2 F5529 FR4133 FR5969 (only) F5529 (P8 x3-bits) FR4133 (P8 x12-bits) F55 & FR59 undef  PxOUT unchg PxDIR 0x00 PxREN PxDS  PxSEL PxIV   FR5969 (only) PxIES PxIE  PxIFG  F5529/FR4133 only (80-pin)  FR5969 only (48-pin) * PJ: 4-bits shared with JTAG pins  G2553 only (20-pin) † P1: 4-bits shared with JTAG pins (‘G2553) Each numbered port has 8 bits, unless noted otherwise At reset, all I/O pins are set to … input You should initialize all pins (to prevent floating inputs) Analog functions can ‘preempt’ pin function selection

13 Include Files Like most C programs, we need to include the required header files Each MSP430 device has its own .h file to define various symbols and registers – include this using msp430.h DriverLib defines all peripherals available for each given device – include hw_memmap.h (from /inc folder) But to make DriverLib easy, TI created a single header file to link in: driverlib.h #include <driverlib.h> GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN7); Disable the dog...

14 Disable WatchDog Timer
MSP430 watchdog timer is always enabled at reset Watchdog timer requires modification password (0x5A) Easiest solution: Begin your program with DriverLib (WDT_A) function #include <driverlib.h> WDT_A_hold(WDT_A_BASE); //Stop watchdog timer

15 Pin UnLocking (Default for FRAM devices)
PM5CTL0.LOCKLPM5 bit disconnects registers from pins – allows pin values to remain constant during low power modes (LPM3.5/4.5) Bit automatically set upon entering LPMx.5 mode (see Low Power Chapter) FRxx FRAM devices always power-on with this mode set – you must clear it for pins to respond to your register settings Hint: Unlock pins before clearing and enabling GPIO port interrupts 1 1 1 1 GPIO Control Registers (IN, OUT, etc) LOCKLPM5 pin locking GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN7 ); GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN7 ); PMM_unlockLPM5(); // unlock pins after setting all gpio registers

16 Lab 3 – Blink with MSP430ware
Lab Worksheet… a Quiz, of sorts on: GPIO DriverLib Path Variables Lab 3a – Embedded ‘Hello World’ Create a MSP430ware DriverLib GPIO project Use IDE path variables to make your project portable Write code to enable LED Use simple (inefficient) delay function to create blinking LED Use CCS debugging windows to view registers and memory Lab 3b – Read Launchpad Push Button Test the state of the push button Only blink LED when button is pushed (again, inefficient, but we’ll fix that in Chapter 5)

17 Launchpad Pins for LEDs/Switches
FR4133 FR5969 LED Color LED1 P1.0 P4.6 Red LED (with Jumper) LED2 P4.7 P4.0 Green LED Button 1 P2.1 P1.2 P4.5 Button 2 P1.1 P2.6

18 Lab3a – Worksheet ________________________________________________
1. Where is your MSP430ware folder located? ________________________________________________ 2. To use the MSP430ware GPIO and Watchdog API, which header file needs to be included in your source file? #include < ________________________ > 3. Which DriverLib function stops the Watchdog timer? ________________________________________________ ; 4a. Which I/O pin on Port 1 is connected to an LED (on your Launchpad)? 4b. What two GPIO DriverLib functions are required to initialize this GPIO pin (from previous question) as an output and set its value to “1”? 4c. For the FRAM devices, what additional function is needed to make it work (i.e. to connect the I/O to the pin)?

19 Lab3a – Worksheet ________________________________________________
1. Where is your MSP430ware folder located? ________________________________________________ 2. To use the MSP430ware GPIO and Watchdog API, which header file needs to be included in your source file? #include < ________________________ > 3. What DriverLib function stops the Watchdog timer? ________________________________________________ ; 4a. Which I/O pin on Port 1 is connected to an LED (on your Launchpad)? 4b. What two GPIO DriverLib functions are required to initialize this GPIO pin (from previous question) as an output and set its value to “1”? 4c. For the FRAM devices, what additional function is needed to make it work (i.e. to connect the I/O to the pin)? Most likely: C:\ti\msp430\MSP430ware_1_97_00_47\ driverlib.h WDT_A_hold( WDT_A_BASE ) F5529/FR5969/FR4133: P1.0 GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN0 ) GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN0 ) PMM_unlockLPM5();

20 Lab3a – Worksheet Using the _delay_cycles() intrinsic function (from the last chapter), write the code to blink an LED with a 1 second delay setting the pin (P1.0) high, then low? #define ONE_SECOND while (1) { //Set pin to “1” (hint, see question 4) _________________________________________________ ; _delay_cycles( ONE_SECOND ); // Set pin to “0” }

21 Lab3a – Worksheet Using the _delay_cycles() intrinsic function (from the last chapter), write the code to blink an LED with a 1 second delay setting the pin (P1.0) high, then low? #define ONE_SECOND while (1) { //Set pin to “1” (hint, see question 4) _________________________________________________ ; _delay_cycles( ONE_SECOND ); // Set pin to “0” } GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0) GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0)

22 Lab3b – Worksheet 1. What 3 function options can be used to set a pin for GPIO input? Hint, one place to look would be the MSP430 Driverlib Users Guide found here: \MSP430ware_1_97_00_47\driverlib\doc\<target>\ _________________________________________ 2. What can happen to an input pin that isn’t tied high or low? 3a. Which I/O pin on Port 1 is connected to a Switch (on your Launchpad)? 3b. Assuming you need a pull-up resistor for a GPIO input, write the line of code required to setup this pin as an input: _________________________________________; or _________________________________________;

23 Lab3b – Worksheet 1. What 3 function options can be used to set a pin for GPIO input? Hint, one place to look would be the MSP430 Driverlib Users Guide found here: \MSP430ware_1_97_00_47\driverlib\doc\<target>\ _________________________________________ 2. What can happen to an input pin that isn’t tied high or low? 3a. Which I/O pin on Port 1 is connected to a Switch (on your Launchpad)? 3b. Assuming you need a pull-up resistor for a GPIO input, write the line of code required to setup this pin as an input: _________________________________________________; or _______________________________________________; GPIO_setAsInputPin() GPIO_setAsInputPinWithPullDownResistor() GPIO_setAsInputPinWithPullUpResistor() The input pin could end up floating up or down. This uses more power … and can give you erroneous results. F5529/FR5969: P FR4133: P1.2 GPIO_setAsInputPinWithPullUpResistor ( GPIO_PORT_P1, GPIO_PIN1) GPIO_setAsInputPinWithPullUpResistor ( GPIO_PORT_P1, GPIO_PIN2)

24 Lab3b – Worksheet Complete the following code to ­read pin P1.1:
volatile unsigned short usiButton1 = 0; while(1) { // Read the pin for push-button S2 usiButton1 = _____________________________________; if ( usiButton1 == GPIO_INPUT_PIN_LOW ) { // If button is down, turn on LED GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN0 ); } else { // Otherwise, if button is up, turn off LED GPIO_setOutputLowOnPin( GPIO_PORT_P1, GPIO_PIN0 ); In embedded systems, what is the name given to the way in which we are reading the button? (Hint, it’s not an interrupt) ____________________________

25 Lab3b – Worksheet Complete the following code to ­read pin P1.1:
volatile unsigned short usiButton1 = 0; while(1) { // Read the pin for push-button S2 usiButton1 = ____________________________________; if ( usiButton1 == GPIO_INPUT_PIN_LOW ) { // If button is down, turn on LED GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN0 ); } else { // Otherwise, if button is up, turn off LED GPIO_setOutputLowOnPin( GPIO_PORT_P1, GPIO_PIN0 ); For ‘FR4133 use GPIO_PIN2 GPIO_getInputPinValue ( GPIO_PORT_P1, GPIO_PIN1 ) In embedded systems, what is the name given to the way in which we are reading the button? (Hint, it’s not an interrupt) ____________________________ “Polling”

26


Download ppt "Blinking an LED Using MSP430ware to Control GPIO"

Similar presentations


Ads by Google