code of lcd16x2.c and l
cd16x2.h
#include <xc.h>
#include "lcd16x2.h"
void lcd_setpins(unsigned char data) {
if ((data & 0x01) == 0x01)
RC0 = 1;
else
RC0 = 0;
if ((data & 0x02) == 0x02)
RC1 = 1;
else
RC1 = 0;
if ((data & 0x04) == 0x04)
RC2 = 1;
else
RC2 = 0;
if ((data & 0x08) == 0x08)
RC3 = 1;
else
RC3 = 0;
}
void lcd_cmd(unsigned char cmd) {
unsigned char high_bits;
unsigned char low_bits;
high_bits = (cmd & 0xF0)>>4;
low_bits = cmd & 0x0F;
RS = 0; // RS = instruction
lcd_setpins(high_bits);
EN = 1; // enable = high
EN = 0;
lcd_setpins(low_bits);
EN = 1; // enable = high
EN = 0;
}
void lcd_init(void) {
RS = 0; // instruction
lcd_setpins(0x00);
__delay_ms(20);
lcd_cmd(0x03);
__delay_ms(5);
lcd_cmd(0x03);
__delay_ms(11);
lcd_cmd(0x03);
/////////////////////////////////////////////////////
lcd_cmd(0x02); // 4-bit mode
lcd_cmd(0x28); // 4-bit, 2 Line, 5x7 Dots 101000
lcd_cmd(0x0C); // Display on Cursor off
lcd_cmd(0x06);// Entry mode, auto increment I/D with no shift.
}
void lcd_print_char(unsigned char data) {
unsigned char high_bits;
unsigned char low_bits;
high_bits = (data & 0xF0)>>4;
low_bits = data & 0x0F;
RS = 1; // RS = data
lcd_setpins(high_bits);
EN = 1; // enable = high
EN = 0;
lcd_setpins(low_bits);
EN = 1; // enable = high
EN = 0;
}
void lcd_print_str(char *str) {
int i = 0;
for (i=0; str[i] != '\0'; i++) {
lcd_print_char(str[i]);
}
}
void lcd_setcursor(unsigned char x, unsigned char y){ // line 1 or 2, 1 tot 16
unsigned char temp;
if(x == 1) // 1000 - 0000 to 1111
{
temp = 0x80 + y - 1;
lcd_cmd(temp);
}
else if (x == 2) // 1100 - 0000 to 1111
{
temp = 0xC0 + y - 1;
lcd_cmd(temp);
}
}
void lcd_makechar(unsigned char *pattern, const char location){
int i=0;
lcd_cmd (0x40+(location*8)); //Send the Address of CGRAM
for (i=0; i<8; i++) {
lcd_print_char(pattern[i]); //Pass the bytes of pattern on LCD
}
}
void lcd_clr(void) {
lcd_cmd(0x01);
lcd_cmd(0x02);
}
//----------------------lcd16x2 header file----------------------
#ifndef LCD16X2_H
#define lcd16X2_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */
#include <xc.h> // include processor files - each processor file is guarded.
#define RS RB5
#define EN RB6
#define _XTAL_FREQ 4000000
void lcd_init(void);
void lcd_cmd(unsigned char cmd);
void lcd_print_char(unsigned char data);
void lcd_setcursor(unsigned char x, unsigned char y);
void lcd_print_str(char *str);
void lcd_makechar(unsigned char *pattern, const char location);
void lcd_clr(void);