LCD

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
Thank you, nerdegutta, I just did something similar

Anyway here is the code

Code:
/*
* File:   main.c
*Here we have adapted the code from JohninTx that is originally for 16F1787
* to our processor 18F4553
*
* Created on 2015/08/06, 16:57
*/

//#include <stdio.h>
#include <stdlib.h>

#include <xc.h>

// #pragma config statements should precede project file includes.

#pragma config PLLDIV = 5  //(20MHz crystal div by 5 preescale
#pragma config CPUDIV = OSC4_PLL6  //we get 16 MHz
#pragma config FOSC = INTOSC_HS   //Internal oscillator, HS oscillator used by USB (INTHS)
#pragma config USBDIV = 2  //Usb from PLL
#pragma config FCMEN    = OFF   //ADDED fail safe clock disabled
#pragma config IESO = OFF  //oscillator switch over mode disabke
#pragma config PWRT = OFF
#pragma config BOR = ON  //CHANGED
#pragma config BORV=3 //ADDED
#pragma config VREGEN = ON //CHANGED
#pragma config WDT = OFF
#pragma config WDTPS = 32768


#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF

#pragma config MCLRE = ON

#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config WRTD = OFF



#define _XTAL_FREQ 4000000      // 4MHz for the delay function

void initIO(void)
{
OSCCON = 0x62; // 0110 0010 set CPU Frequency as 4 MHz Internal Oscillator
TRISD=0x00;
PORTD=0x00;
//no analog inputs on D

}

//---------------------IO DEFINITIONS  --------------------------
// These are for the F4553
#define lcd_port  LATD        // write to LAT, read from PORT
// RD7-4 is the LCD 4 bit databus
#define LCD_RSout LATD2     // moved from ICSP
#define LCD_ENout LATD3

//-------------------- DISPLAY SETTINGS  ---------------------
// Define some display settings.  These could be defined as complete
// commands as well..
#define lcdLINE1 0x00       // where line 1 begins
#define lcdLINE2 0x40       // where line 2 begins

//--------------------- STROBE LCD ---------------------------
// Pulses E line on LCD to write
int strobeLCD(void)
{
LCD_ENout = 1;
__delay_us(2);     // Added a little here
LCD_ENout = 0;
}

//--------------------- WRITE 8 BIT DATA TO LCD  -----------------
// Assumes LCD is ready and RS is set to correct value
// LCD data bus is RD4-RD7
// Enable cycle time is a side effect of execution time - faster clocks
// may require a specific delay.
void writeLCD(unsigned char dat)
{
    lcd_port &= 0x0f;               // get current port, clear upper bits
    lcd_port |= (dat & 0xf0);       // combine w/upper nibble, leave lower same
    strobeLCD();

    lcd_port &= 0x0f;               // get current port, clear upper bits
    lcd_port |= ((dat <<4) & (0xf0)); // combine w/lower nibble, leave lower port same
    strobeLCD();
    __delay_ms(2);                // wait for display to process
}
//-------------------- WRITE LCD COMMAND  -------------------------
// Write cmd to LCD with RS=0
// Assumes E is low and display is NOT busy
void lcd_cmd (unsigned char cmd)
{
    LCD_RSout = 0;       // select command register
    writeLCD(cmd);
}
//---------------------- WRITE LCD DATA  --------------------------
// Write dat to LCD with RS=1
// Assumes E is low and display is NOT busy
void lcd_data (unsigned char dat)
{
    LCD_RSout = 1;       // select data register
    writeLCD(dat);
}

//-------------------- RESET/CONFIGURE LCD  -------------------------
// Delays are generous, trim when able

void lcd_init(void)
{
    lcd_port &= 0x0f;   // clear upper bits of LCD port
    lcd_port |= 0x30;   // direct data to LCD DB7-4
    LCD_RSout = 0;
    strobeLCD();        // write 3h, wait 10ms
    __delay_ms(10);
    strobeLCD();        // write 3h, wait..
    __delay_ms(10);
     strobeLCD();       // write 3h
    __delay_ms(10);

   lcd_port &= 0x0f;    // clear upper bits of LCD port
   lcd_port |= 0x20;    // direct data to LCD DB7-4
   strobeLCD();         // write 2h
   __delay_ms(10);

    lcd_cmd(0x28);       // Funciton Set: 4-bit mode - 2 line - 5x7 font.
    lcd_cmd(0x0e);       // DisplayON, cursor ON, blink OFF
    lcd_cmd(0x06);       // Automatic Increment - No Display shift.
    lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
}

//----------------------- WRITE STRING TO LCD  ---------------------
// Writes null terminated string to LCD from ROM
void lcd_WriteStr(const unsigned char *c)
{
    while(*c != '\0'){
        lcd_data(*c);
        c++;
    }
}

//--------------------  SETS CURSOR ANYWHERE IN DISPLAY MEMORY  ---------
// Valid locations are 0-79 decimal.  This doesn't check for valid location

void lcd_SetCursor(unsigned char loc)
{
    lcd_cmd(loc | 0x80);        // form and send 'set DDRAM address' cmd
}

//----------------- CANNED LINE COMMANDS  -------------------------
// For a 2 line display

void lcd_LINE1(void)
{
    lcd_SetCursor(lcdLINE1);
}

void lcd_LINE2(void)
{
    lcd_SetCursor(lcdLINE2);
}

//======================= MAIN =====================================
const char spin[] = {".oOo. "};

int main(int argc, char** argv)
{
    unsigned char i;
    initIO();               // init the chip IO
    __delay_ms(100);        // power up delay for LCD - adjust as necessary
    __delay_ms(100);
    __delay_ms(100);
    lcd_init();             // init the LCD
    lcd_WriteStr("Howdy there");
    lcd_LINE2();
    lcd_WriteStr("from TX!");
    // a little active display :)
    do{
        for(i=0; spin[i]!= '\0'; i++){
            lcd_SetCursor(lcdLINE2 + 11); // locate spinner
            lcd_data(spin[i]);
            __delay_ms(100);
            __delay_ms(100);
//            __delay_ms(200);
        }
    }while(1);       // do forever






    return (EXIT_SUCCESS);
}
I changed port C to Port D.
Some configuration options seemed not to be available to the 4553.

What to do now....
 

Jswale

Joined Jun 30, 2015
121
Is your wiring correct/is there a chance of the LCD been blown? That is what happened to mine, especially if the wiring is/has not always been correct..

Have you used MPLAB SIM to step through and see the PORTD output the data?
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
To make sure initialization is correct you need to eliminate all data write functions from your code and see if you are still getting those lines or if the LCD just gives blocks (hinting that it is ready for operation). If it still gives those lines then something is wrong.
Thank you. I did that and what I got was the line pattern blinking.

even when I do
C:
  lcd_cmd(0x01);
I still got the blinking




Is your wiring correct/is there a chance of the LCD been blown? That is what happened to mine, especially if the wiring is/has not always been correct..

Have you used MPLAB SIM to step through and see the PORTD output the data?
Oh mine I hope not :confused:, I checked the wiring since I have been changing it for every code I ve tried. In the first try I did the data lines were actually inverted but now they are ok. How do you see for sure if the LCD is blown? I dont want to go to the shop buy another one and have the same problem...

I am going to do the debuggin now

EDIT; while I was writting this the blinking dissapear. Now the LCD is blank as expected....

EDIT2 Now it is blinking again
 
Last edited:

Jswale

Joined Jun 30, 2015
121
If you got the line pattern blinking then there is a good chance that your LCD is fine because that is what is expected.

Try spamming the LCD with characters...

C:
lcd_cmd(0x80);
lcd__WriteStr("HHHHHHHHHHHHHHHH");
lcd_cmd(0xC0);
lcd__WriteStr("HHHHHHHHHHHHHHHH");
while(1)
Then match the characters to the datasheet, or see if you get those lines again...this will indicate in theory the command and data write is not working.

Edit: In the test code provided, there should be a blinking cursor...that is all... if you don't get that then the wiring is wrong OR the LCD is blown.
Please provide a picture of wiring etc.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
If you got the line pattern blinking then there is a good chance that your LCD is fine because that is what is expected.

Try spamming the LCD with characters...

C:
lcd_cmd(0x80);
lcd__WriteStr("HHHHHHHHHHHHHHHH");
lcd_cmd(0xC0);
lcd__WriteStr("HHHHHHHHHHHHHHHH");
while(1)
Then match the characters to the datasheet, or see if you get those lines again...this will indicate in theory the command and data write is not working.

Edit: In the test code provided, there should be a blinking cursor...that is all... if you don't get that then the wiring is wrong OR the LCD is blown.
Please provide a picture of wiring etc.
I got lines situated on two positions but on both lines of writing
I am going to solder up some wiring on a perfboard, put the LCD there and try again. so far I have only connected with jumper wires
 

Jswale

Joined Jun 30, 2015
121
I got lines situated on two positions but on both lines of writing
What do you mean? Lines as in lines of blocks or lines as in your previous pictures?

When you use the contrast pin, do full blocks appear? If not then the LCD is most certainly blown.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
What do you mean? Lines as in lines of blocks or lines as in your previous pictures?

When you use the contrast pin, do full blocks appear? If not then the LCD is most certainly blown.
When leaving everything except power and contrast unconnected, full blocks dont appear. just vertical lines (also the contrast have to be full value, anything lower means nothing appear)

also I notice the patterns change while I leave it there...


Cap3.jpg
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
I am suspecting the delays in the code might be wrong.
Specially the strobeLCD or the writeLCD

in the datasheet of my LCD it says

Enable Cycle Time 1000 ns
Enable Pulse Width Highlevel 450 ns
Enable Rise/Fall Time 25 ns
Address Set up Time 60 ns
Address Hold Time 20 ns
Data Set up time 80 ns
Data Delay Time 195 ns
Data Hold Time Writing 10 ns
Data Hold Time Reading 5 ns

What do you think?
 

Jswale

Joined Jun 30, 2015
121
If the lcd is compatible with the hitachi HD44780 then delays are a minimum NOT a maximum. They can be as big as you like!

The code works for that common type of display, (I'm currently using it) and @nerdegutta has also got it working.

I fear for the LCD if all wiring is correct.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
Make sure you are using 4 bit mode. RW is grounded and you are using the lower nibble (RD3-RD0).
Wha...? wh...? I am pretty sure I am using the upper nibble....:eek: RD4-RD7...

Have you checked the wiring? Check again.

Show us a schematic.
I discarded my last attempt and now I am going for John's program, adapting to a 18F4550. Therefore the schematic is pretty much his except that instead of Port C I use Port D (see attached)

If the lcd is compatible with the hitachi HD44780 then delays are a minimum NOT a maximum. They can be as big as you like!

The code works for that common type of display, (I'm currently using it) and @nerdegutta has also got it working.

I fear for the LCD if all wiring is correct.
I was going for the "they are not as big as necessary... I mean only 2 microseconds for the strobe....ummm suspicious

never mind.. I realized the thing I wrote already...:confused:

ummm I think I run out of ideas then
 

Attachments

Last edited:

Jswale

Joined Jun 30, 2015
121
Yes of course, apologies. It is RD4-RD7, I was doing it from memory.

Have you grounded RW?

EDIT: Schematic looks fine, it must be either physical wiring OR a difference between the PIC config.

What LCD are you using?
 
Last edited:

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
I inserted some delays and when trying to print HHHHHHHHHHHHHHHH and its equivalent in Xs I got
Cap4.jpg
all in the first line
everytime I set the line, the display goes blank
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
Yes of course, apologies. It is RD4-RD7, I was doing it from memory.

Have you grounded RW?

EDIT: Schematic looks fine, it must be either physical wiring OR a difference between the PIC config.

What LCD are you using?
Yes, I grounded RW
I changed the config settings from John since it seems the 16F1787 have totally different config options...
and I am using the TC1602E-06T

http://www.marutsu.co.jp/contents/shop/marutsu/datasheet/TC1602E-06T.pdf
(some parts in japanese sorry but plenty in english)

Also I found another datasheet(attachment) and there I see that for contrast they use a variable resistor of 20to 50K but I am using only one of 10K...
 

Attachments

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
I really dont know what to think anymore....
I did a debug run, and the same thing was happening... and I was thinking...etc
then I press the RESET button

then

Cap5.jpg

I guess some parts of it are blown.. but why display some on reset???

I changed the message and I got this
Cap7.jpg
but after some resets
Cap6.jpg
in the one with black squares I can see "Kansai R" being written

and I guess the other positions are blown
 
Last edited:

Jswale

Joined Jun 30, 2015
121
It looks compatible with the common Hitachi, so the initialization looks correct. Check the wiring and that they are connected to the right pins, the pin layout does look a little strange so don't assume you know what pin is what, check each pin!
Check the contrast pin IS the contrast pin by turning the POT.
Check the backlight by powering it up...

The fact that you are getting something on the LCD indicates that initialization is correct and the configuration is correct. I can only suspect either wiring OR the LCD has gone, ESPECIALLY if MPLAB SIM shows the pin outs performing correctly.
 
Top