/*........................................................................... Name: lcd_write_instruction_4d Purpose: send a byte of information to the LCD instruction register Entry: (theInstruction) is the information to be sent to the instruction register Exit: no parameters Notes: does not deal with RW (busy flag is not implemented) */ void lcd_write_instruction_4d(uint8_t theInstruction) { lcd_RS_port &= ~(1<<lcd_RS_bit); // select the Instruction Register (RS low) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low lcd_write_4(theInstruction); // write the upper 4-bits of the data lcd_write_4(theInstruction << 4); // write the lower 4-bits of the data }
void lcd_write_character_4d(uint8_t theData) { lcd_RS_port |= (1<<lcd_RS_bit); // select the Data Register (RS high) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low lcd_write_4(theData); // write the upper 4-bits of the data lcd_write_4(theData << 4); // write the lower 4-bits of the data }
/* Name: lcd_init_4d Purpose: initialize the LCD module for a 4-bit data interface Entry: equates (LCD instructions) set up for the desired operation Exit: no parameters Notes: uses time delays rather than checking the busy flag */ void lcd_init_4d(void) { // Power-up delay _delay_ms(100); // initial 100 mSec delay // Set up the RS and E lines for the 'lcd_write_4' subroutine. lcd_RS_port &= ~(1<<lcd_RS_bit); // select the Instruction Register (RS low) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low // Reset the LCD controller lcd_write_4(lcd_FunctionSet8bit); // first part of reset sequence _delay_ms(10); // 4.1 mS delay (min) lcd_write_4(lcd_FunctionSet8bit); // second part of reset sequence _delay_us(200); // 100uS delay (min) lcd_write_4(lcd_FunctionSet8bit); // third part of reset sequence _delay_us(200); // this delay is omitted in the data sheet lcd_write_4(lcd_functionSet4bit); // set 4-bit mode _delay_us(5); // 40uS delay (min) // Function Set instruction lcd_write_instruction_4d(lcd_functionSet4bit); // set mode, lines, and font _delay_us(40); // 40uS delay (min) // Clear Display instruction lcd_write_instruction_4d(lcd_clear); // clear display RAM _delay_ms(5); // 1.64 mS delay (min) // Display On/Off Control instruction lcd_write_instruction_4d(lcd_displayOn); // turn the display ON _delay_us(40); // 40uS delay (min) // ; Entry Mode Set instruction lcd_write_instruction_4d(lcd_entryMode); // set desired shift characteristics _delay_us(40); // 40uS delay (min) }
/* Name: lcd_init_4d Purpose: initialize the LCD module for a 4-bit data interface Entry: equates (LCD instructions) set up for the desired operation Exit: no parameters Notes: uses time delays rather than checking the busy flag */ void lcd_init_4d(void) { // configure the microprocessor pins for the data lines lcd_D7_ddr |= (1<<lcd_D7_bit); // 4 data lines - output lcd_D6_ddr |= (1<<lcd_D6_bit); lcd_D5_ddr |= (1<<lcd_D5_bit); lcd_D4_ddr |= (1<<lcd_D4_bit); // configure the microprocessor pins for the control lines lcd_E_ddr |= (1<<lcd_E_bit); // E line - output lcd_RS_ddr |= (1<<lcd_RS_bit); // RS line - output // lcd_RW_ddr |= (1<<lcd_RW_bit); // RS line - output // lcd_RW_port &= ~(1<<lcd_RW_bit); // Power-up delay _delay_ms(100); // initial 40 mSec delay // IMPORTANT - At this point the LCD module is in the 8-bit mode and it is expecting to receive // 8 bits of data, one bit on each of its 8 data lines, each time the 'E' line is pulsed. // // Since the LCD module is wired for the 4-bit mode, only the upper four data lines are connected to // the microprocessor and the lower four data lines are typically left open. Therefore, when // the 'E' line is pulsed, the LCD controller will read whatever data has been set up on the upper // four data lines and the lower four data lines will be high (due to internal pull-up circuitry). // // Fortunately the 'FunctionReset' instruction does not care about what is on the lower four bits so // this instruction can be sent on just the four available data lines and it will be interpreted // properly by the LCD controller. The 'lcd_write_4' subroutine will accomplish this if the // control lines have previously been configured properly. // Set up the RS and E lines for the 'lcd_write_4' subroutine. lcd_RS_port &= ~(1<<lcd_RS_bit); // select the Instruction Register (RS low) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low // Reset the LCD controller lcd_write_4(lcd_FunctionReset); // first part of reset sequence _delay_ms(10); // 4.1 mS delay (min) lcd_write_4(lcd_FunctionReset); // second part of reset sequence _delay_us(200); // 100uS delay (min) lcd_write_4(lcd_FunctionReset); // third part of reset sequence _delay_us(200); // this delay is omitted in the data sheet // Preliminary Function Set instruction - used only to set the 4-bit mode. // The number of lines or the font cannot be set at this time since the controller is still in the // 8-bit mode, but the data transfer mode can be changed since this parameter is determined by one // of the upper four bits of the instruction. lcd_write_4(lcd_FunctionSet4bit); // set 4-bit mode _delay_us(80); // 40uS delay (min) // Function Set instruction lcd_write_instruction_4d(lcd_FunctionSet4bit); // set mode, lines, and font _delay_us(80); // 40uS delay (min) // The next three instructions are specified in the data sheet as part of the initialization routine, // so it is a good idea (but probably not necessary) to do them just as specified and then redo them // later if the application requires a different configuration. // Display On/Off Control instruction lcd_write_instruction_4d(lcd_DisplayOff); // turn display OFF _delay_us(80); // 40uS delay (min) // Clear Display instruction lcd_write_instruction_4d(lcd_Clear); // clear display RAM _delay_ms(4); // 1.64 mS delay (min) // ; Entry Mode Set instruction lcd_write_instruction_4d(lcd_EntryMode); // set desired shift characteristics _delay_us(80); // 40uS delay (min) // This is the end of the LCD controller initialization as specified in the data sheet, but the display // has been left in the OFF condition. This is a good time to turn the display back ON. // Display On/Off Control instruction lcd_write_instruction_4d(lcd_DisplayOn); // turn the display ON _delay_us(80); // 40uS delay (min) lcd_write_instruction_4d(lcd_Home); _delay_ms(10); }