Monday, October 15, 2012

Hello world program in microntrollers world


Our first program: blinking led:

Some microcontroller theory:
Info about one of the launchpad's microcontroller.
Info about whole familly msp430x2xxx
http://www.ti.com/litv/pdf/slau144i

- msp430 low power 16-bit microcontroller
- handy peripherals
- flexible clock system ( 32 KHz external crystal )
- on board programmer and debugger
- usb to uart bridge ( only 9600 bps )
- two leds, user and reset button


Some led blinking theory:
What do we need to light the led:
- power source
- optimal voltage and current level
- polarization
- led:)
How it looks like with uC:
- led is turning on and off by microcontroller pin
- uC pin capabilities 6 mA, all pins 48 mA


Source for our first program:

#include <msp430.h>
void delay( unsigned int delay )
{
        unsigned int i;
        for ( i = 0; i < delay; i++) asm("nop");
}

int main()
{
        WDTCTL = WDTPW + WDTHOLD;       // Stop WDT     
        P1DIR |= BIT0;

        while(1) {
                P1OUT ^= BIT0;
                delay( 50000 );
        }
        return 0;
}
1 - include header file for names of special registers, peripheral registers, vector addreses and so on

2 - msp430 is very fast
Delay to see led blinking, using nop - no operation instruction( to be sure compiler won't cut it ). In this case, it will loop until the variable i increments to delay value.

10 - turning off watchdog:
This line of code turns off the watchdog timer, which can reset the device after a certain period of time.

11 - setting pin to output
P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input.
Register is just address in ram memory.
To set a specific pin as output or input, we write a '1' or '0' on the appropriate bit of the register.
P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
P1DIR = 0000 0001
P1DIR = 0x01     <-- this is the hexadecimal conversion of 0000 0001
For second led P1.6
P1DIR = 0100 0000
P1DIR = 0x40     <-- this is the hexadecimal conversion of 0100 0000

13 - never ending loop, our program will run forever:D

14 - P1OUT is another register which holds the status of the LED.
'1' specifies that it's ON or HIGH, while '0' specifies that it's OFF or LOW
Since our LED is tied to P1.0, we will toggle the 0 bit of the P1OUT register

17 - return 0 - just for compiler doesn't complain

Compiling...
msp430-gcc -Os -Wall -g -mmcu=msp430g2553 -c main.c
Os - code optimalization for efficient size
Wall - give us warnings to produce cleaner code
g - is used to embed debug info
mmcu=msp430g2553 - tells compiler which uC exactly do we use
c - compile and don't link!

Linking..
msp430-gcc  -g  -mmcu=msp430g2553 -o main.elf main.o
-we specify mmcu again that compiler knows which start-up files and run-time libraries to choose.
o - output file name and after this we give name of our compiled program this section can contain many files that's why we split it whole thing into compiling and linking part.

Downloading to launchpad...
sudo mspdebug rf2500 'erase' 'load main.elf' 'exit'
rf2500 - this is name of the launchpad programmer/debuger.
After this we give set of command or we could type them when (mspdebug) prompt appears.
Be sure to invoke this command where our file "main.elf" so we don't have to give full path.

If everything goes "ok" red led should start to blink:D


3 comments:

  1. Hello,

    please make a note that unsigned int covers numbers range from 0 to 65535 (16 bits). For longer delay You can use unsigned long int (32 bits wide numbers).

    With best regards ;)

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Good observation, for best practice to be sure how many bits contain int or long is to use #include <stdint.h> and then:
      int8_t - signed integer 8 bits
      int16_t
      int32_t
      uint8_t
      uint16_t - unsigned integer 16 bits
      uint32_t

      Delete