首页 > 其他分享 >I2C接口与超声测距

I2C接口与超声测距

时间:2023-03-15 22:34:43浏览次数:48  
标签:I2C cmd range time address i2c data 超声 测距

i2c_range_smbus.py

'''ultrasonic ranging using I2C'''
'''software tools : smbus'''
'''hardware       : KS103    '''

import smbus
import time

bus = smbus.SMBus(1) #open /dev/i2c-1
address = 0x74 #i2c device address
wr_cmd = 0xb0  #range 0-5m, return distance(mm)
#rd_cmd = 0xb2 
##range 0-5m, return flight time(us), remember divided by 2
try:
    while True:
        bus.write_byte_data(address, 0x2, wr_cmd)
        time.sleep(1) #MIN ~ 0.033
        HighByte = bus.read_byte_data(address, 0x2)
        LowByte = bus.read_byte_data(address, 0x3)
        Dist = (HighByte << 8) + LowByte
        print('Distance:', Dist/10.0, 'cm')
        #time.sleep(2)
except KeyboardInterrupt:
    pass
bus.close()
print('Range over!')

i2c_range_pigpio.py

'''ultrasonic ranging using I2C'''
'''software tools : pigpio'''
'''hardware       : KS103    '''

import pigpio
import time

pi = pigpio.pi()
if not pi.connected:
    exit()
address = 0x74 #i2c device address
h = pi.i2c_open(1,address) #open device at address on bus 1
wr_cmd = 0xb0  #range 0-5m, return distance(mm)
#rd_cmd = 0xb2 
##range 0-5m, return flight time(us), remember divided by 2
try:
    while True:
        pi.i2c_write_byte_data(h, 0x2, wr_cmd)
        time.sleep(1) #MIN ~ 0.033
        HighByte = pi.i2c_read_byte_data(h, 0x2)
        LowByte = pi.i2c_read_byte_data(h, 0x3)
        Dist = (HighByte << 8) + LowByte
        print('Distance:', Dist/10.0, 'cm')
        #time.sleep(2)
except KeyboardInterrupt:
    pass
pi.i2c_close(h)
print('Range over!')

i2c_range_wiringpi.py

'''ultrasonic ranging using I2C'''
'''software tools : wiringpi'''
'''hardware       : KS103    '''

import wiringpi as wpi

address = 0x74 #i2c device address
h = wpi.wiringPiI2CSetup(address) #open device at address
wr_cmd = 0xb0  #range 0-5m, return distance(mm)
#rd_cmd = 0xb2 
##range 0-5m, return flight time(us), remember divided by 2
try:
    while True:
        wpi.wiringPiI2CWriteReg8(h, 0x2, wr_cmd)
        wpi.delay(1000) #unit:ms  MIN ~ 33
        HighByte = wpi.wiringPiI2CReadReg8(h, 0x2)
        LowByte = wpi.wiringPiI2CReadReg8(h, 0x3)
        Dist = (HighByte << 8) + LowByte
        print('Distance:', Dist/10.0, 'cm')
except KeyboardInterrupt:
    pass
print('Range over!')

i2c_temp.py

'''tempature measure using I2C'''
'''software tools : smbus'''
'''hardware       : KS103    '''

import smbus
import time
import sys

bus = smbus.SMBus(1) #open /dev/i2c-1
address = 0x74 #i2c device address
wr_cmd = [0xc9, 0xca, 0xcb, 0xcc]  #tempature measurement cmd
precision = [0.5, 0.25, 0.125, 0.0625] # tempature measurement precision 
#rd_cmd = 0xb2 
##range 0-5m, return flight time(us), remember divided by 2
try:
    print('Please input number 0~3 to select tempature measurement precision. ')
    print('0 : 0.5℃,')
    print('1 : 0.25℃,')
    print('2 : 0.125℃,')
    print('3 : 0.0625℃,')
    cmd_num = eval(input('Input number:'))
    if(cmd_num not in [0,1,2,3]):
        print('Input error!')
        sys.exit()
    print('precision selected:',precision[cmd_num])
    cmd = wr_cmd[cmd_num]
    while True:
        bus.write_byte_data(address, 0x2, cmd)
        time.sleep(1) #MIN ~ 0.061
        HighByte = bus.read_byte_data(address, 0x2)
        LowByte = bus.read_byte_data(address, 0x3)
        data = (HighByte << 8) + LowByte
        if(HighByte >= 0xF8):
            data = data - 65536 #2**16
        Temp = data * 0.0625
        print('Tempature:', Temp, '℃')
        #time.sleep(2)
except KeyboardInterrupt:
    pass
bus.close()
print('The end!')

标签:I2C,cmd,range,time,address,i2c,data,超声,测距
From: https://www.cnblogs.com/fjnhyzCYL/p/17220451.html

相关文章

  • EEPROM_I2C_verilog
    1、I2C通讯协议I2C通讯协议(Inter-IntegratedCircuit)是由Philips公司开发的一种简单、双向二线制同步串行总线,只需要两根线即可在连接于总线上的器件之间传送信息。1.......
  • 普冉PY32系列(六) 通过I2C接口驱动PCF8574扩展的1602LCD
    目录普冉PY32系列(一)PY32F0系列32位CortexM0+MCU简介普冉PY32系列(二)UbuntuGCCToolchain和VSCode开发环境普冉PY32系列(三)PY32F002A资源实测-这个型号不简......
  • MPU6050开发 -- 进阶之I2C/SPI通信协议
    上一篇基本概念讲了一通,大体上对MPU6050有了一个了解。对于MPU6050开发来讲主要的应该就是I2C/SPI通信和寄存器控制了吧。接下来我们继续看芯片手册,简单了解一下I2C/SPI通......
  • I2C的各种信号
    Start或者RepeatStart信号(RepeatStart是没有Stop之前继续Start)1.先拉高SDA2.后拉高SCLtick3.拉低SDAtick4.拉低SCLtick写数据时,8个bit的每个bit1.根......
  • linux 移植 i2CTools
    一、I2C-Tools介绍i2c-tools工具是一个专门用来调试i2c的。并且是开源的。它可以:1、检测有几组i2c总线在系统上。2、查看挂载在i2c上面设备寄......
  • RS485 MODBUS转PROFINET网关案例 | 超声波明渠流量计接入到PLC1200 PROFINE
    本案例介绍的是用北京小疆智控(北京)技术有限公司生产的GW-PN5003型RS485转PROFINET网关将超声波明渠流量计接入西门子PLC1200PROFINET网络的使用方法:  1、首先创建新......
  • IMX平台I2C总线驱动模型
    i2cadapter——I2C适配器在i2c总线结构中,i2cadapter对应着真实的物理设备i2c适配器。i2c-imx.c是I2C适配器的平台总线驱动,文件中包含了对I2C适配器注册和操作的函数,第......
  • linux驱动移植-I2C驱动移植(OLED SSD1306/AT24C08)
    在这一节将会介绍S3C2440I2C适配器的注册,以及AT24C08、SSD1606OLEDI2C设备驱动的编写。一、I2C适配器注册在Mini2440裸机开发之I2C(AT24C08)我们介绍了S3C2440这款SOC的......
  • linux驱动移植-I2C驱动移植(OLED SSD1306/AT24C08)
    ----------------------------------------------------------------------------------------------------------------------------内核版本:linux5.2.8根文件系统:busybo......
  • STM32 I2C介绍和软件模拟I2C编程要点
    I2C协议层独特特征:1. 通过从地址(Slave Address)区分不同的设备。 2. ACK信号体制,即通过ACK表示是否进行继续传输。3.由SCL、SDA的四种关系,映射数据有效性、起始信号......