樹莓派用Python寫幾個簡單程式__Python

來源:互聯網
上載者:User

1.樹莓派顯示溫度

import commands  def get_cpu_temp():    tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )    cpu_temp = tempFile.read()    tempFile.close()    return float(cpu_temp)/1000    # Uncomment the next line if you want the temp in Fahrenheit    #return float(1.8*cpu_temp)+32  def get_gpu_temp():    gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )    return  float(gpu_temp)    # Uncomment the next line if you want the temp in Fahrenheit    # return float(1.8* gpu_temp)+32  def main():    print "CPU temp: ", str(get_cpu_temp())    print "GPU temp: ", str(get_gpu_temp())  if __name__ == '__main__':    main()

2. 控制GPIO

先寫一個模組 led.py:

import RPi.GPIO as GPIO  import time   channels = [16,18,22,24,26,19,21,23]    def init():      GPIO.setmode(GPIO.BOARD)      for x in channels:          GPIO.setup(x,GPIO.OUT)          pass   def on(i):      GPIO.output(channels[i], GPIO.HIGH)   def off(i):      GPIO.output(channels[i], GPIO.LOW)   def ctrl(data):      for i in channels:          GPIO.output(i, data & 0x1)          data = data >> 1      pass   def test():      for i in xrange(512):          ctrl(i)           time.sleep(0.1)    def clean():      GPIO.cleanup()

再寫一個程式調用模組 test.py:

import led led.init()led.test()led.clean()

RPi.GPIO模組函數說明:

RPi.GPIO.setmode(naming_system)

設定將GPIO針的命名方式。

naming_system可用的取值有 RPi.GPIO.BCM 和 RPi.GPIO.BOARD,分別代表boardcom命名系統和樹莓派板子上的命名系統。而因為使用BCM 的時候(據說)不同的版本BVM針腳定義不一樣,所以同一套程式在多個樹莓派系統上使用的時候建議用BOARD。

RPi.GPIO.setup(channel, state)
將標號為channel的針設定為state模式。

channel取值為1~26,state取值為RPi.GPIO.IN 或者 RPi.GPIO.OUT,分別表示輸入和輸出。例如 RPi.GPIO.setup(1, RPi.GPIO.IN)表示將1號針設定為輸入模式;RPi.GPIO.setup(3, RPi.GPIO.OUT)表示將3號針設定為輸出模式。具體哪個號是哪根取決於setmode()中設定成什麼。

RPi.GPIO.output(channel, state)
將標號為channel的針設定為state指定的電平。

channel取值為1~26,state取值為RPi.GPIO.HIGH 和 RPi.GPIO.LOW,或者1和0,或者True和False,表示高電平和低電平。例如RPi.GPIO.output(1, 1) 表示把1號針設定為高電平,RPi.GPIO.output(3, Flase) 表示將3號針設定為低電平。具體哪個號是哪根取決於setmode()中設定成什麼。

RPi.GPIO.input(channel)
擷取將標號為channel的針的電平磚頭。

channel取值為1~26。例如RPi.GPIO.input(1) 表示擷取1號針的狀態。

RPi.GPIO.cleanup()
清除掉之前RPi.GPIO.setup()設定的狀態。

退出程式之前一定要調用,否則下次調用的時候會報錯。


led.py模組說明

channel 中儲存的是串連中使用的針的標號,按順序。

init() 是初始化GPIO介面的代碼,使用控制lcd去前要調用。主要工作是設定介面命名模式和 將 channel中的針設定為輸出模式
on() / off() 是將channel 中第i個針設定為高電平/低電平
ctrl() 是根據參數設定全8根針的電平。參數的低0位、低1位、低2位…分別表示channel下標為0、1、2…的針的電平狀況,1為高電平、0為低電平
test() 是測試函數。用8位二進位表示8個燈的狀態,每隔0.1秒到下一個狀態: 0000 0000, 0000 0001, 0000 0010, 0000 0011,0000 0100 … 實際上就是從0數到255
clean() 是對 RPi.GPIO.cleanup() 的一個封裝







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.