From e60df4a6dbe57c17923c6fae246e80a1f8ab0d1a Mon Sep 17 00:00:00 2001 From: mandlm Date: Wed, 13 Jan 2016 10:59:42 +0100 Subject: [PATCH] Implemented KS0066 initialization --- LCD/LCD/KS0066.cpp | 179 ++++++++++++++++++++++++++++++++++++++++ LCD/LCD/KS0066.h | 37 +++++++++ LCD/LCD/LCD.cpp | 3 +- LCD/LCD/LCD.cppproj | 6 ++ LCD/LCD/ShiftRegister.h | 19 ----- 5 files changed, 224 insertions(+), 20 deletions(-) create mode 100644 LCD/LCD/KS0066.cpp create mode 100644 LCD/LCD/KS0066.h diff --git a/LCD/LCD/KS0066.cpp b/LCD/LCD/KS0066.cpp new file mode 100644 index 0000000..5f13dd0 --- /dev/null +++ b/LCD/LCD/KS0066.cpp @@ -0,0 +1,179 @@ +#include "KS0066.h" + +#include + +KS0066::KS0066(ShiftRegister &shiftRegister) : m_shiftRegister(shiftRegister) +{ + initDisplay(); +} + +void KS0066::initDisplay() +{ + _delay_ms(30); + + functionSet(false, false, true); + onOffControl(true, true, true); + displayClear(); + entryMode(true, false); + + sendChar('i'); + sendChar('n'); + sendChar('i'); + sendChar('t'); +} + +void KS0066::functionSet(bool interface8bit, bool twoLine, bool displayOn) +{ + uint8_t command = 0b00100000; + + if (interface8bit) + { + command |= (1 << 4); + } + + if (twoLine) + { + command |= (1 << 3); + } + + if (displayOn) + { + command |= (1 << 2); + } + + sendCommand(command); + + _delay_us(39); +} + +void KS0066::onOffControl(bool displayOn, bool cursorOn, bool blinkingOn) +{ + uint8_t command = 0b00001000; + + if (displayOn) + { + command |= (1 << 2); + } + + if (cursorOn) + { + command |= (1 << 1); + } + + if (blinkingOn) + { + command |= (1 << 0); + } + + sendCommand(command); + + _delay_us(39); +} + +void KS0066::displayClear() +{ + uint8_t command = 0b00000001; + + sendCommand(command); + + _delay_ms(1.53); +} + +void KS0066::entryMode(bool incrementMode, bool entireShift) +{ + uint8_t command = 0b00000100; + + if (incrementMode) + { + command |= (1 << 1); + } + + if (entireShift) + { + command |= (1 << 0); + } + + sendCommand(command); + + _delay_us(39); +} + +void KS0066::sendCommand(uint8_t data) +{ + sendByte(data, true); +} + +void KS0066::sendChar(char c) +{ + sendByte(c, false); + + _delay_us(43); +} + +void KS0066::sendByte(uint8_t data, bool isCommand) +{ + uint8_t registerValue = 0; + + if (!isCommand) + { + registerValue |= (1 << m_rs); + } + + if (data & (1 << 7)) + { + registerValue |= (1 << m_db7); + } + + if (data & (1 << 6)) + { + registerValue |= (1 << m_db6); + } + + if (data & (1 << 5)) + { + registerValue |= (1 << m_db5); + } + + if (data & (1 << 4)) + { + registerValue |= (1 << m_db4); + } + + execute(registerValue); + + registerValue &= (1 << m_rs); + + if (data & (1 << 3)) + { + registerValue |= (1 << m_db7); + } + + if (data & (1 << 3)) + { + registerValue |= (1 << m_db6); + } + + if (data & (1 << 1)) + { + registerValue |= (1 << m_db5); + } + + if (data & (1 << 0)) + { + registerValue |= (1 << m_db4); + } + + execute(registerValue); +} + +void KS0066::execute(uint8_t registerValue) +{ + registerValue |= (1 << m_e); + m_shiftRegister.output(registerValue); + + _delay_us(2); + + registerValue &= ~(1 << m_e); + m_shiftRegister.output(registerValue); +} + diff --git a/LCD/LCD/KS0066.h b/LCD/LCD/KS0066.h new file mode 100644 index 0000000..866e3b1 --- /dev/null +++ b/LCD/LCD/KS0066.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include "ShiftRegister.h" + +class KS0066 +{ +private: + ShiftRegister &m_shiftRegister; + + static const uint8_t m_rs = 0; + static const uint8_t m_rw = 1; + static const uint8_t m_e = 2; + static const uint8_t m_db4 = 3; + static const uint8_t m_db5 = 4; + static const uint8_t m_db6 = 5; + static const uint8_t m_db7 = 6; + +public: + KS0066(ShiftRegister &shiftRegister); + +private: + void initDisplay(); + + void functionSet(bool interface8bit, bool twoLine, bool displayOn); + void onOffControl(bool displayOn, bool cursorOn, bool blinkingOn); + void displayClear(); + void entryMode(bool incrementMode, bool entireShift); + + void sendCommand(uint8_t data); + void sendChar(char c); + + void sendByte(uint8_t data, bool isCommand); + + void execute(uint8_t registerValue); +}; \ No newline at end of file diff --git a/LCD/LCD/LCD.cpp b/LCD/LCD/LCD.cpp index e7615ea..601a0c1 100644 --- a/LCD/LCD/LCD.cpp +++ b/LCD/LCD/LCD.cpp @@ -6,6 +6,7 @@ #include "Pin.h" #include "ShiftRegister.h" +#include "KS0066.h" int main(void) { @@ -15,7 +16,7 @@ int main(void) Pin ledPin(&PORTA, PA0); // lcd initialization goes here - LCD lcd(shiftRegister); + KS0066 lcd(shiftRegister); while(1) { diff --git a/LCD/LCD/LCD.cppproj b/LCD/LCD/LCD.cppproj index c19627f..2eaaa52 100644 --- a/LCD/LCD/LCD.cppproj +++ b/LCD/LCD/LCD.cppproj @@ -133,6 +133,12 @@ + + compile + + + compile + compile diff --git a/LCD/LCD/ShiftRegister.h b/LCD/LCD/ShiftRegister.h index 1887dce..a2b2e4d 100644 --- a/LCD/LCD/ShiftRegister.h +++ b/LCD/LCD/ShiftRegister.h @@ -17,22 +17,3 @@ public: void output(int8_t value); }; - -class LCD -{ -private: - ShiftRegister &m_shiftRegister; - -public: - LCD(ShiftRegister &shiftRegister) - : m_shiftRegister(shiftRegister) - { - - } - -private: - void init() - { - - } -}; \ No newline at end of file