LCDPanel implementation for ITDB02-2.2SP LCD module using HX8340B controller

#include "mbed.h"

DigitalOut myled(LED1);

SPI spi(p5,NC,p7);
DigitalOut cs(p8);
DigitalOut rst(p9);

void command(unsigned int i) { spi.write(i&0xff); } // clear 9th bit for command
void data(unsigned int i) { spi.write(i|0x100); } // set 9th bit for data

void lcd_window(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) {
    command(0x2A);  // column
    data(0x00);
    data(x1);
    data(0x00);
    data(x2);
    command(0x2B); // row
    data(0x00);
    data(y1);
    data(0x00);
    data(y2);
    command(0x2C); // start write to ram
}

void lcd_pixel(unsigned int c) {
    data(c>>8); data(c);
}

void lcd_fill(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int color) {
    cs=0;
    lcd_window(x1,y1,x2,y2);
    unsigned int total=(x2-x1+1)*(y2-y1+1);
    for (unsigned int i=0;i<total;i++) { lcd_pixel(color); }
    cs=1;
}

void lcd_clear() {
    lcd_fill(0,0,176,220,0);
}

void lcd_init() {
    cs = 1;
    rst = 0;
    spi.format(9);
    spi.frequency(5000000);
    wait_ms(1);
    rst = 1;
    wait_ms(1);
    cs = 0;
    
    command(0xC1);
    data(0xff);
    data(0x83);
    data(0x40);
    command(0x11);
    wait_ms(150);
    command(0xca);
    data(0x70);
    data(0x00);
    data(0xD9);
    command(0xb0);
    data(0x01);
    data(0x11);
    command(0xc9);
    data(0x90);
    data(0x49);
    data(0x10);
    data(0x28);
    data(0x28);
    data(0x10);
    data(0x00);
    data(0x06);
    wait_ms(20);
    command(0xc2);
    data(0x60);
    data(0x71);
    data(0x01);
    data(0x0e);
    data(0x05);
    data(0x02);
    data(0x09);
    data(0x31);
    data(0x0A);
    command(0xc3);
    data(0x67);
    data(0x30);
    data(0x61);
    data(0x17);
    data(0x48);
    data(0x07);
    data(0x05);
    data(0x33); 
    wait_ms(10);
    command(0xb5);
    data(0x35);
    data(0x20);
    data(0x45); 
    command(0xb4);
    data(0x33);
    data(0x25);
    data(0x4c); 
    wait_ms(10);
    command(0x3a);
    data(0x05);
    command(0x29); 
    wait_ms(10);
    cs=1;
    lcd_clear();
}

int main() {
    lcd_init();
    lcd_fill(9,9,166,210,0xff);
    lcd_fill(19,19,156,200,0x0ff00);
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

Files at this revision

API Documentation at this revision

Comitter:
silviogissi
Date:
Sun Mar 11 01:32:19 2012 +0000
Parent:
0:6339553f69a4
Commit message:

Changed in this revision

LCDPanel_HX8340B.cpp Show annotated file Show diff for this revision Revisions of this file
LCDPanel_HX8340B.h Show annotated file Show diff for this revision Revisions of this file
--- a/LCDPanel_HX8340B.cpp	Fri Mar 09 01:50:15 2012 +0000
+++ b/LCDPanel_HX8340B.cpp	Sun Mar 11 01:32:19 2012 +0000
@@ -1,17 +1,41 @@
+/* LCDPanel base library for LCD/TFT Panels
+ * Copyright (c) 2012, Silvio Gissi */
+
 #include "LCDPanel_HX8340B.h"
 
 LCDPanel_HX8340B::LCDPanel_HX8340B(PinName spi_data, PinName spi_clock, PinName cs, PinName rst) 
-    : LCDPanel (LCDPANEL_HX8340B_WIDTH,LCDPANEL_HX8340B_HEIGHT,cs)
-    , _rst(rst)
-#ifdef LCDPANEL_HX8340B_BITBANG
-    , _scl(spi_clock)
-    , _sda(spi_data)
-#else
-    , _spi(spi_data,NC,spi_clock)
-#endif
+    : LCDPanel_SPI (LCDPANEL_HX8340B_WIDTH,LCDPANEL_HX8340B_HEIGHT,cs,
+	spi_data, spi_clock, rst, 9, 1000000
+    )
 {
-    init();
+    // Reset
+    _rst = 0;
+    wait_ms(5);
+    _rst = 1;
+    wait_ms(5);
+    CsTransaction(this);
+    this->command(0xc1,3,0xff,0x83,0x40);
+    this->command(0x11);
+    wait_ms(10);
+    this->command(0xca,5,0x70,0x00,0xD9,0x01,0x11);
+    this->command(0xc9,8,0x90,0x49,0x10,0x28,0x28,0x10,0x00,0x06);
+    wait_ms(20);
+    this->command(0xc2,9,0x60,0x71,0x01,0x0e,0x05,0x02,0x09,0x31,0x0A);
+    this->command(0xc3,8,0x67,0x30,0x61,0x17,0x48,0x07,0x05,0x33); 
+    wait_ms(10);
+    this->command(0xb5,3,0x35,0x20,0x45); 
+    this->command(0xb4,3,0x33,0x25,0x4c); 
+    wait_ms(10);
+    this->command(0x3a,1,0x05);
+    this->command(0x29); 
+    wait_ms(10);
+    clear();
 }
 
-void LCDPanel_HX8340B::init() {
-}
\ No newline at end of file
+void LCDPanel_HX8340B::window(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) {
+    // Set X window, panel size is less than 255, high byte always 0x00
+    this->command(0x2a,4,0x00,x1,0x00,x2); 
+    this->command(0x2b,4,0x00,y1,0x00,y2);    
+    this->command(0x2c);
+}
+
--- a/LCDPanel_HX8340B.h	Fri Mar 09 01:50:15 2012 +0000
+++ b/LCDPanel_HX8340B.h	Sun Mar 11 01:32:19 2012 +0000
@@ -1,41 +1,50 @@
-/*
-Placeholder for Copyright notice, etc
-
-Author: silviogissi
+/* LCDPanel base library for LCD/TFT Panels
+ * Copyright (c) 2012, Silvio Gissi
+ * 
+ * License:
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * 
+ * Author: Silvio Gissi
+ * Version: 0.1 Alfa
+ * References:
+ * 	Based on demo code from iTeadStudio (www.iteadstudio.com) for
+ * 	ITDB02-2.2SP. License unknown.
+ * 
+ * Known modules:
+ * 	ITDB02-2.2SP (www.iteadstudio.com)
 */
 
 #ifndef LCDPANEL_HX8340B_H
 #define LCDPANEL_HX8340B_H
 
-/*
-Options
-#define LCDPANEL_HX8340B_LANDSCAPE - Use screen in landscape orientation
-#define LCDPANEL_HX8340B_BITBANG   - Bitbang SPI clock and data via DigitalOut instead of hardware SSP
-*/
+#include "LCDPanel_SPI.h"
 
-#ifdef LCDPANEL_HX8340B_LANDSCAPE
-#define LCDPANEL_HX8340B_HEIGHT 176
-#define LCDPANEL_HX8340B_WIDTH  220
-#else
 #define LCDPANEL_HX8340B_HEIGHT 220
 #define LCDPANEL_HX8340B_WIDTH  176
-#endif
 
-#include "LCDPanel.h"
-
-class LCDPanel_HX8340B : public LCDPanel {
+class LCDPanel_HX8340B : public LCDPanel_SPI {
     public:
         LCDPanel_HX8340B(PinName spi_data, PinName spi_clock, PinName cs, PinName rst);
     protected:
-    DigitalOut _rst;
-#ifdef LCDPANEL_HX8340B_BITBANG
-    DigitalOut _scl,_sda;
-#else
-    SPI _spi;
-#endif
-    void init();
-    virtual void pixel(unsigned int color) {}
-    virtual void window(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) {}
+        virtual void window(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);
+
 };
 
 #endif // LCDPANEL_HX8340B_H
\ No newline at end of file