Differenze tra le versioni di "Input analogico I2C: MCP3424"
Jump to navigation
Jump to search
Riga 14: | Riga 14: | ||
I2C_SLAVE = 0x0703 | I2C_SLAVE = 0x0703 | ||
− | devname = "/dev/i2c-0" | + | #per la rev.A |
+ | #devname = "/dev/i2c-0" | ||
+ | #per la rev.B | ||
+ | devname = "/dev/i2c-1" | ||
dev=os.open("/dev/i2c-0", os.O_RDWR) | dev=os.open("/dev/i2c-0", os.O_RDWR) | ||
Riga 52: | Riga 55: | ||
#include <sys/ioctl.h> | #include <sys/ioctl.h> | ||
#include <fcntl.h> | #include <fcntl.h> | ||
+ | |||
+ | #ifdef REVA | ||
+ | #define I2C_DEVICE "/dev/i2c-0" | ||
+ | #else | ||
+ | #define I2C_DEVICE "/dev/i2c-1" | ||
+ | #endif | ||
//#define C18BITS | //#define C18BITS | ||
Riga 59: | Riga 68: | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
− | |||
int data[2]; | int data[2]; | ||
// Open up the I2C bus | // Open up the I2C bus | ||
− | file = open( | + | file = open(I2C_DEVICE, O_RDWR); |
if (file == -1) | if (file == -1) | ||
{ | { |
Versione delle 19:10, 3 mar 2013
Questa pagina fa riferimento a una scheda Input Analogico collegata come descritto in "Collegare dispositivi I2C"
Questa scheda si presenta con identificativo I2C 0x68 ma saldando opportuni punti del circuito l'indirizzo puo' essere modificato in 0x69, 0x6B, 0x6B, 0x6F.
Il programma python che segue legge i 4 ingressi della scheda prima in bassa risoluzione (12 bit) poi in alta risoluzione (18bit). Le misure sono fornite in milliVolt per la bassa risoluzione e in microvolt per quella alta.
#!/usr/bin/env python3 import sys,fcntl,os,time,struct analog_addr = 0x68 I2C_SLAVE = 0x0703 #per la rev.A #devname = "/dev/i2c-0" #per la rev.B devname = "/dev/i2c-1" dev=os.open("/dev/i2c-0", os.O_RDWR) fcntl.ioctl(dev, I2C_SLAVE, analog_addr) for i in range(4): cmd=bytearray([0x80 | 0x10 | (i<<5)]) os.write(dev, cmd) time.sleep(0.004167) ret=os.read(dev, 2) mvolt=struct.unpack('>h',ret)[0] print("{}mV".format(mvolt)) for i in range(4): cmd=bytearray([0x80 | 0x1c | (i<<5)]) os.write(dev, cmd) time.sleep(0.67) ret=os.read(dev, 4) value=struct.unpack('>l',ret)[0] uvolt=uvolt=(value >> 8) * 1000 / 64 print("{}uV".format(uvolt))
Questo programma in C legge 10 volte ogni input. Se si decommenta la costante C18BITS la conversione avviene a 18 bit:
#include <unistd.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdarg.h> #include <string.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> #include <fcntl.h> #ifdef REVA #define I2C_DEVICE "/dev/i2c-0" #else #define I2C_DEVICE "/dev/i2c-1" #endif //#define C18BITS #define ANALOG_ADDRESS 0x68 int file; int main(int argc, char *argv[]) { int data[2]; // Open up the I2C bus file = open(I2C_DEVICE, O_RDWR); if (file == -1) { perror(devName); exit(1); } // Specify the address of the slave device. if (ioctl(file, I2C_SLAVE, ANALOG_ADDRESS) < 0) { perror("Failed to acquire bus access and/or talk to slave"); exit(1); } unsigned char outbuf[1]; unsigned char inbuf[4]; int i,j; for (i=0; i<4; i++) { #ifdef C18BITS outbuf[0]=0x80 | 0x1C | (i<<5); #else /* 12 bits */ outbuf[0]=0x80 | 0x10 | (i<<5); #endif write(file,outbuf,1); for (j=0; j<10; j++) { #ifdef C18BITS usleep(270000); int n=read(file,inbuf,4); int value=(inbuf[0]<<24)+(inbuf[1]<<16)+(inbuf[2]<<8)+inbuf[3]; int uvolt=(value >> 8) * 1000 / 64; printf("%d -> %02x %02x %02x %02x %5duV\n", i, inbuf[0], inbuf[1], inbuf[2], inbuf[3], uvolt); #else usleep(4167); int n=read(file,inbuf,2); short value=(inbuf[0]<<8)+inbuf[1]; int uvolt=value; printf("%d -> %02x %02x %5dmV\n", i, inbuf[0], inbuf[1], uvolt); #endif } } }