Thursday 25 May 2017

c - Why using a pointer interfacing LCD HD44780


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++;
}

}

Answer



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.


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...