Tuesday, 20 October 2015

PIC12F675 4BIT LCD INTERFACE





#include<pic.h>
#include<htc.h>
#define __PIC12F675_H                    //header file for ic
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);
#define _XLAT_FREQ 4000000             //define cpu frequency

// Define Pins
#define LCD_E    GP0   // Enable pin for LCD
#define LCD_CLK  GP1   // Serial clock pin
#define LCD_D    GP2   // Serial data pin

// Define Pins direction registrers
#define LCD_E_Dir      TRISIO0
#define LCD_CLK_Dir    TRISIO1
#define LCD_D_Dir      TRISIO2


void delay(unsigned int i)
{
while(i--);
}


void ToggE(void)
{
LCD_E = 1;                // Give a pulse on E pin
delay(500);      // so that LCD can latch the
LCD_E = 0;                // data from data bus
delay(500);
}


void WriteByteToLCD(unsigned Byte)
{
unsigned char BitCount = 0;

for(BitCount=0;BitCount<8;BitCount++)
{
LCD_D = (((Byte>>BitCount)&0x1)!=0);   // Write bit value

LCD_CLK = 1;                           // Toggle Clock pin to transfer it
delay(500);
LCD_CLK = 0;
delay(500);
}
}


void cmd(unsigned char c)
{
WriteByteToLCD((c&0xF0)>>4);   // Write Upper nibble
ToggE();
WriteByteToLCD(c&0x0F);        // Write Lower nibble
ToggE();
}


void data(unsigned char c)
{
WriteByteToLCD(((c&0xF0)>>4)|0x80);   // Write Upper nibble
ToggE();
WriteByteToLCD((c&0x0F)|0x80);        // Write Lower nibble
ToggE();
}


void lcd_init(void)
{
    cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);

}


void string(unsigned char *p)
{
while(*p!='\0')
{
data(*p);
p++;
}
}




void main(void)
{
ANSEL  = 0x00;       // Set ports as digital I/O, not analog input
ADCON0 = 0x00; // Shut off the A/D Converter
CMCON  = 0x07; // Shut off the Comparator
VRCON  = 0x00;     // Shut off the Voltage Reference
TRISIO = 0x08;       // GP3 input, rest all output
GPIO   = 0x00;       // Make all pins 0
lcd_init(); // Initialize LCD in 4bit mode

    string ("EMBEDDED LAB");
while(1);

}

No comments:

Post a Comment