128x64 grafical display with KS0107B and KS0108B controller

Files at this revision

API Documentation at this revision

Comitter:
hornfeldt
Date:
Tue Nov 17 22:42:50 2009 +0000
Child:
1:1d06bc155ef3
Commit message:

Changed in this revision

Display.cpp Show annotated file Show diff for this revision Revisions of this file
Display.h Show annotated file Show diff for this revision Revisions of this file
font.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Display.cpp	Tue Nov 17 22:42:50 2009 +0000
@@ -0,0 +1,74 @@
+#include "mbed.h"
+#include "Display.h"
+
+Display::Display (PinName _RS, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7)
+    : DB(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7), RS(_RS), RW(_RW), E(_E), CS1(_CS1), CS2(_CS2) {
+    DB.mode(PullNone);
+    E = 0;
+    SendCommand(0x3F, 4|8);
+    for (int c=0;c<128;c++) {
+        for (int r=0;r<8;r++)
+        {
+            write(r,c,0);
+        }
+    }
+}
+DisplayTTY::DisplayTTY (Display *d, int row, int startY, int numOfChars=10, int numOfRows=1, int charOffset=6, int flags=0)
+{
+    display = d;
+}
+int Display::SendCommand(unsigned int Command, int f) {
+    int value = 1;
+    E = 0;
+    RS = 0;
+    RW = 0;
+    CS1 = 0;
+    CS2 = 0;
+    if (f&1)
+        RS = 1;
+    if (f&2)
+        RW = 1;
+    if (f&4)
+        CS1 = 1;
+    if (f&8)
+        CS2 = 1;
+    wait(0.0000005); // 500ns
+    E = 1;
+    if (f&2)
+    {
+        DB.input();
+        wait(0.0000004);
+        value = DB;
+    }
+    else
+    {
+        DB.output();
+        wait(0.0000001);
+        DB = Command;
+    }
+    E = 0;
+    return value;
+}
+
+void Display::write (int page, int y, unsigned int data) {
+    int f = 0;
+    if (y<64)
+        f = 4;
+    else
+        f = 8;
+    CurCol = y;
+    SendCommand(0xB8+(page&0x07), f);
+    SendCommand(0x40+(y&0x3F),f);
+    SendCommand(data, f+1);
+}
+
+void Display::writec (int row, int Y, char c) {
+    if (c>31 && c<127)
+    {
+        write(row,Y,font5x8[(c-32)*5+0]);
+        write(row,Y,font5x8[(c-32)*5+1]);
+        write(row,Y,font5x8[(c-32)*5+2]);
+        write(row,Y,font5x8[(c-32)*5+3]);
+        write(row,Y,font5x8[(c-32)*5+4]);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Display.h	Tue Nov 17 22:42:50 2009 +0000
@@ -0,0 +1,47 @@
+#ifndef DISPLAY_H
+#define DISPLAY_H
+
+#define RS_f = 1
+#define RW_f = 2
+#define CS1_f = 4
+#define CS2_f = 8
+
+#include "mbed.h"
+#include "font.h"
+
+class Display {
+    public:
+        // Constructor:
+        Display (PinName _RS, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7);
+        // Send write command to Display
+        // Page 0-7, y 0-127
+        void write (int page, int y, unsigned int data);
+        // 
+        int SendCommand(unsigned int, int);
+        // Row 0-7, col 0-122
+        void writec(int row, int Y, char c);
+    private:
+        BusInOut DB;
+        DigitalOut RS;
+        DigitalOut RW;
+        DigitalOut E;
+        DigitalOut CS1;
+        DigitalOut CS2;
+        int CurCol;
+};
+
+class DisplayTTY {
+    public:
+        DisplayTTY (Display *d, int row, int startY, int numOfChars, int numOfRows, int charOffset, int flags);
+        int putc (int c);
+    private:
+        Display *display;
+        int row;
+        int startY;
+        int numOfChars;
+        int numOfRows;
+        int charOffset;
+        int flags;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/font.h	Tue Nov 17 22:42:50 2009 +0000
@@ -0,0 +1,99 @@
+// (c) Radosław Kwiecień - http://en.radzio.dxp.pl/ks0108/
+static const char font5x8[] = {
+0x00, 0x00, 0x00, 0x00, 0x00,// (spacja)
+0x00, 0x00, 0x5F, 0x00, 0x00,// !
+0x00, 0x07, 0x00, 0x07, 0x00,// "
+0x14, 0x7F, 0x14, 0x7F, 0x14,// #
+0x24, 0x2A, 0x7F, 0x2A, 0x12,// $
+0x23, 0x13, 0x08, 0x64, 0x62,// %
+0x36, 0x49, 0x55, 0x22, 0x50,// &
+0x00, 0x05, 0x03, 0x00, 0x00,// '
+0x00, 0x1C, 0x22, 0x41, 0x00,// (
+0x00, 0x41, 0x22, 0x1C, 0x00,// )
+0x08, 0x2A, 0x1C, 0x2A, 0x08,// *
+0x08, 0x08, 0x3E, 0x08, 0x08,// +
+0x00, 0x50, 0x30, 0x00, 0x00,// ,
+0x08, 0x08, 0x08, 0x08, 0x08,// -
+0x00, 0x30, 0x30, 0x00, 0x00,// .
+0x20, 0x10, 0x08, 0x04, 0x02,// /
+0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
+0x00, 0x42, 0x7F, 0x40, 0x00,// 1
+0x42, 0x61, 0x51, 0x49, 0x46,// 2
+0x21, 0x41, 0x45, 0x4B, 0x31,// 3
+0x18, 0x14, 0x12, 0x7F, 0x10,// 4
+0x27, 0x45, 0x45, 0x45, 0x39,// 5
+0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
+0x01, 0x71, 0x09, 0x05, 0x03,// 7
+0x36, 0x49, 0x49, 0x49, 0x36,// 8
+0x06, 0x49, 0x49, 0x29, 0x1E,// 9
+0x00, 0x36, 0x36, 0x00, 0x00,// :
+0x00, 0x56, 0x36, 0x00, 0x00,// ;
+0x00, 0x08, 0x14, 0x22, 0x41,// <
+0x14, 0x14, 0x14, 0x14, 0x14,// =
+0x41, 0x22, 0x14, 0x08, 0x00,// >
+0x02, 0x01, 0x51, 0x09, 0x06,// ?
+0x32, 0x49, 0x79, 0x41, 0x3E,// @
+0x7E, 0x11, 0x11, 0x11, 0x7E,// A
+0x7F, 0x49, 0x49, 0x49, 0x36,// B
+0x3E, 0x41, 0x41, 0x41, 0x22,// C
+0x7F, 0x41, 0x41, 0x22, 0x1C,// D
+0x7F, 0x49, 0x49, 0x49, 0x41,// E
+0x7F, 0x09, 0x09, 0x01, 0x01,// F
+0x3E, 0x41, 0x41, 0x51, 0x32,// G
+0x7F, 0x08, 0x08, 0x08, 0x7F,// H
+0x00, 0x41, 0x7F, 0x41, 0x00,// I
+0x20, 0x40, 0x41, 0x3F, 0x01,// J
+0x7F, 0x08, 0x14, 0x22, 0x41,// K
+0x7F, 0x40, 0x40, 0x40, 0x40,// L
+0x7F, 0x02, 0x04, 0x02, 0x7F,// M
+0x7F, 0x04, 0x08, 0x10, 0x7F,// N
+0x3E, 0x41, 0x41, 0x41, 0x3E,// O
+0x7F, 0x09, 0x09, 0x09, 0x06,// P
+0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
+0x7F, 0x09, 0x19, 0x29, 0x46,// R
+0x46, 0x49, 0x49, 0x49, 0x31,// S
+0x01, 0x01, 0x7F, 0x01, 0x01,// T
+0x3F, 0x40, 0x40, 0x40, 0x3F,// U
+0x1F, 0x20, 0x40, 0x20, 0x1F,// V
+0x7F, 0x20, 0x18, 0x20, 0x7F,// W
+0x63, 0x14, 0x08, 0x14, 0x63,// X
+0x03, 0x04, 0x78, 0x04, 0x03,// Y
+0x61, 0x51, 0x49, 0x45, 0x43,// Z
+0x00, 0x00, 0x7F, 0x41, 0x41,// [
+0x02, 0x04, 0x08, 0x10, 0x20,// "\"
+0x41, 0x41, 0x7F, 0x00, 0x00,// ]
+0x04, 0x02, 0x01, 0x02, 0x04,// ^
+0x40, 0x40, 0x40, 0x40, 0x40,// _
+0x00, 0x01, 0x02, 0x04, 0x00,// `
+0x20, 0x54, 0x54, 0x54, 0x78,// a
+0x7F, 0x48, 0x44, 0x44, 0x38,// b
+0x38, 0x44, 0x44, 0x44, 0x20,// c
+0x38, 0x44, 0x44, 0x48, 0x7F,// d
+0x38, 0x54, 0x54, 0x54, 0x18,// e
+0x08, 0x7E, 0x09, 0x01, 0x02,// f
+0x08, 0x14, 0x54, 0x54, 0x3C,// g
+0x7F, 0x08, 0x04, 0x04, 0x78,// h
+0x00, 0x44, 0x7D, 0x40, 0x00,// i
+0x20, 0x40, 0x44, 0x3D, 0x00,// j
+0x00, 0x7F, 0x10, 0x28, 0x44,// k
+0x00, 0x41, 0x7F, 0x40, 0x00,// l
+0x7C, 0x04, 0x18, 0x04, 0x78,// m
+0x7C, 0x08, 0x04, 0x04, 0x78,// n
+0x38, 0x44, 0x44, 0x44, 0x38,// o
+0x7C, 0x14, 0x14, 0x14, 0x08,// p
+0x08, 0x14, 0x14, 0x18, 0x7C,// q
+0x7C, 0x08, 0x04, 0x04, 0x08,// r
+0x48, 0x54, 0x54, 0x54, 0x20,// s
+0x04, 0x3F, 0x44, 0x40, 0x20,// t
+0x3C, 0x40, 0x40, 0x20, 0x7C,// u
+0x1C, 0x20, 0x40, 0x20, 0x1C,// v
+0x3C, 0x40, 0x30, 0x40, 0x3C,// w
+0x44, 0x28, 0x10, 0x28, 0x44,// x
+0x0C, 0x50, 0x50, 0x50, 0x3C,// y
+0x44, 0x64, 0x54, 0x4C, 0x44,// z
+0x00, 0x08, 0x36, 0x41, 0x00,// {
+0x00, 0x00, 0x7F, 0x00, 0x00,// |
+0x00, 0x41, 0x36, 0x08, 0x00,// }
+0x08, 0x08, 0x2A, 0x1C, 0x08,// ->
+0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Nov 17 22:42:50 2009 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "Display.h"
+
+Serial pc(USBTX, USBRX);
+const unsigned char rorschachSmall [] = {
+0x7F, 0xEB, 0x57, 0xFF, 0xF7, 0xFD, 0xB7, 0xFF, 0xCD, 0xFF, 0xFB, 0xEF, 0xBF, 0xFF, 0xED, 0xBF,
+0x3F, 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F, 0x3B, 0xFF, 0xFF,
+0xA8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x01, 0x5C, 0x62, 0xDC, 0xF1, 0x2E, 0xD8, 0x27,
+0x78, 0xC4, 0xB3, 0x24, 0xCA, 0x38, 0xE0, 0x01, 0xFC, 0x8F, 0x89, 0x81, 0x9C, 0xBB, 0xB3, 0x80,
+0x13, 0xFB, 0xA7, 0x40, 0xD5, 0xCE, 0xFA, 0x34, 0xE8, 0x4C, 0xCB, 0xB2, 0xC8, 0xB0, 0x55, 0xB2,
+0xCE, 0x30, 0xF5, 0x0F, 0x52, 0x08, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+0x3E, 0x2A, 0x5C, 0x1E, 0x38, 0x52, 0x38, 0x50, 0x30, 0x29, 0xF0, 0x21, 0x73, 0xA6, 0x52, 0x2D,
+0xFC, 0x49, 0xF3, 0x5D, 0xFF, 0xFA, 0xD5, 0xFE, 0x77, 0xFF, 0xF4, 0xFF, 0xF7, 0xDE, 0xFF, 0xFF,
+0xFF, 0xBD, 0xDF, 0xF3, 0xDF, 0xE4, 0x7F, 0xEF, 0xEB, 0xB7, 0xCA, 0x35, 0xF8, 0x2C, 0x8F, 0x32,
+0x4B, 0x82, 0x58, 0x2A, 0xC0, 0x20, 0xCC, 0xE1, 0xA0, 0xE0, 0xE1, 0xA0, 0x60, 0xB0, 0x48, 0x70
+};
+
+
+
+int main() {
+    Display display (p21, p22, p23, p24, p25, p13, p14, p15, p16, p17, p18, p19, p20);
+    pc.baud(19200);
+    int CursPosY=0;
+    int CursPosB=0;
+    // get the current time from the terminal
+    struct tm t;
+    printf("Enter current date and time:\n");
+    printf("YYYY MM DD HH MM SS[enter]\n");    
+    scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
+                             , &t.tm_hour, &t.tm_min, &t.tm_sec);
+
+    // adjust for tm structure required values
+    t.tm_year = t.tm_year - 1900;
+    t.tm_mon = t.tm_mon - 1;
+    
+    // set the time
+    set_time(mktime(&t));
+    for (int c=0;c<64;c++) {
+        for (int r=0;r<8;r++)
+        {
+            display.write(r,c,rorschachSmall[r*64+c]);
+        }
+    }
+    while(1) {
+        if (++CursPosY>=10)
+        {
+            CursPosY=0;
+            CursPosB++;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Nov 17 22:42:50 2009 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/d1a9de3f4fe0