Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pyb import I2C
- import math
- from pyb import UART
- from pyb import delay
- uart = UART(6, 115200)
- i2c = I2C(1, I2C.MASTER, baudrate = 100000)
- while True:
- #i2c.send(0x03, 0x39) # power-up state/read register 0x03
- i2c.send(0x43, 0x39) # read adc channel 0
- chan0 = i2c.recv(1, 0x39) # read 1 bytes from 0x39
- i2c.send(0x83, 0x39) # read adc channel 1
- chan1 = i2c.recv(1, 0x39) # read 1 bytes from 0x39
- # print("Kanava 0:", chan0,"\nKanava 1:", chan1)
- #VALID CHORD BITS STEP BITS
- #B7 B6 B5 B4 B3 B2 B1 B0
- #VALID C2 C1 C0 S3 S2 S1 S0
- c0 = chan0[0]
- c0 = c0 >> 4 #siirretään bittejä neljä paikkaa
- c0 = c0 & 0x07 #luodaan maski 0000 0111,
- c0_number = c0
- c0 = int(16.5*(2**(c0_number)-1)) #convert chord step to chord value
- uart.write(str(c0) + '\r\n')
- step0 = chan0[0] & 0x0f #luodaan maski 0000 1111
- c1 = chan1[0]
- c1 = c1 >> 4 #siirretään bittejä neljä paikkaa
- c1 = c1 & 0x07 #luodaan maski 0000 0111
- c1_number = c1
- c1 = int(16.5*(2**(c1_number)-1)) #convert chord step to chord value
- uart.write(str(c1) + '\r\n')
- step1 = chan1[0] & 0x0f #luodaan maski 0000 1111
- print("Kanava C0:", c0, "Step:", step0)
- print("Kanava C1:", c1, "Step:", step1)
- c0_count = c0 + (2**c0_number) * step0 #count ADC counts
- c1_count = c1 + (2**c1_number) * step1
- print("Kanava 0 ADC:", c0_count)
- print("Kanava 1 ADC:", c1_count)
- # R = compensation for infrared datasheet pg. 1111"
- R = c1_count / c0_count
- lux = c0_count * 0.46 * math.exp(-3.13*R)
- print("Ch0 counts:", c0_count, "Ch1 counts:", c1_count)
- print("R-kompensaatio:", R, "\nLux-arvo:", lux)
- delay(1000)
- uart.write(str(lux)+'\r\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement