PIC18F452 USART to LK204-25

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

Moderators: Henry, Mods

Post Reply
drkidd22
LCD?
Posts: 8
Joined: Fri Dec 05, 2008 10:02 am

PIC18F452 USART to LK204-25

Post by drkidd22 »

Hello all,

I have written a code in C18 for PIC 18F452 to drive the LCD using the serial cable. I'm able to display a line which states if an LED is on/off. This works just fine, the problem is that at the end of the text in row 1 there is a box that doesn't go away. RB0 LED IS: OFF[] or RB0LED IS: ON[][] (when the button on RA4 is pressed) Any help will be appreciated. Thanks.

Code: Select all

#include <p18f452.h>
#include <stdio.h>
#include <usart.h>
#pragma config WDT = OFF, OSC = HS, PWRT = ON, LVP = OFF, BOR = OFF
#define TRUE 1

void main (void)
{
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX & 
USART_BRGH_HIGH,12);

TRISA = 0xff;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;

WriteUSART(0xFE);
WriteUSART(0x58);

while(TRUE)
		  { 
if (PORTAbits.RA4 == 0)
	{
	
	PORTBbits.RB0 = 1;
	putrsUSART("\rRB0 LED IS: ON");
									}
else
	{
	PORTBbits.RB0 = 0;
	putrsUSART("\rRB0 LED IS: OFF");	
				             		}
           } 
CloseUSART();
}

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

Post by Raquel »

Hi,

Do you notice when this box first appears? Maybe comment out for now the while(1) code, and see if clear screen 0xFE, 0x58 does clear the screen.

Also, you might want to make sure the your code does actually what you expect it to do. Try connecting your output to the PC serial port using our uProject eval software to see all the characters being sent out. This means you will be disconnecting the display temporarily. If your USART is not in RS232 levels, you will have to have a transceiver before connecting the PC's serial port.

Best Regards,
Raquel Malinis
Design and Development
Matrix Orbital

drkidd22
LCD?
Posts: 8
Joined: Fri Dec 05, 2008 10:02 am

Post by drkidd22 »

the clear command works just fine and the lcd is able to display text. I was able to fix the problem i was having before using the printf command. anyways there was an F being left of when the button was pressed and message displayed ONF. I added a space to ON and fixed that, not sure if that is the right way. I think I have to clear the screen when pressing the button.

The other thing is that I'm trying to display text on the second row and that's not working for me. Not sure if the syntax to send the command is right.

Code: Select all

#include <p18f452.h>
#include <stdio.h>
#include <usart.h>
#pragma config WDT = OFF, OSC = HS, PWRT = ON, LVP = OFF, BOR = OFF
#define TRUE 1

void main (void)
{
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,12);

TRISA = 0xff;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;

WriteUSART(0xFE);					//Need to add delay before this...
WriteUSART(0x58);					//Clear the lcd boot screen..

while(TRUE)
        {
if (PORTAbits.RA4 == 0)				//If button RA4 goes LOW (pressed)... 
   {   
   PORTBbits.RB0 = 1;				       //Turn on LED RB0 and...
   	printf("\rRB0 LED IS:  ON");	               //<<Display the message
                               }
else
   {								//Otherwise if RA4 is HIGH...
   PORTBbits.RB0 = 0;				        //RB0 is kept LOW and the LED is off...
    printf("\rRB0 LED IS: OFF");                    //<<Display the message
                               }
           

WriteUSART(0xFE);				//prefix as byte
WriteUSART(0x47);                            //set cursor command byte
WriteUSART(0x02);				//[col] ?????
WriteUSART(0x02);                            //[row] ?????
			printf("\rROW2");
	}	
CloseUSART();
}


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

Post by Raquel »

What is doing right now? Do you see "ROW2" at all?
I wonder how your code is sending '\r' is it 0x10, 0x13 or 0x10 and 0x13?

Also, try adding a delay at the end of the loop to slow things down so we can catch what it is doing.
Raquel Malinis
Design and Development
Matrix Orbital

drkidd22
LCD?
Posts: 8
Joined: Fri Dec 05, 2008 10:02 am

Post by drkidd22 »

Ok..so I went away from my code to try to figure this out...

I wrote the following simple code...

Code: Select all

while(TRUE)
{
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0xFE);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);    
WriteUSART(0x47);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0x01);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);       
WriteUSART(0x01);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
printf("\rROW1");		//Display row1
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0xFE);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);    
WriteUSART(0x47);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0x01);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);       
WriteUSART(0x02);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
printf("\rROW2");		//Display row2
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0xFE);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);    
WriteUSART(0x47);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0x01);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);       
WriteUSART(0x03);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
printf("\rROW3");		//Display row3
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0xFE);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);    
WriteUSART(0x47);
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
WriteUSART(0x01);    
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);       
WriteUSART(0x04);		
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
printf("\rROW4");		//Display row4
for(x=0;x<10000;x++);
for(x=0;x<10000;x++);
			}	
CloseUSART();
} 
There is a delay between each data that is being sent to the LCD. Is there a specific delay at which data has to be sent to the LCD??...By the way this works just fine, but I think it could be better.

LCD Displays:

ROW1
ROW2
ROW3
ROW4

drkidd22
LCD?
Posts: 8
Joined: Fri Dec 05, 2008 10:02 am

Post by drkidd22 »

After days of playing around with the LCD and pic micro in serial I ithink your documentation needs some work. Some of your commands need before and after delays to get it working. The information might be somewhere but I can't find it.

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

Post by Raquel »

Hello,

The delay you have added, if you go back and take them one by one, you will find is not necessary. When you were running a tight loop like you had on your second code post, then it would be necessary.

The displays are meant for the human eye to decipher changes and be able to read. You need not to run it very quickly, otherwise the info displayed will be unintelligible.

We do not specify the delays on the manual because the need becomes totally subjective to the application. If you were not running a tight loop with both if and else constructs writing to the display at the same time ('ON' and 'OFF' in your code) you will find these delays are definitely not necessary.

Thanks,
Raquel Malinis
Design and Development
Matrix Orbital

Bushman
LCD?
Posts: 5
Joined: Tue Jan 16, 2007 10:31 pm

Post by Bushman »

drkidd22 wrote: WriteUSART(0xFE);
After every writeUSART() command you will need a while (BusyUSART());
The PIC's hardware USART has no buffer, so when you write two things back to back it will hose things.

Post Reply