python-on-a-chip online compiler

Dependencies:   mbed TSI

app board LCD

main.py

# app-board-LCD mbed NXP LPC1768
# http://mbed.org/users/chris/code/app-board-LCD/
# http://mbed.org/users/dreschpe/code/C12832_lcd/
import sys
import mbed

class C12832_LCD:
    def __init__(self):
        self._spi = mbed.SPI('p5','NC','p7')
        self._reset = mbed.DigitalOut('p6')
        self._A0 = mbed.DigitalOut('p8')
        self._CS =mbed.DigitalOut('p11')
        self.lcd_reset()

    def lcd_reset(self):
        self._spi.format(8,3)         # 8 bit spi mode 3
        self._spi.frequency(20000000) # 19,2 Mhz SPI clock
        self._A0.write(0)
        self._CS.write(1)
        self._reset.write(0) # display reset
        sys.wait(1) # wait 50us
        self._reset.write(1) # end reset
        sys.wait(5)
        # Start Initial Sequence ----------------------------------------------------
        self.wr_cmd(0xAE)   # display off
        self.wr_cmd(0xA2)   # bias voltage
        self.wr_cmd(0xA0)
        self.wr_cmd(0xC8)   # colum normal
        self.wr_cmd(0x22)   # voltage resistor ratio
        self.wr_cmd(0x2F)   # power on
        #wr_cmd(0xA4)   # LCD display ram
        self.wr_cmd(0x40)   # start line = 0
        self.wr_cmd(0xAF)   # display ON
        self.wr_cmd(0x81)   # set contrast
        self.wr_cmd(0x17)   # set contrast
        self.wr_cmd(0xA6)   # display normal

    def buf_to_lcd(self, page, colum, buffer):
        self.wr_cmd(0x00 + (colum & 0xf))     # set column low nibble
        self.wr_cmd(0x10 + ((colum>>4)&0x0f)) # set column hi  nibble
        self.wr_cmd(0xB0+page)  # set page address
        for d in buffer:
            self.wr_dat(d)

    def wr_cmd(self, cmd):
        self._A0.write(0)
        self._CS.write(0)
        self._spi.write(cmd)
        self._CS.write(1)

    def wr_dat(self, dat):
        self._A0.write(1)
        self._CS.write(0)
        self._spi.write(dat)
        self._CS.write(1)

    def cls(self, data=0):
        for page  in range(4):
            colum = 0
            while colum < 128:
                self.buf_to_lcd(page, colum, [data])
                colum += 1
        self.locate(0, 0)

    def locate(self, row, colum):
        self.page = row
        self.colum = colum * 4

    def crlf(self):
        self.colum = 0
        self.page += 1
        if self.page >= 4:
            self.page = 0

    def puts(self, s):
        for c in s:
            self.putc(c)

    def putc(self, c):
        if c == "\n":
            self.crlf()
            return
        font = self.get_font(c)
        self.buf_to_lcd(self.page, self.colum, font)
        self.colum += len(font)
        if self.colum >= 128:
            self.crlf()            

    def get_font(self, c):
        # misaki_bdf_b11a.tar.gz misaki_4x8_jisx0201.bdf
        font = (
            0,6016,49155,1034559,434070,590866,695986,130,1072896,8001,164746,139016,
            532480,132104,4096,66576,464156,532260,595250,333090,1018392,300334,300316,
            105730,333076,464164,2560,2592,559624,330260,133666,103554,496786,1017022,
            430783,544926,495807,545471,17087,938142,1033279,548769,512016,967231,528447,
            1033023,1015999,495774,99519,1544350,885951,414370,24449,1036351,247871,
            1035327,837171,56835,579249,1081216,352021,16321,32898,1056832,32896,987672,
            397887,594456,1036824,726552,89988,993864,918079,7808,7872,853055,8065,
            921148,918076,397848,397948,2036248,66620,335400,597764,987196,198716,989244,
            592932,993356,727588,1071880,16256,138049,16513,
            )
        i = ord(c) - 0x20
        if i < 0 or i > len(font):
            i = 0
        return(font[i]&0x7f,(font[i]>>7)&0x7f,(font[i]>>14)&0x7f, 0)


lcd = C12832_LCD()
lcd.cls()
lcd.locate(0, 3)
lcd.puts("mbed application board!")
j = 0
while 1:
    lcd.locate(1, 15)
    lcd.puts("Counting : %d" % j);
    j += 1
    sys.wait(1*1000)

All wikipages