Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. from pyb import I2C
  2. import math
  3. from pyb import UART
  4. from pyb import delay
  5. uart = UART(6, 115200)
  6. i2c = I2C(1, I2C.MASTER, baudrate = 100000)
  7. while True:
  8. #i2c.send(0x03, 0x39) # power-up state/read register 0x03
  9. i2c.send(0x43, 0x39) # read adc channel 0
  10. chan0 = i2c.recv(1, 0x39) # read 1 bytes from 0x39
  11. i2c.send(0x83, 0x39) # read adc channel 1
  12. chan1 = i2c.recv(1, 0x39) # read 1 bytes from 0x39
  13. # print("Kanava 0:", chan0,"\nKanava 1:", chan1)
  14.  
  15.  
  16.  
  17. #VALID CHORD BITS STEP BITS
  18. #B7 B6 B5 B4 B3 B2 B1 B0
  19. #VALID C2 C1 C0 S3 S2 S1 S0
  20.  
  21. c0 = chan0[0]
  22. c0 = c0 >> 4 #siirretään bittejä neljä paikkaa
  23. c0 = c0 & 0x07 #luodaan maski 0000 0111,
  24. c0_number = c0
  25. c0 = int(16.5*(2**(c0_number)-1)) #convert chord step to chord value
  26. uart.write(str(c0) + '\r\n')
  27. step0 = chan0[0] & 0x0f #luodaan maski 0000 1111
  28.  
  29.  
  30. c1 = chan1[0]
  31. c1 = c1 >> 4 #siirretään bittejä neljä paikkaa
  32. c1 = c1 & 0x07 #luodaan maski 0000 0111
  33. c1_number = c1
  34. c1 = int(16.5*(2**(c1_number)-1)) #convert chord step to chord value
  35. uart.write(str(c1) + '\r\n')
  36. step1 = chan1[0] & 0x0f #luodaan maski 0000 1111
  37.  
  38.  
  39. print("Kanava C0:", c0, "Step:", step0)
  40. print("Kanava C1:", c1, "Step:", step1)
  41. c0_count = c0 + (2**c0_number) * step0 #count ADC counts
  42. c1_count = c1 + (2**c1_number) * step1
  43. print("Kanava 0 ADC:", c0_count)
  44. print("Kanava 1 ADC:", c1_count)
  45.  
  46. # R = compensation for infrared datasheet pg. 1111"
  47. R = c1_count / c0_count
  48. lux = c0_count * 0.46 * math.exp(-3.13*R)
  49. print("Ch0 counts:", c0_count, "Ch1 counts:", c1_count)
  50. print("R-kompensaatio:", R, "\nLux-arvo:", lux)
  51. delay(1000)
  52.  
  53. uart.write(str(lux)+'\r\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement