Creating a Program That Blinks the Leds on the Board
In:
Submitted By Words 803 Pages 4
Creating a Program that Blinks the LEDs on the Board 1- Create a new project and copy and paste the following code in the main.c file of your project.
//***************************************************************************************
// MSP430 Blink the LED Demo - Software Toggle P1.0
//
// Description; Toggle P1.0 by xor'ing P1.0 inside of a software loop.
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430x5xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.0|-->LED
//
// J. Stevenson
// Texas Instruments, Inc
// July 2011
// Built with Code Composer Studio v5
//***************************************************************************************
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= 0x01; // Set P1.0 to output direction
for (;;) { volatile unsigned int i; // volatile to prevent optimization
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
i = 10000; // SW Delay do i--; while (i != 0); }
}
2- Google the “for” and “while” loops in C++ and describe how they work here. We will use them very often in programming.
Answer: * For loop is used like an infinite loop and it helps for doing same blinking task recursively. * While loop is used to implement a very crude delay loop and the actual length of the delay can vary significantly. (Delay between LED toggles)
4- How much do you guess we can increase i in the next line?
Answer: * We can increase the value of i till 65535 only.
5- What should we do in order to be able to write i=70,000?
Answer: * We should change the data type of ‘i’ to long integer. * “volatile unsigned long int i;”
6- Search in the MSP430G2553 data sheet and find the P1DIR and P1OUT registers. What do they do?
Answer: * P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input. * P1OUT is another register which holds the status of the LED.
7- We want to modify the code so the LED1 and LED2 toggle. Replace the line
P1OUT ^= 0x01;
With
if (P1OUT==0x01) P1OUT=0x40; else if (P1OUT==0x40) P1OUT=0x01; else P1OUT=0x01;
Answer:
* This is a nested if condition. * If the status of the LED equal to 0x01 then the status changes to 0x40. Else if the status of the LED equal to 0x40 then the status changes to 0x01. If the status value is anything else, its value changes to 0x01. * The LED1 and LED2 toggle.
And see what happens. Google “if in C++” and explain how this code will work. What if we use the following code? int X; int Y;
Y=0x01&(~P1OUT);
X=0x40*(0x01&(~Y));
P1OUT = X+Y;
Answer: * LED1 and LED2 toggle. * Same operation like above question, but in here we are using logic operators except using if conditions.
For understanding the above code, you need to check C++ operators (! ~ & ^ / % …). They are very important for the future codes that we will generate.
8- Assume that you have a logic circuit whose inputs are LED01 and LED02. For the toggling task draw the truth table for the logic circuit that generates the next state.
9- As a homework, extend the LED blinking code to generate the SOS morse code (a periodic three fast blinks, then three slow blinks). You can see the following links: https://www.youtube.com/watch?v=mEbkQE3JBPg http://en.wikipedia.org/wiki/SOS
try your code at home, program your boards, and include you code in your lab reports.
//***************************************************************************************
// MSP430 Blink the LED to generate the SOS morse - Software Toggle P1.0
//
// Description; Toggle P1.0 by xor'ing P1.0 inside of a software loop.
// And a periodic three fast blinks, then three slow blinks
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430x5xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.0|-->LED
//
// R. D. Niroshan Lakmal Rajapakse
// S01702527
// Lab 01 Question 09 (SOS)
// 2/4/2013
// Built with Code Composer Studio v5
//***************************************************************************************
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= 0x01; // Set P1.0 to output direction
for (;;) { volatile unsigned long int i; // volatile to prevent optimization int r; int a;
for(r = 6 ; r > 0 ; r--) // A periodic three fast blinks { P1OUT ^= 0x01;
i=10000; do i--; while (i != 0); }
for(a = 6 ; a > 0 ; a--) // A periodic three slow blinks { if(a == 6) { i=50000; do i--; while (i != 0); }
P1OUT ^= 0x01;
i=50000; do i--; while (i != 0); }
for(r = 6 ; r > 0 ; r--) // A periodic three fast blinks { if(a == 6) { i=50000; do i--; while (i != 0); }
P1OUT ^= 0x01;
i=10000; do i--; while (i != 0); } i=250000; do i--; while (i != 0); }
}