From e2d789ccd0adfc301b58af41598311a9705b02f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 23 Jun 2022 10:12:40 +0200 Subject: [PATCH] add init mode --- main.c | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 9e0cc37..db7cd35 100644 --- a/main.c +++ b/main.c @@ -52,7 +52,7 @@ // the functions we can call over I²C enum i2c_mode_t { // custom modes - MODE_CLEAR_DISPLAY, // clear display + MODE_INIT, // initialise HD44780 MODE_LINE1, // write to line 1 MODE_LINE2, // write to line 2 MODE_DISPLAY_ON, // turn display on @@ -60,9 +60,10 @@ enum i2c_mode_t { MODE_BRIGHTNESS, // set backlight brightness // raw instructions, directly mapping to HD44780 - MODE_DISPLAY, + MODE_CLEAR_DISPLAY, MODE_RETURN_HOME, MODE_ENTRY_MODE_SET, + MODE_DISPLAY, MODE_CURSOR_DISPLAY_SHIFT, MODE_FUNCTION_SET, MODE_CGRAM_ADDR, @@ -188,6 +189,30 @@ static void hd44780_write_data(uint8_t data) hd44780_write_byte(data); } +static void hd44780_init(void) +{ + // configure display (as per datasheet) + IWDG_KR = IWDG_KR_KEY_REFRESH; // reset watchdog + HD44780_RS_PORT->ODR.reg &= ~HD44780_RS_PIN; // set low for instruction + hd44780_data_direction(false); // switch to write direction + wait_10us(4000 + 1000); // wait 40 ms after power up + hd44780_write_nibble(3); // 1st function write set to go to state 1 (8-bit) or 2 (4-bit first nibble) (BF cannot be checked) + wait_10us(410 + 100); // wait 4.1 ms + hd44780_write_nibble(3); // 2st function write set to go to state 1 (8-bit) or 3 (4-bit second nibble) (BF cannot be checked) + wait_10us(10 + 1); // wait 100 us + hd44780_write_nibble(3); // 3rd function write set to go to state 1 (8-bit) (BF cannot be checked) + wait_10us(4 + 1); // wait 37 us + hd44780_write_nibble(2); // switch to 4-bit mode + wait_10us(4 + 1); // wait 37 us (BF could be checked at this point) + // we are now for sure in 8-bit more (and could switch do 4-bit). 8-bit mode is actually the default after power up + IWDG_KR = IWDG_KR_KEY_REFRESH; // reset watchdog + hd44780_write_instruction(0x28); // function set (DL=1: 4-bit mode, N=1: 2 lines, F=0: 5x8 dots) + hd44780_write_instruction(0x01); // display clear + hd44780_write_instruction(0x06); // entry mode set + hd44780_write_instruction(0x02); // return home + hd44780_write_instruction(0x0c); // display on +} + void main(void) { sim(); // disable interrupts (while we reconfigure them) @@ -277,25 +302,7 @@ void main(void) TIM2->EGR.fields.UG = 1; // force reload of values TIM2->CR1.fields.CEN = 1; // enable timer - // configure display (as per datasheet) - IWDG_KR = IWDG_KR_KEY_REFRESH; // reset watchdog - HD44780_RS_PORT->ODR.reg &= ~HD44780_RS_PIN; // set low for instruction - hd44780_data_direction(false); // switch to write direction - wait_10us(4000 + 1000); // wait 40 ms after power up - hd44780_write_nibble(3); // 1st function write set to go to state 1 (8-bit) or 2 (4-bit first nibble) (BF cannot be checked) - wait_10us(410 + 100); // wait 4.1 ms - hd44780_write_nibble(3); // 2st function write set to go to state 1 (8-bit) or 3 (4-bit second nibble) (BF cannot be checked) - wait_10us(10 + 1); // wait 100 us - hd44780_write_nibble(3); // 3rd function write set to go to state 1 (8-bit) (BF cannot be checked) - wait_10us(4 + 1); // wait 37 us - hd44780_write_nibble(2); // switch to 4-bit mode - wait_10us(4 + 1); // wait 37 us (BF could be checked at this point) - // we are now for sure in 8-bit more (and could switch do 4-bit). 8-bit mode is actually the default after power up - IWDG_KR = IWDG_KR_KEY_REFRESH; // reset watchdog - hd44780_write_instruction(0x28); // function set (DL=1: 4-bit mode, N=1: 2 lines, F=0: 5x8 dots) - hd44780_write_instruction(0x08); // display off - hd44780_write_instruction(0x01); // display clear - hd44780_write_instruction(0x06); // entry mode set + hd44780_init(); // initialise display rim(); // re-enable interrupts @@ -312,6 +319,9 @@ void main(void) i2c_input_new = false; // clear flag // process mode switch switch (i2c_mode) { + case MODE_INIT: // re-initialise display + hd44780_init(); + break; case MODE_CLEAR_DISPLAY: // clear display hd44780_write_instruction(0x01); // clear display instruction break;