Pages

Wednesday, January 16, 2013

MCP23008 IO Expander - LCD1602

Now I am reading the datasheet's reset procedure.
 
So the reset procedure is writing a sequence of nibbles (DB4~7) as below.
 
1. To set 4 bit interface:
 
0x3, 0x3, 0x3, 0x2
 
2. To config 2 character lines, 5x8 dot character pattern:
 
0x2, 0x8
 
3. To config no cursor, no blinking, and blank screen:
 
0x0, 0x8
 
4. To clear display
 
0x0, 0x1
 
5. To config increment cursor, no display shift
 
0x0, 0x6
 
.END
 
# *****************************************************************************
# * LCD1602 v7.7 2013jan16 *
 
# LCD1602 reset procedure
# Part 1 - Config 4 bit instruction mode
# 1. Power on, wait 20mS (> 15mS)
# 2. Write 0x3, wait 5mS (> 4.1mS)
# 3. Write 0x3, wait 1 mS (> 100uS)
# 4. Write 0x3, no need to wait after write from here
# Part 2- Config display characteristics
# 5. Write 0x2, 0x8 (2 lines, 5x8 dot)
# 6. Write 0x0, 0x8 (no cursor, no blinking, blank screen)
# 7. Write 0x0, 0x1 (clear display)
# 8. Write 0x0, 0x6 (increment cursor, no display shift)
 
Lcd1602BaseAddress = MCP23008BaseAddress3 # 0x26
RegisterSelect = 0 # 0 = instruction register, 1 = data register
WriteEnable = 1 # 1 = enable, 0 = disable
DataBit4 = 2
DataBit5 = 3
DataBit6 = 4
DataBit7 = 5
AllZeroDataByte = 0x00
AllOneDataByte = 0xff
EnableWriteMask  = 0x02 # 0b 0000 0010
DisableWriteMask = 0xfd # 0b 1111 1101
SelectDataRegisterMask        = 0x01 # 0b 0000 0001
SelectInstructionRegisterMask = 0xfe # 0b 1111 1110
def LcdEnableWrite(registerBaseAddress, dataByte):
    dataByte = dataByte | EnableWriteMask
    WriteLcdDataByte(registerBaseAddress, dataByte)
def LcdDisableWrite(registerBaseAddress, dataByte):
    dataByte = dataByte & DisableWriteMask
    WriteLcdDataByte(registerBaseAddress, dataByte)
def LcdWritePulse(registerBaseAddress, dataByte):
    LcdEnableWrite(registerBaseAddress, dataByte)
    LcdDisableWrite(registerBaseAddress, dataByte)
def LcdDisableWrite(registerBaseAddress, dataByte):
    dataByte = dataByte & DisableWriteMask
  
def LcdSelectDataRegister(registerBaseAddress, dataByte):
    dataByte = dataByte | SelectDataRegisterMask
    WriteLcdDataByte(registerBaseAddress, dataByte) 
def LcdSelectInstructionRegister(registerBaseAddress, dataByte):
    dataByte = dataByte & SelectInstructionRegisterMask
    WriteLcdDataByte(registerBaseAddress, dataByte)
def SetDataBit(dataByte, bitIndex):
    setDataByte = 0x01 << bitIndex
    dataByte = dataByte | setDataByte
    return dataByte
def ResetDataBit(dataByte, bitIndex):
    resetDataByte = ~(0x01 << bitIndex)
    dataByte = dataByte & resetDataByte
    return dataByte
def SetUpLcd1602(registerBaseAddress):
    SetupMCP23008PortAllOutput(registerBaseAddress)
def WriteLcdDataByte(registerBaseAddress, dataByte):
    WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, dataByte)
def TestLcd16021(registerBaseAddress):
    SetUpLcd1602(registerBaseAddress)
    PrintEightBitPattern("AllZeroByte = ", AllZeroDataByte)
    PrintEightBitPattern("AllZeroByte = ", AllOneDataByte)
    dataByte = AllZeroDataByte
    dataByte = SetDataBit(dataByte, 5)
    PrintEightBitPattern("AllZeroByte with bit 2 set", dataByte)
    WriteLcdDataByte(registerBaseAddress, dataByte)
    sleep(2)
    dataByte = AllZeroDataByte # toggle Enable Bit
    for i in range(4):
        LcdEnableWrite(registerBaseAddress, dataByte)
        sleep(0.5)
        LcdDisableWrite(registerBaseAddress, dataByte)
        sleep(0.5)

    dataByte = AllOneDataByte # toggle Enable bit
    for i in range(4):
        LcdEnableWrite(registerBaseAddress, dataByte)
        sleep(0.5)
        LcdDisableWrite(registerBaseAddress, dataByte)
        sleep(0.5)
    dataByte = AllZeroDataByte # toggle select Data/Instruction register
    for i in range(4):
        LcdSelectInstructionRegister(registerBaseAddress, dataByte)
        sleep(0.5)
        LcdSelectDataRegister(registerBaseAddress, dataByte)
        sleep(0.5)
    dataByte = AllOneDataByte # toggle select Data/Instruction register
    for i in range(4):
        LcdSelectInstructionRegister(registerBaseAddress, dataByte)
        sleep(0.5)
        LcdSelectDataRegister(registerBaseAddress, dataByte)
        sleep(0.5)

def TestLcd16022(registerBaseAddress):
    SetUpLcd1602(registerBaseAddress)

    ...

.END
 
 
 
 
 

No comments:

Post a Comment