/** * Writes a raw instruction to the LCD. * @param command The 4-bit instruction code. * @param data The 8-bit paramater/data to the specified instruction. */ void lcd_write_command(unsigned char command, unsigned char data) { /* Wait for the busy flag to clear */ lcd_wait_busy(); /* Set Enable low, RW low, RS high to write the instruction command */ lcd_enable_low(); lcd_rw_low(); lcd_rs_high(); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); /* Instruction commands are a maximum of 4 bits long, so * just mask off the rest. */ LCD_DATA_PORT = (command&0x0F); lcd_enable_high(); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); /* Set RW low, RW low to write the instruction data */ lcd_rw_low(); lcd_rs_low(); LCD_DATA_PORT = data; __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); lcd_strobe_enable(); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); __asm("nop;"); }
/** * Initializes the LCD in graphics mode. * Uses a character pitch of 8 (8 bits are plotted whenever a byte is drawn) */ void lcd_graphics_init(void) { unsigned char commandData; /* Set the data direction registers apprioriately */ LCD_DATA_DDR = 0xFF; LCD_CTRL_DDR |= (1<<LCD_CTRL_RS)|(1<<LCD_CTRL_RW)|(1<<LCD_CTRL_E)|(1<<LCD_CTRL_CS)|(1<<LCD_CTRL_RST); /* Assert all control lines to low */ lcd_rw_low(); lcd_enable_low(); /* Reset the LCD controllers */ lcd_rst_low(); _delay_ms(100); /* Delay for 100 ms */ lcd_rst_high(); _delay_ms(100); /* Delay for 100 ms */ /* Assert all registor control line to low */ lcd_rs_low(); /* Send mode configuration command with * Toggle Display On, Master, Mode Graphics bits set */ commandData = LCD_MODE_ON_OFF | LCD_MODE_MASTER_SLAVE | LCD_MODE_MODE; lcd_write_command(LCD_CMD_MODE, commandData); /* Send the set character pitch command with horizontal * character pitch of 8 (so 8 pixels are painted when we draw) */ commandData = LCD_CHAR_PITCH_HP_8; lcd_write_command(LCD_CMD_CHAR_PITCH, commandData); /* Send the number of characters command with the total * number of graphics bytes that can be painted horizontally * (width/8) */ commandData = (LCD_WIDTH/8)-1; lcd_write_command(LCD_CMD_NUM_CHARS, commandData); /* Set the time division */ commandData = 128-1; lcd_write_command(LCD_CMD_TIME_DIVISION, commandData); /* Set the display low/high start address to 0x00 (left corner) */ commandData = 0x00; lcd_write_command(LCD_CMD_DISPLAY_START_LA, commandData); lcd_write_command(LCD_CMD_DISPLAY_START_HA, commandData); /* Reset the cursor to home 0x00 (left corner) */ commandData = 0x00; lcd_write_command(LCD_CMD_CURSOR_LA, commandData); lcd_write_command(LCD_CMD_CURSOR_HA, commandData); }
/** * Strobes the Enable control line to trigger the lcd to process the * transmitted instruction. */ void lcd_strobe_enable(void) { lcd_enable_high(); __asm("nop;"); __asm("nop;"); __asm("nop;"); lcd_enable_low(); __asm("nop;"); __asm("nop;"); __asm("nop;"); }