Pages

Thursday, 14 March 2013

8051 Keil code to Rotate a String on LCD screen

/** Code Source :- www.tentuts.com **/

#include<reg51.h>
sbit rw=P2^0;  //read write pin
sbit rs=P2^1;  // data/command pin
sbit en=P2^2; //enable pin

void lcd_init(void );
void lcd_cmd(unsigned char );
void delay(unsigned int );
void lcd_display(unsigned char);
void lcd_clear(void );
const unsigned char text[]={"Welcome to MUFADDAL KAGDA World  Of  Programming!!"};

void main(void ){
 unsigned char i,loc=0x80;
  rw=0;
  lcd_clear();
  lcd_init();
  for(i=0;text[i]!='\0';i++){
   loc++;
   lcd_display(text[i]);
   if(loc>=0x8f)
    lcd_cmd(0x18);
  }

}
void lcd_clear(void){
 
    lcd_cmd(0x01);
 }
void lcd_init(void ){

   lcd_cmd(0x30);
   lcd_cmd(0x0c);
   lcd_cmd(0x06);
 }

void lcd_cmd(unsigned char cmd){

  rs=0;
  P1=cmd;
  en=1;
  delay(75);
  en=0;
 }

void lcd_display(unsigned char text){

 rs=1;
 P1=text;
 en=1;
 delay(1);
 en=0;
 }
void delay(unsigned int time){
unsigned int i,j;
for (i=0;i<time;i++)
 for(j=0;j<1275;j++);
 }

Friday, 8 March 2013

8051 code of Low Cost path Lightening and Presence Detector


/* Author:- Mufaddal Kagda
8051-P89V51RD2 */


#include <reg51.h>
#include <string.h>

sbit rs = P2^7;  // declare P2.7 as rs pin
sbit en = P2^5;  // declare p2.5 as enable pin
sbit rw = P2^6;  // declare p2.6 as read/write pin
sbit b = P0^7;   // busy flag
sbit port = P1^2;  // connected to IR sensor
void writecmd(unsigned char a); // function to send command to LCD
void writedat(unsigned char b);  // function to send data to LCD
void busy();   // function to check LCD is busy or not
void writestr(unsigned char *s); // function to write string on LCD

void writecmd(unsigned char a)
{
 busy();   // check for LCD is busy or not
 rs = 0;   // clear rs pin for command
 rw = 0;   // clear rw pin to write
 P0 = a;   // send command character
 en = 1;   // strob LCD
 en = 0;

}
void writedat(unsigned char b)
{
 busy();   // check for LCD is busy or not
 rs = 1;   // set rs pin for data
 rw = 0;   // clear rw pin to write
 P0 = b;   // send data character
 en = 1;   // strob LCD
 en = 0;
}
void busy()
{
 en = 0;   // disable display
 P0 = 0xFF;  // configur P0 as input
 rs = 0;   // clear rs pin for command
 rw = 1;   // set rw pin to read
 while(b==1)
 {
  en=0;   // strob LCD till P0.7 is 1
  en=1;
 }
 en=0;
}
void send(unsigned char d)
{
while(!TI);
TI=0;
SBUF=d;
}
void writestr(unsigned char *s)
{
 unsigned char l,i;
 l = strlen(s);               // get the length of string
 for(i=1;i<=l;i++)
 {
  writedat(*s);              // write every char one by one
  s++;
 }
}
 
main()
{
    P0=0x00;                      // P0 and P0 as output ports
    P2=0x00;
port=0;
TMOD=0x20;            //Enable Timer 1
    TH1=0XFD;
    SCON=0x50;
    TR1=1;
TI=1;
        writecmd(0x3C);               // initialize LCD
        writecmd(0x0E);  
        writecmd(0x01);

    writecmd(0x01);
send('0');
writestr("Not Occupied");
while(port==0);

    writecmd(0x01);
send('1');
writestr("Occupied");
        while(port==1);                       // continuous loop

 }