import RPIO import time import sys import getopt # suppress warning RPIO.setwarnings(False) # Which RPIO Numbering you like to use RPIO.setmode(RPIO.BCM) # Set the used RPIO PORTs # OUT cs_port = 17 clk_port = 27 RPIO.setup(cs_port, RPIO.OUT) RPIO.setup(clk_port, RPIO.OUT) # IN do_port = 22 RPIO.setup(do_port, RPIO.IN) # Clock Time clock_time = 0.001 # 1ms # Global referenz valu ref_voltage = 2.540 # runs v_runs = 10 # Helper Function to set the bits # set a bit to 1 def setBit(int_type, offset): mask = 1 << offset return (int_type | mask) # set a bit to 0 def clearBit(int_type, offset): mask = ~(1 << offset) return (int_type & mask) # get the real Voltage value from the extracted value def getVolt(vvalue): # Value of the Voltage at the Pin 5 - Vref # Not real exact 5V in Raspberry! # Adjust to your real value! global ref_voltage # the ADC0831 has a 8bit resolution => 256 discrete values possible return_val = (vvalue * ref_voltage) / float(255) return return_val # Read the Value from Pin 6 - DO def read_meter(value): # start reading the MSB first offset = 7 #to debug the binary value bin_string = "" # first Clock Cycle RPIO.output(clk_port, RPIO.HIGH) time.sleep(clock_time) RPIO.output(clk_port, RPIO.LOW) time.sleep(clock_time) #second Clock Cycle RPIO.output(clk_port, RPIO.HIGH) # Now we can start measurement for clock in range(8): time.sleep(clock_time) RPIO.output(clk_port, RPIO.LOW) # Read if HIGH or LOW and set the value if RPIO.input(do_port): bin_string = bin_string + "1" value = setBit(value, offset) else: bin_string = bin_string + "0" value = clearBit(value, offset) # debug the result # print "Port 22 => {0:5} :: bit offset {1}".format(str(RPIO.input(do_port)),offset) offset -= 1 # time.sleep(clock_time) RPIO.output(clk_port, RPIO.HIGH) #Print the result print "Value => {0:10.4f} :: binFormat :: {1} :: Bin Value :: {2}".format(getVolt(value), bin_string, value) return getVolt(value) def main(argv): global ref_voltage global v_runs try: opts, args = getopt.getopt(argv, "hv:r:", ["vref=", "runs="]) except getopt.GetoptError: print sys.argv[0] + " -v -r " sys.exit(2) for opt, arg in opts: if opt == '-h': print sys.argv[0] + " -v -r " sys.exit() elif opt in ("-v", "--vref"): ref_voltage = float(arg) elif opt in ("-r", "--runs"): v_runs = int(arg) print "Referenz Voltage => {0:10.4f} :: Runs :: {1}".format(ref_voltage, v_runs) if __name__ == "__main__": main(sys.argv[1:]) # init first time RPIO.output(cs_port, RPIO.HIGH) RPIO.output(clk_port, RPIO.LOW) time.sleep(clock_time) RPIO.output(cs_port, RPIO.LOW) RPIO.output(clk_port, RPIO.HIGH) time.sleep(clock_time) RPIO.output(cs_port, RPIO.HIGH) RPIO.output(clk_port, RPIO.HIGH) v_avg = 0.0 #read the values and get the average value as result for clock in range(v_runs): # set the CS Line to LOW and wait RPIO.output(cs_port, RPIO.LOW) time.sleep(clock_time) # set the clock to LOW as Starting value RPIO.output(clk_port, RPIO.LOW) # Read the DO output v_avg += read_meter(0) # End reading the DO Value RPIO.output(cs_port, RPIO.HIGH) time.sleep(clock_time) RPIO.output(clk_port, RPIO.LOW) print "Total Value => {0:10.4f}".format(v_avg / float(v_runs)) #Set a defined Value for the next start RPIO.output(cs_port, RPIO.HIGH) RPIO.output(clk_port, RPIO.HIGH)