Tuesday, 30 May 2017

LED blinking with lpc2148

So here we are, writing our first program for LPC2148. They say blinking LED is "hello world" of Embedded world. So we are going to start with with that.

1.)  First of all we need a compiler of course. I personally prefer keil uvision. But it really doesn't matter as we are not going to use much libraries.
2.)  A LPC2148 development board is preferred. Those of you who don't have it, can use Proteus simulation software. We are going to go through that too.

Some Explanation-

1. include the LPC2148 header file.
    #include <lpc214x.h>

2. In the main function, set the PINSEL0 register as GPIO function
    PINSEL0 = 0;

3. Set the direction of pin as output
    IODIR0 |= (1<<3)
    We are setting pin3 of port0 as output so we can turn the led on by setting it at HIGH.

4. One more thing, we need to create some delay between the LED on and off. So write a delay function
    void delay (unsigned int count)
unsigned int value = count*1000;
        while (value--);

5. Inside while loop turn the led on and off continually using delay
    while (1)
         IOSET0 |= (1<<3);
         delay (1000);
         IOCLR0 |= (1<<3);
         delay (1000);
   IOSET0 will set the pin3 of port0 HIGH and IOCLR0 will clear the pin3 of port0.
   For more details about registers, visit the first post of this blog.

So that's it, we are ready to blink our first led.

1. open uvision, goto project->new project. Save the project name
2. If you are using uvision 5, you need to select legacy pack
3. select lpc2148

4. click ok to add startup file to the code 

5. In the source Group 1, add new file to the group

6. add a .c file. I named it main

7. Now write the code 

#include <lpc214x.h>

void delay (unsigned int count)
  unsigned int value = count*1000;
  while (value--);

int main (void)
   PINSEL0 = 0;   // setting the pins as GPIO function
   IODIR0 |= (1<<3);   // setting the direction of port0.3 as output
    while (1)   // always true or infinite loop
         IOSET0 |= (1<<3);   // set port0.3 as high
         delay (1000);    // wait for some time
         IOCLR0 |= (1<<3);   // set port0.3 as low
         delay (1000);    // wait for some time
     return 0;    

Before compiling our code, we have to go through some settings in keil. 
so first look for target 1 box and just beside that there is option for target

select the  XTAL frequency  of your board. I have 12 MHz 
Go to output select create hex file. This is the file that we are going to upload to the board.
And at last select compile, you can use f7 also for compiling.

Now let's go through simulation in Proteus

first select the ISIS mode
Next select P to add components we are going to use
Now select LPC2138 (LPC2148 libraries are not available so we are going to use LPC2138. they are almost same.) and red LED

Now we have to add the hex file to it. Also change the Clock frequency to your XTAL frequency

Now add the positive end of led to the port0.3 and negative to the ground. Also we have add +3.3v to Vbat, Vref and v3 and ground to Vss. My final setup look as follows
That's it. Just press START button and see the magic.

download full project here : DOWNLOAD

 Note:- Just in case if your LED is not blinking, try to change the delay values. This may be due to  your XTAL frequency. 
 Please leave your comments in case of any doubts. And subscribe for more tutorials.


Servo motor with STM32

I have already covered a tutorial about Pulse Width Modulation in STM32  HERE  and in this tutorial i am going to cover one of its applicat...