HD44780 Library and displaying a char array ( ?° ?? ?°) - Page 1
Forum section: electronics / beginners

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 06:45:45 pm
content: Hi,I am a beginner in the field of programming. I am trying to write some code for PIC16F1829 to display characters on a standard HD44780 16x2 display, and i am facing a problem. I want to display more than a single character on the display, how can i do that? Sorry, i suck at programming, but want to learn. Posting code below.Using XC8 & MPLAB XCode:#include <xc.h>#include <stdio.h>#include <stdlib.h>#define _XTAL_FREQ 8000000#define RS LATBbits.LATB4#define EN LATBbits.LATB6// CONFIG1#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)// CONFIG2#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)void lcdint(){RS = 0;LATC = 0b00110000;EN = 1;__delay_ms(1);EN = 0;LATC = 0b00001111;EN = 1;__delay_ms(1);EN = 0;LATC = 0b00000110;EN = 1;__delay_ms(1);EN = 0;LATC = 0;}void lcdc(unsigned char lcdchar){RS = 1;LATC = lcdchar;EN = 1;__delay_ms(1);EN = 0;RS = 0;}/***/int main(int argc, char** argv){OSCCONbits.IRCF = 1111;TRISC = 0;TRISB = 0;lcdint();lcdc('h');lcdc('e');lcdc('l');lcdc('l');lcdc('o');lcdc(' ');lcdc('w');lcdc('o');lcdc('r');lcdc('l');lcdc('d');while(1);}

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 07:27:21 pm
content: Use this for the string, this will call your write character function.void LCD_WriteString_4(const char *buffer){while(*buffer){LCD_WriteChar_4(*buffer);buffer++;}}//LCD_WriteString_4David.

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 07:36:45 pm
content: "illegal conversion from integrer to pointer"

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 08:16:04 pm
content: It works fine with meDo a "Clean and build project"MPLAB X is sometimes buggy, sometimes I had to close it and reopen it again.Using MPLAB X 2.30 and XC8 1.33David.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 08:21:09 pm
content: You need to use it like:LCD_WriteString_4("Hello world !");David.

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 08:32:52 pm
content: So instead of the LCD_write_char i use lcdc?

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 08:46:12 pm
content: No:Use something like this: (I removed the 4)void LCD_WriteString(const char *buffer){while(*buffer){lcdc(*buffer);buffer++;}}//LCD_WriteStringAnd when you call LCD_WriteString use it like:LCD_WriteString("Hello World !");Put this function below lcdcDavid.

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 08:49:05 pm
content: Same error, const char too long.Code:void lcdint(){RS = 0;LATC = 0b00110000;EN = 1;__delay_ms(1);EN = 0;LATC = 0b00001111;EN = 1;__delay_ms(1);EN = 0;LATC = 0b00000110;EN = 1;__delay_ms(1);EN = 0;LATC = 0;}void lcdc(unsigned char lcdchar){RS = 1;LATC = lcdchar;EN = 1;__delay_ms(1);EN = 0;RS = 0;}void LCD_WriteString(const char *buffer){while(*buffer){lcdc(*buffer);buffer++;}}//LCD_WriteString/***/int main(){OSCCONbits.IRCF = 1111;TRISC = 0;TRISB = 0;lcdint();LCD_WriteString('oi');while(1);}

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 08:56:05 pm
content: You need to use "oi"and you are using 'oi'Use " ... your message.. "David.

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 09:09:58 pm
content: YES!! THANK YOU SO MUCH!!It works well, now if you could tell me how it works? What is happening in the code? And how can i display an integrer?BTW Awesome avatar. Rest in pepperoni doge.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 09:30:36 pm
content: This is what I use to print an integer:void LCD_WriteInt16(int Number, const char *flag){char Counter;char Length;char Data;Length = sprintf(Data, flag, Number);for ( Counter=0; Counter<Length; Counter++){LCD_WriteChar(Data);}}//LCD_WriteInt16But there maybe better implementations out there.Use is like:void LCD_WriteInt16(23, "%d") for decimalvoid LCD_WriteInt16(23, "%x") for hexadecimalYou can use a lot of combinations on the second part of the signature, google for number formatting in c or Format Specifiers in c.David.

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 09:48:36 pm
content: Thanks, could you explain me what is happening in the Write_string command? why do i put * before char and why do i increment the 'buffer'?

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 12, 2015, 10:08:32 pm
content: The * is a pointer operator.It points to the memory location of the string ( first character), if you want the second character you need to increase the value of the memory location and so on...David.

author: katzohki
author_rank: Frequent Contributor
timestamp: March 12, 2015, 10:11:58 pm
content: So basically it's stepping through the string and writing one character at a time.

author: dannyf
author_rank: Super Contributor
timestamp: March 12, 2015, 10:16:29 pm
content: Easy:Code:void lcds(unsigned char * str) {while (*str) {lcdc(*str++);}}

author: Moondeck
author_rank: Regular Contributor
timestamp: March 12, 2015, 10:18:35 pm
content: Thanks a lot guys. Now i can go make my ADC measurement projectAdmin can close the topic.