adctest.py 693 B

12345678910111213141516171819202122232425
  1. from smbus2 import SMBus
  2. import time
  3. # 定数
  4. ADRS2040_ADDR=0x41
  5. ADRS2040_CMD_INVALID = 0
  6. ADRS2040_CMD_ADC_START = 1
  7. ADRS2040_CMD_ADC_STOP = 2
  8. ADRS2040_CMD_SET_RATE = 3
  9. ADRS2040_CMD_GET_COUNT = 4
  10. ADRS2040_CMD_GET_VALUE = 5
  11. with SMBus(1) as i2c:
  12. # 1000Hzにセット
  13. i2c.write_word_data(ADRS2040_ADDR, ADRS2040_CMD_SET_RATE, 100)
  14. i2c.write_byte(ADRS2040_ADDR, ADRS2040_CMD_ADC_START)
  15. while True:
  16. counter = 0
  17. counter = i2c.read_word_data(ADRS2040_ADDR,ADRS2040_CMD_GET_COUNT )
  18. print(counter)
  19. for i in range(counter):
  20. value = i2c.read_word_data(ADRS2040_ADDR,ADRS2040_CMD_GET_VALUE)
  21. print( value )