From 814cf5867944e2ac4a8dd493ad2425da1a06f59e Mon Sep 17 00:00:00 2001 From: Carsten Keller Date: Sun, 9 Jun 2024 17:58:18 +0200 Subject: [PATCH] Starte Display Integration --- Source/Display.cpp | 186 +++++++++++++++++++++++++++++++++++ Source/Display.hpp | 110 +++++++++++++++++++++ Source/ElektronischeLast.cpp | 4 + 3 files changed, 300 insertions(+) create mode 100644 Source/Display.cpp create mode 100644 Source/Display.hpp diff --git a/Source/Display.cpp b/Source/Display.cpp new file mode 100644 index 0000000..2faf020 --- /dev/null +++ b/Source/Display.cpp @@ -0,0 +1,186 @@ +/* + * Display.cpp + * + * Created on: Aug 12, 2023 + * Author: Carst + */ + +#include +#include "STM32G071KBT6.hpp" +#include "Display.hpp" + +#define pulse_delay() for(uint32_t i = 0UL; i < 1UL; i++); +#define PIN_RS LL_GPIO_PIN_4 +#define PIN_RW LL_GPIO_PIN_5 +#define PIN_E LL_GPIO_PIN_6 +#define PIN_BL LL_GPIO_PIN_7 + +namespace ElektronischeLast +{ + Display::Display(void) + { + LL_GPIO_InitTypeDef init = + { + .Pin = LL_GPIO_PIN_0 | LL_GPIO_PIN_1 | LL_GPIO_PIN_2 | LL_GPIO_PIN_3 | + PIN_RS | PIN_RW | PIN_E | PIN_BL, + .Mode = LL_GPIO_MODE_OUTPUT, + .Speed = LL_GPIO_SPEED_FREQ_LOW, + .OutputType = LL_GPIO_OUTPUT_PUSHPULL, + .Pull = LL_GPIO_PULL_NO, + }; + LL_GPIO_Init(GPIOB, &init); + + this->pin_input = false; + this->set_backlight(true); + this->current_state = StateDelay; + this->next_state = StateInit01; + this->start_timer(15UL); // 15ms Startup Delay + } + + Display::~Display(void) + { + + } + + void Display::run(void) + { + switch (this->current_state) + { + case StateIdle: + // do nothing; + break; + case StateDelay: + if(this->timer_elapsed()) + { + this->current_state = this->next_state; + this->run(); + } + break; + case StateWaitWhileBusy: + break; + case StateInit01: + write_command_8bit_4pin(0x30u); + this->start_timer(5UL); // 5ms Delay + this->next_state = StateInit02; + break; + case StateInit02: + write_command_8bit_4pin(0x30u); + this->start_timer(1UL); // 1ms Delay + this->next_state = StateInit03; + break; + case StateInit03: + write_command_8bit_4pin(0x30u); + this->start_timer(1UL); // 1ms Delay + this->next_state = StateInit04; + break; + case StateInit04: + // 4-bit mode + write_command_8bit_4pin(0x20u); + this->start_timer(1UL); + this->next_state = StateInit05; + break; + case StateInit05: + // 4-bit mode, 2 lines + write_command_4bit(0x28u); + this->start_timer(1UL); + this->next_state = StateInit06; + break; + case StateInit06: + // Display off + write_command_4bit(0x08u); + this->start_timer(1UL); + this->next_state = StateInit07; + break; + case StateInit07: + // Clear Display + write_command_4bit(0x01u); + this->start_timer(1UL); + this->next_state = StateInit08; + break; + case StateInit08: + // increment DDRAM + write_command_4bit(0x06u); + this->start_timer(1UL); + this->next_state = StateInit09; + break; + case StateInit09: + // return cursor home + write_command_4bit(0x02u); + this->start_timer(1UL); + this->next_state = StateInit10; + break; + case StateInit10: + // Display on, Cursor on, Blink on + write_command_4bit(0x0Fu); + this->start_timer(1UL); + this->next_state = StateIdle; + break; + default: + break; + } + } + + void Display::set_backlight(bool on) + { + if(on) + { + LL_GPIO_SetOutputPin(GPIOB, PIN_BL); + } + else + { + LL_GPIO_ResetOutputPin(GPIOB, PIN_BL); + } + } + + void Display::set_cursor(Line_t line, std::uint32_t position) + { + + } + + void Display::print(const char* const string) + { + + } + + bool Display::timer_elapsed(void) + { + return (this->timer < systick); + } + + void Display::start_timer(std::uint32_t timeout) + { + this->timer = systick + 1U + timeout; // +1 damit auch mindestens 1ms vorbei geht. + this->current_state = StateDelay; + } + + void Display::write_command_8bit_4pin(std::uint32_t data) + { + // RS=low, RW=low + LL_GPIO_ResetOutputPin(GPIOB, PIN_RS | PIN_RW | PIN_E); + 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); + } + + void Display::write_command_4bit(std::uint32_t command) + { + // RS=low, RW=low + LL_GPIO_ResetOutputPin(GPIOB, PIN_RS | PIN_RW | PIN_E); + std::uint32_t tmp = command >> 4U; + tmp |= ((~(command >> 4U) & 0xFU) << GPIO_BSRR_BR0_Pos); + __NOP(); + LL_GPIO_SetOutputPin(GPIOB, PIN_E); + pulse_delay(); + LL_GPIO_ResetOutputPin(GPIOB, PIN_E); + tmp = command; + tmp |= ((~command & 0xFU) << GPIO_BSRR_BR0_Pos); + __NOP(); + LL_GPIO_SetOutputPin(GPIOB, PIN_E); + pulse_delay(); + LL_GPIO_ResetOutputPin(GPIOB, PIN_E); + } +} diff --git a/Source/Display.hpp b/Source/Display.hpp new file mode 100644 index 0000000..12e487f --- /dev/null +++ b/Source/Display.hpp @@ -0,0 +1,110 @@ +/* + * Display.hpp + * + * Created on: Aug 12, 2023 + * Author: Carst + */ + +#ifndef DISPLAY_HPP_ +#define DISPLAY_HPP_ + +#include + +namespace ElektronischeLast +{ + class Display + { + public: + /// \brief User defined character number + typedef enum + { + FirstSign = 0, //!< First user defined character (accassible via data 0x0) + SecondSign = 8, //!< Second user defined character (accassible via data 0x1) + ThirdSign = 16, //!< Third user defined character (accassible via data 0x2) + FourthSign = 24, //!< Fourth user defined character (accassible via data 0x3) + FifthSign = 32, //!< Fifth user defined character (accassible via data 0x4) + SixthSign = 40, //!< Sixth user defined character (accassible via data 0x5) + SeventhSign = 48,//!< Seventh user defined character (accassible via data 0x6) + EithSign = 56 //!< Eith user defined character (accassible via data 0x7) + } UserDefinedCharakter_t; + + /// \brief Shift direction + typedef enum + { + ShiftLeft = 0, //!< Shift text left + ShiftRight = 2 //!< Shift text right + } ShiftDirection_t; + + /// \brief Shift mode + typedef enum + { + ShiftOff = 0, //!< Shift disabled + ShiftOn = 1 //!< Shift enabled + } ShiftMode_t; + + /// \brief Display State + typedef enum + { + DisplayOff = 0, //!< Display off + DispalyOn = 4 //!< Display on + } Display_t; + + /// \brief Cursor State + typedef enum + { + CursorOff = 0, //!< Cursor off + CursorOn = 2 //!< Cursor on + } Cursor_t; + + /// \brief Cursor blink mode + typedef enum + { + CursorBlinkOff = 0, //!< no Cursor blink + CursorBlinkOn = 1 //!< Cursor blink + } CursorBlink_t; + + /// \brief Display line selection + typedef enum + { + Line1 = 0, //!< Select first display line + Line2 = 0x40, //!< Select second display line + } Line_t; + + typedef enum + { + StateIdle, + StateDelay, + StateWaitWhileBusy, + StateInit01, + StateInit02, + StateInit03, + StateInit04, + StateInit05, + StateInit06, + StateInit07, + StateInit08, + StateInit09, + StateInit10, + + } State_t; + + Display(void); + ~Display(void); + void run(void); + void set_backlight(bool on); + void set_cursor(Line_t line, std::uint32_t position); + void print(const char* const string); + private: + State_t current_state; + State_t next_state; + bool pin_input; + std::uint32_t timer; + bool timer_elapsed(void); + void start_timer(std::uint32_t timeout); + void wait_while_busy(void); + void write_command_8bit_4pin(std::uint32_t data); + void write_command_4bit(std::uint32_t command); + }; +} + +#endif /* DISPLAY_HPP_ */ diff --git a/Source/ElektronischeLast.cpp b/Source/ElektronischeLast.cpp index 844d742..a448d08 100644 --- a/Source/ElektronischeLast.cpp +++ b/Source/ElektronischeLast.cpp @@ -22,6 +22,7 @@ #include "FanControl.hpp" #include "CLI.h" #include "I2C.hpp" +#include "Display.hpp" using namespace ElektronischeLast; @@ -74,6 +75,7 @@ int main (void) printf("\r\nElektronische Last\r\n"); CLI_Init(commands, sizeof(commands)/sizeof(commands[0])); LED led = LED(500U); + Display lcd = Display(); FanControl fan = FanControl(); iI2C eeprom = iI2C(0xA0U); @@ -136,6 +138,8 @@ int main (void) spannung = adc.get_voltage(); temperatur = adc.get_temperature(); geschwindigkeit = fan.get_speed(); + + lcd.run(); break; } serial_cyclic();