PythonLED制御サンプルプログラム
*サンプルプログラムです。RasPi5の環境の差で動かないこともありますので自己責任で
行ってください。
教則本を基にして爺さんが編集したもので爺さんのRasPi5で動作したプログラムです
(SPI設定が desable で動作しました)
デバッグのために 無駄な print文が入っています
import time
import os
import RPi.GPIO as GPIO
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8
#set BCM_GPIO 18(GPIO1) as LED pin
#sudo raspi-config : Interface -> SPI off
#
#LEDPIN = 17
LEDPIN = 18
analogChannel = 0
#setup function for some setup---custom function
def setup():
global p
GPIO.setwarnings(False)
#set the gpio modes to BCM numbering
GPIO.setmode(GPIO.BCM)
# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
#set all LedPin's mode to output,and initial level to HIGH(3.3V)
GPIO.setup(LEDPIN,GPIO.OUT,initial=GPIO.LOW)
#set LEDPIN as PWM output,and frequency=100Hz
p = GPIO.PWM(LEDPIN,100)
#set p begin with ualue 0
p.start(0)
pass
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
# print ('serial input ADC = %d shift:%d'%(adcout,i))
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
#main function
def main():
while True:
adc = readadc(analogChannel, SPICLK, SPIMOSI, SPIMISO, SPICS)
# print ('LightSensor ADC = %d'%(adc))
adcdu=(1023-adc)*100/1023
print ('LightSensor ADC = %d (duty:%d)'%(adc,adcdu))
p.ChangeDutyCycle(int(adcdu))
time.sleep(0.5)
#define a destroy function for clean up everything after the script finished
def destroy():
#stop p
p.stop()
#turn off led
GPIO.output(LEDPIN,GPIO.LOW)
#release resource
GPIO.cleanup()
#
# if run this script directly ,do:
if __name__ == '__main__':
setup()
try:
main()
#when 'Ctrl+C' is pressed,child program destroy() will be executed.
except KeyboardInterrupt:
destroy()