Sunday 4 October 2015

LCD interfacing with 8051-II

Hi guys. Now I am going to show “How to interface LCD to 8051” using “Embedded C” programming. And in this post we are going to some advanced than the part-1 by using functions. This coding is useful to display more number of strings as well as it is rectifying drawbacks of the part-I. First show the hardware implementation of LCD. 

 Hardware implementation:-  


                                          Hardware implementation is same as you see in part-1 if you’re not see the part-1  click on the link for Hardware implementation . Now quick starts with Software coding by using 'Keil' software tool



Software implementation:- 


By using ‘Keil’ we can write code in embedded c language and also very easy to create ‘.hex’ files which is used for the dumping and run the 8051 micro-controller. Let see the code for LCD on 8051 micro-controller.

Program:-

#include<regx52.h>
#define lcd P2            // define LCD DATA pins to Port-2
sbit RS = P3^0;          // Register select
sbit RW = P3^1;         // Read/write
sbit EN = P3^2;          // Enable pin
void delay (unsigned int ms)      // Delay function             
{
    unsigned int i, j;                                                       
    for (i=0; i<=ms; i++)
    for (j=0; j<1275; j++);                               
}
void lcd_cmd(unsigned char x)    //Lcd command function
{
   lcd = x;
   RS = 0;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
void lcd_data(unsigned char t)    // Lcd Data function
{
   lcd = t;
   RS = 1;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
void lcd_initial()                  //Lcd initialization function
{
            lcd_cmd(0x38);        //Function set: 8-bit, 2-line 5x7 dots
            lcd_cmd(0x0c);        //Display ON, cursor OFF
            lcd_cmd(0x0E);        //Display ON, cursor ON
            lcd_cmd(0x01);        //Clear Display
}
           
void disp_str(unsigned char *p)        // Display String function
                                                           //*p is pointer variable
{
     for(*p=0;*p!='\0';*p++)
         {
           lcd_data(*p);
         }
}
void main()
{
            lcd_initial();
            disp_str("*** Hi Guys ***");
            lcd_initial();
            disp_str("Welcome to My");
            lcd_cmd(0xc0);                    //Cursor move from first line to second line
            delay(50);
            disp_str("Youtube channel");
            lcd_initial();
            disp_str("Please like this");
            lcd_cmd(0xc0);                   //Cursor move from first line to second line
            delay(50);
            disp_str("and Subscribe it");
            lcd_initial();
            disp_str("Thanks for watch");
            lcd_cmd(0xc0);                   //Cursor move from first line to second line
            delay(50);
            disp_str("**** Adios ****");
            delay(50);
            lcd_cmd(0x02);                  //Return Home
            lcd_cmd(0x0c);                  //Display ON cursor OFF
            delay(1000);

}  

As you see above program it is very simple by using function label with ‘lcd_str’, and easy why because we can’t use for-loops for many times to print number of strings on LCD. This is the main advantage in the coding.


For better understanding see the below video on Youtube. Any questions regarding this put a comment below, Thanks for watching keep enjoy and do better.




                                                        
   

Friday 2 October 2015

LCD interfacing with 8051-I

Hi guys. Now I am going to show “How to interface LCD to 8051” using “Embedded C” programming. In this post I am going to show how to interface LCD to 8051 micro-controller and how to display strings on that LCD using simple code in embedded c. First show the hardware implementation of LCD.



Hardware implementation:- 

                                   
Now going to hardware implementation first, and you know about the tool called “Proteus”, by using this you can implement the hardware design. Now go to how to connect LCD 16x2 display in Proteus is shown below.



As you see in above picture, shows how to interfacing LCD with 8051. Place the 8051 micro-controller and connect the LCD  data pins(D0-D7) to port-2 of the 8051 micro-controller similarly register select(RS), Read/write(RW) and Enable(EN) pins are connected to P-3.0,P-3.1,P-3.2 respectively. And Variable resistor(VR1) is used to either increase or decrease the contrast of the 16x2 Lcd display. Despite of these connections we also know about the commands for LCD 16x2 display those commands are useful for our project. Some commands are show in below image..



After completed everything go to the software simulation and understand the program carefully.

Software implementation:-
                                   
Now quickly starts with software implementation, for this purpose you know about the software called “Keil” this tool used to write the code in “Embedded C” language and it is very easy to use. Let see the code for LCD on 8051 micro-controller.

Program:-

#include<regx52.h>
#define lcd P2                                            // define lcd DATA pins to Port-2
sbit RS = P3^0;                                         // Register select
sbit RW = P3^1;                                       // Read/write
sbit EN = P3^2;                                        // Enable pin
 void delay (unsigned int ms)                   // Delay function          
{
    unsigned int i, j;                                                       
    for (i=0; i<=ms; i++)
        for (j=0; j<1275; j++);                               
}
void lcd_cmd(unsigned char x)                 //Lcd command function
{
   lcd = x;
   RS = 0;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
void lcd_data(unsigned char t)                 // Lcd Data function
{
   lcd= t;
   RS = 1;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
 void main()                                             //Main function
{
    int i,j;
    char str1[]="welcome to My";
    char str2[]="  Blog";
    lcd_cmd(0x38);                                  //Function set: 8-bit, 2-line 5x7 dots
    lcd_cmd(0x0c);                                  //Display ON cursor OFF
    lcd_cmd(0x0E);                                 //Display ON cursor ON
       while(1)
          {
               for(i=0;i<13;i++)     
                 {
                       lcd_data(str1[i]);
                  }
         lcd_cmd(0xc0);                             //Cursor move from first line to second line
         delay(50);                                      // delay 50ms
                for(j=0;j<7;j++)
                  {
                        lcd_data(str2[j]);
                   }            
           lcd_cmd(0x02);                           //Return Home
           lcd_cmd(0x0c);                           //Display ON cursor OFF
   
   }      
}

As you see above program it is very simple and easy, why because we can use strings and programs consists of two strings str1 and str2, similarly we use two for-loops for both the strings and print data on LCD. If you have clear understand about this topic then you must watch the below video.



Pros:-
  • This program is very simple and easy.
  • Using strings and loops we can easy to develop and understand.
Cons:-
  • In this program is, if you display another string on Lcd we can use another string and for-loop it is very difficult to display more number of strings and code memory is also increases this is the main draw back of this program instead of this it is very easy. 

Saturday 14 February 2015

Robotic Car using 8051

Hi guys. Now I am going to show "How to make robot car using 8051" using "Embedded C" programming. I can show in software simulation mode using Proteus software and, for compiling Embedded C-programme in Keil software. Now see the program of this project..

Software programming:-   

This program is very simple and easy by making robot car using 8051. And this programme is used for controlling four DC_MOTORS used for running the robot car in efficiently. Lets see the program..

#include<reg51.h>
sbit m1p = P2^0;                           //  Motor 1 positive terminal is assigned to port 2(P2^0)
sbit m1n = P2^1;                           // Motor 1 negative terminal is assigned to port 2(P2^1)
sbit m2p = P2^2;                           // Motor 2 positive terminal is assigned to port 2(P2^2)
sbit m2n = P2^3;                           // Motor 2 negative terminal is assigned to port 2(P2^3)
sbit m3p = P2^4;                           // Motor 3 positive terminal is assigned to port 2(P2^4)
sbit m3n = P2^5;                           // Motor 3 negative terminal is assigned to port 2(P2^5)
sbit m4p = P2^6;                           // Motor 4 positive terminal is assigned to port 2(P2^6)
sbit m4n = P2^7;                           // Motor 4 negative terminal is assigned to port 2(P2^7)
sbit F = P1^0;                                // Forward switch is connected to port 1(P1^0)
sbit Ba = P1^1;                              // Backward switch is connected to port 1(P1^1)
sbit R = P1^2;                                // Right switch is connected to port 1(P1^2)
sbit L = P1^3;                                // Left switch is connected to port 1(P1^3)  */
                                                     
void forward()                                //  Robot forwarding function
{
m1p = 1;
m2p = 1;
m3p = 1;
m4p = 1;
m1n = 0;
m2n = 0;
m3n = 0;
m4n = 0;
}
void backward()                             // Robot backward function
{
m1p = 0;
m2p = 0;
m3p = 0;
m4p = 0;
m1n = 1;
m2n = 1;
m3n = 1;
m4n = 1;
}
void left()                                        // Robot left turning function
{
m1p = 1;
m1n = 0;
m2p = 1;
m2n = 0;
m3p = 0;
m3n = 1;
m4p = 0;
m4n = 1;
}
void right()                                   // Robot right turning function
{
m1p = 0;
m1n = 1;
m2p = 0;
m2n = 1;
m3p = 1;
m3n = 0;
m4p = 1;
m4n = 0;
}
void stop()                                     // Robot stop function
{
m1p = 0;
m1n = 0;
m2p = 0;
m2n = 0;
m3p = 0;
m3n = 0;
m4p = 0;
m4n = 0;
}
void main()                                   // Main function 
{
F = 0;                                           // Initialise all switches are equal to zero 
  Ba = 0;
L = 0;
R = 0;
while(1)
{
if(F==1)
forward();                                  
else if(Ba==1)
backward();
else if(L==1)
left();
else if(R==1)
right();
else
stop();
}
}

This is very simple program for understanding.

Hardware implementation :- 

Now see the robot hardware simulation in Proteus software. And assume the first two motors are in one side of the robo and next two motors are in another side of the robo car. For easy understanding see the below figure


 while pressing Forward(F) button all motors are running forward direction and pressing Backward(BA) button all motors are running in reverse direction. Similarly while pressing Left(L) button, first two motors are rotate in forward and next two motors are rotate in backward direction then robot is turning left direction and pressing Right(R) button, first two motors are rotate in backward and next two motors are rotate in forward direction then robot is turning right direction.
This process is enough for making simple robot car. Then implement this on hardware it looks like this

And see the below video for easily understanding..

  

Monday 9 February 2015

Led chaser using 8051

Hi friends. Now I am going to show the basic “Embedded C” programming for “LED CHASER using 8051”. I am using ‘ KEIL ’ Software for compiling Embedded C program and, for Hardware simulation I am using ‘ PROTEUS ’ Software.


Software programming:-



This is the basic program for LED chaser using 8051, because as you see this program is very large that’s why it is very basic program for easily understanding the Embedded C programming. Before you seeing this program you may know about keil software. Lets see the program..


#include<reg51.h>           // This command is used for including all register file of 8051.
sbit led1=P2^0;                // This is used for initializing single bit of P2.0 to led1.
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit led5=P2^4;
sbit led6=P2^5;
sbit led7=P2^6;
sbit led8=P2^7;

void delay (unsigned int ms)                   // This loop is used for time taken by each led is ON or OFF.
{
                unsigned int i, j;                                                        
                for (i=0; i<=ms; i++)
                 for (j=0; j<1275; j++);                                 // Here 1275 indicates 1 millisecond.
}
void main ()
{  
                                led1=0;
                                led2=0;                      // Firstly I initialize all leds into OFF state
                                led3=0;                      //          i.e. led (n) =0;
                                led4=0;                      //                   n: - No. of Leds */
                                led5=0;
                                led6=0;
                                led7=0;
                                led8=0;
                while (1)
                {
                               
                                delay (10);                  // delay (10) means led 1 is ON by delay 10 milliseconds.
                                led1=1;
                                delay (10);
                                led1=0;
                                led2=1;
                                delay (10);
                                led2=0;
                                led3=1;
                                delay (10);
                                led3=0;
                                led4=1;
                                delay (10);
                                led4=0;
                                led5=1;
                                delay (10);
                                led5=0;
                                led6=1;
                                delay (10);
                                led6=0;
                                led7=1;
                                delay (10);
                                led7=0;
                                led8=1;
                                delay (10);
                                led8=0;
                                led1=1;
                }
}               

You can use Arrays concept on this program then shorten the program. But I make this program for easily understanding to every one.
  
Hardware implementation:-

You can run this program on 8051 micro-controller by using ‘PROTEUS’ Software. Before you can go this you know about this software like Open New Project, dump ‘.hex ‘file on your controller so on. Lets see the circuit diagram of this project..
     

In the above circuit Power supply(Vcc=5v) and Ground terminals are not necessary in this software because these terminals are inbuilt in Proteus and there is no need using crystal oscillator for small applications but, you can do large applications on Proteus then you need crystal oscillator for accuracy output. If you need RESET switch then design it in this software. By click on this switch(sw1) then the circuit will go to initial stage directly, instead of ON and OFF the power supply. This 8051 micro-controller give the input voltage is in between 4.5v to 5.5v. For more information about 8051 micro-controller you can see the data-sheet. Now see the circuit design when using crystal oscillator and RESET button in Proteus..
  

I make a video tutorial for this project see this and enjoy it. Thanks for watching please subscribe it on youtube