Ds1621.c
De MicElectroLinGenMet.
Origine page: Circuit_DS1621
/* Test DS1621 sur interface I2C de type philips (port //) (6/2004) */ /* Affichage de la t° tout les 10s */ /* Par domos domos78<at>free<point>fr */ /* 8/4/2007 */ /* Test avec accés device i2c sans fonction smbus (i2c.h). */ /* Exemple inspiré de Linux Magazine HS23 (Electronique). */ /* gcc -Wall -o ds1621 -O3 ds1621_3.c */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> #define I2C_SLAVE 0x0703 /* Change slave address */ static int i2c_fd ; int res ; #define DEVICE "/dev/i2cpar" // Lien vers /dev/i2c-x #define ds1621_addr 0x9E >> 1 // Adr. i2c DS1621 (A0, A1, A2 à Vcc) /*------------------------------------------------------------------------------*/ /* Fonctions i2c */ /*------------------------------------------------------------------------------*/ void i2c_init() { i2c_fd = open( DEVICE, O_RDWR ); if ( i2c_fd < 0 ) { printf("Erreur, impossible d'ouvrir %s", DEVICE ); exit(1) ; } } //----------------------------------------------------------------------------- void SelectSlave(unsigned char slaveaddr) { int res ; res = ioctl(i2c_fd, I2C_SLAVE, slaveaddr) ; //printf("Code retour 'SelectSlave': %d\n", res) ; if ( res < 0 ) { printf("Erreur, impossible de sélectionner le composant i2c à l'adresse 0x%x: '%s'", slaveaddr, strerror(errno)); close ( i2c_fd ); exit(1) ; } } /*------------------------------------------------------------------------------*/ /* Fonctions DS1621 */ /*------------------------------------------------------------------------------*/ int i2c_init_ds1621(unsigned char addr) { int res ; char buff[10] ; SelectSlave(addr) ; buff[0] = 0xAC ; buff[1] = 0x01 ; res = write( i2c_fd, buff, 2 ); if ( res < 0 ) { printf("Erreur init DS1621 addr 0x%x: '%s', Abandon programme !", addr, strerror(errno)); close ( i2c_fd ); exit(1) ; } /* start temperature conversion */ errno = 0 ; buff[0] = 0xEE ; write( i2c_fd, buff, 1 ); sleep(1) ; return 1 ; } //----------------------------------------------------------------------------- double i2c_gettemp_ds1621(unsigned char addr) { int k, count, slope, temp; char buff[10] ; int res ; SelectSlave(addr) ; /* stop conversion */ errno = 0 ; buff[0] = 0x22 ; res = write( i2c_fd, buff, 1 ); if ( res < 0 ) { printf("Erreur écriture DS1621 addr 0x%x: '%s'", ds1621_addr, strerror(errno)); return 255 ; // 255 car retourne normalement la t°. } else { /* Temperature reading (1 Celsius degree precision) */ buff[0] = 0xAA ; write( i2c_fd, buff, 1 ); read(i2c_fd, buff, 1) ; temp = buff[0] ; /* Counter reading (fraction of degree) ) */ buff[0] = 0xA8 ; write( i2c_fd, buff, 1 ); read(i2c_fd, buff, 1) ; count = buff[0] ; /* slope reading */ buff[0] = 0xA9 ; write( i2c_fd, buff, 1 ); read(i2c_fd, buff, 1) ; slope = buff[0] ; k = temp; if (slope != 0) { k = temp*100-25+(100*(slope-count))/slope; } //printf("Temp DS1621_1: %2.1f°\n", (float)k/100) ; /* start temperature conversion */ buff[0] = 0xEE ; write( i2c_fd, buff, 1 ); return (float)k/100 ; } } /*------------------------------------------------------------------------------*/ int main ( int argc, char ** argv ) { double temp_1 ; // Init i2c. i2c_init() ; // Init ds1621. i2c_init_ds1621(ds1621_addr) ; while (1) { temp_1 = i2c_gettemp_ds1621(ds1621_addr) ; // Lit t° ds1621. printf("Temp: %2.1f°\n", temp_1) ; sleep(10) ; } } /*------------------------------------------------------------------------------*/
