• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Initialising 16x2 LCD

Status
Not open for further replies.

noobC

New Member
Hi, I like to ask how is the procedure like to program a 16x2 LCD (RT1602C) using 4-bits transmission and MCC-18 and PIC18F4520.

I had th"ESE" code which I found from the internet. However, I had tried all of them but still could not show anything on the LCD. There are times where it flashed some black box on the LCD but there are really rare and when I just amend some lines, there are no boxes anymore. Hence I could not find out what is the mistake.

I am using RT1602C. where the pins are as followed:

Code:
1---------------------GND      
2---------------------VDD (This is the supply  voltage for Logic)       
3    ----------------V0 (Contrast) Connect to 1k resistor then to GND [color="#ff0000"] 
[/color]4    ----------------RS       
5    ----------------R/W      
6    ---------------E (Enable)      
7    ---------------DB0 (ignore as I am using 4  bits)       
8    ---------------DB1 (ignore as I am using 4  bits)      
9    ---------------DB2 (ignore as I am using 4  bits)      
10  ---------------DB3 (ignore as I am using 4  bits)     
11    -------------DB4 (Connected to Microchip  RD0)      
12    -------------DB5 (Connected to Microchip  RD1)      
13    -------------DB6 (Connected to Microchip  RD2)      
14    -------------DB7 (Connected to Microchip  RD3)     
15    -------------BL1 (connected straight to  +5V)       
16    -------------BL2 (connected straight to  GND)

After initializing the port as output, these are the program:

Code:
Lcd_VEE = 1;
LATD=0b10000000; 
Delay10KTCYx(50); 
Write_Lcd (0x03,0);
Delay10KTCYx(50); 
Write_Lcd (0x03,0);
Delay10KTCYx(50); 
Write_Lcd (0x03,0);
Delay10KTCYx(50); 
Write_Lcd (0x02,0);
Delay10KTCYx(50); 
Write_Lcd (0x02,0);
Delay10KTCYx(50); 


Write_Lcd (0x20,0); //B0010 0000
Write_Lcd (0x28,0); //B0010 1000
Write_Lcd (0x06,0); //B0010 1000
Write_Lcd (0x0e,0); //B0000 1100
Write_Lcd (0x01,0); //B0000 0001
Write_Lcd (0x02,0); //B0000 0010
I had these code from "some" website, but realising the need to set x03 and x02 in the begining. But I am still not know what is wrong with the program.

Pls help. Thanks
 

colin mac

New Member
This is some C code I wrote for the microchip compiler to send strings to a LCD in 4-bit mode. You can modify it for your compiler.

Code:
#include <p18F2520.h> 
#include <delays.h>

#pragma config OSC = RC
#pragma config LVP = OFF, PWRT = ON, WDT = OFF
#pragma config PBADEN = OFF
#pragma config DEBUG = OFF

#define D7 LATBbits.LATB7
#define D6 LATBbits.LATB6
#define D5 LATBbits.LATB5
#define D4 LATBbits.LATB4
#define E LATBbits.LATB3
#define RS LATBbits.LATB2

void functionSet(void);
void entryMode(void);
void displayOn(void);
void sendChar(char c);
void enable (void);
void sendString(far rom char *str);
void moveTo2nd(void);

void main(void)
{
  TRISB = 0;
  Delay10TCYx(80);
  functionSet();
  entryMode();
  displayOn();
  sendString("Hello");
  moveTo2nd();
  sendString("World");
  while(1);
}

// 0010 - 4 bit mode
// 0010 1010 - 2 lines, 5 X 8.
void functionSet(void)
{
  RS= 0;
  D7 = 0;
  D6 = 0;
  D5 = 1;
  D4 = 0;
  enable();
  Delay10TCYx(80);
  enable();
  D7 = 1;
  enable();
  Delay10TCYx(80);
}

void enable(void)
{
  E = 1;
  Delay10TCYx(1);
  E = 0;
}

void entryMode(void) //0000 0110, increment with no shift
{
  RS = 0;
  D7 = 0;
  D6 = 0;
  D5 = 0;
  D4 = 0;
  enable();
  D6 = 1;
  D5 = 1;
  enable();
  Delay10TCYx(80);
}

void displayOn(void) //0000 1111. cursor on. blinking on.
{
  RS = 0;
  D7 = 0;
  D6 = 0;
  D5 = 0;
  D4 = 0;
  enable();
  D7 = 1;
  D6 = 1;
  D5 = 1;
  D4 = 1;
  enable();
  Delay10TCYx(80);
}


void sendString(far rom char *str)
{
  int index = 0;
  while (str[index] != '\0')
  {
     sendChar(str[index]);
     index++;
  }
}

void sendChar(char c)
{
  RS = 1;
  D7 = (c >> 7) & 1;
  D6 = (c >> 6) & 1;
  D5 = (c >> 5) & 1;
  D4 = (c >> 4) & 1;
  enable();
  D7 = (c >> 3) & 1;
  D6 = (c >> 2) & 1;
  D5 = (c >> 1) & 1;
  D4 = (c & 1);
  enable();
  Delay10TCYx(80);
}

void moveTo2nd(void)   //1100 0000 - move cursor to 2nd line
{
  RS =  0;
  D7 = 1;
  D6 = 1;
  D5 = 0;
  D4 = 0;
  enable();
  D7 = 0;
  D6 = 0;
  enable();
   Delay10TCYx(80);
}
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top