access unipi from raspberry - Raspberry Pi Forums


hi, beginner respect raspberry , unipi experienced c , python programmer on various systems.

have raspberry unipi , need measure voltage of 1 of analog inputs. says have take account coefficient stored in eeprom on unipi.

analog input specified address 0x68 , assume address 0x68 on i2c.

1. how read voltage value for, say, analog input 2?
2. how read eeprom coefficient stored related analog input 2?

example python:

import smbus
bus = smbus.smbus(1)
foo = bus.read_byte_data(0x68, ???) # put ??? read voltage of ai2?

eeprom_coeff = ???? # how read eeprom on unipi?

appreciate if fill in code question marks in code above or tell me how can go find code fill in question marks.

taking @ unipi docs @ http://unipi.technology/wp-content/uplo ... ual_en.pdf

i2c 0x68 adc, mcp3422

reverse-engineering arduino library mcp3422 @ https://github.com/stevemarple/mcp342x/tree/master/src, looks there may bit more simple read - looks have tell channel want data first , required resolution (12, 14, 16, or 18 bit) sending config byte , *then* reading result.

config byte made of number of bits different settings - see http://www.phanderson.com/picaxe/mcp342x_config.pdf

if send, example, 0x88, you'll sending

7 6 5 4 3 2 1 0
--------
1 0 0 0 1 0 0 0

bit 7 ready bit, 1 means "ready new conversion"
bits 6&5 represent channel, 00 channel 1 (01 channel 2)
bit 4 conversion mode bit (1 continuous, 0 one-shot)
bits 3&2 sample rate (00 gives 12-bit result 01 14, 10 16, 11 18)
bits 1&0 pga gain - 00 x1 (01 x2, 10 x4, 11 x8)

0x88 gives new one-shot conversion on channel 1, sample rate of 16 bits, pga gain x1.
want channel two, though, means need send 0xa8

you'll receive three-byte response - hi , lo bytes of result, followed config byte (unless selected 18-bit, in case 3 bytes of data before config byte)

simplicity's sake, i'd use 16-bit - bus.write_byte(0x68, 0xa8), 3 bus.read_byte(0x68) calls, take first 2 hi , low bytes word value, , optionally error-check config byte contains expecting.

reference, full mcp3422 datasheet can found @ http://ww1.microchip.com/downloads/en/d ... 22088c.pdf


raspberrypi



Comments