Skip to content

Instantly share code, notes, and snippets.

@Robotonics
Created March 18, 2017 00:38
Show Gist options
  • Save Robotonics/fda42521402cb67f1f4b8b18de7f6abd to your computer and use it in GitHub Desktop.
Save Robotonics/fda42521402cb67f1f4b8b18de7f6abd to your computer and use it in GitHub Desktop.
BBC Micro:Bit LCD 2
from microbit import *
# pin connections
rs = pin0
enable = pin1
datapins = [pin8, pin12, pin2, pin13]
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
def InitDisplay():
# at least 50ms after power on
sleep(50)
# send rs, enable low - rw is tied to GND
rs.write_digital(0)
enable.write_digital(0)
write4bits(0x03)
sleep(5)
write4bits(0x03)
sleep(5)
write4bits(0x03)
sleep(2)
write4bits(0x02)
send(LCD_FUNCTIONSET | 0x08, 0)
sleep(5)
send(LCD_FUNCTIONSET | 0x08, 0)
sleep(2)
send(LCD_FUNCTIONSET | 0x08, 0)
sleep(2)
send(LCD_FUNCTIONSET | 0x08, 0)
sleep(2)
send(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF,0)
clear()
send(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT,0)
# high level commands
def clear():
send(LCD_CLEARDISPLAY,0)
sleep(2)
def home():
send(LCD_RETURNHOME,0)
sleep(2)
def setCursor(col, row):
orpart = col
if row>0:
orpart = orpart + 0x40
send(LCD_SETDDRAMADDR | orpart, 0)
def showText(t):
for c in t:
send(ord(c), 1)
def send(value, mode):
rs.write_digital(mode)
write4bits(value>>4)
write4bits(value)
def pulseEnable():
enable.write_digital(0)
sleep(1)
enable.write_digital(1)
sleep(1)
enable.write_digital(0)
sleep(1)
def write4bits(value):
for i in range(0,4):
datapins[i].write_digital((value>>i) & 0x01)
pulseEnable()
def start_text():
display.show(Image.YES)
clear()
setCursor(1,0) # (column,line)
showText("BBC Micro:Bit")
sleep(100)
setCursor(1,1) # (column,line)
showText(" Initialised")
def show_compass():
clear()
setCursor(4,0)
showText("Compass")
while True:
setCursor(1,1)
showText("Heading: " + str(compass.heading()))
sleep(200)
def x_axis():
clear()
display.show("X")
x=accelerometer.get_x()
setCursor(1,0)
showText("Accelerometer")
setCursor(1,1)
showText("X axis: " + str(x))
def y_axis():
clear()
display.show("Y")
y=accelerometer.get_y()
setCursor(1,0)
showText("Accelerometer")
setCursor(1,1)
showText("Y axis: " + str(y))
def z_axis():
clear()
display.show("Z")
z=accelerometer.get_z()
setCursor(1,0)
showText("Accelerometer")
setCursor(1,1)
showText("Z axis: " + str(z))
def gesture():
clear()
# Start Display
InitDisplay() # Initialise LCD - 2 lines, 4 bit mode
start_text()
sleep(1000)
#show_compass()
x_axis()
sleep(1000)y
y_axis()
sleep(1000)
z_axis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment