Continue to Site

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.

  • 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.

C18 LCD code and 18F1320

Status
Not open for further replies.

edeca

Active Member
I'm trying to use the C18 xlcd library to control a 16x2 LCD with an 18F1320. I've never used the screen before, but it initalises OK according to my question in another thread. I can see all blocks on one row (I assume the top).

I've only built this on breadboard so far and I have the following connections (tested with a multimeter between the PIC pin and the LCD):

RB0: Pin 11 of LCD
RB1: Pin 12 of LCD
RB2: Pin 13 of LCD
RB3: Pin 14 of LCD
RB4: Pin 6 of LCD (Enable)
RB5: Pin 4 of LCD (RS)
RB6: PGC to Junebug
RB7: PGD to Junebug

I have tied pin 5 (RW) and pins 7 to 10 (data) to ground.

I'm using U5V and the Junebug power (approx. voltage is about 4.3V on the LCD control pins). I have a capacitor across the power rails and also a resistor/LED to show when it is on.

My code is below. I've traced it with MPLAB and it never gets past the call to OpenXLCD(). A breakpoint on the line below never fires and it appears to sit waiting for the busy flag to clear.

One thing that seems really odd is this. When the code hits the line if(DATA_PORT&0x08), which checks the BUSY flag, pin 14 of the LCD (RB3) reads 0v. But a watch on PORTB shows 10011111. This means the BUSY flag is always set and it gets stuck in the BusyXLCD() loop, despite the fact there is no voltage on the pin.

The only other things that share the pin are CCP and P1A, I have tried disabling these with the following code but it didn't help:

Code:
CCP1CONbits.CCP1M0 = 0;
CCP1CONbits.CCP1M1 = 0;
CCP1CONbits.CCP1M2 = 0;
CCP1CONbits.CCP1M3 = 0;

If anybody can tell me what's wrong with it, or test it to see if it works for you, that would be excellent.

Code:
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config OSC = INTIO2
#pragma config MCLRE = ON
#pragma config DEBUG = ON
#pragma config PWRT = ON
#pragma config BOR = OFF

#include <p18F1320.h>
#include <xlcd.h>
#include <delays.h>

void DelayFor18TCY( void )
{
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
}
void DelayPORXLCD (void)
{
	Delay1KTCYx(30); // Delay of 15ms
	// Cycles = (TimeDelay * Fosc) / 4
	// Cycles = (15ms * 8MHz) / 4
	// Cycles = 30,000
	return;
}
void DelayXLCD (void)
{
	Delay1KTCYx(10); // Delay of 5ms
	// Cycles = (TimeDelay * Fosc) / 4
	// Cycles = (5ms * 8MHz) / 4
	// Cycles = 10,000
	return;
}

void main( void )
{
	OSCCON = 0x72;  /* Set internal oscillator at 8 Mhz */
	ADCON0 = 0x00;  /* Disable internal ADC */
	ADCON1 = 0x7F;  /* Set all PORTB to digital I/O */

	// Have attempted delaying for up to 6*15ms here

	// configure external LCD
	OpenXLCD( FOUR_BIT & LINES_5X7 );
	while(BusyXLCD());
	WriteCmdXLCD(BLINK_ON);
	while(BusyXLCD());

	WriteDataXLCD(0x47);	// write "Good Morning Dave"
	while(BusyXLCD());
	WriteDataXLCD(0x6F);
	while(BusyXLCD());
	// More the same as the above

	while(1);
}
 
I should have been clearer, sorry. The 18F1320 is also on the breadboard and I'm only using the ICSP/debug features of the Junebug.

I hope to turn it into something useful, so would rather it ran under its own steam eventually!
 
edeca said:
I have tied pin 5 (RW) and pins 7 to 10 (data) to ground.
...
My code is below. I've traced it with MPLAB and it never gets past the call to OpenXLCD(). A breakpoint on the line below never fires and it appears to sit waiting for the busy flag to clear.

I did not look at the code. But if you have R/W tied to gnd then the LCD busy flag (IRRC pin 14 aka D7) will always be an input. You can not read from the LCD without setting the LCD RW pin to read, you have it hard wired to write.

You can make it work by delaying instead of checking the busy flag. Check the LCD lib and see if there is a setting for this. If not you will have to alter the code, maybe in several places.

HTH
 
Thanks 3V0, I didn't know that! I'll give it a go tomorrow. Unfortunately one of the ICSP connectors is on the default RW port, so I'll have to modify the header file to move it elsewhere. Even in 4-bit mode, it still takes so many pins!

Your sample code (found via Google hosted on an IP address somewhere) and the JPUG newsletter were both really useful when understanding this.

Thanks again, if this fixes it I'll owe you! :)
 
That is one of the tidiest breadboards I have ever seen! Mine rely on pieces of wire sticking up all over the place.

I think I'll build that circuit after I've got it working with all pins, thanks!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top