From 3cd86e1b09c789248fc20a003cb05ade55fef331 Mon Sep 17 00:00:00 2001 From: Carsten Keller Date: Sun, 9 Jun 2024 17:58:20 +0200 Subject: [PATCH] Printe String in Display --- Source/Display.cpp | 65 ++++++++++++++++++++++++++++++++++-- Source/Display.hpp | 9 +++-- Source/ElektronischeLast.cpp | 2 ++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/Source/Display.cpp b/Source/Display.cpp index 85d8630..216c263 100644 --- a/Source/Display.cpp +++ b/Source/Display.cpp @@ -5,6 +5,7 @@ * Author: Carst */ +#include #include #include "STM32G071KBT6.hpp" #include "Display.hpp" @@ -41,7 +42,11 @@ namespace ElektronischeLast switch (this->current_state) { case StateIdle: - // do nothing; + if(this->new_data) + { + this->new_data = false; + this->current_state = StateWriteData; + } break; case StateDelay: if(this->timer_elapsed()) @@ -109,6 +114,17 @@ namespace ElektronischeLast this->start_timer(1UL); this->next_state = StateIdle; break; + case StateWriteData: + write_data_4bit((std::uint8_t)this->string[this->pointer++]); + if(this->string[this->pointer] == '\0') + { + this->next_state = StateIdle; + } + else + { + this->next_state = StateWriteData; + } + break; default: break; } @@ -128,12 +144,14 @@ namespace ElektronischeLast void Display::set_cursor(Line_t line, std::uint32_t position) { - + lcd_cmd(0x80u | line | (position & 0x7F)); } void Display::print(const char* const string) { - + strncpy(this->string, string, sizeof(this->string)); + this->pointer = 0U; + this->new_data = true; } bool Display::timer_elapsed(void) @@ -179,4 +197,45 @@ namespace ElektronischeLast pulse_delay(); LL_GPIO_ResetOutputPin(GPIOB, PIN_E); } + + /// \brief write command to lcd and wait until busy is gone + /// \param[in] cmd command to transfer + void Display::lcd_cmd(uint8_t cmd) + { + write_command_4bit(cmd); + this->start_timer(1UL); + } + + /// \brief write data to display + /// \param[in] data data which to write to display + void Display::lcd_data(uint8_t data) + { + write_data_4bit(data); + this->start_timer(1UL); + } + + /// \brief write data to lcd in 4 bit mode + /// \details write data given by \p data to lcd. + /// First write upper nibble and in second step lower nibble + /// \param[in] data data to transfer + void Display::write_data_4bit(uint8_t data) + { + // RS=high, RW=low + LL_GPIO_SetOutputPin(GPIOB, PIN_RS | PIN_RW << GPIO_BSRR_BR0_Pos | PIN_E << GPIO_BSRR_BR0_Pos); + std::uint32_t tmp = data >> 4U; + tmp |= ((~(data >> 4U) & 0xFU) << GPIO_BSRR_BR0_Pos); + LL_GPIO_SetOutputPin(GPIOB, tmp); + __NOP(); + LL_GPIO_SetOutputPin(GPIOB, PIN_E); + pulse_delay(); + LL_GPIO_ResetOutputPin(GPIOB, PIN_E); + tmp = data & 0x0FU; + tmp |= ((~data & 0xFU) << GPIO_BSRR_BR0_Pos); + LL_GPIO_SetOutputPin(GPIOB, tmp); + __NOP(); + LL_GPIO_SetOutputPin(GPIOB, PIN_E); + pulse_delay(); + LL_GPIO_ResetOutputPin(GPIOB, PIN_E); + this->start_timer(1UL); + } } diff --git a/Source/Display.hpp b/Source/Display.hpp index 0b7c072..e6cff51 100644 --- a/Source/Display.hpp +++ b/Source/Display.hpp @@ -85,7 +85,7 @@ namespace ElektronischeLast StateInit08, StateInit09, StateInit10, - + StateWriteData, } State_t; void init(void); @@ -97,11 +97,16 @@ namespace ElektronischeLast State_t current_state; State_t next_state; std::uint32_t timer; + char string[17]; + std::uint8_t pointer; + bool new_data; bool timer_elapsed(void); void start_timer(std::uint32_t timeout); - void wait_while_busy(void); + void lcd_cmd(uint8_t cmd); + void lcd_data(uint8_t data); void write_command_8bit_4pin(std::uint32_t data); void write_command_4bit(std::uint32_t command); + void write_data_4bit(uint8_t data); }; } diff --git a/Source/ElektronischeLast.cpp b/Source/ElektronischeLast.cpp index 6e57217..597b9da 100644 --- a/Source/ElektronischeLast.cpp +++ b/Source/ElektronischeLast.cpp @@ -110,6 +110,8 @@ int main (void) PIDController_Init(&pid); + lcd.print("Miep Miep"); + while(1) { led.blink();