Thursday, 25 February 2016

"LCD.C" by Engr Rashid Malik
If you want to use LCD in your project then use the following program of lcd.c interfacing with lcd



#define rRS PIN_B7      //4
#define renable PIN_B6  //6
//#define rRW PIN_B1   //5

#define  db4 PIN_B2//LCD pin 11
#define  db5 PIN_B5//LCD pin 12
#define  db6 PIN_B3//LCD pin 13
#define  db7 PIN_B4//LCD pin 14
int8 line[21]="Engr Rashid";
//int8 date,month,year,dow,hr,min,sec;
//int8 line2[21]="V1=1 V2=1 V3=0 V4=0 ";
   

////////////////////////////////////////////////////////////////////////////
////                             LCD.C                                  ////
////                 Driver for common LCD modules                      ////
////                                                                    ////
////  lcd_init()   Must be called before any other function.            ////
////                                                                    ////
////  lcd_putc(c)  Will display c on the next position of the LCD.      ////
////                     The following have special meaning:            ////
////                      \f  Clear display                             ////
////                      \n  Go to start of second line                ////
////                      \b  Move back one position                    ////
////                                                                    ////
////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)     ////
////                                                                    ////
////  lcd_getc(x,y)   Returns character at position x,y on LCD          ////
////                                                                    ////
////        (C) Copyright 1996,1997 Custom Computer Services            ////
////                                                                    ////
////////////////////////////////////////////////////////////////////////////

void lcd_send_nibble( int8 val )
{
   if (val&1)  output_high(db4); else output_low(db4);
   if (val&2)  output_high(db5); else output_low(db5);
   if (val&4)  output_high(db6); else output_low(db6);
   if (val&8)  output_high(db7); else output_low(db7);
   delay_cycles(1);
   output_high(renable);
   delay_us(2);
   output_low(renable);
}
//=============================

void lcd_send_byte( int8 address, int8 n ) {

      output_low(rrs);
     delay_ms(1);
      if(address)output_high(rrs);else output_low(rrs);
      delay_cycles(1);
      //output_low(rrw);
      delay_cycles(1);
      output_low(renable);
      lcd_send_nibble(n >> 4);
      lcd_send_nibble(n & 0xf);
}

//=====================================
void lcd_init() {
    int8 i;
int8 CONST LCD_INIT_STRING[4] = {40,12,1,6};
    set_tris_b(0);
     output_low(rrs);
     //output_low(rrw);
     output_low(renable);
    delay_ms(5);
    for(i=1;i<=3;++i) {
       lcd_send_nibble(3);
       delay_ms(2);
    }
    lcd_send_nibble(2);
    for(i=0;i<=3;++i)
       lcd_send_byte(0,LCD_INIT_STRING[i]);
     
     
}
//===================================
void lcd_gotoxy(unsigned char Row,unsigned char Col)
{  
   if (Row==1) lcd_send_byte(0,0x80+Col-1);
   if (Row==2) lcd_send_byte(0,0xc0+Col-1);
   if (Row==3) lcd_send_byte(0,0x94+Col-1);
   if (Row==4) lcd_send_byte(0,0xd4+Col-1);
}
//!void lcd_putc( char c) {
//!   switch (c) {
//!     case '\f'   : lcd_send_byte(0,1);
//!                   delay_ms(2);
//!                                           break;
//!     case '\n'   : lcd_gotoxy(1,2);        break;
//!     case '\b'   : lcd_send_byte(0,0x10);  break;
//!     default     : lcd_send_byte(1,c);     break;
//!   }
//!}
void printlcd(void)
{
int8 i=0;

   do
   {
   lcd_send_byte(1,line[i]);
   i++;//delay_ms(50);
   }
   while(line[i]!='\0'); // '\0' is null point
}


//!char lcd_getc( byte x, byte y) {
//!   char value;
//!
//!    lcd_gotoxy(x,y);
//!    lcd.rs=1;
//!    value = lcd_read_byte();
//!    lcd.rs=0;
//!    return(value);
//!}
//!

void clrscr(void)
{
lcd_send_byte(0,1);
 delay_ms(2);
}
void print_time(int8 hr,int8 min,int8 sec)
{


lcd_send_byte(1,(hr/10)+48);
lcd_send_byte(1,(hr%10)+48);
lcd_send_byte(1,':');
lcd_send_byte(1,(min/10)+48);
lcd_send_byte(1,(min%10)+48);
lcd_send_byte(1,':');
lcd_send_byte(1,(sec/10)+48);
lcd_send_byte(1,(sec%10)+48);
lcd_send_byte(1,' ');

}
void print_val(int16 val)
{
lcd_send_byte(1,(val/100)+48);
val%=100;

lcd_send_byte(1,(val/10)+48);
lcd_send_byte(1,'.');
lcd_send_byte(1,(val%10)+48);

}

void print_val1(int16 val)
{
lcd_send_byte(1,(val/100)+48);
val%=100;

lcd_send_byte(1,(val/10)+48);
//lcd_send_byte(1,'.');
lcd_send_byte(1,(val%10)+48);

}


void space(int8 d)
{
while(d--)lcd_send_byte(1,' ');
}

//--------- display --------
void display(int16 u,int8 a,int16 v,int16 amp)
{
a%=100;v%=1000;amp%=100;
lcd_gotoxy(1,1);
lcd_send_byte(1,'U');
lcd_send_byte(1,'N');
lcd_send_byte(1,'I');
lcd_send_byte(1,'T');
lcd_send_byte(1,'S');
lcd_send_byte(1,' ');
lcd_send_byte(1,(u/10000)+48);u%=10000;
lcd_send_byte(1,(u/1000)+48);u%=1000;
lcd_send_byte(1,(u/100)+48);u%=100;
lcd_send_byte(1,(u/10)+48);
lcd_send_byte(1,(u%10)+48);
lcd_send_byte(1,'.');
lcd_send_byte(1,(a/10)+48);
lcd_send_byte(1,(a%10)+48);

lcd_gotoxy(2,1);
lcd_send_byte(1,'V');
lcd_send_byte(1,'O');
lcd_send_byte(1,'L');
lcd_send_byte(1,'T');
lcd_send_byte(1,'S');
lcd_send_byte(1,' ');

lcd_send_byte(1,(v/100)+48);v%=100;
lcd_send_byte(1,(v/10)+48);
lcd_send_byte(1,(v%10)+48);

lcd_gotoxy(2,14);
lcd_send_byte(1,'A');
lcd_send_byte(1,'M');
lcd_send_byte(1,'P');
lcd_send_byte(1,' ');

lcd_send_byte(1,(amp/10)+48);
lcd_send_byte(1,(amp%10)+48);
}

No comments:

Post a Comment