LCD display works

master
mandlm 2016-01-15 13:19:29 +01:00
parent d28b573e76
commit 7c85e773c6
2 changed files with 54 additions and 19 deletions

View File

@ -2,7 +2,8 @@
#include <util/delay.h> #include <util/delay.h>
KS0066::KS0066(ShiftRegister &shiftRegister) : m_shiftRegister(shiftRegister) KS0066::KS0066(ShiftRegister &shiftRegister)
: m_shiftRegister(&shiftRegister)
{ {
initDisplay(); initDisplay();
} }
@ -11,18 +12,24 @@ void KS0066::initDisplay()
{ {
_delay_ms(30); _delay_ms(30);
functionSet(false, false, true); execute((1 << m_db5) | 1 << m_db4);
onOffControl(true, true, true); _delay_ms(5);
execute((1 << m_db5) | 1 << m_db4);
_delay_us(100);
execute((1 << m_db5) | 1 << m_db4);
execute((1 << m_db5));
functionSet(false, true, true);
onOffControl(true, false, false);
displayClear(); displayClear();
entryMode(true, false); entryMode(true, false);
sendChar('i'); sendString("ready.");
sendChar('n');
sendChar('i');
sendChar('t');
} }
void KS0066::functionSet(bool interface8bit, bool twoLine, bool displayOn) void KS0066::functionSet(bool interface8bit, bool twoLine, bool displayFont)
{ {
uint8_t command = 0b00100000; uint8_t command = 0b00100000;
@ -36,14 +43,14 @@ void KS0066::functionSet(bool interface8bit, bool twoLine, bool displayOn)
command |= (1 << 3); command |= (1 << 3);
} }
if (displayOn) if (displayFont)
{ {
command |= (1 << 2); command |= (1 << 2);
} }
sendCommand(command); sendCommand(command);
_delay_us(39); _delay_us(53);
} }
void KS0066::onOffControl(bool displayOn, bool cursorOn, bool blinkingOn) void KS0066::onOffControl(bool displayOn, bool cursorOn, bool blinkingOn)
@ -67,7 +74,7 @@ void KS0066::onOffControl(bool displayOn, bool cursorOn, bool blinkingOn)
sendCommand(command); sendCommand(command);
_delay_us(39); _delay_us(53);
} }
void KS0066::displayClear() void KS0066::displayClear()
@ -76,7 +83,7 @@ void KS0066::displayClear()
sendCommand(command); sendCommand(command);
_delay_ms(1.53); _delay_ms(2.16);
} }
void KS0066::entryMode(bool incrementMode, bool entireShift) void KS0066::entryMode(bool incrementMode, bool entireShift)
@ -95,7 +102,12 @@ void KS0066::entryMode(bool incrementMode, bool entireShift)
sendCommand(command); sendCommand(command);
_delay_us(39); _delay_us(53);
}
void KS0066::setCursorPos(uint8_t line, uint8_t column)
{
sendCommand(0b10000000 + column + (line * 40));
} }
void KS0066::sendCommand(uint8_t data) void KS0066::sendCommand(uint8_t data)
@ -107,7 +119,22 @@ void KS0066::sendChar(char c)
{ {
sendByte(c, false); sendByte(c, false);
_delay_us(43); _delay_us(53);
}
void KS0066::sendString(const char *string)
{
uint8_t count = 0;
while (string != nullptr && *string != 0 && count < (m_lines * m_columns))
{
if (count % m_columns == 0)
{
setCursorPos(count / m_columns, 0);
}
sendChar(*string);
string++;
count++;
}
} }
void KS0066::sendByte(uint8_t data, bool isCommand) void KS0066::sendByte(uint8_t data, bool isCommand)
@ -148,7 +175,7 @@ void KS0066::sendByte(uint8_t data, bool isCommand)
registerValue |= (1 << m_db7); registerValue |= (1 << m_db7);
} }
if (data & (1 << 3)) if (data & (1 << 2))
{ {
registerValue |= (1 << m_db6); registerValue |= (1 << m_db6);
} }
@ -169,11 +196,10 @@ void KS0066::sendByte(uint8_t data, bool isCommand)
void KS0066::execute(uint8_t registerValue) void KS0066::execute(uint8_t registerValue)
{ {
registerValue |= (1 << m_e); registerValue |= (1 << m_e);
m_shiftRegister.output(registerValue); m_shiftRegister->output(registerValue);
_delay_us(2); _delay_us(2);
registerValue &= ~(1 << m_e); registerValue &= ~(1 << m_e);
m_shiftRegister.output(registerValue); m_shiftRegister->output(registerValue);
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "environment.h"
#include <avr/io.h> #include <avr/io.h>
#include "ShiftRegister.h" #include "ShiftRegister.h"
@ -7,7 +9,7 @@
class KS0066 class KS0066
{ {
private: private:
ShiftRegister &m_shiftRegister; ShiftRegister *m_shiftRegister;
static const uint8_t m_rs = 0; static const uint8_t m_rs = 0;
static const uint8_t m_rw = 1; static const uint8_t m_rw = 1;
@ -17,6 +19,9 @@ private:
static const uint8_t m_db6 = 5; static const uint8_t m_db6 = 5;
static const uint8_t m_db7 = 6; static const uint8_t m_db7 = 6;
uint8_t m_lines = 2;
uint8_t m_columns = 16;
public: public:
KS0066(ShiftRegister &shiftRegister); KS0066(ShiftRegister &shiftRegister);
@ -28,8 +33,12 @@ private:
void displayClear(); void displayClear();
void entryMode(bool incrementMode, bool entireShift); void entryMode(bool incrementMode, bool entireShift);
void setCursorPos(uint8_t line, uint8_t column);
void sendCommand(uint8_t data); void sendCommand(uint8_t data);
void sendChar(char c); void sendChar(char c);
void sendString(const char *string);
void sendByte(uint8_t data, bool isCommand); void sendByte(uint8_t data, bool isCommand);