add init mode

This commit is contained in:
King Kévin 2022-06-23 10:12:40 +02:00
parent d37edd9e9d
commit e2d789ccd0
1 changed files with 31 additions and 21 deletions

52
main.c
View File

@ -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;