void lcdControlWrite(u08 data) { // write the control byte to the display controller #ifdef LCD_PORT_INTERFACE lcdBusyWait(); // wait until LCD not busy cbi(LCD_CTRL_PORT, LCD_CTRL_RS); // set RS to "control" cbi(LCD_CTRL_PORT, LCD_CTRL_RW); // set R/W to "write" #ifdef LCD_DATA_4BIT // 4 bit write sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)|0xF0); // set data I/O lines to output (4bit) outb(LCD_DATA_POUT,(inb(LCD_DATA_PIN) &0x0F) | (data&0xF0) ); // output data, high 4 bits LCD_DELAY; // wait LCD_DELAY; // wait cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line LCD_DELAY; // wait LCD_DELAY; // wait sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line outb(LCD_DATA_POUT,(inb(LCD_DATA_PIN)&0x0F) | (data<<4) ); // output data, low 4 bits LCD_DELAY; // wait LCD_DELAY; // wait cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line #else // 8 bit write sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line outb(LCD_DATA_DDR, 0xFF); // set data I/O lines to output (8bit) outb(LCD_DATA_POUT,data); LCD_DELAY; // wait LCD_DELAY; // wait cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line #endif // leave data lines in input mode so they can be most easily used for other purposes #ifdef LCD_DATA_4BIT outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit) outb(LCD_DATA_POUT,inb(LCD_DATA_PIN) |0xF0); // set pull-ups to on (4bit) #else outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit) outb(LCD_DATA_POUT,0xFF); // set pull-ups to on (8bit) #endif #else // memory bus write //sbi(MCUCR, SRW); // enable RAM waitstate lcdBusyWait(); // wait until LCD not busy *((volatile unsigned char *) (LCD_CTRL_ADDR)) = data; //cbi(MCUCR, SRW); // disable RAM waitstate #endif }
u08 lcdDataRead(void) { // read a data byte from the display register u08 data; #ifdef LCD_PORT_INTERFACE lcdBusyWait(); // wait until LCD not busy #ifdef LCD_DATA_4BIT outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F); // set data I/O lines to input (4bit) outb(LCD_DATA_POUT, inb(LCD_DATA_POUT)|0xF0); // set pull-ups to on (4bit) #else outb(LCD_DATA_DDR, 0x00); // set data I/O lines to input (8bit) outb(LCD_DATA_POUT, 0xFF); // set pull-ups to on (8bit) #endif sbi(LCD_CTRL_PORT, LCD_CTRL_RS); // set RS to "data" sbi(LCD_CTRL_PORT, LCD_CTRL_RW); // set R/W to "read" #ifdef LCD_DATA_4BIT // 4 bit read sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line LCD_DELAY; // wait LCD_DELAY; // wait data = inb(LCD_DATA_PIN)&0xF0; // input data, high 4 bits cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line LCD_DELAY; // wait LCD_DELAY; // wait sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line LCD_DELAY; // wait LCD_DELAY; // wait data |= inb(LCD_DATA_PIN)>>4; // input data, low 4 bits cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line #else // 8 bit read sbi(LCD_CTRL_PORT, LCD_CTRL_E); // set "E" line LCD_DELAY; // wait LCD_DELAY; // wait data = inb(LCD_DATA_PIN); // input data, 8bits cbi(LCD_CTRL_PORT, LCD_CTRL_E); // clear "E" line #endif // leave data lines in input mode so they can be most easily used for other purposes #else // memory bus read //sbi(MCUCR, SRW); // enable RAM waitstate lcdBusyWait(); // wait until LCD not busy data = *((volatile unsigned char *) (LCD_DATA_ADDR)); //cbi(MCUCR, SRW); // disable RAM waitstate #endif return data; }
/** * Writes instructions to the LCD * * Inputs: db LCD instruction * rs for commands, rs = 0 * for sending data, rs = 1 */ static void lcdWrite(BYTE db, BYTE rs) { lcdBusyWait(); LCD_RW(0); LCD_RS(rs); LCD_DATA_DDR = 0xFF; /* output */ LCD_DATA_PORT = db; delay_us(LCD_DELAY_EN); /* RS, RW Setup Time (Minimum: 60ns) */ lcdToggleEn(); }
/** * Initializes LCD */ void lcdInit() { LCD_CTRL_DDR = _BV(LCD_RS_PIN)|_BV(LCD_RW_PIN)|_BV(LCD_EN_PIN); delay_us(LCD_DELAY_START); lcdWrite(LCD_CMD_FUNCTION, 0); lcdBusyWait(); lcdWrite(LCD_CMD_DISP, 0); lcdBusyWait(); lcdClear(); lcdWrite(LCD_CMD_ENTRY, 0); lcdBusyWait(); /* printf */ fdevopen((void*)lcdPrint, 0); }
/** * Clears LCD screen */ void lcdClear() { lcdWrite(LCD_CMD_CLEAR, 0); lcdBusyWait(); delay_us(LCD_DELAY_SHORT); lcdGoTo(0,0); }