2020年4月

openwrt 操作IIC

img_3075-removebg.png
img_3082-removebg.png

固件内置了i2ctools软件包

  1. 用i2cdetect -l 命令来列出所有I2C总线
    root@Widora:/# i2cdetect -l
    i2c-0 i2c 10000900.i2c I2C adapt
  2. 用i2cdetect -F 0 来查看I2C 0号总线启用的功能

    root@Widora:/# i2cdetect -F 0
    Functionalities implemented by /dev/i2c-0:
    I2C yes
    SMBus Quick Command yes
    SMBus Send Byte yes
    SMBus Receive Byte yes
    SMBus Write Byte yes
    SMBus Read Byte yes
    SMBus Write Word yes
    SMBus Read Word yes
    SMBus Process Call yes
    SMBus Block Write yes
    SMBus Block Read no
    SMBus Block Process Call no
    SMBus PEC yes
    I2C Block Write yes
    I2C Block Read yes

  3. 查看I2C 0号总线上挂载的设备

    root@Widora:/# i2cdetect -y 0
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    00: -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- UU -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --

其中标识为UU的设备(地址分别为0x1a和0x34)表示内核已经加载了相应的驱动,而地址为0x48的I2C设备尚未被内核所驱动。

  1. 在sysfs文件中可查看已被内核驱动的I2C设备的名称

     root@Widora:/# ls /sys/bus/i2c/devices
     0-001a  0-0034  i2c-0
     root@Widora:/# cat /sys/bus/i2c/devices/0-001a/name
     wm8960
     root@Widora:/# cat /sys/bus/i2c/devices/0-0034/name
     codec_wm8960
    
  2. 用i2cdump -y 0 0x48 W 命令来读取地址为0x48的I2C设备(LM75温度传感器)所有寄存器中的内容
 root@Widora:/# i2cdump -y 0 0x48 W
    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
    00: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    10: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    20: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    30: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    40: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    50: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
    60: 1e 00 4b 00 ff ff ff ff 1e 00 4b 00 ff ff ff ff    ?.K.....?.K.....
  1. 用i2cget -y 0 0x48 0 w 命令来读取地址为0x48的I2C设备0号寄存器中的内容

K210 例程

20200424105607.png

扫描IIC设备

from machine import I2C
i2c = I2C(I2C.I2C0, freq=100000, scl=28, sda=29)
devices = i2c.scan()
print(devices)

PWM

from machine import Timer,PWM
import time

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=500000, duty=50, pin=board_info.LED_G)
duty=0
dir = True
while True:
    if dir:
        duty += 10
    else:
        duty -= 10
    if duty>100:
        duty = 100
        dir = False
    elif duty<0:
        duty = 0
        dir = True
    time.sleep(0.05)
    ch.duty(duty)

实时拍照

   import sensor
    import image
    import lcd
    lcd.init()
    sensor.reset()
    sensor.set_pixformat(sensor.RGB565)
    sensor.set_framesize(sensor.QVGA)
    sensor.run(1)
    while True:
        img=sensor.snapshot()
        lcd.display(img)

找颜色

import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
green_threshold   = (0,   80,  -70,   -10,   -0,   30)
while True:
    img=sensor.snapshot()
    blobs = img.find_blobs([green_threshold])
    if blobs:    
        for b in blobs:
            tmp=img.draw_rectangle(b[0:4]) 
            tmp=img.draw_cross(b[5], b[6]) 
            c=img.get_pixel(b[5], b[6])
    lcd.display(img)

显示fps

import sensor
import image
import lcd
import time

clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
sensor.skip_frames(30)
while True:
    clock.tick()
    img = sensor.snapshot()
    fps =clock.fps()
    img.draw_string(2,2, ("%2.1ffps" %(fps)), color=(0,128,0), scale=2)
    lcd.display(img)

扫描二维码

import sensor
import image
import lcd
import time

clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.run(1)
sensor.skip_frames(30)
while True:
    clock.tick()
    img = sensor.snapshot()
    res = img.find_qrcodes()
    fps =clock.fps()
    if len(res) > 0:
        img.draw_string(2,2, res[0].payload(), color=(0,128,0), scale=2)
        print(res[0].payload())
    lcd.display(img)