ad_convter.py 1.7 KB
'''
@ author: jie
@ tools: pycharm 
@ content: AD转换实现类
@ date: 2021.6.18
'''
import smbus
import time
import logging

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)
address = 0x48

#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
# def setup(Addr):
#     global address 
#     address = Addr

def read(chn): #channel
    if chn == 0:
        bus.write_byte(address,0x40)
        # control = 0x40
        # write = bus.write_byte_data(address, control, 0)
    if chn == 1:
        bus.write_byte(address,0x41)
    if chn == 2:
        bus.write_byte(address,0x42)
    if chn == 3:
        bus.write_byte(address,0x43)
    bus.read_byte(address) # dummy read to start conversion
    return bus.read_byte(address)

def write(val):
    temp = val # move string value to temp 
    temp = int(temp) # change string to integer 
    # print temp to see on terminal else comment out
    bus.write_byte_data(address, 0x40, temp)

# if __name__ == '__main__':
#     # setup(0x48)
#     while True:
#         print ("0x40",read(0))
#         print ("0x41",read(1))
#         print ("0x42",read(2))
#         print ("0x43",read(3))
#         time.sleep(1)

# bus = smbus.SMBus(1)
# address = 0x48
# A0 = 0x40

# class AdConvter():
#     def __init__(self):
#         self.A0 = 0x40
#         self.A1 = 0x41
#         self.A2 = 0x42
#         self.A3 = 0x43

#     # 读取
#     def read(self):
#         # bus = self.bus
#         write = bus.write_byte_data(address, A0, 0)
#         data = bus.read_byte(address)
#         return data

# if __name__ == '__main__':
#     ad = AdConvter()
#     while True:
#         value = ad.read()
#         print (value)
#         time.sleep(1)