/* Program 3-3 Example of Cursor Control */ #include const int REG_SEL = 12; /* R/S pin */ const int ENABLE = 11; /* EN pin */ const int D4 = 5; const int D5 = 4; const int D6 = 3; const int D7 = 2; const int dataBus[] = {D4, D5, D6, D7}; const int RS = 1; /* R/S bit mask */ void setup() { LCD_init(); } void loop() { char buffer[21]; float temp = 89.72; LCD_command(0x83); /* move cursor to first line fourth column */ LCD_puts("Temperature"); delay(500); LCD_command(0xC5); /* move cursor to second line sixth column */ int i = (int)temp; /* extract interger part */ sprintf(buffer, "%3d", i); /* convert interger part to char string */ LCD_puts(buffer); /* display the integer part */ LCD_puts("."); /* display the decimal point */ int f = (temp - i) * 100; /* extract fractional part */ sprintf(buffer, "%02d", f); /* convert fraction to char string */ LCD_puts(buffer); /* display the fraction */ LCD_data(0xDF); /* degree sign */ LCD_puts("F"); delay(1000); /* clear LCD display */ LCD_command(1); delay(500); } void LCD_init(void) { pinMode (REG_SEL, OUTPUT); pinMode (ENABLE, OUTPUT); digitalWrite(ENABLE, LOW); /* Enable idles low */ pinMode (D4, OUTPUT); pinMode (D5, OUTPUT); pinMode (D6, OUTPUT); pinMode (D7, OUTPUT); /* LCD controller reset sequence */ delay(20); LCD_nibble_write(0x30, 0); delay(5); LCD_nibble_write(0x30, 0); delay(1); LCD_nibble_write(0x30, 0); delay(1); LCD_nibble_write(0x20, 0); /* use 4-bit data mode */ delay(1); LCD_command(0x28); /* set 4-bit data, 2-line, 5x7 font */ LCD_command(0x06); /* move cursor right */ LCD_command(0x01); /* clear screen, move cursor to home */ LCD_command(0x0F); /* turn on display, cursor blinking */ } /* write 4 bits to LCD controller */ void LCD_nibble_write(char data, unsigned char select) { digitalWrite(REG_SEL, select ? HIGH : LOW); digitalGroupWrite(dataBus, 4, data >> 4); digitalWrite(ENABLE, HIGH); delayMicroseconds(1); digitalWrite(ENABLE, LOW); } /* write a command to LCD controller */ void LCD_command(unsigned char command) { LCD_nibble_write(command & 0xF0, 0); /* upper nibble first */ LCD_nibble_write(command << 4, 0); /* then lower nibble */ if (command < 4) delay(2); /* command 1 and 2 needs up to 1.64ms */ else delay(1); /* all others 40 us */ } /* write a byte of data to LCD controller */ /* since the connection is 4-bit, it takes two writes for a byte */ void LCD_data(char data) { LCD_nibble_write(data & 0xF0, RS); /* upper nibble first */ LCD_nibble_write(data << 4, RS); /* then lower nibble */ delay(1); } /* write a char string to the LCD controller */ void LCD_puts(char* s) { while(*s) { LCD_data(*s++); } } /* write to a group of output pins */ void digitalGroupWrite(const int* group, int length, int data) { for (int i = 0; i < length; i++) digitalWrite(group[i], data & (1 << i) ? HIGH : LOW); }