HD44780 4 bit interface on PIC - Page 1
Forum section: electronics / general

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 01:58:53 pm
content: Hi, ive made some code for interface of a HD44780 display by the 4 bit interface, and it wont compile, it saysCode:main.c:57: warning: (361) function declared implicit intmain.c:61: error: (984) type redeclaredmain.c:61: error: (1098) conflicting declarations for variable "dispchar" (main.c:60)i dont get this, its probably an effect of my noobnessBut can anyone help?RD7-3 pins are connected as dataRD0 and RD1 are connected as RS and ENCode:#include <xc.h>#include <stdlib.h>#define EN LATDbits.LATD1#define RS LATDbits.LATD0#define _XTAL_FREQ 8000000void 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;}

author: PA0PBZ
author_rank: Super Contributor
timestamp: September 22, 2015, 02:14:48 pm
content: Looks like it doesn't like that you stuff an int into LATD?int charbitshifter;......LATD = charbitshifter;

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 02:25:47 pm
content: But i have tried it with other types, wont work either.

author: amyk
author_rank: Super Contributor
timestamp: September 22, 2015, 02:29:51 pm
content: Move dispchar()'s declaration before main(). C requires declaration before use.

author: matseng
author_rank: Frequent Contributor
timestamp: September 22, 2015, 02:31:58 pm
content: 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 intodispchar('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 functionvoid 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 justdeclareit before the main() by having this line up there:Code: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'.

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 02:42:15 pm
content: OK, i got it to work, but my screen shows crap, and it changes from time to time.

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 03:22:16 pm
content: I have tested it on 2 displays, so its not broken.

author: Stonent
author_rank: Super Contributor
timestamp: September 22, 2015, 03:41:16 pm
content: Quote from: Moondeck on September 22, 2015, 03:22:16 pmI 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.

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 04:07:01 pm
content: I did it, its now displaying UDUUUTEU and doing some random stuff like reseting (?)Code:/** 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 8000000void 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);}}

author: Andy Watson
author_rank: Super Contributor
timestamp: September 22, 2015, 06:33:27 pm
content: Your bit nibbling is not right, try:Quote from: Moondeck on September 22, 2015, 04:07:01 pm

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 06:48:20 pm
content: 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?

author: Andy Watson
author_rank: Super Contributor
timestamp: September 22, 2015, 07:04:48 pm
content: 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.

author: Moondeck
author_rank: Regular Contributor
timestamp: September 22, 2015, 08:08:02 pm
content: Yeah, but thats what i do, it still does not work.

author: Andy Watson
author_rank: Super Contributor
timestamp: September 22, 2015, 08:24:13 pm
content: Can you confirm this:RD7-3 pins are connected as dataRD0 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: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.

author: mariush
author_rank: Super Contributor
timestamp: September 22, 2015, 08:56:36 pm
content: 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.htmland 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.

author: Moondeck
author_rank: Regular Contributor
timestamp: September 23, 2015, 05:03:41 am
content: Thanks !"TEST" works, but its still "reseting", flashing and doing crazy shit. Tried changing all delays from 2ms to 200msQuote from: Andy Watson on September 22, 2015, 08:24:13 pm

author: obiwanjacobi
author_rank: Super Contributor
timestamp: September 23, 2015, 07:53:08 am
content: 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.hhttp://atl.codeplex.com/SourceControl/latest#Source/Code/ArduinoTemplateLibrary/HD44780_Controller.hhttp://atl.codeplex.com/SourceControl/latest#Source/Code/ArduinoTemplateLibrary/HD44780_Profile.h

author: Moondeck
author_rank: Regular Contributor
timestamp: September 23, 2015, 07:57:47 am
content: 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 IOQuote from: obiwanjacobi on September 23, 2015, 07:53:08 am