7 セグメントディスプレイ
ピンを開く
{:ok, sdi} = Circuits.GPIO.open("GPIO17", :output)
{:ok, rclk} = Circuits.GPIO.open("GPIO18", :output)
{:ok, srclk} = Circuits.GPIO.open("GPIO27", :output)
2進数をビットに分割する
import Bitwise
0b10100001 |> inspect(base: :binary)
0b10100001 <<< 1 |> inspect(base: :binary)
0b10100001 <<< 2 |> inspect(base: :binary)
0b10100001 <<< 3 |> inspect(base: :binary)
(0b10100001 <<< 0 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 1 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 2 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 3 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 4 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 5 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 6 &&& 0b10000000) |> IO.inspect(base: :binary)
(0b10100001 <<< 7 &&& 0b10000000) |> IO.inspect(base: :binary)
data = 0b10100001
for bit <- 0..7 do
div(data <<< bit &&& 0x80, 128)
end
74HC595 に並列出力を設定する
shift_out = fn data, sdi, rclk, srclk ->
for bit <- 0..7 do
bit = data <<< bit &&& 0x80
Circuits.GPIO.write(sdi, bit)
Circuits.GPIO.write(srclk, 1)
Process.sleep(1)
Circuits.GPIO.write(srclk, 0)
end
Circuits.GPIO.write(rclk, 1)
Process.sleep(1)
Circuits.GPIO.write(rclk, 0)
end
shift_out.(0b00111111, sdi, rclk, srclk)
16 進数を表示する
seg_code = [
0x3F,
0x06,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x07,
0x7F,
0x6F,
0x77,
0x7C,
0x39,
0x5E,
0x79,
0x71
]
seg_code
|> Enum.map(fn code ->
shift_out.(code, sdi, rclk, srclk)
Process.sleep(500)
end)
HELLO. を表示する
elixir_code = [
0b01110110,
0b01111001,
0b00111000,
0b00111000,
0b00111111,
0b10000000
]
elixir_code
|> Enum.map(fn code ->
shift_out.(code, sdi, rclk, srclk)
Process.sleep(500)
shift_out.(0b00000000, sdi, rclk, srclk)
Process.sleep(200)
end)
ピンを閉じる
Circuits.GPIO.close(sdi)
Circuits.GPIO.close(rclk)
Circuits.GPIO.close(srclk)