LCD control with PIC24HJ128GP502

Status
Not open for further replies.

drejas

Newbie level 1
Joined
Jul 20, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Poland
Activity points
1,291
Hello!

I have a little problem with controlling the LCD: https://pl.farnell.com/everbouquet/mc1602m-syr/modul-lcd-alfanumeryczny-16x2/dp/9448632?Ntt=9448632 with PIC24HJ128GP502.

I tried different configurations and programms, it still doesn't work. The LCD is connected in the correct way, because I can see something on the screen, problem is surely with the program. I want it to be in 4-bit mode. The for older bits of the controller are connected to pins: RB9-RB12, RS is RA0, RW is RA1 and E is RA4, busyflag is read from pin RB12 of course.

In the code there is a function called "beep" with pin "piezo" defined. Don't worry about it, it's just an external sound when displaying a text.

All configuration bits are being set in MPLAB and I'm using internal clock, or 20,48MHz external.

here's the code, with this one and any different code I tried, what I get is black rectangles in all 16x2 places. Please help! What is wrong?

Code:
#include <p24HJ128GP502.h>
#include <stdio.h>
#include <stdlib.h>

void lcd_line1(void);			//function prototypes
void lcd_line2(void);
void lcd_cmd(unsigned char);
void lcd_char(char);
void e_togg(void);
void lcd_init(void);
void lcd_string(char *);
void lcd_busy(void);
void delay(void);
void beep(int);

#define	E		LATAbits.LATA4
#define	RS		LATAbits.LATA0
#define RW		LATAbits.LATA1
#define	busyflag	PORTBbits.RB12
#define RW_TrisBit	TRISAbits.TRISA1
#define D7_TrisBit	TRISBbits.TRISB12
#define piezo		LATBbits.LATB8

char sentence1[] = "pic24h  ";
char sentence2[] = "bbbbb    ";
char sentence3[] = "aaaa";
char sentence4[] = "dfsdfsfsdsd  ";

int main(void)
{
	ADPCFG = 0xffff;		//all digital
	TRISA = 0;			//PORTs all outputs
	TRISB = 0;
	RW = 0;				//set R/W low
	E = 0;				//set E low
	lcd_busy();			//wait for LCD to settle
	lcd_init();

	
	while(1)
	{
		lcd_line1();
		lcd_string(sentence1);	//send string to LCD
		lcd_line2();
		lcd_string(sentence2);
		delay();
		lcd_line1();
		lcd_string(sentence3);	//send string to LCD
		lcd_line2();
		lcd_string(sentence4);
		beep(80);
		delay();
	}
	return 0;
}

void beep(int tone)
{
	int x,y;
	for(x=0;x<80;x++)
	{
		piezo = 1;
		for(y=0;y<tone;y++){}
		piezo = 0;
		for(y=0;y<tone;y++){}
	}
}	

void lcd_string(char *senpoint)
{
	while(*senpoint != '\0')
	{
		lcd_char(*senpoint);
		senpoint++;
	}
}	

void lcd_busy(void)
{
	RW_TrisBit = 1;			//make R/W input (read)
	D7_TrisBit = 1;			//make D7 input
	RS = 0;				//set RS low
	RW = 1;				//set R/W high
	E = 1;				//set E high
	while(busyflag);		//wait for busy flag to go low
	E = 0;				//set E low
	RW = 0;				//set R/W low
	TRISB = 0;			//make D7 output
	RW_TrisBit = 0;			//make R/W output (write)
}	
	
void lcd_line1(void)
{
	lcd_cmd(0x80);
}

void lcd_line2(void)
{
	lcd_cmd(0xc0);
}		

void lcd_cmd(unsigned char letter)
{
	LATB = letter;			//put char in PORTB
	lcd_busy();
	PORTB = PORTB << 5;		//shift over to output high 4 bits on RB9,10,11,12
	RS = 0;				//RS low
	e_togg();			//latch the data
	PORTB = PORTB << 4;		//shift over to output low 4 bits
	RS = 0;				//RS low
	e_togg();			//latch it
}

void lcd_char(char letter)
{
	LATB = letter;			//put char in PORTB
	lcd_busy();
	PORTB = PORTB << 5;		//shift over to output high 4 bits on RB9,10,11,12
	RS = 1;				//RS high
	e_togg();			//latch the data
	PORTB = PORTB << 4;		//shift over to output low 4 bits
	RS = 1;				//RS high
	e_togg();			//latch it
}

void lcd_init(void)
{
	LATB = 0x0600;			//send 3
	e_togg();
	lcd_busy();
	LATB = 0x0600;
	e_togg();
	lcd_busy();
	LATB = 0x0600;
	e_togg();
	lcd_busy();
	LATB = 0x0400;			//send 2 - set 4-bit mode
	e_togg();
	lcd_busy();
	lcd_cmd(0x28);			//set 4-bit mode and 2 lines
	lcd_busy();
	lcd_cmd(0x10);			//cursor move & shift left
	lcd_busy();
	lcd_cmd(0x06);			//entry mode = increment
	lcd_busy();
	lcd_cmd(0x0d);			//display on - cursor blink on
	lcd_busy();
	lcd_cmd(0x01);			//clear display
	lcd_busy();
}

void e_togg(void)
{
	E=1;
	E=0;
}

void delay(void)
{
	int x,y;
	for(x=0;x<50;x++)
	{
		for(y=0;y<10000;y++){}
	}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…