1
\$\begingroup\$

I'm trying to interface with an LCD display (HD44780). I came across a tutorial on the internet to send characters and strings to the display. Everything went well and it works fine but I have no idea why a pointer is used. I'm not quite familiar with pointer in C. I know that thay can hold addresses of variables etc.

Can someone explain me why it is used to send data to the LCD and what it exactly does in the code below?

#define lcdE 0x04
#define lcdRW 0x02
#define lcdRS 0x01
#define lcdport PORTC //port where LCD is connected
#define lcdDIR DDRC
void lcd_init(void); //initialize
void cmd_to_lcd(unsigned char cmd); //send command to LCD display
void char_to_lcd(unsigned char character); //send character to LCD display
void set_cursor_to_1st_line(void); //set cursor at the beginning of the first line
void set_cursor_to_2nd_line(void); //set cursos at the beginning of the second line
void clear_display(void); //clear display and return to home position
void string_to_lcd(unsigned char *ptr); //send a string to LCD display
void cmd_to_lcd(unsigned char cmd)
{
 unsigned char temp = 0;
 temp = cmd; //store cmd in temp
 cmd &= 0xF0; // clear lower nibble
 lcdport &= ~(lcdRS|lcdRW); //pull RS and RW low
 lcdport |= lcdE; //pull E high
 lcdport = cmd | lcdE; //output cmd and hold E high
 _delay_us(10);
 lcdport &= ~(lcdE); //pull E low
 _delay_us(10);
 cmd = ((temp << 4) & 0xF0); //shift command 4 left and clear lower nibble (send lower nibble of cmd)
 lcdport |= lcdE; //pull E high
 lcdport = cmd | lcdE; //output cmd and hold E high
 _delay_us(10);
 lcdport &= ~(lcdE); //pull E low
 _delay_us(50);
}
void lcd_init(void)
{
 lcdDIR = 0xFF; //port C as output
 _delay_ms(30); //wait for lcd internal init
 cmd_to_lcd(0x28); //4-bits, 2 lines, 5x8 dots
 _delay_us(10);
 cmd_to_lcd(0x0C); //turn on display, cursor, cursor blink
 _delay_us(10);
 cmd_to_lcd(0x06); //increment AC on operation and move set cursor move to right
 _delay_us(10);
 cmd_to_lcd(0x01); //clear screen and move to home position
 _delay_ms(2);
}
void char_to_lcd(unsigned char character)
{
 unsigned char temp = 0;
 temp = character;
 character &= 0xF0;
 lcdport |= lcdRS; //RS = 1: data register
 lcdport &= ~(lcdRW); //RW = 0: write operation
 lcdport |= lcdE;
 lcdport = character | (lcdRS | lcdE);
 _delay_us(10);
 lcdport &= ~(lcdE);
 character = ((temp << 4) & 0xF0);
 lcdport |= lcdE;
 lcdport = character | (lcdRS | lcdE);
 _delay_us(10);
 lcdport &= ~(lcdE);
 _delay_us(50);
}
void string_to_lcd(unsigned char *ptr)
{
 while(*ptr)
 {
 char_to_lcd(*ptr);
 ptr++;
 }
}
Ignacio Vazquez-Abrams
48.9k4 gold badges76 silver badges105 bronze badges
asked Nov 15, 2014 at 14:48
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

C language does not have a native string format, so instead, strings are implemented as character arrays. The string_to_lcd function receives a pointer to the memory location where the first character of the string is.

By conventions in C strings are terminated with a null character (0x00).

Edit

The while statement first checks if it reached the end of the string (current character is different from null), if it is, the character is printed and the pointer is incremented in order to point to the next character. The cycle stops when the null character is reached.

answered Nov 15, 2014 at 14:54
\$\endgroup\$
2
  • \$\begingroup\$ Thank you! Can you also tell me what happens with the pointer in the while? And what happens if the last character of the string is pointer to by the pointer? The pointer is incremented and would therefore not become null, how do we get out the while loop again? \$\endgroup\$ Commented Nov 15, 2014 at 14:58
  • \$\begingroup\$ @maikel Sure, check the edited answer. \$\endgroup\$ Commented Nov 15, 2014 at 15:04
4
\$\begingroup\$

This is a well commented source code, at least it already gives the functions' main function in brief.

In C language, there is a data type "array", when you need display some "words" or "character strings" on the display, you can present this in C language as a "characters array". And you can use a "pointer" to point the array and use it. The pointer is just the address of the first character in the array, and "pointer + 1" is the second character in the array, "pointer + 2" is the third,.... By passing this pointer to the

string_to_lcd

function, you can cite all the characters in the array in this function, and send them one by one to the LCD. And show them on the screen.

answered Nov 15, 2014 at 15:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.