zoukankan      html  css  js  c++  java
  • 树莓GPIO &&python

     
    from http://www.cnblogs.com/xiaobo-Linux/p/8969324.html
    命令行控制LED灯 

    echo 12 > /sys/class/gpio/export  写入输出口

    cd /sys/class/gpio/ gpio12/

    进入这个端口

    主要的2个文件 direction / value

    direction 控制输出 echo out > direction /控制输入 echo in > direction

    value  控制高低电平 1高电平 ; 0低电平 echo 1 > value点亮 / echo 0 > value 熄灭 

    读取高低电平 cat value图片



    用Python 点亮一盏灯

    $sudo python //启动python;
    >>import RPi.GPIO as GPIO;//导入RPi.GPIO模块
    >>GPIO.setmode(GPIO.BCM);//设置模块;
    >>GPIO.setup(12,GPIO.out)//设置GPIO 12 输出状态;
    >>GPIO.output(12,GPIO.HIGH)//高电平点亮
    >>GPIO.output(12,GPIO.LOW)//低电平熄灭
    >>exit()

    让LED闪烁(python)
     
    import RPi.GPIO as GPIO //导入模块
    import time   //时间
     
     
    GPIO.setmode(GPIO.BCM)  //设置
    GPIO.setup(12,GPIO.OUT)   //输出口12
    while True:    //无线循环
        GPIO.output(12,GPIO.HIGH)  //高电平
        time.sleep(1)    //暂停1S
        GPIO.output(12,GPIO.LOW)  //低电平
        time.sleep(1)    //暂停1s
     
    LED闪瞎眼(Python)
    import RPi.GPIO as GPIO
    import time
    import math
     
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(21,GPIO.OUT)
    GPIO.setup(22,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    GPIO.setup(12,GPIO.OUT)
    GPIO.setup(23,GPIO.OUT)
    GPIO.setup(7,GPIO.OUT)
    GPIO.setup(8,GPIO.OUT)
    for i in range(0,1):
        GPIO.output(18,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(18,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(21,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(21,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(22,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(22,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(23,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(12,GPIO.HIGH)
     time.sleep(0.5)
        GPIO.output(12,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(16,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(16,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(7,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(7,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(8,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(8,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(8,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(8,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(7,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(7,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(16,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(16,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(12,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(12,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(23,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(22,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(22,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(21,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(21,GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(18,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(18,GPIO.LOW)
        time.sleep(0.5)
    while True:
        GPIO.output(18,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(18,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(21,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(21,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(22,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(22,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(23,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(12,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(12,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(16,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(16,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(7,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(7,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(8,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(8,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(8,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(8,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(7,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(7,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(16,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(16,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(12,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(12,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.1)
        GPIO.output(23,GPIO.LOW)
        time.sleep(0.1)
        GPIO.output(22,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(22,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(21,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(21,GPIO.LOW)
        time.sleep(0.01)
        GPIO.output(18,GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(18,GPIO.LOW)
        time.sleep(0.01)
     
     
     
     
     
     
    python 定义
    函数描述
    int(x [,base])
     
    将x转换为一个整数。基数指定为base,如果x是一个字符串。
     
    long(x [,base] )
     
    将x转换为一个长整数。基数指定为base,如果x是一个字符串。
     
    float(x)
     
    将x转换到一个浮点数。
     
    complex(real [,imag])
     
    创建一个复数。
     
    str(x)
     
    转换对象x为字符串表示形式。
     
    repr(x)
     
    对象x转换为一个表达式字符串。
     
    eval(str)
     
    计算一个字符串,并返回一个对象。
     
    tuple(s)
     
    把s转换为一个元组。
     
    list(s)
     
    把s转换为一个列表。
     
    set(s)
     
    把s转换为一个集合。
     
    dict(d)
     
    创建一个字典。 d必须的(键,值)元组序列。
     
    frozenset(s)
     
    把s转换为冻结集。
     
    chr(x)
     
    整数转换为一个字符。
     
    unichr(x)
     
    整数转换为一个Unicode字符。
     
    ord(x)
     
    转换单个字符为整数值。
     
    hex(x)
     
    将整数转换为十六进制字符串。
     
    oct(x)
     
    将整数转换为以八进制的字符串。
     




     
    1.安装python
    $ sudo apt-get install python-dev
    复制代码
     
     
    2.执行更新
    $ sudo easy_install -U distribute
    复制代码
     
    3.安装python-pip
    $ sudo apt-get install python-pip
    复制代码
     
    4.安装python的GPIO库
    $ sudo pip install rpi.gpio
    复制代码
  • 相关阅读:
    [转载]使用uiautomator做UI测试
    [转载]Android相关开发网站
    [转载]Android开发必备的21个免费资源和工具
    c# List集合的Find方法适用
    c# GridView Footor列求合计
    c# List集合排序
    mysql中插入多条记录-微软批处理
    mysql中插入多条记录-微软批处理
    VS2005快捷键
    LinqToSql 小例子
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/8969324.html
Copyright © 2011-2022 走看看