100% found this document useful (1 vote)
117 views

16x2 Basic LCD Driver Files XC8

This document defines functions for controlling a 16x2 LCD display in 4-bit parallel mode using a PIC microcontroller. It includes functions for initializing the LCD, writing commands and data, setting the cursor address, and writing strings to the display. The LCD is controlled using 4 I/O pins for data lines and two additional pins for the read/write control and enable signals. Delays are used when toggling the enable signal to synchronize data transfers to the LCD.

Uploaded by

asmedmiranda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
117 views

16x2 Basic LCD Driver Files XC8

This document defines functions for controlling a 16x2 LCD display in 4-bit parallel mode using a PIC microcontroller. It includes functions for initializing the LCD, writing commands and data, setting the cursor address, and writing strings to the display. The LCD is controlled using 4 I/O pins for data lines and two additional pins for the read/write control and enable signals. Delays are used when toggling the enable signal to synchronize data transfers to the LCD.

Uploaded by

asmedmiranda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//16x2 LCD functions - XC8 - 4 bit parallel mode

#define _XTAL_FREQ 16000000


#define LCD_WR_Data LATC // RC0 thru RC3
#define LCD_RS LATCbits.LATC4
#define LCD_E LATCbits.LATC5

#define LCD_Data_TRIS TRISC


#define LCD_RS_TRIS TRISCbits.TRISC4
#define LCD_E_TRIS TRISCbits.TRISC4

//lcddriver.h header file


/* 16x2 lcd driver header file - 4 bit */

void delay_ms(unsigned int delay);


void LCD_Write_Data(unsigned char data);
void LCD_Write_Cmd(unsigned char cmd);
void LCD_Set_Addr(unsigned char line,unsigned char addr);
void LCD_Init(void);
void LCD_Write_Str(char * st);
void LCD_Write_ConstStr(const char * st);

//lcdheader.c function source file


/* 16x2 LCD driver functions - 4bit mode */
/* modified for LATx */

#include <xc.h>
#include "hardware.h"

// LCD commands
#define Clear 0x01
#define Home 0x02

void delay_ms(unsigned int delay)


{
unsigned int i;
for (i=0;i<delay/10;i++)
__delay_ms(10);
}
void LCD_Write_Data(unsigned char data)
{
LCD_RS = 1;
LCD_WR_Data = ((LCD_WR_Data&0xF0)|(data>>4&0x0F)); // upper nibble first
__delay_us(10);
LCD_E = 1;
__delay_us(10);
LCD_E = 0;
__delay_us(10);
LCD_RS = 1;
LCD_WR_Data = ((LCD_WR_Data&0xF0)|((data)&0x0F)); // lower nibble
__delay_us(10);
LCD_E = 1;
__delay_us(10);
LCD_E = 0;
}

void LCD_Write_Cmd(unsigned char cmd)


{
char temp;
LCD_RS = 0;
LCD_WR_Data = ((LCD_WR_Data&0xF0)|(cmd>>4&0x0F)); // upper nibble first?
__delay_us(10);
LCD_E = 1;
__delay_us(10);
LCD_E = 0;
__delay_us(10);
LCD_WR_Data = ((LCD_WR_Data&0xF0)|((cmd)&0x0F)); // lower nibble
__delay_us(10);
LCD_E = 1;
__delay_us(10);
LCD_E = 0;
}

void LCD_Set_Addr(unsigned char line,unsigned char addr) // line (1 ro 2), char


(1,2,3....)
{
if (((line==1)||(line==2))&&((addr>0)&&(addr<41)))
LCD_Write_Cmd(0x80|(((line-1)*0x40)+addr-1)); // 4 bit interface - set
address
}

void LCD_Init(void)
{
LCD_Data_TRIS = 0xC0; // set to output mode 4 bit lower nibble mode
delay_ms(150);
LCD_Write_Cmd(0x30); // 4 bit interface
delay_ms(150);
LCD_Write_Cmd(0x30); // 4 bit interface
delay_ms(150);
LCD_Write_Cmd(0x20); // 4 bit interface
delay_ms(150);
LCD_Write_Cmd(0x20); // 4 bit interface
delay_ms(150);
LCD_Write_Cmd(0x28); // 4 bit interface - 2 lines
delay_ms(150);
LCD_Write_Cmd(0x08); // 4 bit interface - display off
delay_ms(150);
LCD_Write_Cmd(0x01); // 4 bit interface - clear display
delay_ms(150);
LCD_Write_Cmd(0x06); // 4 bit interface - entry mode
delay_ms(150);
LCD_Write_Cmd(0x0E); // 4 bit interface - display ON
delay_ms(150);
LCD_Write_Cmd(0x80); // 4 bit interface - set address 1st char 1st line
delay_ms(150);
}

void LCD_Write_Str(char * st)


{
while (*st != 0x00)
{
LCD_Write_Data(*st);
st++;
}
}

void LCD_Write_ConstStr(const char * st)


{
while (*st != 0x00)
{
LCD_Write_Data(*st);
st++;
}
}

You might also like