putixの日記

ガジェット好き。iPad mini とオンボロPC を使ってblog更新しています。

GPIO3(raspberry pi その12)

はじめての電子工作ステップ50
(日経linux 2014年11月号)

■ステップ24(7セグLEDを表示)

f:id:putix:20141017002914j:plain
C-551SRD
http://akizukidenshi.com/catalog/g/gI-00640/
http://pc.watch.impress.co.jp/docs/2008/1120/musashino019.htm

import RPi.GPIO as GPIO
import time

listA = [23, 22, 27,  7, 17,  4, 24, 25]
listB = [ 1,  2,  4,  5,  6,  7,  9, 10]
listC = ["E", "D", "C", "DP", "B", "A", "F", "G"]

GPIO.setmode(GPIO.BCM)

for i in range(len(listA)):
        GPIO.setup(listA[i], GPIO.OUT)
        GPIO.output(listA[i], True)

        print "no.%d[%s] GPIO:%d" % (listB[i], listC[i], listA[i])
        raw_input()

GPIO.cleanup()

※数字表示は省略


■ステップ25(LEDをもっと明るく表示)
2SC1815GR
http://akizukidenshi.com/catalog/g/gI-00881/

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)

flag = True

while 1:
        if flag == True:
                flag = False
        else:
                flag = True
        GPIO.output(4, flag)
        time.sleep(1)

p.stop()
GPIO.cleanup()


■ステップ26(i2cで文字表示)

f:id:putix:20141017141549j:plain
AQM0802
http://akizukidenshi.com/catalog/g/gK-06795/
http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2

import smbus
import time

bus = smbus.SMBus(1)

# lcd init
bus.write_i2c_block_data(0x3e, 0x0, [0x38, 0x39, 0x14, 0x78, 0x5f, 0x6a])
time.sleep(0.5)

# display on, clear
bus.write_i2c_block_data(0x3e, 0x0, [0x0c, 0x01])
time.sleep(0.5)
bus.write_i2c_block_data(0x3e, 0x0, [0x06])
time.sleep(0.5)


# 1
bus.write_byte_data(0x3e, 0x0, 0x80)
for c in '1234':
        bus.write_byte_data(0x3e, 0x40, ord(c))

# 2
bus.write_byte_data(0x3e, 0x0, 0xc0)
for c in 'ABCDE':
        bus.write_byte_data(0x3e, 0x40, ord(c))