Tuesday, 20 October 2015

PIC16F1518 LCD INTERFACE BY TRANSISTOR LOGIC





#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define __PIC16F1518_H
#define _XTAL_FREQ   16000000
#define RS LATB2
#define EN LATB4
void cmd(unsigned char c);

__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF);

void msdelay(unsigned int time)
{
 unsigned int i=0,j=0;

 for(i=0;i<=time;i++)
   for(j=0;j<=3000;j++);
}

/* function of simple delay*/
void delay(unsigned int i)
{
   while(i)
      i--;
}



/*to blink cursor*/
void lcd_cursorblink(void)
{
   cmd(0x0D);
}



/*to off cursorblinking*/
void lcd_cursorOff(void)
{
   cmd(0x0C);
}



void lcd_init()
{
  cmd(0x03);    //lcd reset cmds
  msdelay(1);
  cmd(0x02);    //lcd reset cmds
  msdelay(1);
  cmd(0x28);    //2 lines 5x7 matrix
  msdelay(1);
  cmd(0x0C);    //display on,cursor off,blink off
  msdelay(1);
  cmd(0x01);    //clear display
  msdelay(1);
  cmd(0x06);    //entry mode, set increment
  msdelay(1);
}


void dat (unsigned char data1)
{
    data1=~data1;
    unsigned char l,h;
    h = data1 & 0xF0;
    l = data1 << 4;

    delay(4);
    RS=0;
    delay(4);
    PORTC = 0XF0;
    delay(4);
    PORTC = h;   // Write Upper nibble of data
    delay(4);
    EN=0;
    delay(4000);
    EN=1;
    delay(4);
    PORTC = 0XF0;  // Make Data pins zero
    delay(4);
    PORTC = l;   // Write Lower nibble of data
    delay(4);
    EN=0;
    delay(4000);
    EN=1;

}



void cmd(unsigned char command)
{

    command =~ command;
    unsigned char l,h;

    delay(4);
    RS=1;
    delay(4);
    h = command&0xF0;
    l=command<<4;

    PORTC=0xf0;
    delay(4);
    PORTC=h;
    delay(4);
    EN=0;
    delay(4000);
    EN=1;


    delay(4);
    PORTC=0xf0;
    delay(4);
    PORTC=l;
    delay(4);
    EN=0;
    delay(4000);
    EN=1;
}








void string(unsigned char *p)
{
    while(*p!='\0')
    {
        //delay(20000);
        dat(*p);
        p++;
   }
}






int main()
{
    TRISB = 0x00;
    TRISC = 0x00;
    lcd_init();
    string ("EMBEDDED_LAB");

   
    while(1)
    {
    LATB6 = 1;
    delay(4);
    LATB6 = 0;
    delay(4);
    }
}

No comments:

Post a Comment