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.

Weird C18 function problem, or my stupidity?

Status
Not open for further replies.

edeca

Active Member
The following is all written in C18 and tested with the Oshonsoft simulator, which is an excellent bit of software.

I'm trying to write some basic LCD code that uses delays, so I can save the RW pin (yes, I've ordered the bits for the 2 wire interface!).

This function works fine for a normal LCD and I can get the word "Test" on the display:

Code:
void LCDChar(unsigned char c) {
	LATB = (c >> 4) & 0x0f;
	LCD_RS = 0;
	LCDEnable();

	LATB = (c & 0x0f);
	LCD_RS = 0;
	LCDEnable();

	Delay5ms();
	return;
}

LCD_RS is just defined to the right LATA pin, I'll do the same for LCD_DATA later.

So I tried to make an LCDNibble (like the 2 wire example has):

Code:
void LCDNibble(unsigned char n, unsigned char rs) {
	//LATB &= 0xf0;	// Clear lower 4 bits of port
	//LATB ^= n;		// Move nibble to port (COMMENTED FOR TESTING)
	LATB = n;
	LCD_RS = rs;	// Set RS line
	LCDEnable();	// Pulse EN line
}

And I call it like this:

Code:
void LCDChar(unsigned char c) {
	unsigned char tmp;

	tmp = (c >> 4) & 0x0f;
	LCDNibble(tmp, 0);

	tmp = c & 0x0f;
	LCDNibble(tmp, 0);

	Delay5ms();
	return;
}

I have tried with and without the temporary variable, e.g. calling like LCDNibble((c>>4)&0x0f, 0). However this displays "P`pp".

I'm really tempted to simply have two seperate functions that do their own port setting but there must be something funny that the compiler does which breaks it. Or maybe I've been really stupid!

Can anybody spot my mistake?
 
I've given up on this as it doesn't reduce code anyway. The resulting C18 demo code is in another thread.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top