Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

/*

* File: lcdFunctions.c
* Author: david
*
* Created on August 9, 2021, 12:19 PM
*/

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

// Function to write control instruction, 4-bit at a time


void lcdWriteCtrl(unsigned char x)
{
LCD_RS = 0;
LCD_DATA = x;
LCD_E = 1;
__delay_ms(1);
LCD_E = 0;
__delay_ms(1);
LCD_DATA = x<<4;
LCD_E = 1;
__delay_ms(1);
LCD_E = 0;
__delay_ms(1);
}

// Function to write data, 4-bit at a time


void lcdWriteData(unsigned char x)
{
LCD_RS = 1;
LCD_DATA = x;
LCD_E = 1;
__delay_ms(1);
LCD_E = 0;
__delay_ms(1);
LCD_DATA = x<<4;
LCD_E = 1;
__delay_ms(1);
LCD_E = 0;
__delay_ms(1);
}

// Function to initialize LCD


void initLCD()
{
// Special Sequence a) to d) required for 4-bit interface
__delay_ms(15); // a) 15ms LCD power-up delay
lcdWriteCtrl(0b00000011); // b) Function Set (DB4-DB7:8-bit interface)
__delay_ms(5); // c) 5ms delay
lcdWriteCtrl(0b00000010); // d) Function Set (DB4-DB7:4-bit interface)
lcdWriteCtrl(0b00101000); // Function Set - 4-bit, 2 lines, 5x7
lcdWriteCtrl(0b00001100); // Display on, cursor off
lcdWriteCtrl(0b00000110); // Entry mode - inc addr, no shift
lcdWriteCtrl(0b00000001); // Clear display & home position
}

void lcdSetPosition(unsigned char row, unsigned char col)


{
unsigned char ramAddr;

if(row == 0) // If row is 0
ramAddr = 0x00 + (col - 1); // Subtract 1 from the col
else // If row is 1
ramAddr = 0x40 + (col - 1); // Add 0x40 to ramAddr, and
// subtract 1 from the col

lcdWriteCtrl(0x80 + ramAddr); // Write ctrl instruction to


// DD RAM Addr
}

You might also like