JHD162A LCD Code

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

/*----------------------------------------------------------------

-------------------- HEADER FILES -------------------------------


-----------------------------------------------------------------*/

#include<avr/io.h>
#include<string.h>
#include <util/delay.h>

/*----------------------------------------------------------------
------------- DEFINITIONS -------------------------------------
-----------------------------------------------------------------*/

#define DATA_DDR DDRC // ENTER THE PORT WHICH CONTROLS


#define DATA_PORT PORTC // THE DATA PINS D0 TO D7

#define CONTROL_DDR DDRD // ENTER THE PORT WHICH CONTROLS


#define CONTROL_PORT PORTD // THE CONTROL PINS
#define Enable 2 // CoNNECTION OF ENABLE TO PIN OF ABOVE PORT
#define RS 0 // CoNNECTION OF RegisterSelect TO PIN OF ABOVE
PORT
#define RW 1 // CoNNECTION OF Read/~Write TO PIN OF ABOVE
PORT
#define CONTROL_MASK 0X07 // CHANGE THIS VALUE CONSIDERING THE PINS ABOVE
AS HIGH

/*----------------------------------------------------------------
-------------CONTROL BITS OF LCD --------------------------------
-----------------------------------------------------------------*/

#define Set_Enable CONTROL_PORT |=_BV(Enable) //


THE MACROS HERE ARE
#define Clear_Enable CONTROL_PORT &=~_BV(Enable)
//SELF EXPLANATORY
#define Write_Lcd CONTROL_PORT &=~_BV(RW) //
#define Read_Lcd CONTROL_PORT |=_BV(RW) //
#define Select_InstructionRegister CONTROL_PORT &=~_BV(RS) //
#define Select_DataRegister CONTROL_PORT |=_BV(RS) //
#define Data_Lcd(b) DATA_PORT=b //
#define delay(b) _delay_ms(b) //

/*----------------------------------------------------------------
-----------------FUNCTION DECLARATIONS ---------------------------
-----------------------------------------------------------------*/

void Init_Ports(void); // Initialise Ports , Necessary for


selecting input output pins in the MCU
void Init_Lcd(void); // Initialise LCD , Necessary for starting
and clearing LCD screen
void Lcd_Send(unsigned char a); // For sending a character to the LCD
void newline(void); // For printing characters on the next line
in the LCD

/*----------------------------------------------------------------
----------------MAIN FUNCTION--------------------------------------
-----------------------------------------------------------------*/
void main()
{
int len,len1,len2;
char str1[] = "Ankit Daftery"; // String to be printed in 1st line
char str2[] = "'Tronix & Proud"; // String to be printed in 2nd line
Init_Ports();
Init_Lcd();
len1=strlen(str1);
len2=strlen(str2);

for (len=0;len<len1;len++) // Printing the first string


Lcd_Send(str1[len]);

newline(); // Go to new line

for (len=0;len<len2;len++) // Printing the 2nd string


Lcd_Send(str2[len]);

/*----------------------------------------------------------------
-----------------SEND A CHARACTER TO LCD-------------------------
-----------------------------------------------------------------*/
void Lcd_Send(unsigned char a)
{
Select_DataRegister; // Declares information that follows
as data and not instruction
Write_Lcd; // Declared information is to be
written
Data_Lcd(a); // Send the character passed to the
function to LCD to write
Set_Enable; // Sets enable,
delay(5); // and then
Clear_Enable; // Clears it,
delay(5); // to be ready for next character.
}

/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
DATA_DDR=0XFF; // Setting data port for
output
CONTROL_DDR=CONTROL_MASK; // Setting selected pins of
control port for output
CONTROL_PORT&=~(_BV(Enable)|_BV(RS )|_BV(RW)); // Setting values to 0 at
start
}

/*----------------------------------------------------------------
------------FUNCTION TO INITIATE LCD -----------------------------
-----------------------------------------------------------------*/
void Init_Lcd(void)
{
char init[10];
int i;
init[0] = 0x01; // Initialises the display
init[1] = 0x38; // 8 - Bit Operation, 2 Line Display,5*8
Dot Character Font
init[2] = 0x0e; // Turns on display and cursor
init[3] = 0x06; // Entry Mode Set
init[4] = 0x80; // Sets DDRAM address to beginning of
screen

Select_InstructionRegister;

Write_Lcd;
delay(15);

for(i=0;i<5;i++)
{
Data_Lcd(init[i]);
Set_Enable;
delay(5);
Clear_Enable;
delay(5);
}
}

/*----------------------------------------------------------------
------------FUNCTION TO GOTO NEXT LINE IN LCD --------------------
-----------------------------------------------------------------*/

void newline(void)
{
delay(5);
Select_InstructionRegister; // Declares information to follow as
instruction
Data_Lcd(0xc0); // Code to go to next line of LCD
Set_Enable; // -- Enable cycle --
delay(5); // | |
Clear_Enable; // | |
delay(5); // -- --
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy