Created
December 12, 2019 15:21
-
-
Save zacck-zz/d6cfb76951d824368bcc9652831180ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LCD | |
def four_bit_init do | |
{:ok, rs} = GPIO.open(22, :output) | |
{:ok, d4} = GPIO.open(25, :output) | |
{:ok, d5} = GPIO.open(24, :output) | |
{:ok, d6} = GPIO.open(23, :output) | |
{:ok, d7} = GPIO.open(18, :output) | |
GPIO.write(rs, 0) | |
GPIO.write(d7, 0) | |
GPIO.write(d6, 0) | |
GPIO.write(d5, 1) | |
GPIO.write(d4, 1) | |
pulse_en() | |
:timer.sleep(5) | |
GPIO.write(rs, 0) | |
GPIO.write(d7, 0) | |
GPIO.write(d6, 0) | |
GPIO.write(d5, 1) | |
GPIO.write(d4, 1) | |
pulse_en() | |
:timer.sleep(5) | |
GPIO.write(rs, 0) | |
GPIO.write(d7, 0) | |
GPIO.write(d6, 0) | |
GPIO.write(d5, 1) | |
GPIO.write(d4, 1) | |
pulse_en() | |
:timer.sleep(5) | |
GPIO.write(rs, 0) | |
GPIO.write(d7, 0) | |
GPIO.write(d6, 0) | |
GPIO.write(d5, 1) | |
GPIO.write(d4, 0) | |
pulse_en() | |
:timer.sleep(5) | |
:ok | |
end | |
def function_set do | |
{:ok, rs} = GPIO.open(22, :output) | |
{:ok, d4} = GPIO.open(25, :output) | |
{:ok, d5} = GPIO.open(24, :output) | |
{:ok, d6} = GPIO.open(23, :output) | |
{:ok, d7} = GPIO.open(18, :output) | |
GPIO.write(rs, 0) | |
GPIO.write(d7, 0) | |
GPIO.write(d6, 0) | |
GPIO.write(d5, 0) | |
GPIO.write(d4, 0) | |
pulse_en() | |
:timer.sleep(5) | |
GPIO.write(d7, 1) | |
GPIO.write(d6, 1) | |
GPIO.write(d5, 1) | |
GPIO.write(d4, 1) | |
pulse_en() | |
:timer.sleep(5) | |
:ok | |
end | |
def blink_cursor do | |
write_byte(0xF, 0) | |
:ok | |
end | |
def display_on do | |
write_byte(0xC, 0) | |
:ok | |
end | |
def display_off do | |
write_byte(0x08, 0) | |
:ok | |
end | |
def set_line_2 do | |
write_byte(0x194, 0) | |
:ok | |
end | |
def shift_display_right do | |
write_byte(0x18, 0) | |
:ok | |
end | |
def right_entry do | |
write_byte(0x01, 0) | |
:ok | |
end | |
def lcd_clear do | |
write_byte(0x01, 0) | |
:ok | |
end | |
def lcd_home do | |
write_byte(0x02, 0) | |
:ok | |
end | |
def lcd_print(string) do | |
String.to_charlist(string) | |
|> Enum.each(fn(char) -> write_byte(char, 1) end) | |
:ok | |
end | |
def lcd_2_line_print(line1, line2) do | |
padded_line = String.pad_trailing(line1, 40) | |
padded_line <> line2 | |
|> lcd_print() | |
end | |
def write_byte(byte, rs) do | |
write_4_bits(byte >>> 4, rs) | |
write_4_bits(byte, rs) | |
end | |
def write_4_bits(bits, rs_val) do | |
{:ok, rs} = GPIO.open(22, :output) | |
{:ok, d4} = GPIO.open(25, :output) | |
{:ok, d5} = GPIO.open(24, :output) | |
{:ok, d6} = GPIO.open(23, :output) | |
{:ok, d7} = GPIO.open(18, :output) | |
GPIO.write(rs, rs_val) | |
GPIO.write(d4, bits &&& 0x01) | |
GPIO.write(d5, bits >>> 1 &&& 0x01) | |
GPIO.write(d6, bits >>> 2 &&& 0x01) | |
GPIO.write(d7, bits >>> 3 &&& 0x01) | |
pulse_en() | |
:timer.sleep(5) | |
end | |
def pulse_en do | |
{:ok, en} = GPIO.open(17, :output) | |
GPIO.write(en, 0) | |
GPIO.write(en, 1) | |
GPIO.write(en, 0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment