Thursday, May 17, 2012

Real delay() and how to use it.

We have all seen the posts about the header files to bring the ease of use of the Arduino to the Launchpad/MSP430 series chips, however one thing that I have found about this that you DO NOT have to have libraries for.....

delay! Yes folks! Delay IS in the realm of possiblility for you folk coming from the Arduino IDE to CCS/GCC. There is only a few small differences you have to know about it.

1. It's not delay(); it's _delay_cycles()
2. The time given in _delay_cycles(x) is NOT in milliseconds per-say based on time. Its based on the speed of the microcontroller, so if you have a chip set for 16mhz, that's 16,000,000 cycles per second. That's mighty fast. If we divide 16,000,000 cycles by 1000 milliseconds, we get: 16,000 cycles per millisecond. So therefore, each millisecond will be a multiple of 16,000. Oh sounds like a pain in the tuccas eh? Well... There is over the counter medication for that, but for the issue at hand here we will clean it up by creating a function in code! (in the brain of course... this is actually less efficient for the micro but not by much really).


/ Delays by the specified Milliseconds
//Justifiably ripped straight from http://www.threadabort.com/?p=26 Mad propellers to that person.
void delay(unsigned int ms)
{
while (ms–)
{
__delay_cycles(16000); // set for 16Mhz change it to 1000 for 1 Mhz
}
}


With this in our hands, we have some serious power here. Basically, we can now address a delay in code for the micro in ms and it will work as it should.

Also let me show this in one line, for your copy/pasting needs :)


void delay(unsigned int ms){while (ms–){__delay_cycles(16000);}}

Remember, all code in c is ensured to end the line with a semicolon (;) EXCEPT for when we are commenting (//stufstufstufetc). So since I have taken out the comment for changing the delay in clocks compaired to speed for timing, it all fits in one line and will keep you (semi) sane through the coding process :)


Just to go ahead and throw an alternative out there, here is a different way we can make a void function:

void delay(int n){int i;for(i = 0; i < n; i++){_delay_cycles(16000);}}

Now mind you, this version is probably not as accurate as the first one, however the same thing is happening. For simple projects involving nothing more than a flashing light or two this will do for any timing that you need on the micro. However as we will find out later, this will not suffice when it comes to having multiple things going on at once.

The big value of this function is it can take care of a lot of your delay needs AS LONG AS you have the delay cycles number correct for the speed it is running at.

Here is le' free app o-teh-day.

/*2 line Insta-Delay on MSP430 Launchpad
 * Free as in free coffee! <|0.o|>
 * Hardware Hacking Honkey 2012
*/

#include  <msp430g2553.h>
void delay(int n);

void delay(int n){int i;for(i = 0; i < n; i++){_delay_cycles(16000);}}

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  BCSCTL1 = CALBC1_16MHZ;   //set speed of chip to 16mhz
  DCOCTL = CALDCO_16MHZ;    //set speed of chip to 16mhz
  P1DIR |= 0x01;            // Set P1.0 to output direction
  P1DIR |= 0x40;            // Set P1.6 to output direction
  P1OUT &=~ 0x40; //Start off turning on P1.6 so it will go back and fourth right.
  for (;;)
   {
    P1OUT ^= 0x01; //toggle P1.0 on and off
    P1OUT ^= 0x40; //toggle P1.6 on and off
    delay(1000);   //your delay for exactly one second, sir!
   }
}

No comments:

Post a Comment