Blinking LEDs
#include <msp430g2231.h> volatile long t=0; //Define t and set it to 0 void main(void){ WDTCTL = WDTPW + WDTHOLD; //Stop Watchdog Timer P1DIR |= BIT0; //sets pin 1.0 RLED for output and turns it on P1DIR |= BIT6; //sets pin 1.6 GLED for output and turns it on for (;;) //start infinite loop { for (t; t<50000; t++) //timer1 { P1OUT &= ~BIT0; //pin 1.0 RLED off for timer1 } t=0; //set t to zero for (t; t<25000; t++) //timer2 { P1OUT |=(BIT0 + BIT6); //pin 1.0 and pin 1.6 on for timer2 } t=0; //set t to zero for (t; t<50000; t++) //timer3 { P1OUT &= ~BIT6; //pin 1.6 GLED off for timer3 } t=0; //set t to zero for (t; t<25000; t++) //timer4 { P1OUT |=(BIT0 + BIT6); //pin 1.0 and pin 1.6 on for timer4 } t=0; //set t to zero } //end of infinite loop } //end of main |
|
This code is timed by using loops within an infinite loop.
The code on the left is ready to be copied and pasted into Code Composer Studio and installed on the Launchpad.
Text after // on a line is just for reference and doesn't actually do anything. It can be safely deleted or added to. It shows as green text on Code Composer Studio.
The code on the left is ready to be copied and pasted into Code Composer Studio and installed on the Launchpad.
Text after // on a line is just for reference and doesn't actually do anything. It can be safely deleted or added to. It shows as green text on Code Composer Studio.
Result
Something you can do to make it easy to change the speed the code runs at:
With that change, the code will run exactly the same. However, if you change the line "volatile long x=0;" to something like
"volatile long x=12500;", you will see the code run faster. This is because the variable t is not resetting to 0, but to 12500 instead. This makes the 'for' loops run for less time.
- Underneath the line "volatile long t=0;" add another line: "volatile long x=0;" (without the quotes, of course.)
- For every instance of "t=0;", change it to "t=x".
With that change, the code will run exactly the same. However, if you change the line "volatile long x=0;" to something like
"volatile long x=12500;", you will see the code run faster. This is because the variable t is not resetting to 0, but to 12500 instead. This makes the 'for' loops run for less time.
After Modification
I would love to hear your thoughts on the above as well as anything you'd add or change. Please get in touch.