/************************************************************************* Initialize display and select type of cursor Input: dispAttr LCD_DISP_OFF display off LCD_DISP_ON display on, cursor off LCD_DISP_ON_CURSOR display on, cursor on LCD_DISP_CURSOR_BLINK display on, cursor on flashing Returns: none *************************************************************************/ void lcd_init(uint8_t dispAttr) { #if LCD_PCF8574_INIT == 1 //init pcf8574 pcf8574_init(); #endif #if LCD_LED == 1 lcd_led(1); #endif dataport = 0; pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport); delay(16000); /* wait 16ms or more after power-on */ /* initial write to lcd is 8bit */ // dataport |= _BV(LCD_DATA1_PIN); // _BV(LCD_FUNCTION)>>4; // dataport |= _BV(LCD_DATA0_PIN); // _BV(LCD_FUNCTION_8BIT)>>4; // lcd_e_toggle(); // delay(4992); /* delay, busy flag can't be checked here */ /* repeat last command */ // lcd_e_toggle(); //delay(64); /* delay, busy flag can't be checked here */ // delay(4992); /* delay, busy flag can't be checked here */ /* repeat last command a third time */ // lcd_e_toggle(); //delay(64); /* delay, busy flag can't be checked here */ // delay(4992); /* delay, busy flag can't be checked here */ /* now configure for 4bit mode */ //dataport &= ~_BV(LCD_DATA0_PIN); dataport |= _BV(LCD_DATA1_PIN); lcd_e_toggle(); delay(128); /* some displays need this additional delay */ lcd_e_toggle(); delay(128); dataport &= ~_BV(LCD_DATA1_PIN); dataport |= _BV(LCD_DATA3_PIN); lcd_e_toggle(); delay(128); /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */ lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */ // lcd_command(LCD_DISP_OFF); /* display off */ lcd_clrscr(); /* display clear */ lcd_command(LCD_MODE_DEFAULT); /* set entry mode */ lcd_command(dispAttr); /* display/cursor control */ }/* lcd_init */
/************************************************************************* Low-level function to write byte to LCD controller Input: data byte to write to LCD rs 1: write data 0: write instruction Returns: none *************************************************************************/ static void lcd_write(uint8_t data,uint8_t rs) { if (rs) /* write data (RS=1, RW=0) */ dataport |= _BV(LCD_RS_PIN); else /* write instruction (RS=0, RW=0) */ dataport &= ~_BV(LCD_RS_PIN); dataport &= ~_BV(LCD_RW_PIN); /* output high nibble first */ dataport &= ~_BV(LCD_DATA3_PIN); dataport &= ~_BV(LCD_DATA2_PIN); dataport &= ~_BV(LCD_DATA1_PIN); dataport &= ~_BV(LCD_DATA0_PIN); if(data & 0x80) dataport |= _BV(LCD_DATA3_PIN); if(data & 0x40) dataport |= _BV(LCD_DATA2_PIN); if(data & 0x20) dataport |= _BV(LCD_DATA1_PIN); if(data & 0x10) dataport |= _BV(LCD_DATA0_PIN); lcd_e_toggle(); /* output low nibble */ dataport &= ~_BV(LCD_DATA3_PIN); dataport &= ~_BV(LCD_DATA2_PIN); dataport &= ~_BV(LCD_DATA1_PIN); dataport &= ~_BV(LCD_DATA0_PIN); if(data & 0x08) dataport |= _BV(LCD_DATA3_PIN); if(data & 0x04) dataport |= _BV(LCD_DATA2_PIN); if(data & 0x02) dataport |= _BV(LCD_DATA1_PIN); if(data & 0x01) dataport |= _BV(LCD_DATA0_PIN); lcd_e_toggle(); /* all data pins high (inactive) */ dataport |= _BV(LCD_DATA0_PIN); dataport |= _BV(LCD_DATA1_PIN); dataport |= _BV(LCD_DATA2_PIN); dataport |= _BV(LCD_DATA3_PIN); delay(128); pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport); delay(128); }
void lcd_write(uint8_t data, uint8_t rs) { __IO uint8_t outputdata; if(rs) { //write data lcd_rs_high(); } else { //write instruction lcd_rs_low(); } lcd_rw_low(); //set to write //set data pins to output GPIO_InitStructure.GPIO_Pin = DATA_PORT_Pin_0 | DATA_PORT_Pin_1 | DATA_PORT_Pin_2 | DATA_PORT_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(LCD_PORT, &GPIO_InitStructure); /** * Preserve the RS,RW,E bits in order to write out: * our data goe son the 4 most significant bits with * the high nibble output first. */ outputdata = (uint8_t)GPIO_ReadOutputData(LCD_PORT); outputdata &= 0x05; //we're only interested in the lower bits (RS,RW,E) //output high nibble first GPIO_Write(LCD_PORT, outputdata | (data & 0xF0)); //push data lcd_e_toggle(); GPIO_Write(LCD_PORT, outputdata | ((data << 4) & 0xF0)); //push data lcd_e_toggle(); GPIO_Write(LCD_PORT, outputdata | 0xF0); //set pins high as inactive state }
void lcd_init(uint8_t dispAttr) { //__IO uint8_t outputdata; //holds whatever is on the port /* a few things will happen here so the * students won't need to do them: * - configuration of the "LCD port" * defined in the defines as PORT D * - initializing the LCD */ /* we want to initialize delays that will enable * to delay by microseconds */ //if (SysTick_Config(SystemCoreClock/1000000)) //{ /* Capture error */ // while (1); //} //SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); /** configure the "LCD port" **/ /* "LCD PORT" Periph clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_LCD_PORT, ENABLE); /* Configure: * pin 0,1,2 for output: (RS,RW,E) * pin 4,5,6,7 for data output: DB11-DB14 in 4-bit mode */ //first set output... GPIO_InitStructure.GPIO_Pin = RS_PORT_Pin | RW_PORT_Pin | E_PORT_Pin | DATA_PORT_Pin_0 | DATA_PORT_Pin_1 | DATA_PORT_Pin_2 | DATA_PORT_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(LCD_PORT, &GPIO_InitStructure); /** //...then input pin GPIO_InitStructure.GPIO_Pin = DB7_PORT_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(LCD_PORT, &GPIO_InitStructure); **/ /** this ends the configuration of the "LCD PORT" **/ //outputdata = (uint8_t)GPIO_ReadOutputData(LCD_PORT); //outputdata &= 0x05; //we're only interested in the lower bits (RS,RW,E) Delay(50000); /** initialize the LCD display by software instruction **/ GPIO_Write(LCD_PORT, (1<<LCD_DATA0_PIN) | (1<<LCD_DATA1_PIN)); //enable writing to LCD lcd_e_toggle(); Delay(5000); lcd_e_toggle(); Delay(5000); lcd_e_toggle(); //lcd_waitbusy(); Delay(160); //configure for 4-bit mode GPIO_Write(LCD_PORT, 1<<LCD_DATA1_PIN); lcd_e_toggle(); Delay(160); lcd_command(LCD_FUNCTION_4BIT_2LINES); /* function set: display lines */ lcd_command(LCD_DISP_OFF); /* display off */ lcd_clrscr(); /* display clear */ lcd_command(LCD_MODE_DEFAULT); /* set entry mode */ /** end initialize the LCD **/ lcd_command(dispAttr); }