Skip to content

Commit eee2565

Browse files
committed
Add HD44780 example
1 parent ab62b6b commit eee2565

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

ioio/samples/hd44780.bas

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
' HD44780 - Text LCD
2+
' ==================
3+
'
4+
' This example demonstrates how to use the alphanumeric
5+
' dot matrix liquid crystal display HD44780.
6+
'
7+
' Connect the LCD to the IOIO-OTG board:
8+
'
9+
' ------- ------ 1 (GND)
10+
' IOIO | |HD44780 |
11+
' PIN 41|-------|11 (DB4) ---
12+
' PIN 42|-------|12 (DB5) | |
13+
' PIN 43|-------|13 (DB6) |10K|<---3 (VEE)
14+
' PIN 44|-------|14 (DB7) | |
15+
' GND |-------| 5 (RW) ---
16+
' PIN 45|-------| 6 (E) |
17+
' PIN 46|-------| 4 (RS) 2 (VIN)
18+
' GND |-------| 1 (GND)
19+
' 5V |-------| 2 (VIN)
20+
'-------- --------
21+
22+
' A potentiometer needs to be connected to the display
23+
' to control the contrast of the display. If the display
24+
' has a background light, connect power according to the
25+
' data sheet to the pins 15 and 16.
26+
'
27+
' Code based on https://www.mikrocontroller.net/articles/AVR-GCC-Tutorial/LCD-Ansteuerung
28+
29+
30+
'#########################################################
31+
'# Constant definition #
32+
'#########################################################
33+
34+
const LCD_CLEAR_DISPLAY = 0x01
35+
const LCD_CURSOR_HOME = 0x02
36+
37+
const LCD_SET_ENTRY = 0x04
38+
const LCD_ENTRY_DECREASE = 0x00
39+
const LCD_ENTRY_INCREASE = 0x02
40+
const LCD_ENTRY_NOSHIFT = 0x00
41+
const LCD_ENTRY_SHIFT = 0x01
42+
43+
const LCD_SET_DISPLAY = 0x08
44+
const LCD_DISPLAY_OFF = 0x00
45+
const LCD_DISPLAY_ON = 0x04
46+
const LCD_CURSOR_OFF = 0x00
47+
const LCD_CURSOR_ON = 0x02
48+
const LCD_BLINKING_OFF = 0x00
49+
const LCD_BLINKING_ON = 0x01
50+
51+
const LCD_SET_SHIFT = 0x10
52+
const LCD_CURSOR_MOVE = 0x00
53+
const LCD_DISPLAY_SHIFT = 0x08
54+
const LCD_SHIFT_LEFT = 0x00
55+
const LCD_SHIFT_RIGHT = 0x04
56+
57+
const LCD_SET_FUNCTION = 0x20
58+
const LCD_FUNCTION_4BIT = 0x00
59+
const LCD_FUNCTION_8BIT = 0x10
60+
const LCD_FUNCTION_1LINE = 0x00
61+
const LCD_FUNCTION_2LINE = 0x08
62+
const LCD_FUNCTION_5X7 = 0x00
63+
const LCD_FUNCTION_5X10 = 0x04
64+
65+
const LCD_SOFT_RESET = 0x30
66+
67+
const LCD_SET_CGADR = 0x40
68+
69+
const LCD_DDADR_LINE1 = 0x00
70+
const LCD_DDADR_LINE2 = 0x40
71+
const LCD_DDADR_LINE3 = 0x10
72+
const LCD_DDADR_LINE4 = 0x50
73+
74+
const LCD_GC_CHAR0 = 0
75+
const LCD_GC_CHAR1 = 1
76+
const LCD_GC_CHAR2 = 2
77+
const LCD_GC_CHAR3 = 3
78+
const LCD_GC_CHAR4 = 4
79+
const LCD_GC_CHAR5 = 5
80+
const LCD_GC_CHAR6 = 6
81+
const LCD_GC_CHAR7 = 7
82+
83+
const LCD_SET_DDADR = 0x80
84+
85+
'#########################################################
86+
'# Main program #
87+
'#########################################################
88+
89+
import ioio
90+
91+
Print "Connect to HD44780"
92+
RS = ioio.openDigitalOutput(46)
93+
E = ioio.openDigitalOutput(45)
94+
DB4 = ioio.openDigitalOutput(41)
95+
DB5 = ioio.openDigitalOutput(42)
96+
DB6 = ioio.openDigitalOutput(43)
97+
DB7 = ioio.openDigitalOutput(44)
98+
ioio.waitForConnect(10)
99+
Print "Connection established"
100+
101+
102+
Init()
103+
LCD_Write(" Hello World")
104+
LCD_Locate(1,2)
105+
LCD_Write("-= SmallBASIC =-")
106+
print "Done"
107+
108+
109+
end
110+
111+
'#########################################################
112+
'# Functions and subs #
113+
'#########################################################
114+
115+
sub Init()
116+
117+
RS.write(0)
118+
E.write(0)
119+
DB4.write(0)
120+
DB5.write(0)
121+
DB6.write(0)
122+
DB7.write(0)
123+
124+
delay(50)
125+
126+
' Send soft-reset 3 time to initialize LCD
127+
Send4Bit(LCD_SOFT_RESET)
128+
delay(5)
129+
SendEnable()
130+
delay(1)
131+
SendEnable()
132+
delay(1)
133+
134+
' Set 4-bit mode
135+
Send4Bit(LCD_SET_FUNCTION BOR LCD_FUNCTION_4BIT)
136+
delay(5)
137+
138+
' 2 lines and 5x7 pixel in 4 bit mode
139+
SendCommand(LCD_SET_FUNCTION BOR LCD_FUNCTION_4BIT BOR LCD_FUNCTION_2LINE BOR LCD_FUNCTION_5X7)
140+
' Display on, cursor off and blinking off
141+
SendCommand(LCD_SET_DISPLAY BOR LCD_DISPLAY_ON BOR LCD_CURSOR_OFF BOR LCD_BLINKING_OFF)
142+
' Cursor increment no scrolling
143+
SendCommand(LCD_SET_ENTRY BOR LCD_ENTRY_INCREASE BOR LCD_ENTRY_NOSHIFT )
144+
145+
LCD_Cls()
146+
end
147+
148+
sub LCD_Write(Text)
149+
' Write characters to lcd
150+
local length, t
151+
length = len(Text)
152+
153+
for i = 1 to length
154+
t = asc(mid(text, i, 1))
155+
SendData(t)
156+
next
157+
end
158+
159+
sub LCD_Cls()
160+
SendCommand(LCD_CLEAR_DISPLAY)
161+
SendCommand(LCD_CURSOR_HOME)
162+
end
163+
164+
sub LCD_Off()
165+
SendCommand(LCD_SET_DISPLAY BOR LCD_DISPLAY_OFF)
166+
end
167+
168+
sub LCD_On()
169+
SendCommand(LCD_SET_DISPLAY BOR LCD_DISPLAY_ON)
170+
end
171+
172+
sub LCD_Locate(x, y)
173+
local dat
174+
175+
if(x < 1) then x == 1
176+
177+
select case y
178+
case 1 ' 1. line
179+
dat = LCD_SET_DDADR + LCD_DDADR_LINE1 + x - 1
180+
case 2 ' 2. line
181+
dat = LCD_SET_DDADR + LCD_DDADR_LINE2 + x - 1
182+
case 3 ' 3. line
183+
dat = LCD_SET_DDADR + LCD_DDADR_LINE3 + x - 1
184+
case 4 ' 4. line
185+
dat = LCD_SET_DDADR + LCD_DDADR_LINE4 + x - 1
186+
case else
187+
return
188+
end select
189+
190+
SendCommand(dat)
191+
end
192+
193+
sub SendCommand(cmd)
194+
RS.write(0)
195+
SendByte(cmd)
196+
end
197+
198+
sub SendData(dat)
199+
RS.write(1)
200+
SendByte(dat)
201+
end
202+
203+
sub SendEnable()
204+
E.write(1)
205+
delay(1)
206+
E.write(0)
207+
delay(1)
208+
end
209+
210+
sub SendByte(byte)
211+
Send4Bit(byte) ' Send high bits first
212+
Send4Bit(byte lshift 4) ' Send low bits
213+
end
214+
215+
sub Send4Bit(byte)
216+
DB7.write(GetBit(byte, 7))
217+
DB6.write(GetBit(byte, 6))
218+
DB5.write(GetBit(byte, 5))
219+
DB4.write(GetBit(byte, 4))
220+
SendEnable()
221+
end
222+
223+
func GetBit(value, bit)
224+
return (value rshift bit) BAND 1
225+
end

0 commit comments

Comments
 (0)