please help: lcd2041 and pic18f458

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

Post Reply
roo
LCD?
Posts: 5
Joined: Mon Oct 17, 2005 3:01 pm

please help: lcd2041 and pic18f458

Post by roo »

Hi guys,
I am a newbie, so please bear with me.
I am trying to connect PIC18F458 to the LCD2041 (rev.1.5).
I used the code below (CCS C complier) and it didn't work: the LCD keeps displaying a bunch of meaningless characters.

Then I found out since I only have the bare PIC, I can't directly use
RS232. I read in LCD 2041 manual that the LCD can be talked to in TTL
directly by "closing" 3 gaps above the "TTL" mark above the
RS232 connection.

So, i soldered those 3 gaps and tried the same code again.
Still the same meaningless characters!
So, please advise how to talk in TTL to the LCD.
Also, it'd be greatly appreciated if you guys could tell me how to
use I2C or convert TTL to RS232 to talk to the LCD.
Thanks a lot



Code:

#include <18F458.h>
#fuses EC,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay (clock=1000000)
#use rs232(baud=2400, xmit=PIN_C6, rcv = PIN_C7)

void main() {

while(TRUE){

printf("Hello");

}
}

Tom
Matrix Orbital
Matrix Orbital
Posts: 1030
Joined: Mon Jul 19, 2004 4:43 pm
Location: Calgary
Contact:

Post by Tom »

Hi roo,

Thank you for posting on the forum.

When you communicate with the device, you must also make sure the baud rate is set the same on the pic and the display. The default baud rate is 19,200, but you can change it by configuring the jumpers to correspond to the table shown on page 11 of the manual at http://www.matrixorbital.ca/manuals/LCD ... 41_150.pdf

Let me know how things turn out.

Best Regards,

roo
LCD?
Posts: 5
Joined: Mon Oct 17, 2005 3:01 pm

Post by roo »

While trying RS232, i used baud 9600
and set the jumpers accordingly (J1,2,4 out, J3 in).
It didn't work.
The code
#use rs232(baud=2400, xmit=PIN_C6, rcv = PIN_C7)
was my last attempt (the baud was the only
thing I could change, so I just tried different rates in desperation).
Thanks, Tom

Tom wrote:Hi roo,

Thank you for posting on the forum.

When you communicate with the device, you must also make sure the baud rate is set the same on the pic and the display. The default baud rate is 19,200, but you can change it by configuring the jumpers to correspond to the table shown on page 11 of the manual at http://www.matrixorbital.ca/manuals/LCD ... 41_150.pdf

Let me know how things turn out.

Best Regards,

Raquel
Matrix Orbital
Matrix Orbital
Posts: 813
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi roo,
I am sorry to hear that the suggestion given did not work; we were almost certain that the cause of the problem was the baud rate since the code you've sent looks good. How about we start again and let's take one step at a time, i checked that with the 1MHz you are running the PIC, you should run 9600 baud reliably. So let's try 9600 baud to start and hopefully get something meaningful before we go back to your desired baud rate of 2400. After setting the jumpers for 9600 (Jumper3 in, others out), make sure that you change the #use rs232 baud to 9600. We will explicitly write a RS232 transmit function in this test and skip the printf for now, though with the code you sent, it looks good.
Give the following code a try; BUT you must supply the correct address for the registers, and pin out for RS232 since this sample code is written for PIC16F648.

Code: Select all

//UART Register definitions
#byte TXSTA	=0x98
#byte RCSTA	=0x18
#byte TXREG	=0x19
#byte RCREG	=0x1A
#byte SPBRG	=0x99
#bit  BRGH		=TXSTA.2
#bit  SPEN		=RCSTA.7
#bit  TXIF		=PIR1.4
#bit  RCIF		=PIR1.5
#bit  RCIE		=PIE1.5

// RS232 Pin Definitions
#bit RS232_BUS_RX=PORTB.1
#bit RS232_BUS_RX_DIR=TRISB.1
#bit RS232_BUS_TX=PORTB.2
#bit RS232_BUS_TX_DIR=TRISB.2

void Uart_TX (byte c) {
	while(~TXIF);
	TXREG = c;	
}	

void InitRS232 (void) {
	// setup USART comm parameters
	RS232_BUS_RX_DIR = 1;			// make the receive pin input
	RS232_BUS_TX_DIR = 0;			// make the transmit pin output
	RS232_BUS_TX = 0;					// output a low to start
	RCSTA = 0x90;						// only bits SPEN, CREN are set/enabled
	TXSTA = 0x20;						// only bit TXEN is set/enabled  			 
	BRGH = 1;							// overwrite BRGH bit in TXSTA for 16F648A
	SPBRG = 12;							// 19200 for 4 MHz, BRGH = 1, double check this value for 16F648A
}

void main () {
	byte string1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
	byte i;
	
	InitRS232();
	
	for (;;)	{
		// send "Hello World" to the module via UART
		i = 0;
		while (string1[i] != '\0') {
			Uart_TX(string1[i++]);
			delay_ms(100);					// delay in between character send
		}		
}
Please let me know how things turn out.
Raquel Malinis
Design and Development
Matrix Orbital

roo
LCD?
Posts: 5
Joined: Mon Oct 17, 2005 3:01 pm

Post by roo »

Man you're good! Thanks a lot.
It has finally worked!
I used your code (please see the code after modification for PIC18F458 below) and it worked with 4Mhz and baud=19200.
However, when I use 1Mhz and 2400baud, the meaningless characters
came back. CCS compiler says "Baud rate out of range" when i used
1Mhz and 9600baud. The next baud option I found in the manual
was 2400, and that didn't work.
That is not a big deal now that the LCD is at least working at 4Mhz and
19200.
Could you please tell me how to use printf() now or if I can't, how to use
Uart_TX to print values of variables.. ?
It's midnight and am so excited i can't sleep.
Thanks !!!




#include <18f458.h>

#fuses EC,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay (clock=4000000)

#use rs232(baud=19200, xmit=PIN_C6, rcv = PIN_C7,ERRORS)

//UART Register definitions

#byte TXSTA =0xFAC
#byte RCSTA =0xFAB
#byte TXREG =0xFAD
#byte RCREG =0xFAE
#byte SPBRG =0xFAF
#byte PIR1 =0xF9E
#byte PIE1 =0xF9D
#byte TRISC =0xF94
#byte PORTC =0xF82

#bit BRGH =TXSTA.2
#bit SPEN =RCSTA.7
#bit TXIF =PIR1.4
#bit RCIF =PIR1.5
#bit RCIE =PIE1.5





// RS232 Pin Definitions
#bit RS232_BUS_RX=PORTC.7
#bit RS232_BUS_RX_DIR=TRISC.7
#bit RS232_BUS_TX=PORTC.6
#bit RS232_BUS_TX_DIR=TRISC.6

void Uart_TX (byte c) {
while(~TXIF);
TXREG = c;
}

void InitRS232 (void) {
// setup USART comm parameters
RS232_BUS_RX_DIR = 1; // make the receive pin input
RS232_BUS_TX_DIR = 0; // make the transmit pin output
RS232_BUS_TX = 0; // output a low to start
RCSTA = 0x90; // only bits SPEN, CREN are set/enabled
TXSTA = 0x20; // only bit TXEN is set/enabled
BRGH = 1; // overwrite BRGH bit in TXSTA for 16F648A
SPBRG = 12; // 19200 for 4 MHz, BRGH = 1, double check this value for 16F648A
}

void main () {
byte string1[12] = {'H','e','l','l','o',' ','W','o','r','l','d','\0'};
byte i;

InitRS232();

for (;;) {
// send "Hello World" to the module via UART
i = 0;
while (string1 != '\0') {
Uart_TX(string1[i++]);
delay_ms(100); // delay in between character send
}
}
}

Raquel
Matrix Orbital
Matrix Orbital
Posts: 813
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

I am glad to hear that you have something working now. I checked the baud rate table for the PIC that you are using and it says that it should have no problem with 2400, in fact the percent error for 1200, 2400, 9600 and 19200 are all the same. Maybe you've forgotten to set the SPBRG value when you tried 2400? And if the compiler is already complaining about baud rate out of range, then it something to watch out for -- and appently it does not work. Be careful when setting the SPBRG value, you can find this value in the datasheet.
As per sending values of variables using Uart_TX, you just pass the variable as parameter. I think you can make out of the sample code. Honestly I myself stay away from printf's, they use up more code space anyway, and they require you need to follow the compiler carefully.

Code: Select all

void main () {
	byte varA;
	varA = 'A';
	
	for (;;) {
		Uart_TX(varA++);
		if (varA == 'z') break;		
	}	
}
Again, if you need help, do not hesitate to post.
Raquel Malinis
Design and Development
Matrix Orbital

roo
LCD?
Posts: 5
Joined: Mon Oct 17, 2005 3:01 pm

Post by roo »

That works great. Thank you!
This makes me wonder what exactly was wrong earlier.
Was it the baud rate or because I didn't close the TTL gaps ?
Would it work now if I desoldered the TTL gaps ?
I'd like to know for sure to be ready for my next project.
Please advise !

P.S. I also gave printf() another shot and it worked at 4Mhz and 19200.
printf() did sometimes print out garbage though, and normally
doesn't start printing stuff out until the 3rd iteration of a loop.
A stand alone printf() wouldn't work !!!

Raquel
Matrix Orbital
Matrix Orbital
Posts: 813
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi roo,
Glad to hear that things are working out. I have a feeling that it is likely the baud rate that caused your problem earlier. If your microcontroller will be tied to the LCD2041 directly (which is what you have right now), then the TTL connections (gaps) MUST be made. Only if there will be RS232 level translation in between then the jumps are unnecessary.
Please post any more concerns you may have so that we may help you out to start your project. I wish you luck.
Raquel Malinis
Design and Development
Matrix Orbital

roo
LCD?
Posts: 5
Joined: Mon Oct 17, 2005 3:01 pm

Post by roo »

Thanks for all the help, Raquel :D
I appreciate it, a lot.

Post Reply