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 example (with delays)

Status
Not open for further replies.

edeca

Active Member
Here's a quick and dirty set of routines for C18 and the usual Hitachi LCD chipset. I need to figure out strings in C18 next and then I think it's finished.

The code was tested with the Oshonsoft Simulator which helped greatly with the development.

If anybody actually finds this useful, I could wrap it up into a header. For now, I'll just use it as is.

I might even try it on breadboard tonight, but I don't know if I hold out any hope for it actually working ;)

Thanks to Nigel, 3v0 and Futz who I borrowed code from. This has taken me a week, I guess I should really move on and make something useful now.
 

Attachments

  • simple lcd.c
    3.7 KB · Views: 2,292
You will want to check out the C18 documentaton and look for the parts dealing with H_USER.

The system is setup so that if you define a function user_putc (need to check the name) you can then write

fprintf(H_USER,"Hello World);
fprintf(H_USER,"Count %i",x);
 
You will want to check out the C18 documentaton and look for the parts dealing with H_USER.

Thanks 3v0. I hadn't even checked the documentation yet, I think it should be possible with a static string and a function to iterate over it.

I still can't get this darn thing to work on a breadboard though. The unused data lines read 5v on the LCD itself, but I was assuming that this didn't matter as RW is tied to GND.

If anybody with a definitely working LCD can try the code I'd be really grateful. I'm going to find a 16F628 in my box and wire up Nigel's example to make sure mine isn't broken.
 
I do not have an LCD of that type setup but

Is the enable high long enough with a single nop ?
Code:
#define PulseEnable { LCD_EN = 1; Nop(); LCD_EN = 0; }

The recheck to be sure you are sending the correct init commands/bytes. This is often a problem.
 
Is the enable high long enough with a single nop ?
Code:
#define PulseEnable { LCD_EN = 1; Nop(); LCD_EN = 0; }

The recheck to be sure you are sending the correct init commands/bytes. This is often a problem.

It's what Nigel uses in his example code and it works with the simulator. The simulator is pretty good at warning on timing errors.

I have just tried Nigel's code with a 16F628 and get nothing from the LCD again. I do have another on the way.. I'll test with that.

For now it is time to move on to a useful project that doesn't need an LCD, or I'll lose all of my hair!
 
It's what Nigel uses in his example code and it works with the simulator. The simulator is pretty good at warning on timing errors.

I have just tried Nigel's code with a 16F628 and get nothing from the LCD again. I do have another on the way.. I'll test with that.

My LCD tutorial code is well tried and tested, and in use by many people all over the world. If you wire it up EXACTLY as my tutorial it WILL work, most common problems are wiring it wrong, or not fitting (and adjusting) the contrast control.

Even without a PIC connected, the LCD can be tested roughly, adjust the contrast and you should be able to get a line of full black squares, adjust the contrast until they are fairly faint black squares, and that should be enough to work when you connect the PIC to it.

For now it is time to move on to a useful project that doesn't need an LCD, or I'll lose all of my hair!

It's quite difficult to get an LCD working, I went through loads of examples off the net that didn't work, and eventually wrote my own.
 
Last edited:
Thanks Nigel. I will check the wiring again tonight. I do get a line of black squares and haven't forgotten the contrast control!

Originally I was going to use an LCD in my project but for now I'll use a few status LEDs as they are easier to put in a box neatly :)
 
hi,
dont use c18 library, just use my code.before using my code,define the RS RW E and Data pins that you use.I wrote this code for 8 bit mood.in this code,only you have to convert your data to a string.thats all



#include <stdio.h>
#include <P18CXXX.h>
#include <delays.h>
#include <timers.h>

#pragma code page // This code make sure the program starts from 0x0004, not from 0x0000
#pragma config WDT = OFF

#define RS PORTEbits.RE0
#define dirRS TRISEbits.TRISE0
#define RW PORTEbits.RE1
#define dirRW TRISEbits.TRISE1
#define E PORTEbits.RE2
#define dirE TRISEbits.TRISE2
#define DATALCD PORTD
#define dirDATA TRISD
#define LCDBusy PORTDbits.RD7



void LCDdata (unsigned char);
void busylcd(void);
void LCDinit (void);
void LCDDelay( void );
void lcdcmd(unsigned char);
void stringtoLCD(unsigned char *m);
unsigned char * itoa (unsigned int value, unsigned char * string);

unsigned char dataString[10];


void main (void) // main function
{
int integer=650;
unsigned char *biginin;
biginin=&dataString[0];
TRISD = 0;
PORTD = 0;
TRISB = 0; // Port,pin direction configuration
PORTB = 0;
TRISC = 0;
PORTC = 0;
TRISCbits.TRISC7 = 1; // make sure this pin is input
ADCON1=7;
dirRS = 0;
LCDinit();
while (1) // this is how to make a never ending loop.
{
lcdcmd(0x1);
lcdcmd(0x2);
itoa (integer, biginin);
stringtoLCD(dataString);
}

}
//////////////////////////////////////////////////////////////////////////////////////
//LCD finctions------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////////////
void LCDdata (unsigned char value)
{
busylcd();
TRISD = 0;
DATALCD = value;
RS=1;
RW=0;
E=1;
LCDDelay();
E=0;

}
//--------------------------------------------------------------------------------------
void LCDinit(void)
{
TRISE=0;
lcdcmd(0x38); //set 8-bit mode and 2 lines
lcdcmd(0x10); //cursor move & shift left
lcdcmd(0x06); //entry mode = increment
lcdcmd(0x0d); //display on - cursor blink on
lcdcmd(0x01); //clear display
}
//-------------------------------------------------------------------------------------
void busylcd(void)
{
RS=0;
RW=1;
TRISD=255;
E=0;
LCDDelay();
E=1;
while(LCDBusy==1){
E=0;
LCDDelay();
E=1;
}
}
//-------------------------------------------------------------------------------------
void lcdcmd(unsigned char temp)
{
busylcd();
RS=0;
RW=0;
TRISD=0;
DATALCD=temp;
E=1;
LCDDelay();
E=0;
}

//--------------------------------------------------------------------------------------
void LCDDelay(void)
{
int i=0;
for (i=0;i<250;i++);

}
//---------------------------------------------------------------------------------------
void stringtoLCD(unsigned char *m)
{
unsigned char i;
i = 0;
while(m != 0)
{
LCDdata(m);
i++;
}

}
 
For my lcd i use this:

Code:
void LCDString(unsigned char *str){
    while(*str != 0)
        LCDdata(*str++);
}

and to display a string on it i call it like:
Code:
LCDString((unsigned rom char*)"AtomSoftTech");
 
this code is written in c18. i sead do not use c18 lcd library.in c18 library, it asks delay function to wait until lcd finished its work. but when you are checking busy pin, you know the exact time when lcd get free. you do not need to write delay functions. try it if you think it will worth.


kasunth

if your code is not in c18, what is it in?
PICC18?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top