FB temperature
サンプルコード
# coding: utf-8
import spidev
import time
import sys
# A0コネクタにTemperatureを接続
TEMPPIN = 0
def readadc(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
def arduino_map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
# 初期化
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 5000
try:
while True:
data = readadc(TEMPPIN)
volt = arduino_map(data, 0, 1023, 0, 5000)
temp = arduino_map(volt, 0, 1500, 0, 150)
sys.stdout.write("\rtemp = %f" % (temp))
sys.stdout.flush()
time.sleep( 0.5 )
except KeyboardInterrupt:
spi.close()
sys.exit(0)