This is a sample program to drive a 128x128 LCD with t6963 controller through SPI by means of an MCP23S17 16-Bit I/O Expander with Serial Interface

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
gertk
Date:
Sun Dec 05 14:31:49 2010 +0000
Child:
1:e98e29ea2d03
Commit message:
0.0001

Changed in this revision

MCP23S17.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/MCP23S17.h	Sun Dec 05 14:31:49 2010 +0000
@@ -0,0 +1,54 @@
+/* MCP23S17 - drive the Microchip MCP23S17 16-bit Port Extender using SPI
+* Copyright (c) 2010 Romilly Cocking
+* Released under the MIT License: http://mbed.org/license/mit
+*
+* version 0.4
+*/
+#include "mbed.h"
+
+
+#define INTERRUPT_POLARITY_BIT 0x02
+#define INTERRUPT_MIRROR_BIT   0x40
+
+// all register addresses assume IOCON.BANK = 0 (POR default)
+
+#define IODIRA      0x00
+#define IODIRB      0x01
+#define IPOLA       0x02
+#define IPOLB       0x03
+#define GPINTENA    0x04
+#define GPINTENB    0x05
+#define DEFVALA     0x06
+#define DEFVALB     0x07
+#define INTCONA     0x08
+#define INTCONB     0x09
+#define IOCONA      0x0A
+#define IOCONB      0x0B
+#define GPPUA       0x0C
+#define GPPUB       0x0D
+#define INTFA       0x0E
+#define INTFB       0x0F
+#define INTCAPA     0x10
+#define INTCAPB     0x11
+#define GPIOA       0x12
+#define GPIOB       0x13
+#define OLATA       0x14
+#define OLATB       0x15
+
+#define LCD_HALT    0x80
+#define LCD_FS      0x20
+#define LCD_RST     0x10
+#define LCD_CD      0x08
+#define LCD_CE      0x04
+#define LCD_RD      0x02
+#define LCD_WR      0x01
+
+//      T6963C OPCODES
+#define TXHOME      0x40    // SET TXT HM ADD
+#define TXAREA      0x41    // SET TXT AREA
+#define GRHOME      0x42    // SET GRAPHIC HOME ADDRESS
+#define GRAREA      0x43    // SET GRAPHIC AREA
+#define OFFSET      0x22    // SET OFFSET ADDRESS
+#define ADPSET      0x24    // SET ADDRESS POINTER
+#define AWRON       0xB0    // SET AUTO WRITE MODE
+#define AWROFF      0xB2    // RESET AUTO WRITE MODE
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Dec 05 14:31:49 2010 +0000
@@ -0,0 +1,177 @@
+#include "mbed.h"
+#include "MCP23S17.h"
+
+#define LCDFONTSEL  0xFF
+DigitalOut myled(LED1);
+SPI spi(p5, p6, p7); // mosi, miso, sclk
+DigitalOut cs(p20);
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+
+// write 8 bits lcd data
+void lcd_data(unsigned char d)
+{
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOB);  // select GPIOB
+    spi.write(d);      // set data byte
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL-LCD_CE-LCD_CD);   
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL - LCD_WR - LCD_CE - LCD_CD);   
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL - LCD_CD);   
+    cs=1;
+    
+}
+
+// write 8 bits lcd command
+void lcd_command(unsigned char c)
+{
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOB);  // select GPIOB
+    spi.write(c);      // set data byte
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL-LCD_CE);   
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL - LCD_WR - LCD_CE);   
+    cs=1;
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA
+    spi.write(LCDFONTSEL);   
+    cs=1;
+}
+
+void lcd_init()
+{
+    cs=0;
+    spi.write(0x40);
+    spi.write(IODIRA);  // select IODIRA at start
+    spi.write(0x00);    // IODIRA all outputs
+    spi.write(0x00);    // IODIRB all outputs
+    cs=1;
+    wait(0.1);
+    
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA at start
+    spi.write(LCDFONTSEL-LCD_RST);    // activate reset
+    spi.write(0x00);    // all B outputs 0
+    cs=1;
+    wait(0.1);
+
+    cs=0;
+    spi.write(0x40);
+    spi.write(GPIOA);  // select GPIOA at start
+    spi.write(LCDFONTSEL);    // deactivate reset
+    cs=1;
+    wait(0.1);
+
+    
+}
+
+void lcd_string(char *s)
+{
+    while (s[0])
+    {
+        lcd_data(s[0]-32);
+        s++;
+    }
+}
+        
+
+
+int main() {
+
+    unsigned char a;
+    pc.printf("SPI test\n");
+    
+    spi.format(8,0);
+    spi.frequency(1000000);
+//    spi.frequency(10000);
+    wait(0.1);
+
+    pc.printf("MCP init\n");
+    lcd_init();
+
+    // set text home address at 0x0000
+    lcd_data(0x00);
+    lcd_data(0x00);
+    lcd_command(TXHOME);
+    
+    // set graphic home address at 0x0200
+    lcd_data(0x00);
+    lcd_data(0x20);
+    lcd_command(GRHOME);
+    
+    // set text area 20 columns
+    lcd_data(0x14);
+    lcd_data(0x00);
+    lcd_command(TXAREA);
+
+    // set graphic area 20 columns
+    lcd_data(0x14);
+    lcd_data(0x00);
+    lcd_command(GRAREA);
+    
+    // mode set (internal character generation mode)
+    lcd_command(0x80);
+    
+    // set offset register
+    lcd_data(0x02);
+    lcd_data(0x00);
+    lcd_command(OFFSET);
+    
+    // display mode (text on graphics on cursor off)
+    lcd_command(0x94);
+    
+    // write text blank code
+    lcd_data(0x00);
+    lcd_data(0x00);
+    lcd_command(ADPSET);
+    lcd_command(AWRON);
+    for (a=0; a<160; a++) lcd_data(0);
+    lcd_command(AWROFF);
+
+    // write some text    
+    lcd_data(0x40);
+    lcd_data(0x00);
+    lcd_command(ADPSET);
+    lcd_command(AWRON);
+    lcd_string("Hello World!");
+    lcd_string("0123456789");
+    lcd_string("abcdefghijklmnopqrstuvwxyz");
+    lcd_command(AWROFF);
+    lcd_data(0x80);
+                    
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Dec 05 14:31:49 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e