Now I have written a function to initialize LCD1602. The program is compiled OK. But I have 1% confidence that it has no bugs. I guess I need 1 hour at least to debug.
# * LCD1602 v8.1 2013jan17 *
Lcd1602BaseAddress = MCP23008BaseAddress3 # 0x26
# References
# 1. ShenZhen YaJingDa Electronics YJD1602A-1 datasheet
# 2. PowerTip PC-1602F datasheet
# LCD1602 reset procedure
# Part 1 - Pseudo 8 bit instructions to config 4 bit instruction mode (0x30, 0x30, 0x30, 0x20)
# 1. Power on, wait 20mS (> 15mS)
# 2. Write 0x3, wait 5mS (> 4.1mS) # set 8 bit interface
# 3. Write 0x3, wait 1mS (> 100uS) # set 8 bit interface
# 4. Write 0x3, wait 1mS (> 37uS) # set 8 bit interface
# 5. Write 0x2, wait 1mS (> 37uS) # set 4 bit interface
# Part 2- 4 bit instructions to config display characteristics (0x28, 0x08, 0x01, 0x06, 0x02)
# 6. Write 0x2, wait 1mS (> 37uS) 0x8 wait 1mS (> 37uS) (2 lines, 5x8 dot)
# 7. Write 0x0, wait 1mS (> 37uS) 0x8 wait 1mS (> 37uS) (cursor on, no blinking, blank screen)
# 8. Write 0x0, wait 1mS (> 37uS) 0x1 wait 2mS (> 1.52mS)(clear display)
# 9. Write 0x0, wait 1mS (> 37uS) 0x6 (cursor increment, display no shift)
# a. Write 0x0, wait 1mS (> 37uS) 0x2 wait 2mS (cursor home)
# * Pin numbering *
RegisterSelect = 0 # 0 = instruction register, 1 = data register
WriteEnable = 1 # 1 = enable, 0 = disable
DataBit4 = 2
DataBit5 = 3
DataBit6 = 4
DataBit7 = 5
# * Constant data bytes *
AllZeroDataByte = 0x00
AllOneDataByte = 0xff
# * Write Enable/Disable, Select Data/Instruction masks *
EnableWriteMask = 0x02 # 0b 0000 0010
DisableWriteMask = 0xfd # 0b 1111 1101
SelectDataRegisterMask = 0x01 # 0b 0000 0001
SelectInstructionRegisterMask = 0xfe # 0b 1111 1110
# Register selection *
InstructionRegister = 0
DataRegister = 1
# * Instructions *
PowerOnResetDelay = 0.05 # 50mS
VeryLongOperationDelay = 0.005 # 5mS
LongOperationDelay = 0.002 # 2mS
ShortOperationDelay = 0.00005 # 50uS
EightBitInstructionMode = 0x3
FourBitInstructionMode = 0x2
TwoLineFiveTimesEightDot1 = 0x2
TwoLineFiveTimesEightDot2 = 0x8
CursorOnNoBlinkingBlankScreen1 = 0x0
CursorOnNoBlinkingBlankScreen2 = 0x8
ClearDisplay1 = 0x0
ClearDisplay2 = 0x1
CursorIncrementDisplayNoShift1 = 0x0
CursorIncrementDisplayNoShift2 = 0x6
CursorHome1 = 0x0
CursorHome2 = 0x2
def LcdSelectDataRegister(registerBaseAddress, dataByte):
dataByte = dataByte | SelectDataRegisterMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdSelectInstructionRegister(registerBaseAddress, dataByte):
dataByte = dataByte & SelectInstructionRegisterMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdEnableWrite(registerBaseAddress, dataByte):
dataByte = dataByte | EnableWriteMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdDisableWrite(registerBaseAddress, dataByte):
dataByte = dataByte & DisableWriteMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdWritePulse(registerBaseAddress, dataByte):
LcdEnableWrite(registerBaseAddress, dataByte)
LcdDisableWrite(registerBaseAddress, dataByte)
return dataByte
def LcdSetup(registerBaseAddress):
SetupMCP23008PortAllOutput(registerBaseAddress)
def LcdWriteDataByte(registerBaseAddress, dataByte):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, dataByte)
return dataByte
def LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble, registerType, operationDelay):
dataNibble = dataNibble << 2
#dataNibble = dataNibble | 0b11000011
dataByte = dataByte & 0b11000011
dataByte = dataByte | dataNibble
if (registerType == InstructionRegister):
LcdSelectInstructionRegister(registerBaseAddress, dataByte)
elif (registerType == DataRegister):
LcdSelectDataRegister(registerBaseAddress, dataByte)
LcdDisableWrite(registerBaseAddress, dataByte)
dataByte = LcdWriteDataByte(registerBaseAddress, dataByte)
LcdWritePulse(registerBaseAddress, dataByte)
sleep(operationDelay)
return dataByte
def LcdFourBitInstructionModeInitialization(registerBaseAddress):
dataByte = AllZeroDataByte
LcdDisableWrite(registerBaseAddress, dataByte)
sleep(PowerOnResetDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, VeryLongOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, LongOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, FourBitInstructionMode, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, TwoLineFiveTimesEightDot1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, TwoLineFiveTimesEightDot2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorOnNoBlinkingBlankScreen1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorOnNoBlinkingBlankScreen2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, ClearDisplay1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, ClearDisplay2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorIncrementDisplayNoShift1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorIncrementDisplayNoShift2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorHome1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorHome2, InstructionRegister, ShortOperationDelay)
def TestLcd16021(registerBaseAddress):
LcdSetup(registerBaseAddress)
PrintEightBitPattern("AllZeroByte = ", AllZeroDataByte)
PrintEightBitPattern("AllZeroByte = ", AllOneDataByte)
dataByte = AllZeroDataByte
dataByte = SetDataBit(dataByte, 5)
PrintEightBitPattern("AllZeroByte with bit 2 set", dataByte)
LcdWriteDataByte(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):
LcdSetup(registerBaseAddress)
dataByte = AllZeroDataByte
PrintEightBitPattern("dataByte1 = ", dataByte)
dataByte = LcdSelectDataRegister(registerBaseAddress, dataByte)
sleep(0.2)
PrintEightBitPattern("dataByte2 = ", dataByte)
#dataByte = LcdSelectInstructionRegister(registerBaseAddress, dataByte)
#sleep(0.2)
dataByte = LcdEnableWrite(registerBaseAddress, dataByte)
PrintEightBitPattern("dataByte3 = ", dataByte)
sleep(0.2)
#dataByte = LcdDisableWrite(registerBaseAddress, dataByte)
#PrintEightBitPattern("dataByte4 = ", dataByte)
#sleep(0.2)
OneBeep()
dataNibble = 0x05
PrintEightBitPattern("dataNibb1 = ", dataNibble)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble)
PrintEightBitPattern("dataByte5 = ", dataByte)
sleep(0.2)
OneBeep()
dataNibble = 0x0a
PrintEightBitPattern("dataNibb2 = ", dataNibble)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble)
PrintEightBitPattern("dataByte6 = ", dataByte)
sleep(0.2)
def TestLcd16023(registerBaseAddress):
LcdSetup(registerBaseAddress)
LcdFourBitInstructionModeInitialization(registerBaseAddress)
.END
# *****************************************************************************
# tkp82.py - 2013jan17
# Author - tlfong01 http://tlfong01.blogspot.hk/
# Raspberry Pi (Bv2) ,Raspbian Wheezy, Python 2.7.3, GPIO 0.4.1a
# *****************************************************************************
# *****************************************************************************
# License - GNU GPLv3
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see http://www.gnu.org/licenses/.
# *****************************************************************************
# *** Import Python modules ***
import smbus
import sys
import RPi.GPIO as GPIO
from time import sleep
import select # interrupt module, not tested yet
# *****************************************************************************
# * Rpi GPIO pin assignments for LED, buzzer, and button *
GPIO.setmode(GPIO.BOARD) # Use RPi GPIO numbering, Not BCM numbering
GPIO.setwarnings(False) # Disable linux's "pin already in use warning"
# RPi GPIO pin numbering *
# P1-02 5V, P1-04 5V, P1-06 Gnd
# P1-01 3V3, P1-03 I2C SDA1, P1-05 I2C SCL1
# P1-08 UART TxD (MCP23017 Reset)
# P1-10 UART RxD (MCP23017 INTB)
# P1-12 RPi GPIO_GEN1 (BCM18) LED (P1-12 > LED > 330R > Gnd)
# P1-14 Gnd
# P1-16 GPIO_GEN4 - Buzzer, 3V3 5mA (P1-16 > Buzzer > Gnd)
# P1-18 GPIO_GEN5 Button (3V3 > 10K > Contact 1/2 > 330R > Gnd)
# P1-20 Gnd
# P1-22 GPIO_GEN6 - MCP23008 INT / MCP23017 INTA
RPiGPIOgen1 = 12 # Brown (P1-12, BCM GPIO 18) LED
RPiGPIOgen4 = 16 # Yellow (P1-16, BCM GPIO 23) Buzzer
RPiGPIOgen5 = 18 # Green (P1-18, BCM GPIO 24) Button
RPiGPIOgen6 = 22 # Blue (P1-22, BCM GPIO 25) IOx/keypad interrupt
RPiTxD = 8 # Orange (P1-08) UART TxD
RPiRxD = 10 # Yellow (P1-10) UART RxD
# * GPIO device pins assignment *
LEDpin = RPiGPIOgen1
BuzzerPin = RPiGPIOgen4
ButtonPin = RPiGPIOgen5
TxDpin = RPiTxD
RxDpin = RPiRxD
KeypadInterruptPin = ButtonPin
# * GPIO pins list *
OutputPinList = [LEDpin, BuzzerPin, TxDpin]
InputPinWithNoPullUpList = [ButtonPin, KeypadInterruptPin, RPiGPIOgen6]
InputPinWithPullUpList = []
# *****************************************************************************
# * Global constants *
# * Loop counters *
TwoTimes = 2
FourTimes = 4
EightTimes = 8
TenTimes = 10
TwentyTimes = 20
FiftyTimes = 50
OneHundredTimes = 100
TwoHundredTimess = 200
FourHundredTimes = 400
# * Elapse times *
TwentyMilliSeconds = 0.02
FiftyMilliSeconds = 0.05
TenthSecond = 0.1
QuarterSecond = 0.25
HalfSecond = 0.5
OneSecond = 1
OneAndHalfSeconds = 1.5
TwoSeconds = 2
OnTime = TenthSecond
OffTime = QuarterSecond
ButtonDebouncingTime = QuarterSecond
TestTime = FiftyMilliSeconds
# * Nibble naming *
LowNibble = 0
HighNibble = 1
BothNibble = 2 # full byte of 8 bits
# * Nibble constants *
HighNibble1LowNibble0 = 0xf0
HighNibble0LowNibble1 = 0x0f
# * LED and buzzer states *
Off = False
On = True
# * Button states *
ButtonPressed = False
ButonReleased = True
# * Interrupt states *
Low = False
High = True
# *****************************************************************************
# * GPIO functions *
# 5V0 max = 50 mA, 3V3 max = 300 mA, GPIO per pin max = 17mA source, 12mA sink
setupOutputPin = lambda oPin: GPIO.setup(oPin, GPIO.OUT) # set GPIO pin as output
setupInputPinWithNoPullUp = lambda iPin: GPIO.setup(iPin, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # set GPIO pin as input, no pull up
setupInputPinWithPullUp = lambda iPin: GPIO.setup(iPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO pin as input, with pull up
writeOutputPin = lambda oPin, oValue: GPIO.output(oPin, oValue) # write value to output pin
setupWriteOutputPin = lambda oPin, oValue: (setupOutputPin(oPin), writeOutputPin(oPin, oValue)) # set and write
readInputPin = lambda iPin: GPIO.input(ButtonPin) # read value from input pin
def SetupGPIOpins(outputPinList, inputPinWithNoPullUpList, inputPinWithPullUpList): # set up GPIO pins in InputPinList and OutputPinList
for oPin in outputPinList:
setupWriteOutputPin(oPin, Off)
for iPin in inputPinWithNoPullUpList:
setupInputPinWithPullUp(iPin)
for iPin in inputPinWithPullUpList:
setupInputPinWithPullUp(iPin)
def SetupGPIO(): # set up GPIO pins
SetupGPIOpins(OutputPinList, InputPinWithNoPullUpList, InputPinWithPullUpList )
# * pulse and echo functions *
def pulsePin(oPin, onTime, offTime): # blink LED or beep buzzer
writeOutputPin(oPin, On)
sleep(onTime)
writeOutputPin(oPin, Off)
sleep(offTime)
def echoPin(iPin, oPin): # echo input pin to output pin, e.g. button to LED or buzzer
while True:
if readInputPin(iPin) == ButonReleased:
pass
else:
pulsePin(oPin, OnTime, OffTime)
break
continue
def togglePin(oPin, toggleTime): # toggle pin
writeOutputPin(oPin, On)
sleep(toggleTime)
writeOutputPin(oPin, Off)
sleep(toggleTime)
# * Test Buzzer, LED, Button functions *
def TestBuzzer(): # beep 4 times
SetupGPIO()
for i in range (FourTimes):
pulsePin(BuzzerPin, OnTime, OffTime)
def TestLED(): # blink 8 times
SetupGPIO()
for i in range (EightTimes):
pulsePin(LEDpin, OnTime, OffTime)
def TestButtonEchoBuzzer(): #
SetupGPIO()
for i in range (TenTimes):
echoPin(ButtonPin, BuzzerPin)
def TestButtonEchoLED(): #
SetupGPIO()
for i in range (TenTimes):
echoPin(ButtonPin, LEDpin)
# GPIO Interrupt function !!! not yet tested !!!
def TestInterruptPinFallingEdgeDetection(): # !!! Not tested !!!
GPIO.cleanup() # set all input pins no pull up, disable all interutp detection setting
SetupGPIO()
GPIO.set_low_event(InterruptPin) # set up low level detection
for i in range(30):
if GPIO.event_detected(InterruptPin):
break
else:
print "No interrupt detected.", i
sleep(1)
continue
GPIO.set_low_event(InterruptPin, enable = False) # disable detection
print "End of test, or interrupt detected"
# *****************************************************************************
# * Beep functions *
OnTime = 0.1
OffTime = 0.25
def Beep(count):
for i in range(count):
pulsePin(BuzzerPin, OnTime, OffTime)
def StartBeep():
Beep(4)
sleep(1)
def EndBeep():
Beep(4)
def OneBeep():
Beep(1)
def FourBeeps():
Beep(4)
# *****************************************************************************
# * Start / End Program messages *
def StartProgram(message):
SetupGPIO()
StartBeep()
print "\n", message, "\n"
def EndProgram():
EndBeep()
endMessage = "*** End of program.***"
print "\n", endMessage, "\n"
# *****************************************************************************
# * MCP23008 / MCP23017 IO Expander *
# * References *
# 1. Mcirochip MCP23008/MCP23017 datasheet
# 2. Microchip Application Notes AN1081 (Matrix Keypad), AN1043 (GPIO Expander)
# * i2cdetect/i2cset commands *
# To detect I2C device base addresses
# sudo i2cdetect -y 1
# * To toggle GPIO pin *
#!/bin/bash
# i2cset -y 1 0x20 0x00 0x00
# count=0
# while [ $count -lt 10 ];
# do
# i2cset -y 1 0x20 0x0a 0x00
# sleep 0.5
# i2cset -y 1 0x20 0x0a 0xff
# sleep 0.5
# let count++
# done
# To run i2c-X commands in user mode: sudo chmod 666 /dev/i2c-X
# sudo chmod 666 /dev/i2c-1
# * Setup SMBus *
I2C_BUS_NUMBER = 1 # P1-03 = SDA1, P1-05 = SCL1
smBus1 = smbus.SMBus(I2C_BUS_NUMBER) # global variable, cannot be set by a function
# * Register base addresses *
MCP23017BaseAddress1 = 0x22 # 1 x LED, 1 x button
MCP23008BaseAddress1 = 0x24 # 2 x unipolar stepping motors
MCP23008BaseAddress2 = 0x25 # 1 x decimal keypad
MCP23008BaseAddress3 = 0x26 # 1 x LCD1602
# * Port type *
PortA = 0
PortB = 1
# * Data bytes *
AllOutput = 0x00
AllHigh = 0xff
AllLow = 0x00
AlternateHighLow = 0xaa
AlternateLowHigh = 0x55
# * Direction setting bytes *
HalfHighHalfLow = 0xf0
HalfLowHalfHigh = 0x0f
Nibble1HighNibble2Low = 0xf0
Nibble1LowNibble2High = 0x0f
HighNibbleInputLowNibbleOutput = 0xf0
# * MCP23008/MCP23017 register address offsets *
InputOutputDirection = 0
InputPolarity = 1
InterruptOnChangeEnable = 2
InterruptOnChangeDefaultValue = 3
InterruptOnChangeMode = 4
Configuration = 5
PushPullOpenDrain = 6
InterruptFlag = 7
InterruptCapture = 8
PortStatus = 9
OutputLatch = 10
# * MCP23008/MCP23017 Band0/Band1 register address offset arrays *
RegisterAddressOffsetArray0 = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a]
RegisterAddressOffsetArray1 = [0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x11, 0x13, 0x15, 0x17, 0x19,
0x01, 0x03, 0x05, 0x07, 0x09, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a]
# * Setup MCP23008/MCP23017 ports all output *
def SetupMCP23008PortAllOutput(registerBaseAddress):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InputOutputDirection, PortA, AllOutput)
def SetupMCP23017BothPortAllOutput(registerBaseAddress):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InputOutputDirection, PortA, AllOutput)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InputOutputDirection, PortB, AllOutput)
# * Write/Read MCP23008/MCP23017 registers *
def WriteDataByte(registerBaseAddress, registerAddressArray, dataRegisterIndex, portType, dataByte):
if (portType == PortA) | (portType == PortA):
addressOffset = registerAddressArray[dataRegisterIndex]
if (portType == PortB):
addressOffset = registerAddressArray[dataRegisterIndex + 11]
smBus1.write_byte_data(registerBaseAddress, addressOffset, dataByte)
def ReadDataByte(registerBaseAddress, registerAddressArray, dataRegisterIndex, portType):
if (portType == PortA):
addressOffset = registerAddressArray[dataRegisterIndex]
if (portType == PortB):
addressOffset = registerAddressArray[dataRegisterIndex + 11]
dataByte = smBus1.read_byte_data(registerBaseAddress, addressOffset)
return dataByte
def ReadUpperDataNibble(registerBaseAddress, registerAddressArray, dataRegisterIndex, portType):
dataByte = ReadDataByte(registerBaseAddress, registerAddressArray, dataRegisterIndex, portType)
upperDataNibble = dataByte >> 4
return upperDataNibble
# * Test MCP23008/MCP23017 toggle port functions *
def ToggleMCP23008GP(registerBaseAddress, toggleTime, toggleCount):
SetupMCP23008PortAllOutput(registerBaseAddress)
for i in range(toggleCount):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, AllHigh)
sleep(toggleTime)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, AllLow)
sleep(toggleTime)
def ToggleMCP23017GP(registerBaseAddress, toggleTime, toggleCount):
SetupMCP23017BothPortAllOutput(registerBaseAddress)
for i in range(toggleCount):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, AllHigh)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortB, AllHigh)
sleep(toggleTime)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, AllLow)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortB, AllLow)
sleep(toggleTime)
def TestToggleMCP23008GP():
ToggleTime = 0.5
ToggleCount = 4
ToggleMCP23008GP(MCP23008BaseAddress1, ToggleTime, ToggleCount)
def TestToggleMCP23017GP():
ToggleTime = 0.5
ToggleCount = 4
ToggleMCP23008GP(MCP23017BaseAddress1, ToggleTime, ToggleCount)
# * MCP23008/MCP23017 Interrupt functions *
# * MCP23008 Interrupt setting *
def EnableInterruptOnChangeHighNibble(registerBaseAddress):
EnableInterruptHighNibble = 0xf0
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeEnable, PortA, EnableInterruptHighNibble)
def DisableInterruptOnChangeHighNibble(registerBaseAddress):
DisableInterruptHighNibble = 0x00
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeEnable, PortA, DisableInterruptHighNibble)
def SetInterruptOnChangeDefaultHighNibble(registerBaseAddress):
DefaultValueByte = 0xf0
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeDefaultValue, PortA, DefaultValueByte)
def SetInterruptOnChangeCompareDefaultHighNibble(registerBaseAddress):
InterruptOnChangeDefaultValueHighNibble = 0xf0 # GP4~7 change from default value will cause interrupt
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeMode, PortA, InterruptOnChangeDefaultValueHighNibble)
def SetInterruptOutputPushPull(registerBaseAddress): # interrupt pin open drain, no auto add inc, no slew rate
PushPull = 0b00111010 # 0x32, seq/slew disabled, interrupt output drive drive (push pull) active High
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PushPullOpenDrain, PortA, PushPull)
def SetInterruptOutputOpenDrain(registerBaseAddress): # interrupt pin open drain, no auto add inc, no slew rate
OpenDrain = 0b00111000 # 0x34, seq/slew disabled, interrupt output open drain (don't care active High or Low)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PushPullOpenDrain, PortA, OpenDrain)
# * Port A Interrupt checking *
def ReadInterruptFlagHighNibble(registerBaseAddress):
interruptFlagByte = ReadDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptFlag, PortA)
interruptFlagNibble = interruptFlagByte >> 4
return interruptFlagNibble
def ReadInterruptCaptureHighNibble(registerBaseAddress):
interruptCaptureByte = ReadDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptCapture, PortA)
interruptCaptureNibble = interruptCaptureByte >> 4
return interruptCaptureNibble
def DetectOneInterrupt(registerBaseAddress): # !!! NOT tested !!!
interruptFlagNibble = ReadInterruptFlagHighNibble(registerBaseAddress)
interruptCaptureNibble = ReadInterruptCaptureHighNibble(registerBaseAddress)
print "Interrupt capture high nibble = ", ConvertIntegerToFourBitPattern(interruptCaptureNibble)
print "Interrupt capture high nibble = ", ConvertIntegerToFourBitPattern(interruptCaptureNibble)
ClearInterruptOnChangeHighNibble(registerBaseAddress) # !!! clear interrupt !!!
sleep(2)
def TestDetectInterrupt(count): # !!! NOT Tested !!!
RegisterBaseAddress = MCP23008BaseAddress2
SetupKeypadInterruptHighNibbleCompareDefaultValueOpenDrainOutput(RegisterBaseAddress)
for i in range(count):
DetectOneInterrupt(RegisterBaseAddress)
# *****************************************************************************
# * Unipolar Stepping Motor 28BYJ48/NPM-PF35/PX245 *
# Unipolar Stepping Motor Switching Sequence
# 1. Wave sequence = 1 - 3 - 2 - 4 (A-, B-, A+, B+)
# 2. Full step sequence = 13 - 14 - 24 - 23 (A-B-, A-B+, A+B+, A+B-)
# 3. Half step sequence = 13 - 1 - 14 - 4 - 24 - 2 - 23 - 3
# 4. One step swing = 1 - 3 - 1 - 3 (A-, B-, A-, B-)
# Winding A-(1) A+(2) B-(3) B+(4) COM
# NPM PF35 Black Yellow Brown Orange Red
# 28BYJ48 Blue Pink Yellow Orange Red
# PX245 Black Green Blue Red Yelow/White
# * Convert decimal pin number to hex *
def convert1PinToHex(p): # convert 1 of 8 high pin to hex
hexString = 0x01
for count in range(p-1):
hexString = hexString << 1
return hexString
def convert2PinToHex(p1, p2): # convert 2 of 8 high pins to hex
return (convert1PinToHex(p1) | convert1PinToHex(p2))
def convert2PinToHighNibble(p1, p2): # convert 2 of 8 high pins to high nibble
lowNibble = convert1PinToHex(p1) | convert1PinToHex(p2)
highNibble = lowNibble << 4
return highNibble
# * Move unipolar stepping motor *
def WriteMotorWindingWaveSequence1324(RegisterBaseAddress, NibbleType, StepCount, StepTime): # move motor using 13-24 sequence
# Set port all output
WriteDataByte(MCP23008BaseAddress1, RegisterAddressOffsetArray0, InputOutputDirection, PortA, AllOutput)
if NibbleType == LowNibble:
hexString1 = convert2PinToHex(1, 3)
hexString2 = convert2PinToHex(2, 4)
else:
hexString1 = convert2PinToHighNibble(1, 3)
hexString2 = convert2PinToHighNibble(2, 4)
for i in range(StepCount):
WriteDataByte(MCP23008BaseAddress1, RegisterAddressOffsetArray0, OutputLatch, PortA, hexString1)
sleep(StepTime)
WriteDataByte(MCP23008BaseAddress1, RegisterAddressOffsetArray0, OutputLatch, PortA, hexString2)
sleep(StepTime)
def WriteMotorWindingFullStepSequence13232414(RegisterBaseAddress, NibbleType, StepCount, StepTime): #move motor using 13-23-24-14 sequence
# Set port all output
WriteDataByte(MCP23008BaseAddress1, RegisterAddressOffsetArray0, InputOutputDirection, PortA, AllOutput)
if NibbleType == LowNibble:
motorWindingActivationPatternArray = (0x05, 0x06, 0x0a, 0x09)
else:
motorWindingActivationPatternArray = (0x50, 0x60, 0xa0, 0x90)
for i in range(StepCount):
for pattern in motorWindingActivationPatternArray:
WriteDataByte(MCP23008BaseAddress1, RegisterAddressOffsetArray0, OutputLatch, PortA, pattern)
sleep(StepTime)
def TestConvert1PinToHex(): # test convert 1 high pin to hex
print "*** Testing 1 pin number decimal 0 ~ 7 converted to hexdecimal 0x01 ~ 0x80"
for d in range(8):
print hex(convert1PinToHex(d))
def TestConvert2PinToHex(p1, p2): # test convert 2 high pins to hex
print "*** Testing 2 pin numbers decimal 0 ~ 7 converted to hexdecimal"
print "Pin 1 = ", p1, "Pin 2 = ", p2
print "Hex = ", hex(convert2PinToHex(p1, p2))
def TestConvert2PinToHighNibble(p1, p2): # test convert 2 of 8 high pins to high nibble
print "*** Testing 2 pin numbers decimal 0 ~ 7 converted to high nibble"
print "Pin 1 = ", p1, "Pin 2 = ", p2
print "HighNibble = ", hex(convert2PinToHighNibble(p1, p2))
def MoveTwoMotors(RegisterBaseAddress):
OneBeep()
WriteMotorWindingWaveSequence1324(MCP23008BaseAddress1, LowNibble, TwentyTimes, FiftyMilliSeconds)
OneBeep()
WriteMotorWindingWaveSequence1324(MCP23008BaseAddress1, HighNibble, TwentyTimes, FiftyMilliSeconds)
# OneBeep()
# WriteMotorWindingFullStepSequence13232414(MCP23008BaseAddress1, LowNibble, TwentyTimes, FiftyMilliSeconds)
# OneBeep()
# WriteMotorWindingFullStepSequence13232414(MCP23008BaseAddress1, HighNibble, TwentyTimes, FiftyMilliSeconds)
def TestMotor():
MotorRegisterBaseAddress = MCP23008BaseAddress1
MoveTwoMotors(MotorRegisterBaseAddress)
# *****************************************************************************
# * Decimal keypad *
# * Function descriptions *
# SetupKeypad(registerBaseAddress)
# Assign GP0~3 as output (Col0~2, Col3 n.c), GP4~7 input (Row0~3)
# WriteAllColumnsLow(registerBaseAddress)
# Set all column outputs (GP0~2 actually, GP3 dummy)
# LoopUntilGpioPortHighNibbleChange(registerBaseAddress)
# Loop until GPIO register's GP4~7 not all High (some key pressed)
# LoopUntilInterruptCaptureHighNibbleChange(registerBaseAddress)
# Loop until INTCAP register's ICP4~7 not all High (some key pressed)
# Keypad scanning procedure version 1.0
# 1. Set 3 column ports GP0, GP1, GP2 as output (GP3 don't care)
# 2. Set 4 row ports GP4, GP5, GP6, GP7 as input
# 3. Write Low to all column ports
# 4. Wait until any key pressed (pooling or interrupt)
# 5. Find rowNumber and column number
# 6. Calculate key number
# * keypad base address and smBus setting *
KeypadRegisterBaseAddress = MCP23008BaseAddress2
KeypadSmBus = smBus1
PollGpioRegister = 0
PollKeypadInterruptPin = 1
# * Set up keypad matrix GPIO and interrupts *
def SetupKeypad(registerBaseAddress):
SetupKeypadGPIO(registerBaseAddress)
SetupKeypadInterrupt(registerBaseAddress)
ClearKeypadInterrupt(registerBaseAddress)
def SetupKeypadGPIO(registerBaseAddress):
UpperNibbleInputLowerNibbleOutput = 0xf0 # (GP4~7, Row0~3) = input, lower nibble (GP0~3, Column0~2, 3 = no coonect) = output
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InputOutputDirection, PortA, UpperNibbleInputLowerNibbleOutput)
WriteAllColumnsLow(registerBaseAddress)
def SetupKeypadInterrupt(registerBaseAddress):
EnableInterruptOnChangeHighNibble(registerBaseAddress) # Enable interrupt on upper nibble (GP4~7)
SetInterruptOnChangeDefaultHighNibble(registerBaseAddress) # Interrupt default values = all high (GP4~7 0b1111)
SetInterruptOnChangeCompareDefaultHighNibble(registerBaseAddress) # select compare default value interrutpt
SetInterruptOutputOpenDrain(registerBaseAddress) # set interrupt output (INT) open drain
def EnableInterruptOnChangeHighNibble(registerBaseAddress):
EnableInterruptHighNibble = 0xf0
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeEnable, PortA, EnableInterruptHighNibble)
def SetInterruptOnChangeDefaultHighNibble(registerBaseAddress):
DefaultValueByte = 0xf0
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeDefaultValue, PortA, DefaultValueByte)
def SetInterruptOnChangeCompareDefaultHighNibble(registerBaseAddress):
InterruptOnChangeDefaultValueHighNibble = 0xf0 # GP4~7 change from default value will cause interrupt
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, InterruptOnChangeMode, PortA, InterruptOnChangeDefaultValueHighNibble)
def SetInterruptOutputPushPull(registerBaseAddress): # interrupt pin open drain, no auto add inc, no slew rate
PushPull = 0b00111010 # 0x32, seq/slew disabled, interrupt output drive drive (push pull) active High
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PushPullOpenDrain, PortA, PushPull)
def SetInterruptOutputOpenDrain(registerBaseAddress): # interrupt pin open drain, no auto add inc, no slew rate
OpenDrain = 0b00111000 # 0x34, seq/slew disabled, interrupt output open drain (don't care active High or Low)
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PushPullOpenDrain, PortA, OpenDrain)
# * Clear and poll interrupts *
def ClearKeypadInterrupt(registerBaseAddress):
WriteAllColumnsLow(registerBaseAddress)
sleep(0.5)
dummyRead = ReadInterruptCaptureHighNibble(registerBaseAddress)
def ReadKeypadInterruptPin(interruptPin):
interruptStatus = readInputPin(interruptPin)
return interruptStatus
# * Write columns *
def WriteColumns(registerBaseAddress, columnDataNibble):
columnDataByte = columnDataNibble | 0x00
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, columnDataByte)
def WriteAllColumnsLow(registerBaseAddress):
AllColumnsLowNibble = 0x0
WriteColumns(registerBaseAddress, AllColumnsLowNibble)
def WriteColumn0Low(registerBaseAddress):
Column0LowNibble = 0xe
WriteColumns(registerBaseAddress, Column0LowNibble)
def WriteColumn1Low(registerBaseAddress):
Column1LowNibble = 0xd
WriteColumns(registerBaseAddress, Column1LowNibble)
def WriteColumn2Low(registerBaseAddress):
Column2LowNibble = 0xb
WriteColumns(registerBaseAddress, Column2LowNibble)
# * Read rows *
def GetRowDataByte(registerBaseAddress):
rowDataByte = ReadDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
return rowDataByte
def GetRowDataNibble(registerBaseAddress):
rowDataByte = ReadDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
rowDataNibble = rowDataByte >> 4
return rowDataNibble
# * Get row number, column number, and key number *
def GetRowNumber(registerBaseAddress):
rowDataByte = ReadDataByte(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
rowDataNibble = rowDataByte >> 4
if rowDataNibble == 0b1110:
rowNumber = 0
return rowNumber
elif rowDataNibble == 0b1101:
rowNumber = 1
return rowNumber
elif rowDataNibble == 0b1011:
rowNumber = 2
return rowNumber
elif rowDataNibble == 0b0111:
rowNumber = 3
return rowNumber
else:
rowNumber = 99
return rowNumber
def GetColumnNumber(registerBaseAddress):
RowsAllHigh = 0xf
WriteColumn0Low(registerBaseAddress)
rowDataNibble = ReadUpperDataNibble(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
if rowDataNibble != RowsAllHigh:
columnNumber = 0
return columnNumber
WriteColumn1Low(registerBaseAddress)
rowDataNibble = ReadUpperDataNibble(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
if rowDataNibble != RowsAllHigh:
columnNumber = 1
return columnNumber
WriteColumn2Low(registerBaseAddress)
rowDataNibble = ReadUpperDataNibble(registerBaseAddress, RegisterAddressOffsetArray0, PortStatus, PortA)
if rowDataNibble != RowsAllHigh:
columnNumber = 2
return columnNumber
def GetKeyNumber(rowNumber, columnNumber):
# print "Row Number = ", rowNumber
# print "Column Number = ", columnNumber
keyNumber = (rowNumber * 3) + (columnNumber + 1)
return keyNumber
# * Get one key and a sequence of keys *
def GetKey(registerBaseAddress, pollType):
PollGpioRegister = 0
PollKeypadInterruptPin = 1
if (pollType == PollGpioRegister):
# print "Polling GpioRegister"
LoopUntilGpioPortHighNibbleChange(registerBaseAddress)
elif (pollType == PollKeypadInterruptPin):
# print "Polling Interrupt Pin"
LoopUntilKeypadInterruptPinLow(registerBaseAddress, KeypadInterruptPin)
rowNumber = GetRowNumber(registerBaseAddress)
columnNumber = GetColumnNumber(registerBaseAddress)
keyNumber = GetKeyNumber(rowNumber, columnNumber)
return keyNumber
def GetKeyString(registerBaseAddress, pollType, count):
for i in range (count):
OneBeep()
keyNumber = GetKey(registerBaseAddress, pollType)
print "Key", i + 1, " = ", keyNumber
# * Loop until row data changes *
def LoopUntilGpioPortHighNibbleChange(registerBaseAddress):
WriteAllColumnsLow(registerBaseAddress)
NoKeyPressedNibble = 0xf
rowDataNibble = NoKeyPressedNibble
while (rowDataNibble == NoKeyPressedNibble):
rowDataNibble = GetRowDataNibble(registerBaseAddress)
sleep(0.05) # debouncing time 50mS
# * Loop until interrupt key low *
def LoopUntilKeypadInterruptPinLow(registerBaseAddress, interruptPin):
ClearKeypadInterrupt(registerBaseAddress)
interruptPinState = readInputPin(interruptPin)
#print "interruptPinState = ", interruptPinState
while (interruptPinState == True):
interruptPinState = readInputPin(interruptPin)
#print "interruptPinState = ", interruptPinState
#sleep(1)
#print "interruptPinState = ", interruptPinState
sleep(0.05) # debouncing time 50mS
# * Test keypad *
def TestKeypad(registerBaseAddress, pollType, count):
SetupKeypad(registerBaseAddress)
if (pollType == PollKeypadInterruptPin):
SetupKeypadInterrupt(registerBaseAddress)
ClearKeypadInterrupt(registerBaseAddress)
print "*** Start testing keypad. ***"
print "Press keypad ", count, "times."
GetKeyString(registerBaseAddress, pollType, count)
ClearKeypadInterrupt(registerBaseAddress)
print "*** Stop testing keypad. ***\n"
def TestPollingKeypad():
TestKeypad(KeypadRegisterBaseAddress, PollGpioRegister, count = 4)
def TestInterruptKeypad():
TestKeypad(KeypadRegisterBaseAddress, PollKeypadInterruptPin, count = 4)
# *****************************************************************************
# * Data bit processing and printing *
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 ConvertIntegerToFourBitPattern(integer):
FourBitPattern = [""]
for k in range(4):
FourBitPattern = [i+j for i in ['0','1'] for j in FourBitPattern]
return FourBitPattern[integer]
def ConvertIntegerToEightPattern(integer):
EightBitPattern = [""]
for k in range(8):
EightBitPattern = [i+j for i in ['0','1'] for j in EightBitPattern]
return EightBitPattern[integer]
def PrintEightBitPattern(message, dataByte):
eightBitPattern = ConvertIntegerToEightPattern(dataByte)
print message, eightBitPattern
# *****************************************************************************
# * LCD1602 v8.1 2013jan17 *
Lcd1602BaseAddress = MCP23008BaseAddress3 # 0x26
# References
# 1. ShenZhen YaJingDa Electronics YJD1602A-1 datasheet
# 2. PowerTip PC-1602F datasheet
# LCD1602 reset procedure
# Part 1 - Pseudo 8 bit instructions to config 4 bit instruction mode (0x30, 0x30, 0x30, 0x20)
# 1. Power on, wait 20mS (> 15mS)
# 2. Write 0x3, wait 5mS (> 4.1mS) # set 8 bit interface
# 3. Write 0x3, wait 1mS (> 100uS) # set 8 bit interface
# 4. Write 0x3, wait 1mS (> 37uS) # set 8 bit interface
# 5. Write 0x2, wait 1mS (> 37uS) # set 4 bit interface
# Part 2- 4 bit instructions to config display characteristics (0x28, 0x08, 0x01, 0x06, 0x02)
# 6. Write 0x2, wait 1mS (> 37uS) 0x8 wait 1mS (> 37uS) (2 lines, 5x8 dot)
# 7. Write 0x0, wait 1mS (> 37uS) 0x8 wait 1mS (> 37uS) (cursor on, no blinking, blank screen)
# 8. Write 0x0, wait 1mS (> 37uS) 0x1 wait 2mS (> 1.52mS)(clear display)
# 9. Write 0x0, wait 1mS (> 37uS) 0x6 (cursor increment, display no shift)
# a. Write 0x0, wait 1mS (> 37uS) 0x2 wait 2mS (cursor home)
# * Pin numbering *
RegisterSelect = 0 # 0 = instruction register, 1 = data register
WriteEnable = 1 # 1 = enable, 0 = disable
DataBit4 = 2
DataBit5 = 3
DataBit6 = 4
DataBit7 = 5
# * Constant data bytes *
AllZeroDataByte = 0x00
AllOneDataByte = 0xff
# * Write Enable/Disable, Select Data/Instruction masks *
EnableWriteMask = 0x02 # 0b 0000 0010
DisableWriteMask = 0xfd # 0b 1111 1101
SelectDataRegisterMask = 0x01 # 0b 0000 0001
SelectInstructionRegisterMask = 0xfe # 0b 1111 1110
# Register selection *
InstructionRegister = 0
DataRegister = 1
# * Instructions *
PowerOnResetDelay = 0.05 # 50mS
VeryLongOperationDelay = 0.005 # 5mS
LongOperationDelay = 0.002 # 2mS
ShortOperationDelay = 0.00005 # 50uS
EightBitInstructionMode = 0x3
FourBitInstructionMode = 0x2
TwoLineFiveTimesEightDot1 = 0x2
TwoLineFiveTimesEightDot2 = 0x8
CursorOnNoBlinkingBlankScreen1 = 0x0
CursorOnNoBlinkingBlankScreen2 = 0x8
ClearDisplay1 = 0x0
ClearDisplay2 = 0x1
CursorIncrementDisplayNoShift1 = 0x0
CursorIncrementDisplayNoShift2 = 0x6
CursorHome1 = 0x0
CursorHome2 = 0x2
def LcdSelectDataRegister(registerBaseAddress, dataByte):
dataByte = dataByte | SelectDataRegisterMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdSelectInstructionRegister(registerBaseAddress, dataByte):
dataByte = dataByte & SelectInstructionRegisterMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdEnableWrite(registerBaseAddress, dataByte):
dataByte = dataByte | EnableWriteMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdDisableWrite(registerBaseAddress, dataByte):
dataByte = dataByte & DisableWriteMask
LcdWriteDataByte(registerBaseAddress, dataByte)
return dataByte
def LcdWritePulse(registerBaseAddress, dataByte):
LcdEnableWrite(registerBaseAddress, dataByte)
LcdDisableWrite(registerBaseAddress, dataByte)
return dataByte
def LcdSetup(registerBaseAddress):
SetupMCP23008PortAllOutput(registerBaseAddress)
def LcdWriteDataByte(registerBaseAddress, dataByte):
WriteDataByte(registerBaseAddress, RegisterAddressOffsetArray0, OutputLatch, PortA, dataByte)
return dataByte
def LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble, registerType, operationDelay):
dataNibble = dataNibble << 2
#dataNibble = dataNibble | 0b11000011
dataByte = dataByte & 0b11000011
dataByte = dataByte | dataNibble
if (registerType == InstructionRegister):
LcdSelectInstructionRegister(registerBaseAddress, dataByte)
elif (registerType == DataRegister):
LcdSelectDataRegister(registerBaseAddress, dataByte)
LcdDisableWrite(registerBaseAddress, dataByte)
dataByte = LcdWriteDataByte(registerBaseAddress, dataByte)
LcdWritePulse(registerBaseAddress, dataByte)
sleep(operationDelay)
return dataByte
def LcdFourBitInstructionModeInitialization(registerBaseAddress):
dataByte = AllZeroDataByte
LcdDisableWrite(registerBaseAddress, dataByte)
sleep(PowerOnResetDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, VeryLongOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, LongOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, EightBitInstructionMode, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, FourBitInstructionMode, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, TwoLineFiveTimesEightDot1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, TwoLineFiveTimesEightDot2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorOnNoBlinkingBlankScreen1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorOnNoBlinkingBlankScreen2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, ClearDisplay1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, ClearDisplay2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorIncrementDisplayNoShift1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorIncrementDisplayNoShift2, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorHome1, InstructionRegister, ShortOperationDelay)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, CursorHome2, InstructionRegister, ShortOperationDelay)
def TestLcd16021(registerBaseAddress):
LcdSetup(registerBaseAddress)
PrintEightBitPattern("AllZeroByte = ", AllZeroDataByte)
PrintEightBitPattern("AllZeroByte = ", AllOneDataByte)
dataByte = AllZeroDataByte
dataByte = SetDataBit(dataByte, 5)
PrintEightBitPattern("AllZeroByte with bit 2 set", dataByte)
LcdWriteDataByte(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):
LcdSetup(registerBaseAddress)
dataByte = AllZeroDataByte
PrintEightBitPattern("dataByte1 = ", dataByte)
dataByte = LcdSelectDataRegister(registerBaseAddress, dataByte)
sleep(0.2)
PrintEightBitPattern("dataByte2 = ", dataByte)
#dataByte = LcdSelectInstructionRegister(registerBaseAddress, dataByte)
#sleep(0.2)
dataByte = LcdEnableWrite(registerBaseAddress, dataByte)
PrintEightBitPattern("dataByte3 = ", dataByte)
sleep(0.2)
#dataByte = LcdDisableWrite(registerBaseAddress, dataByte)
#PrintEightBitPattern("dataByte4 = ", dataByte)
#sleep(0.2)
OneBeep()
dataNibble = 0x05
PrintEightBitPattern("dataNibb1 = ", dataNibble)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble)
PrintEightBitPattern("dataByte5 = ", dataByte)
sleep(0.2)
OneBeep()
dataNibble = 0x0a
PrintEightBitPattern("dataNibb2 = ", dataNibble)
dataByte = LcdWriteDataNibble(registerBaseAddress, dataByte, dataNibble)
PrintEightBitPattern("dataByte6 = ", dataByte)
sleep(0.2)
def TestLcd16023(registerBaseAddress):
LcdSetup(registerBaseAddress)
LcdFourBitInstructionModeInitialization(registerBaseAddress)
# ****************************************************************************
# * Main test functions *
# * Setup RPi GPIO *
StartProgram("*** Test LCD1602 - v8.0 TL Fong 2013jan16 ***")
# TestBuzzer() # beep buzzer 4 times
# TestLED() # blink LED 8 tmes
# TestButtonEchoBuzzer() # echo buton with buzzer 10 times
# TestButtonEchoLED() # echo button with LED 10 times
# TestToggleMCP23008GP() # toggle MCP23008 output port
# TestToggleMCP23017GP() # toggle MCP23017 output ports
# TestMotor() # move two stepping motors
# TestPollingKeypad() # read 4 keys by polling IO Expander GPIO port of rows
# TestInterruptKeypad() # read 4 keys by polling IO Expander INT pin
# ToggleMCP23008GP(MCP23008BaseAddress1, toggleTime = 0.25, toggleCount = 4)
# ToggleMCP23008GP(MCP23008BaseAddress3, toggleTime = 2.0, toggleCount = 20)
#TestLcd16021(Lcd1602BaseAddress)
#TestLcd16022(Lcd1602BaseAddress)
TestLcd16023(Lcd1602BaseAddress)
EndProgram()
# *****************************************************************************
# End of Program
# *****************************************************************************
.END
No comments:
Post a Comment