frederic blanc / Mbed 2 deprecated OneWireDrv

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS2450.c Source File

DS2450.c

Go to the documentation of this file.
00001 /**
00002 * @file DS2450.c
00003 * @brief library of DS2450 1-Wire Quad A/D Converter  (http://www.maxim-ic.com/datasheet/index.mvp/id/2921)
00004 * @author Frederic BLANC
00005 */
00006 #include "mbed.h"
00007 #include "onewire.h"
00008 #include "DS2450.h"
00009 #include "crc8.h"
00010 #include "crc16.h"
00011 #include "utils.h"
00012 
00013 /**
00014  *     @brief lancement lecture DS2450 ADC
00015  *    @param  [in] id[] tableau d'identifiant OW
00016  *    @param  [out] adc[] tableau des valeurs des adc
00017  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00018  *     @date 20/06/2011
00019  *
00020  */
00021 uint8_t DS2450_read_ADC(uint8_t id[], uint16_t adc[]) {
00022     uint8_t i,j;
00023     uint8_t error;
00024     uint8_t sp[DS2450_SP_SIZE];
00025     //waiting for convertion time ( nbchannel x resolution x 80�s +160�s)
00026 
00027     error=DS2450_read_page(&id[0],DS2450_PAGE0,&sp[0]); //read data
00028     if (error)
00029         return error;
00030     j=0;
00031     for (i=0;i<8;i+=2)
00032         adc[j++]=uint8_to_uint16(sp[i+3],sp[i+4]); //sp[i+3] LSB ,sp[i+4] MSB
00033     return OW_OK;
00034 }
00035 /**
00036  *     @brief lancement & lecture DS2450 ADC
00037  *    @param  [in] id[] tableau d'identifiant OW
00038  *    @param  [out] adc[] tableau des valeurs des adc
00039  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00040  *     @date 20/06/2011
00041  *
00042  */
00043 uint8_t DS2450_start_and_read_ADC(uint8_t id[], uint16_t adc[]) {
00044     uint8_t i,j;
00045     uint8_t error;
00046     uint8_t sp[DS2450_SP_SIZE];
00047 
00048     error=DS2450_convert(&id[0],0x0F,0x00); //start convert
00049     if (error)
00050         return error;
00051 
00052     wait_ms(15);                           //waiting for convertion time ( nbchannel x resolution x 80&#65533;s +160&#65533;s)
00053 
00054     error=DS2450_read_page(&id[0],DS2450_PAGE0,&sp[0]); //read data
00055     if (error)
00056         return error;
00057 
00058     j=0;
00059     for (i=0;i<8;i+=2)
00060         adc[j++]=uint8_to_uint16(sp[i+3],sp[i+4]); //sp[i+3] LSB ,sp[i+4] MSB
00061     return OW_OK;
00062 }
00063 /**
00064  *     @brief lancement lecture page DS2450 ADC
00065  *    @param  [in] uint8_t id[] tableau d'identifiant OW
00066  *    @param  [in] uint8_t adresse de la page a lire
00067  *    @param  [out] uint8_t uint16_t sp tableau des valeurs de la page
00068  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00069  *     @date 20/06/2011
00070  *
00071  */
00072 uint8_t DS2450_read_page(uint8_t id[], uint8_t adresse,
00073                          uint8_t *sp) {
00074     uint8_t i;
00075     if (id[0] == DS2450_ID) {
00076         if (ow_reset())                 //reset OW
00077             return OW_SHORT_CIRCUIT;
00078         sp[0]=DS2450_READ_MEMORY;   // command
00079         sp[1]=adresse;              //adress page LSB
00080         sp[2]=0;                    //adress page MSB
00081         ow_command(sp[0], &id[0]);
00082         ow_byte_wr(sp[1]);
00083         ow_byte_wr(sp[2]);
00084 
00085         for ( i=3 ; i< DS2450_SP_SIZE; i++ ) { //read 8xdata + CRC16
00086             sp[i]=ow_byte_rd();
00087         }
00088 #ifdef DEBUG
00089         printf( "\n" );
00090         for ( i=0 ; i< DS2450_SP_SIZE; i++ )
00091             printf(":%2.2X",sp[i]);
00092         printf( "\n" );
00093 #endif
00094         if (ctrl_crc16( &sp[0], DS2450_SP_SIZE ) ) { //CRC16 (command+adress page LSB+adress page MSB+8xdata)
00095             wait_ms(100);                   //wait 100ms if error
00096             if ((sp[DS2450_SP_SIZE-1]==0xFF) && (sp[DS2450_SP_SIZE-2]==0xFF))
00097                 return OW_ERROR;    // bus error
00098             if ((sp[DS2450_SP_SIZE-1]==0x00) && (sp[DS2450_SP_SIZE-2]==0x00))
00099                 return OW_BUSY;
00100 #ifdef DEBUG_L1 
00101 printf( "\n" );
00102 for ( i=0 ; i< DS2450_SP_SIZE; i++ )
00103     printf(":%2.2X",sp[i]);
00104 printf( "\n" ); 
00105 #endif    
00106             return OW_ERROR_CRC;    // data error
00107         }
00108         return OW_OK;
00109     }
00110     return OW_ERROR_BAD_ID;
00111 }
00112 
00113 /**
00114  *     @brief lancement convertion DS2450 ADC
00115  *    @param  [in] uint8_t id[] tableau d'identifiant OW
00116  *    @param  [in] uint8_t input_select_mask
00117  *    @param  [in] uint8_t read_out_control
00118  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00119  *     @date 20/06/2011
00120  *
00121  */
00122 uint8_t DS2450_convert(uint8_t id[], uint8_t input_select_mask,uint8_t read_out_control) {
00123     uint8_t i;
00124     uint8_t sp[5];
00125     if (id[0] == DS2450_ID) {
00126         if (ow_reset())                 //reset OW
00127             return OW_SHORT_CIRCUIT;
00128         sp[0]=DS2450_CONVERT;       // command
00129         sp[1]=input_select_mask;    //mask
00130         sp[2]=read_out_control;     //control
00131         ow_command(sp[0], &id[0]);
00132         ow_byte_wr(sp[1]);
00133         ow_byte_wr(sp[2]);
00134         for ( i=3 ; i< 5; i++ ) {   // read CRC16
00135             sp[i]=ow_byte_rd();
00136         }
00137 #ifdef DEBUG
00138         printf( "\n" );
00139         for ( i=0 ; i< 5; i++ )
00140             printf(":%2.2X",sp[i]);
00141         printf( "\n" );
00142 #endif
00143         if (ctrl_crc16( &sp[0], 5 ) ) { //CRC16 (command+mask LSB+control)
00144             if ((sp[3]==0xFF) && (sp[3]==0xFF))
00145                 return OW_ERROR;
00146             return OW_ERROR_CRC;
00147         }
00148         return OW_OK;
00149     }
00150     return OW_ERROR_BAD_ID;
00151 }
00152 
00153 /**
00154  *     @brief configure canal ADC  DS2450
00155  *    @param  [in] id[] tableau d'identifiant OW
00156  *    @param  [in] channel
00157  *  @param  [in] conflsb configuration OE-A OC-A 0 0 RC3-A RC2-A RC1-A RC0-A
00158  *  @param  [in] confmsb configuration POR 0 AFH-A AFL-A AEH-A AEL-A 0 IR-A
00159  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00160  *     @date 20/06/2011
00161  *
00162  */
00163 uint8_t DS2450_configure_channel_ADC(uint8_t id[],uint8_t channel,uint8_t conflsb,uint8_t confmsb) {
00164     uint8_t i;
00165     uint8_t sp[7];
00166     if (id[0] == DS2450_ID) {
00167         if (ow_reset())                 //reset OW
00168             return OW_SHORT_CIRCUIT;
00169         sp[0]=DS2450_WRITE_MEMORY;  // command
00170         sp[1]=DS2450_PAGE1+channel; //adress page LSB
00171         sp[2]=0x00;                 //adress page MSB
00172         sp[3]=conflsb;              //databyte
00173         ow_command(sp[0], &id[0]);
00174         ow_byte_wr(sp[1]);
00175         ow_byte_wr(sp[2]);
00176         ow_byte_wr(sp[3]);
00177         for ( i=4 ; i< 7; i++ ) {   //read CRC16+databyte
00178             sp[i]=ow_byte_rd();
00179         }
00180 #ifdef DEBUG
00181         printf( "\n" );
00182         for ( i=0 ; i< 7; i++ )
00183             printf(":%2.2X",sp[i]);
00184         printf( "\n" );
00185 #endif
00186         if (ctrl_crc16( &sp[0], 6 ) ) //CRC16 (command+adress page LSB+adress page MSB+databyte)
00187             return OW_ERROR_CRC;
00188         sp[3]=confmsb;                //databyte
00189         ow_byte_wr(sp[3]);
00190         for ( i=4 ; i< 7; i++ ) {   //read CRC16+databyte
00191             sp[i]=ow_byte_rd();
00192         }
00193 #ifdef DEBUG
00194         printf( "\n" );
00195         for ( i=0 ; i< 7; i++ )
00196             printf(":%2.2X",sp[i]);
00197         printf( "\n" );
00198 #endif
00199         if (sp[3]!=sp[6] ) //control data
00200             return OW_ERROR_CRC;
00201         return OW_OK;
00202     }
00203     return OW_ERROR_BAD_ID;
00204 }
00205 /**
00206  *     @brief configure PAGE
00207  *    @param  [in] id[] tableau d'identifiant OW
00208   *    @param  [in] uint8_t adresse de la page a ecrire
00209  *  @param  [in] config_page tableau de 8 byte
00210  *    @return OW_OK si erreur retourne OW_ERROR_CRC
00211  *     @date 20/06/2011
00212  *
00213  */
00214 uint8_t DS2450_configure_page(uint8_t id[], uint8_t adresse,uint8_t configpage[]) {
00215     uint8_t i,j;
00216     uint8_t sp[7];
00217     if (id[0] == DS2450_ID) {
00218         if (ow_reset())                 //reset OW
00219             return OW_SHORT_CIRCUIT;
00220         sp[0]=DS2450_WRITE_MEMORY;      // command
00221         sp[1]=adresse;                  //adress page LSB
00222         sp[2]=0x00;                     //adress page MSB
00223         sp[3]=configpage[0];            //databyte
00224         ow_command(sp[0], &id[0]);
00225         ow_byte_wr(sp[1]);
00226         ow_byte_wr(sp[2]);
00227         ow_byte_wr(sp[3]);
00228         for ( i=4 ; i< 7; i++ ) {       //read CRC16+databyte
00229             sp[i]=ow_byte_rd();
00230         }
00231 #ifdef DEBUG
00232         printf( "\n" );
00233         for ( i=0 ; i< 7; i++ )
00234             printf(":%2.2X",sp[i]);
00235         printf( "\n" );
00236 #endif
00237         if (sp[3]!=sp[6] ) //control data
00238             return OW_ERROR_CRC;
00239 
00240         for ( j=1 ; j< 7; j++ ) {
00241             sp[3]=configpage[j];        //databyte
00242             ow_byte_wr(sp[3]);
00243             for ( i=4 ; i< 7; i++ ) {   //read CRC16+databyte
00244                 sp[i]=ow_byte_rd();
00245             }
00246             if (sp[3]!=sp[6] ) //control data
00247                 return OW_ERROR_CRC;
00248 
00249 #ifdef DEBUG
00250             printf( "\n" );
00251             for ( i=0 ; i< 7; i++ )
00252                 printf(":%2.2X",sp[i]);
00253             printf( "\n" );
00254 #endif
00255         }
00256         return OW_OK;
00257     }
00258     return OW_ERROR_BAD_ID;
00259 }