Thao Nguyen / LCD5110

Files at this revision

API Documentation at this revision

Comitter:
nguyenmanhthao996tn
Date:
Sat Sep 30 09:12:29 2017 +0000
Commit message:
Worked version

Changed in this revision

LCD5110.cpp Show annotated file Show diff for this revision Revisions of this file
LCD5110.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD5110.cpp	Sat Sep 30 09:12:29 2017 +0000
@@ -0,0 +1,185 @@
+#include "LCD5110.h"
+
+/*********** Variables ***********/
+static DigitalOut *_clk, *_din, *_ce, *_rst, *_dc, *_light;
+
+void LCD5110_init(PinName clk, PinName din, PinName ce, PinName rst, PinName dc, PinName light)
+{
+    LCD5110_GPIO_Config(clk, din, ce, rst, dc, light);
+
+    LCD5110_light_on(); // Light on
+    LCD5110_DC(1);      // LCD_DC = 1;
+    LCD5110_DIN(1);     // SPI_MO = 1;
+    LCD5110_CLK(1);     // SPI_SCK = 1;
+    LCD5110_CE(1);      // SPI_CS = 1;
+
+    LCD5110_RST(0); // LCD_RST = 0;
+    LCD5110_LCD_delay_ms(10);
+    LCD5110_RST(1); // LCD_RST = 1;
+
+    /* LCD Initialize sequence */
+    LCD5110_LCD_write_byte(0x21, 0);
+    LCD5110_LCD_write_byte(0xc6, 0);
+    LCD5110_LCD_write_byte(0x06, 0);
+    LCD5110_LCD_write_byte(0x13, 0);
+    LCD5110_LCD_write_byte(0x20, 0);
+    LCD5110_clear();
+    LCD5110_LCD_write_byte(0x0c, 0);
+}
+
+void LCD5110_LCD_write_byte(unsigned char dat, unsigned char mode)
+{
+    unsigned char i;
+
+    /* Simulate SPI Transfer */
+    LCD5110_CE(0); // SPI_CS = 0;
+
+    if (0 == mode)
+        LCD5110_DC(0); // LCD_DC = 0;
+    else
+        LCD5110_DC(1); // LCD_DC = 1;
+
+    for (i = 0; i < 8; i++) {
+        LCD5110_DIN((dat & 0x80) >> 7); // SPI_MO = dat & 0x80;
+        dat = dat << 1;
+        LCD5110_CLK(0); // SPI_SCK = 0;
+        LCD5110_CLK(1); // SPI_SCK = 1;
+    }
+
+    LCD5110_CE(1); // SPI_CS = 1;
+}
+
+void LCD5110_write_char(unsigned char c)
+{
+    unsigned char line;
+    unsigned char ch = 0;
+
+    c = c - 32;
+
+    for (line = 0; line < 6; line++) {
+        ch = font6_8[c][line];
+        LCD5110_LCD_write_byte(ch, 1);
+    }
+}
+
+void LCD5110_write_char_reg(unsigned char c)
+{
+    unsigned char line;
+    unsigned char ch = 0;
+
+    c = c - 32;
+
+    for (line = 0; line < 6; line++) {
+        ch = ~font6_8[c][line];
+        LCD5110_LCD_write_byte(ch, 1);
+    }
+}
+
+void LCD5110_write_string(char *s)
+{
+    unsigned char ch;
+    while (*s != '\0') {
+        ch = *s;
+        LCD5110_write_char(ch);
+        s++;
+    }
+}
+
+void LCD5110_clear(void)
+{
+    unsigned char i, j;
+    for (i = 0; i < 6; i++)
+        for (j = 0; j < 84; j++)
+            LCD5110_LCD_write_byte(0, 1);
+}
+
+void LCD5110_set_XY(unsigned char X, unsigned char Y)
+{
+    unsigned char x = 6 * X;
+
+    /* Set cursor location */
+    LCD5110_LCD_write_byte(0x40 | Y, 0);
+    LCD5110_LCD_write_byte(0x80 | x, 0);
+}
+
+void LCD5110_write_Dec(unsigned int b)
+{
+
+    unsigned char datas[4];
+
+    datas[0] = b / 1000;
+    b = b - datas[0] * 1000;
+    datas[1] = b / 100;
+    b = b - datas[1] * 100;
+    datas[2] = b / 10;
+    b = b - datas[2] * 10;
+    datas[3] = b;
+
+    datas[0] += 48;
+    datas[1] += 48;
+    datas[2] += 48;
+    datas[3] += 48;
+
+    LCD5110_write_char(datas[0]);
+    LCD5110_write_char(datas[1]);
+    LCD5110_write_char(datas[2]);
+    LCD5110_write_char(datas[3]);
+
+    //a++;
+}
+
+void LCD5110_light_on(void)
+{
+    LCD5110_LIGHT(0); // Light on
+}
+
+void LCD5110_light_off(void)
+{
+    LCD5110_LIGHT(1); // Light off
+}
+
+
+void LCD5110_LCD_delay_ms(unsigned int t)
+{
+    wait_ms(t);
+}
+
+void LCD5110_GPIO_Config(PinName clk, PinName din, PinName ce, PinName rst, PinName dc, PinName light)
+{
+    _clk = new DigitalOut(clk);
+    _din = new DigitalOut(din);
+    _ce = new DigitalOut(ce);
+    _rst = new DigitalOut(rst);
+    _dc = new DigitalOut(dc);
+    _light = new DigitalOut(light);
+}
+
+void LCD5110_CLK(unsigned char temp)
+{
+    *(_clk) = temp;
+}
+
+void LCD5110_DIN(unsigned char temp)
+{
+    *(_din) = temp;
+}
+
+void LCD5110_CE(unsigned char temp)
+{
+    *(_ce) = temp;
+}
+
+void LCD5110_RST(unsigned char temp)
+{
+    *(_rst) = temp;
+}
+
+void LCD5110_DC(unsigned char temp)
+{
+    *(_dc) = temp;
+}
+
+void LCD5110_LIGHT(unsigned char temp)
+{
+    *(_light) = temp;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD5110.h	Sat Sep 30 09:12:29 2017 +0000
@@ -0,0 +1,129 @@
+#ifndef __LCD5110__
+#define __LCD5110__
+
+/*********** Libraries ***********/
+#include "mbed.h"
+#include "PinConfiguration.h"
+
+/*********** Character table ***********/
+static unsigned char font6_8[][6] =
+{
+    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   // sp
+    { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // !
+    { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
+    { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
+    { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
+    { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // %
+    { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
+    { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
+    { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
+    { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
+    { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
+    { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
+    { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },   // ,
+    { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // -
+    { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
+    { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
+    { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
+    { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
+    { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
+    { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
+    { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
+    { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
+    { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
+    { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
+    { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
+    { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
+    { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
+    { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
+    { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
+    { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
+    { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
+    { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
+    { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
+    { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },   // A
+    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
+    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
+    { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
+    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
+    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
+    { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G
+    { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
+    { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
+    { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
+    { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
+    { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
+    { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
+    { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
+    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
+    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
+    { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
+    { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
+    { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
+    { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
+    { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
+    { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
+    { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
+    { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
+    { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
+    { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
+    { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
+    { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // 55
+    { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
+    { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
+    { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
+    { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
+    { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
+    { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
+    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
+    { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
+    { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
+    { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
+    { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },   // g
+    { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
+    { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
+    { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },   // j
+    { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
+    { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
+    { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
+    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
+    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
+    { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },   // p
+    { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },   // q
+    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
+    { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
+    { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
+    { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
+    { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
+    { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
+    { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
+    { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },   // y
+    { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },   // z
+    { 0x00, 0x00, 0x06, 0x09, 0x09, 0x06 }     // horiz lines
+};
+
+/*********** Methods ***********/
+void LCD5110_init(PinName clk, PinName din, PinName ce, PinName rst, PinName dc, PinName light);
+void LCD5110_write_char(unsigned char c);
+void LCD5110_write_char_reg(unsigned char c);
+void LCD5110_clear(void);
+void LCD5110_set_XY(unsigned char X,unsigned char Y);
+void LCD5110_write_string(char *s);
+void LCD5110_write_Dec(unsigned int buffer);
+void LCD5110_light_on(void);
+void LCD5110_light_off(void);
+
+// Define the LCD Operation function
+void LCD5110_LCD_write_byte(unsigned char dat,unsigned char LCD5110_DINde);
+void LCD5110_LCD_delay_ms(unsigned int t);
+
+// Define the hardware operation function
+void LCD5110_GPIO_Config(PinName clk, PinName din, PinName ce, PinName rst, PinName dc, PinName light);
+void LCD5110_CLK(unsigned char temp);
+void LCD5110_DIN(unsigned char temp);
+void LCD5110_CE(unsigned char temp);
+void LCD5110_RST(unsigned char temp);
+void LCD5110_DC(unsigned char temp);
+void LCD5110_LIGHT(unsigned char temp);
+
+#endif /* __LCD5110__ */