Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

trouble with interfacing 4x20 lcd to PIC24HJ64GP502

Status
Not open for further replies.

pani212

Newbie level 1
Joined
Dec 5, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,304
Hi

I am using microchip microstick which uses pic24hj64gp502. there are only 28 pins in the ic and for my application i can afford to use the lcd only in 4 bit mode. with my code i think there is some problem with initialization(i am not sure though). the clock that i am using is of 40MHz. all i can see flickering screen.
my code is as below:

#include "pic24_all.h"
#define RS_HIGH() _LATB9 = 1
#define RS_LOW() _LATB9 = 0
#define CONFIG_RS() CONFIG_RB9_AS_DIG_OUTPUT()
#define RW_HIGH() _LATB13 = 1
#define RW_LOW() _LATB13 = 0
#define CONFIG_RW() CONFIG_RB13_AS_DIG_OUTPUT()
#define En _LATB14
#define CONFIG_E() CONFIG_RB14_AS_DIG_OUTPUT()
#define LCD4O _LATB5
#define LCD5O _LATB6
#define LCD6O _LATB7
#define LCD7O _LATB8
#define LCD8 _LATB15


#define CONFIG_LCD4_AS_OUTPUT() CONFIG_RB5_AS_DIG_OUTPUT()
#define CONFIG_LCD5_AS_OUTPUT() CONFIG_RB6_AS_DIG_OUTPUT()
#define CONFIG_LCD6_AS_OUTPUT() CONFIG_RB7_AS_DIG_OUTPUT()
#define CONFIG_LCD7_AS_OUTPUT() CONFIG_RB8_AS_DIG_OUTPUT()
#define CONFIG_LCD8_AS_OUTPUT() CONFIG_RB15_AS_DIG_OUTPUT()


#define strobe() ((En=1),(En=0))

void configBusAsOutLCD(void)
{
RW_LOW(); //RW=0 to stop LCD from driving pins
CONFIG_LCD4_AS_OUTPUT(); //D4
CONFIG_LCD5_AS_OUTPUT(); //D5
CONFIG_LCD6_AS_OUTPUT(); //D6
CONFIG_LCD7_AS_OUTPUT(); //D7
}
void LCDbus(uint8_t u8_c)
{
LCD4O = u8_c & 0x01; //D4
LCD5O = (u8_c >> 1)& 0x01; //D5
LCD6O = (u8_c >> 2)& 0x01; //D6
LCD7O = (u8_c >> 3)& 0x01; //D7
}
void writeLCD(unsigned char c)
{
configBusAsOutLCD();
RS_HIGH();
LCDbus(c >>4);
strobe();
LCDbus(c);
strobe();
}

void initLCD()
{
RS_HIGH();
RW_LOW();
En =0;
DELAY_MS(50);
writeLCD(0x20); // 4 bit interface
DELAY_US(40);
writeLCD(0x28); // 2 line display, 5x7 font
DELAY_US(40);
writeLCD(0x06); // set entry mode
DELAY_US(40);
writeLCD(0x0E);
DELAY_US(40);
writeLCD(0x01); // clear display, move cursor to home
DELAY_MS(2);
}
void writeString(char *data)
{
RS_HIGH();
while(*data)
{
writeLCD(*data);
data++;
}
}

int main ()
{
configBasic(HELLO_MSG); // Set up heartbeat, UART, print hello message and diags
CONFIG_LCD8_AS_OUTPUT();

doHeartbeat();




initLCD(); //initialize the LCD

writeString("******Hello, my name is Bob********");
LCD8=1;
DELAY_MS(2000);
writeLCD(0xC0); // cursor to 2nd line

writeString("-----these lines are moving!-------");

while (1)
{
writeLCD(0x18); // shift left
DELAY_MS(200);
doHeartbeat();
}

return 0;
}
 

I found that timing is critical when writing LCDs otherwise you get nothing or it flickers
this code work for the Microchip Explorer 16 LCD
Code:
//Define LCD line position
#define LCD_Line1    0
#define LCD_Line2    40
#define LCD_Line3    80 


// LCD display ports etc
#define LCDdata PORTE //LATE					// data portE
#define LCDdataEnable	TRISE

#define E  LATDbits.LATD8   			// LCD E Clock RD9
#define Eenable TRISDbits.TRISD8

#define CS  LATDbits.LATD11   			// LCD chip select RD10
#define CSenable TRISDbits.TRISD11

#define RS LATDbits.LATD10				// LCD RS Register Select bit RD11
#define RSenable TRISDbits.TRISD10

#define RW LATDbits.LATD13				// read/write bit in LCDstatus RD12
#define RWenable TRISDbits.TRISD13

void lcd_delay() {int i; for(i=0;i<2;i++)  Nop();}// mSecDelay(2); }  // if LCD does not work make this longer

// Write instruction to the LCD
void writeInstruction(int LCDregister)
{
    //mSecDelay(2);					// delay for instruction
    // check if last instruction complete
    LCDdataEnable |= 0xff;          // set bits 0-7 input for data
    Nop();
    RW=1;
    Nop();
    RS=1;
    Nop();
    while(LCDdata &0x80)Nop();			// read bit of status
    LCDdataEnable &= 0x00;          // set bits 0-7 output for data
    RS=1;
    Nop()
    RW=0;
    Nop();
    LCDdata=LCDregister;			// set up instruction
    E=1;							// take clock E high 
	lcd_delay();
    E=0;
 }

// Write to register of the LCD
void writeDataRegister(int data)
{
    RS=0;
    Nop()
    RW=0;
    Nop()
    LCDdata=data & 0xff;
	lcd_delay();
    E=1;					// take clock E high 
	lcd_delay();
    E=0;
 }

// Write a register and data register value to the LCDs
void writeCommand(int LCDregister, int data)
{
 	writeInstruction(LCDregister);
    writeDataRegister(data);
}

void LCDcursor(int cursor)
{
	writeCommand(0x0A, cursor & 0xF);		// write lower addres
	writeCommand(0x0B, (cursor > 4) & 0xF);	// write upper addres
}

// write a data character byte to LCD
int lcdPutchar(int data)
{
    writeCommand(0x0C, data);
    return 1;
}




// Initialise the LCD 
void lcdInit()
{
	int i,data = 0;
    int m=128, n=240;
    int Hn=40;
    int Hp=n/Hn, Vp=m/16;
    int Nx=m;
    printf("m %d n %d Hn %d Hp %d Vp %d Nx %d\n",m , n , Hn , Hp , Vp , Nx );

	E=0;				// take E low
 	RS=0;				// Take RS pin low for command
 	RW=0;				// Take RS pin low for command
 	CS=0;				// Take CS pin low for command
    
    // set RS, RW and E bits as output
	Eenable =0;
	RSenable =0;
	RWenable =0;
	CSenable =0;
	
    LCDdataEnable = 0x00;          // set bits 0-7 output for data
  //  LCDdataEnable &= 0x0;          // set bits 0-3 output for data


 
  	writeCommand(0x0, 0x34);		// Set Mode: display ON, Master, blink on, cursor on, character mode internal CG
  	writeCommand(0x1, 0x75);	
  	writeCommand(0x2, 0x27);	
  	writeCommand(0x3, 0x7F);	
  	writeCommand(0x4, 0x07);	
  	writeCommand(8, 0x00);	
  	writeCommand(9, 0x00);	
  	writeCommand(10, 0x00);	
  	writeCommand(11, 0x00);	
	lcdClear();						// Clear LCD screen
	lcdHome();						// goto home position	
}

// move to line (1 or 2) at position >= 1
void lcdCursor(unsigned char line,unsigned char position)
{
    int cursor = 40*(line-1) + position-1;
  	writeCommand(0x0a,cursor);
  	writeCommand(0x0b,cursor>>8);
}

void lcdString(unsigned char line, unsigned char position, unsigned char *string)
{

	lcdCursor(line,position);
	while (*string)
	    lcdPutchar(*string++);

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top