Uploaded by nabeel khan

LAB 1 FLASHING LEDs

advertisement
The objective of this lab gives the introduction to the documentation files of the
MSP430FR6989 microcontroller board and simple C-programs written that flashes the LEDs.
Subsequently, the code is then modified by writing nested delay loops to obtain larger delays.
Additionally, the code is then further modified so that it flashes both LEDs that are connected
to the board; one red and the other green. The code is then finally tweaked such that the red
LED flashes at twice the frequency of the green LED. Ultimately, this project enforces using bit
mask with logic equations to alter only specific bits within a word to direct those LEDs within
the board while leaving all other bits unchanged from their original configurations.
Part 1 (1.2 Flashing the LED)
In this initial part we were supplied with the fundamental code that allows the red LED to flash
at a given frequency. We were given free range to manipulate the code to alter the LED flashing
frequency to slow it down and speed it up by increasing or decreasing the “FOR” counter delay.
// Code that flashes the red LED
#include <msp430fr6989.h>
#define redLED BIT0 // Red LED at P1.0
void main(void) {
volatile unsigned int i;
WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable GPIO power-on default high impedance mode
P1DIR |= redLED; // Direct pin as output
P1OUT &= ~redLED; // Turn LED Off
for(;;) {
// Delay loop
for(i=0; i<20000; i++) {}
P1OUT ^= redLED; // Toggle the LED
}
}
When running the code in the debug mode, the reset button; 3rd on the board, had no
functionality and there was no change in the operation of the code, regardless of if the code
was running or paused. However, running the same code in the normal mode, the reset button
functioned as it should and properly reset the code to the beginning. It was observed that
whilst the button was held down the LED would remain off until the button was released in
which the code would then resume running again. When board was unplugged from the
computer then plugging it back in, the same code began to run as expected. This was due to the
code being stored in non-volatile memory.
Variables
Registers
Part 2 (1.3 Setting a Long Period)
In this second part, the task is to now alter the code from the previous part to allow for a longer
period of delay. It must be understood that there is a limited 16-bit integer cap of 65,535, so to
allow for longer periods of delay the ‘i’ must be change to a 32-bit value using uint32_t. This
would allow for the simulation of a 32-bit data type on a 16-bit device and use a much larger
value of ‘i’ in turn for a longer period of delay. Shown below is the code that contains back-toback FOR loops which works as an alternative to the method just previously mentioned.
// Code that flashes the red LED for a long period of time using back-to-back FOR loops
#include <msp430fr6989.h>
#define redLED BIT0 // Red LED at P1.0
void main(void) {
volatile unsigned int i, j, k; // unsigned int 16-bit type
WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable GPIO power-on default high-impedance mode
P1DIR |= redLED; // Direct pin as output
P1OUT &= ~redLED; // Turn red LED Off
for(;;) {
// Delay loop
for(i=0; i<40000; i++) {}
for(j=0; j<40000; j++) {}
for(k=0; k<40000; k++) {}
P1OUT ^= redLED; // Toggle red LED
}
}
// Code that flashes the red LED for a long period of time using uint32_t data type.
#include <msp430fr6989.h>
#include <stdint.h>
#define redLED BIT0 // Red LED at P1.0
void main(void) {
volatile uint32_t i; // unsigned int 32-bit type
WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable GPIO power-on default high-impedance mode
P1DIR |= redLED; // Direct pin as output
P1OUT &= ~redLED; // Turn red LED Off
for(;;) {
// Delay loop
for(i=0; i<120000; i++) {}
P1OUT ^= redLED; // Toggle red LED
}
}
Part 3 (1.4 Flashing two LEDs)
In this third part, the task is to now flash the other LED, which is the green LED out of sync with
one another. The previous code will be used but now with additional lines that includes a mask
for the green LED with its corresponding BIT number (BIT7). The LED output is then OR with the
correct port (port 9). A toggle for the green LED is then added to the infinite loop containing the
red LED toggle.
A commented line of code was added to direct how the two LEDs can flash in sync with one
another, rather than appearing to oscillate between the two.
// Code that flashes the red LED and Green LED out sync
#include <msp430fr6989.h>
#include <stdint.h>
#define redLED BIT0 // Red LED at P1.0
#define greenLED BIT7 // Green LED at P9.7
void main(void) {
volatile uint32_t i;
WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable GPIO power-on default high impedance mode
P1DIR |= redLED; // Direct pin as output
P1OUT &= ~redLED; // Turn LED Off
P9DIR |= greenLED; // Direct pin as output
P9OUT |= greenLED; // Turn LED on
//P9OUT &= ~greenLED; //Turn LED off. Line for flashing LEDs in sync.
for(;;) {
// Delay loop
for(i=0; i<20000; i++) {}
P1OUT ^= redLED; // Toggle the LED
P9OUT ^= greenLED; // Toggle the LED
}
}
Part 4 (1.5 Flashing Two LEDs at Different Rates)
In this final part of the lab, the task is to now flash both LEDs; however, allowing the red LED to
flash at twice the frequency of the green LED. This is done by simply adding a second FOR loop
to the infinite loop, whereby the placing the green LED toggle along with the second red LED
toggle commands following the new FOR loop which will only have the red LED toggle
command. This can all be seen in the code below.
// Code that flashes the red LED at twice the frequency of the Green LED
#include <msp430fr6989.h>
#include <stdint.h>
#define redLED BIT0 // Red LED at P1.0
#define greenLED BIT7 // Green LED at P9.7
void main(void) {
volatile uint32_t i;
WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable GPIO power-on default high impedance mode
P1DIR |= redLED; // Direct pin as output
P1OUT &= ~redLED; // Turn LED Off
P9DIR |= greenLED; // Direct pin as output
P9OUT |= greenLED; // Turn LED on
//P9OUT &= ~greenLED; //Turn LED off
for(;;) {
// Delay loop
for(i=0; i<20000; i++) {}
P1OUT ^= redLED; // Toggle the LED
for(i=0; i<20000; i++) {}
P1OUT ^= redLED; // Toggle the LED
P9OUT ^= greenLED; // Toggle the LED
}
}
Student Q & A
1. In this lab, we used a delay loop to create a small delay; what is its effect on the battery
life if the device is battery operated? Is it a good way of generating delays?
The delay loop used in this lab was not the most energy efficient compared to that
which is built-in timers and delay clocks that comes with MSP430 board. Thus, the
battery of the device if it was battery operated can be impacted by large ‘FOR’ loop
counters.
2. The MSP430 CPU runs on a clock that can be slowed down or sped up; what happens to
the delay generated by the delay loop if the clock driving the CPU speeds up or slows
down?
If the clock speeds up then the delay by the delay loop will decrease; however, if the
clock slows down then that delay will take a longer time.
3. How does the code run in the debug mode? Is the microcontroller running as an
independent computer?
No, the microcontroller run as a dependent computer. This means that in the debug
mode the code runs when the computer commands it to do so.
4. How does the code run in the normal mode? Is the microcontroller running as an
independent computer?
Yes, in the normal mode the microcontroller is running completely independent of the
computer; given that it has a power source.
5. In which mode does the reset button work?
From observation during the lab, the reset button functions perfectly when the code is
running in the normal mode. Whilst when in debug mode the reset button is seemed to
be disabled and having no effect on the code.
6. What is the data type uint16_t? What about int16_t? Are these standard C syntaxes?
Data type uint16_t corresponds to a 16-bit unsigned integer.
Data type int16_t corresponds to a 16-bit integer.
Technically yes, these data types are standard C syntax with the understanding that they
come from the <stdint.h> header file.
Conclusion
Finishing this lab, it helped and provided a solid foundation for the basics in understanding and
coding the MSP430 microcontroller. The main topics that were covered in the four main parts
included; delay timers through FOR loops, bit masks for altering specific bits in a work
pertaining specifically to LED port bits, uploading, and debugging code was introduced as well
as reading variables and registers from within the debug portion of the CCS (code composer
studio). Finally, but very importantly was the introduction of simulating 32-bit data types on the
16-bit MSP430 device such that longer delay could be implemented through FOR loop counters.
Download