sexta-feira, 15 de abril de 2011

Understanding the program's MSP430-F2013

In this post we will explain how sample program that flashes the LEDs embedded in the board and also understand a little more of time operations in the hard way. Below is the example of program coded in C language, since there is no display on the board to write "Hello World", there is an LED to inform you if the computer is responding, and we will use this LED to program.

#include "msp430.h"

int main(void)
{
            WDTCTL = WDTPW + WDTHOLD;         // Stop o watchdog
            P1DIR |= 0x01;                                             // Define Door 1 like exit
            volatile unsigned int i;         
            for (;;)                                                           // Start infinite loop
            {
                        P1OUT ^= 0x01;                               // Alternates P1 usando X-OR*
                        for (i=0;i<10000;i++);                                              // Decrement of time by software     
      }
}

We can see that the change of variable P1OUT is done using an XOR operation *, known as the Exclusive OR, a logic comparison that results 1 if compares different values:

Value A
0
0
1
1
 Value B
0
1
0
1
 A XOR B
0
1
1
0

In the example we are doing an XOR with the value 1. So each time this operation is performed the value of P1OUT will be changed.
Taking the same basic program, we modify it to test the device. Let's try to modify intuitively (trial and error) how long the LED is lit through the delay generated by the function for.

void main(void)
{
            volatile unsigned long int i;              // unsigned long int can receive up to 4*10^6
            WDTCTL = WDTPW + WDTHOLD;
            P1DIR |= 0x01;

            for(;;)
            {
                        P1OUT ^= 0x01;
                        for (i=0;i<100000;i++);                    // Bigger incremental delay
            }
}

The variable i has been modified so long as we wanted to test as long as possible. Making an analysis of empirical values​​, we obtained an average time trial between a wink and another 1.70 seconds (i = 100000).
Folding the final value of i (200 000) obtained the time of 2.8 seconds and when we did the test by dividing the final value of i by half (50000) resulted in 1.05 seconds. Making the relationship between the times and values ​​of i obtained the following proportions:



led [μs]
2,80
1,70
1,05
for [loop]
200.000,00
100.000,00
50.000,00
T [μs/loop]
1,40E-05
1,70E-05
2,10E-05

T = 1,7 10-5 second (17μs)

.
Making one evaluation of the graph we inferred that time average period T is 1.16 (uS) and one loop with I from 0 to 0 would 0.5 (s).

Realize there one big difference between times for each situation. This is due to overhead instructions that it constant and impacts more times smaller and mainly to measurement errors.
So we can approximate an account:

Suppose we want a 60 time (s) or 1 minute.

So the code for the LED flash every 1 minute should be:
void main(void)
{
            volatile unsigned long int i;             
            WDTCTL = WDTPW + WDTHOLD;
            P1DIR |= 0x01;

            for(;;)
            {
                        P1OUT ^= 0x01;
                        for (i=0;i<3529411;i++);                  // Delay of 60s
            }
}

With this information we can create a first function that creates a delay of t (ms):
void tempo (unsigned long int t)
{
            unsigned long int i;
            unsigned long int max;

            if (t < t_max)              //t_max is the maximm that t can have witgout breaking                 {
                        max = t*58823;                      // max = t_wanted/t_cycle
                        for (i=0;i<max;i++);              // Delay of 60s
            }
}

In the next post will be learning more about the basic libraries of the MSP and create our own library.
Bye-bye!

Nenhum comentário:

Postar um comentário