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.

Some C Code...

Status
Not open for further replies.

bsodmike

New Member
Hello guys,

I'm about to start working with a 1x16 LCD and am trying to start somewhere. I'm currently looking at an example included with the PICC-Lite compiler but this code has me quite confused:

Code:
/*
 *	PORTA bit 2 is connected to the LCD RS input (register select)
 *	PORTA bit 3 is connected to the LCD EN bit (enable)
*/

static bit LCD_RS	@ ((unsigned)&PORTA*8+2);	// Register select
static bit LCD_EN	@ ((unsigned)&PORTA*8+3);	// Enable

#define	LCD_STROBE	((LCD_EN = 1),(LCD_EN=0))


/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{
	PORTB = (PORTB & 0xF0) |  (c >> 4);
	LCD_STROBE;
	PORTB = (PORTB & 0xF0) |  (c & 0x0F);
	LCD_STROBE;
	DelayUs(40);
}

First of all the little bit at the top about PORTA*8+2 :shock: Could some one please explain this bit of code, it's quite alien to me.

Strobing: Why can't we just set PORTA, 3 high, then low rather than using this odd code?

Running the LCD in 4bit mode has me confused. From what I can see it takes the '8bit' value, rotates it right 4 times, so you end up sending the lower 4 bits (why the | ?) Then c & 0x0F is that masking off the lower 4 bits such that you only send the top most bits (i.e. bits 0 - 3) which makes up the full 8bits?

Thanks,

Mike
 
the | is more bitwise operation.
they clear the lower 4 bits of PORTB, then use the 'or' to set them equal to first the upper 4 bits of the data (c>>4) and then do it again for the lower 4 bits, with a strobe after each one.

the PORTA*8+2 crap at the top looks like PICC's way of defining a bit variable mapped to a certain bit in a certain register. it takes the address of PORTA (&PORTA) and multiplies it by 8 (8 bits per byte) and then adds 2... therefore if PORTA were at address 100, it would be the 802nd bit you were looking at. yes, it's an odd way of doing it, but i've seen it used in other compilers... (although it's usually not the preferred method because it's rather obscure)

for the strobing, well, that's basically what they're doing... if this is sample code for PICC, most likely that happened to be a more efficient way of doing it. toggling PORTA bit 3 with boolean operators would be much more intuitive and you could do it that way instead.
 
Thanks for clearing that up evandude... I think I'm gonna go with custom code rather than basing this on the example by Hi-Tech.

--Mike
 
good plan. and do yourself a favor, don't get too discouraged if you can't get any signs of life out of the LCD at first... getting the LCD initialization routine working was 90% of the work for me :lol: after that, displaying characters is a snap.

what compiler are you using? or are you just doing assembly?

I have some code you might be able to use as well, but it would be formatted for BoostC (and without much modification, CC5x)
 
I'm thinking of purchasing the 14-bit compiler from CCS, as I'm a bit tired of PICC-Lite, what do you suggest?

Code would be handy thanks :D
 
I'd highly recommend BoostC and corresponding SourceBoost IDE.

You can use the free version of BoostC to compile this LCD stuff, if you just want to try it out. I've attached the entire project directory zipped up.

Note that the code is for a serial LCD controller, so you will probably want to just look at the LCD init and display routines. a lot of this stuff isn't anywhere near to optimized code, it's just that once I got it all working I didn't mess with it any more :lol:
 

Attachments

  • serlcdc.zip
    50.1 KB · Views: 367
Hey there evandude... I'm pulling my hair out as I've developed some fairly complex subroutines for rest of the tasks but I can't use/test them as I simply can not get the LCD to work.

Will try again next week...buh...

--Mike
 
yeah, I remember that well. I'd just say go over the init routine in the LCD datasheet very carefully, and use plenty of delay time in between steps, and also verify that your outputs are behaving as expected. Part of my problem was that one of the outputs I was using for the LCD was disabled because I hadn't turned off the comparator module or something like that. also, are you using 4-bit or 8-bit mode? I have used both, and I think 4-bit is generally better for normal use since saving 4 I/O lines is nice, but for initially getting signs of life I think 8-bit is easier, just one less thing to do wrong :lol:
 
evandude said:
I'd highly recommend BoostC and corresponding SourceBoost IDE.
Yes I too recommend BoostC. Good low-cost compiler with great features.
 
Thing is I'm comfortable with picclite and I'm actually running it in 4 bit mode via a 8bit shift register, via 3 control lines as per:

**broken link removed**

I'm using a 16f84A @ 20Mhz (HS/WDT off for now) and I'm using some 'delay' code supplied by Picclite them selves that define customs 'DelayUs/DelayMs' routines. I'm gonna pulse some LEDs on port B to see how far I've gotten in the code (only way to debug it visually). Right now Port A is dedicated for LCD coms :)

Thanks, Mike
 
unfortunately, between the shift register, and using PICC, I'm not sure how much I can help you, at least not from my past experiences... again, it seems to me that you should try to get it working with 8-bit, and THEN try 4-bit, and THEN move it over to the shift register... you're adding 2 extra levels of complication to it, no wonder it's challenging :lol:
 
Actually yes and no - but I think I've got it sorted. The problem is that I don't have the I/O pins to use to go from 8 - 4 - 3 :)

I'll try again on Monday and see, if all fails I'll take your advice!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top