Author Topic: HD44780 4 bit interface on PIC  (Read 5455 times)

0 Members and 1 Guest are viewing this topic.

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
HD44780 4 bit interface on PIC
« on: September 22, 2015, 01:58:53 pm »
Hi, ive made some code for interface of a HD44780 display by the 4 bit interface, and it wont compile, it says
Code: [Select]
main.c:57: warning: (361) function declared implicit int
main.c:61: error: (984) type redeclared
main.c:61: error: (1098) conflicting declarations for variable "dispchar" (main.c:60)
i dont get this, its probably an effect of my noobness  :P But can anyone help?
RD7-3 pins are connected as data
RD0 and RD1 are connected as RS and EN
Code: [Select]
#include <xc.h>
#include <stdlib.h>

#define EN LATDbits.LATD1
#define RS LATDbits.LATD0
#define _XTAL_FREQ 8000000


void main(void)
{
    OSCCONbits.IRCF = 0b1111;
    TRISD = 0b00000000;
    RS = 0;
    LATD = 0b00000000;
    LATD = 0b00100000;
    EN = 1;
    __delay_ms(2);
    EN = 0;EN = 1;
    __delay_ms(2);
    EN = 0;
    __delay_ms(2);
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b10000000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b11110000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    LATD = 0b01100000;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    dispchar("A");
}

void dispchar(char character)
{
    int charbitshifter;
    charbitshifter = character;
    charbitshifter << 4;
    LATD = charbitshifter;
    RS = 1;
    EN = 1;
    __delay_ms(2);
    EN = 0;
    charbitshifter = character;
    charbitshifter >> 4;
    charbitshifter << 4;
    LATD = charbitshifter;
    RS = 1;
    EN = 1;
    __delay_ms(2);
    EN = 0;
}
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5127
  • Country: nl
Re: HD44780 4 bit interface on PIC
« Reply #1 on: September 22, 2015, 02:14:48 pm »
Looks like it doesn't like that you stuff an int into LATD?

int charbitshifter;
...
...
LATD = charbitshifter;
Keyboard error: Press F1 to continue.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #2 on: September 22, 2015, 02:25:47 pm »
But i have tried it with other types, wont work either.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8270
Re: HD44780 4 bit interface on PIC
« Reply #3 on: September 22, 2015, 02:29:51 pm »
Move dispchar()'s declaration before main(). C requires declaration before use.
 

Offline matseng

  • Frequent Contributor
  • **
  • Posts: 563
  • Country: se
    • My Github
Re: HD44780 4 bit interface on PIC
« Reply #4 on: September 22, 2015, 02:31:58 pm »
Eh? The error message clearly states what the error is - it's not related to charbitshifter.

dispchar("A"); tries to pass a string to the dispchar function that is only accepting a char.   Change this line into dispchar('A'); and it will work. 

You should also either make sure that all functions are defined before they are used, or declare them first at the top.  In your code the function void dispchar(char character) is defined after it is being used in the main() function. This is not good and will generally emit warnings and errors.

So either move the entire function to above the main() or leave it where it is and just declare it before the main() by having this line up there:
Code: [Select]
void dispchar(char character);



Double quotes " denotes a string that can be empty "" hold one character "A" or many characters "I am a string".
A single quote is a singe char. It can and must only hold exactly one character. Like 'Q'.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #5 on: September 22, 2015, 02:42:15 pm »
OK, i got it to work, but my screen shows crap, and it changes from time to time.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #6 on: September 22, 2015, 03:22:16 pm »
I have tested it on 2 displays, so its not broken.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline Stonent

  • Super Contributor
  • ***
  • Posts: 3824
  • Country: us
Re: HD44780 4 bit interface on PIC
« Reply #7 on: September 22, 2015, 03:41:16 pm »
I have tested it on 2 displays, so its not broken.

I guess make sure that you're not shoving data too quickly into the lcd controller.  Maybe throw a few more delays in there in various locations to see if it helps.

Another option would be if you could slow down the mcu and see if it helps.
The larger the government, the smaller the citizen.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #8 on: September 22, 2015, 04:07:01 pm »
I did it, its now displaying UDUUUTEU and doing some random stuff like reseting (?)
Code: [Select]
/*
 * File:   main.c
 * Author: moondeck
 *
 * Created on 22 September 2015, 10:14
 */


#include <xc.h>
#include <stdlib.h>

#define EN LATDbits.LATD1
#define RS LATDbits.LATD0
#define _XTAL_FREQ 8000000
void dispchar(char character)
{
    int charbitshifter;
    charbitshifter = character;
    charbitshifter << 4;
    LATD = charbitshifter;
    RS = 1;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    charbitshifter = character;
    charbitshifter >> 4;
    charbitshifter << 4;
    LATD = charbitshifter;
    RS = 1;
    EN = 1;
    __delay_ms(200);
    EN = 0;
}

void main(void)
{
    OSCCONbits.IRCF = 0b1111;
    TRISD = 0b00000000;
    RS = 0;
    LATD = 0b00000000;
    LATD = 0b00100000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    __delay_ms(200);
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b10000000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b11110000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b00000000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    LATD = 0b01100000;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    while(1)
    {
        dispchar('T');
        dispchar('E');
        dispchar('S');
        dispchar('T');
        __delay_ms(1000);
    }
}
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: HD44780 4 bit interface on PIC
« Reply #9 on: September 22, 2015, 06:33:27 pm »
Your bit nibbling is not right, try:
    charbitshifter = character;
    charbitshifter  &= 0xf;
    LATD = charbitshifter;
    RS = 1;
    EN = 1;
    __delay_ms(200);
    EN = 0;
    charbitshifter = character;
    charbitshifter >>=  4;
    charbitshifter &= 0xf;
    LATD = charbitshifter;
  ... etc.
}
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #10 on: September 22, 2015, 06:48:20 pm »
Done, not showing UDUUUTEU anymore, but same amout of lines, looks kinda like E without the left side line. maybe i should just drop 4 bit and use 8 bit?
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: HD44780 4 bit interface on PIC
« Reply #11 on: September 22, 2015, 07:04:48 pm »
Oops! I've had a quick look at the data sheet and I think you need to reverse the order that you send the nibbles. I.e. send the high-order nibble first.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #12 on: September 22, 2015, 08:08:02 pm »
Yeah, but thats what i do, it still does not work.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: HD44780 4 bit interface on PIC
« Reply #13 on: September 22, 2015, 08:24:13 pm »
Can you confirm this:
RD7-3 pins are connected as data
RD0 and RD1 are connected as RS and EN
?
I think I may have mis-read your code and put the data bits at the wrong end of the byte. Also, when you write the data bits you need to have ANDed or ORed (as appropriate) in the RS and EN bits prior to writing to LATD - I believe "LATD = whatever" will set all the bits in the byte. What happens to the R/W bit?

Edit: I think this is how it should be done:
Code: [Select]
    charbitshifter = character  & 0xf0;    /* Get high order nibble */
    charbitshifter |=  b00000011;          /* Set EN and RS bits high */
    LATD = charbitshifter;                 /* Write byte to port */
     charbitshifter &=  b11111101;         /* Reset EN bit */
    LATD = charbitshifter;                 /* Write byte to port */

     charbitshifter = character << 4; ;    /* Get low order nibble */
      charbitshifter &=  0xf0;             /* Remove superfluous bits */
                                           /*  and repeat the writing operations */
    charbitshifter |=  b00000011;          /* Set EN and RS bits high */
    LATD = charbitshifter;                 /* Write byte to port */
     charbitshifter &=  b11111101;         /* Reset EN bit */
    LATD = charbitshifter;                 /* Write byte to port */
 
    __delay_ms(200);
  ... etc.
« Last Edit: September 22, 2015, 08:49:24 pm by Andy Watson »
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5024
  • Country: ro
  • .
Re: HD44780 4 bit interface on PIC
« Reply #14 on: September 22, 2015, 08:56:36 pm »
I'd suggest going over the initialization code again and the other functions, it looks like you're not doing it right.

For example, see the datasheet of some 2x16 or 4x16 displays using controllers compatible with hd44870 , they have all the steps explained and first thing I noticed is that usually a larger delay is required before actually sending data to lcd displays - you may be sending data too soon, and that data may be ignored by the lcd controller.

See page 18 and 19 in this datasheet : http://savedonthe.net/download/708/lcd_2x16.html   and same datasheet explains a few pages above what each command does and so on.

And if everything fails, test your lcd display with 8 bit mode and then work your way to 4bit.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #15 on: September 23, 2015, 05:03:41 am »
Thanks !  :D "TEST" works, but its still "reseting", flashing and doing crazy shit. Tried changing all delays from 2ms to 200ms
Can you confirm this:
RD7-3 pins are connected as data
RD0 and RD1 are connected as RS and EN
?
I think I may have mis-read your code and put the data bits at the wrong end of the byte. Also, when you write the data bits you need to have ANDed or ORed (as appropriate) in the RS and EN bits prior to writing to LATD - I believe "LATD = whatever" will set all the bits in the byte. What happens to the R/W bit?

Edit: I think this is how it should be done:
Code: [Select]
    charbitshifter = character  & 0xf0;    /* Get high order nibble */
    charbitshifter |=  b00000011;          /* Set EN and RS bits high */
    LATD = charbitshifter;                 /* Write byte to port */
     charbitshifter &=  b11111101;         /* Reset EN bit */
    LATD = charbitshifter;                 /* Write byte to port */

     charbitshifter = character << 4; ;    /* Get low order nibble */
      charbitshifter &=  0xf0;             /* Remove superfluous bits */
                                           /*  and repeat the writing operations */
    charbitshifter |=  b00000011;          /* Set EN and RS bits high */
    LATD = charbitshifter;                 /* Write byte to port */
     charbitshifter &=  b11111101;         /* Reset EN bit */
    LATD = charbitshifter;                 /* Write byte to port */
 
    __delay_ms(200);
  ... etc.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
« Last Edit: September 23, 2015, 07:54:52 am by obiwanjacobi »
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: HD44780 4 bit interface on PIC
« Reply #17 on: September 23, 2015, 07:57:47 am »
I did tie it to ground. In theory, i am able to use the 8 bit thing, i actually did a library for the 8 bit mode some time ago, but would like it to use less IO
Did you tie R/W to ground?

If its any help, here's a link to my implementation (that works).
The timeout values are in the profile class.

http://atl.codeplex.com/SourceControl/latest#Source/Code/ArduinoTemplateLibrary/HD44780_Driver.h
http://atl.codeplex.com/SourceControl/latest#Source/Code/ArduinoTemplateLibrary/HD44780_Controller.h
http://atl.codeplex.com/SourceControl/latest#Source/Code/ArduinoTemplateLibrary/HD44780_Profile.h

[2c]
I'm selling 100ml bottles of free energy, PM me for pricing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf