projekty/Source/example1/src/maxim.c

Go to the documentation of this file.
00001 /*
00002  * $Id: maxim.c,v 1 2006/03/22 12:48:00 BorisK Exp $
00003  *
00004  * Copyright (C) 2004 ONTRACK s.r.o.
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    
00019  * 02111-1307, USA.
00020  *
00021  * [1] MAXIM 2.7V to 5.5V,Low-Power,4-/12-Channel 2-Wire Serial 8-Bit ADCs 19-244; Rev1;10/02
00022  *
00023  * Written by Boris Kralik <kralikbo@yahoo.com>, 2006
00024  *
00025  */
00026 #include "maxim.h"
00027 
00028 #define AD_HS_MOD       0x08    //0000 1xxx
00029 #define AD_RESET        0xFE    //RST byte to 0 = reset
00030 
00031 #define AD_SETUP        0x80    //REG SEL2 SEL1 SEL0 CLK BIP/#UNI #RST X
00032                                 //REG == 1 SetupByte, == 0 Configuration byte   (1)
00033                                 //SEL2, SEL1, SEL0                              (000)   Vdd=Vref,(111)Internal reference and connect Vref to A11
00034                                 //CLK                                           (0)     Internal Clock
00035                                 //BIP/#UNI                                      (0)     Unipolar
00036                                 //RST   0-reset         1-No action             (0)     Reset Configuration register
00037                                 //X - Don't care                                (0)
00038 #define AD_CONF         0x61    //REG SCAN1 SCAN0 CS3 CS2 CS1 CS0 SGL/DIF
00039                                 //REG - Configuration byte                      (0)
00040                                 //SCAN1, SCAN0                                  (11)    Select channel by CS3-CS0
00041                                 //CS3-CS0                                       (000)   Select channel to scan
00042                                 //SGL/DIR                                       (1)     Single ended input
00043 short DO_H(void)
00044 {
00045         char ret=0;
00046         I2C_SetSTO();
00047         if (SendSlaveAdress(MAX_7300AAI_W) != 0) 
00048                 {
00049                 #if 0
00050                         printf("\n\rError DO_H");
00051                 #else
00052                         ;
00053                 #endif
00054                         return -1;
00055                 }
00056         WriteOnI2C(0x0C);//Write Configuration byte 
00057         WriteOnI2C(0x55);//Write Port Configuration matrix as output,page. 7 Table 1,2[5]
00058         I2C_SetSTO();   
00059         SendSlaveAdress(MAX_7300AAI_W);
00060         WriteOnI2C(0x32);       //Port P0.18, Page 13 Table 5 in [5]
00061         WriteOnI2C(0xFF);       // as H
00062         WriteOnI2C(0xFF);       // as H
00063         SendSlaveAdress(MAX_7300AAI_W);         
00064         WriteOnI2C(0x31);       //Port P0.17, Page 13 Table 5 in [5]
00065         WriteOnI2C(0x00);       // as H
00066         I2C_SetSTO();
00067         return ret;
00068 }
00069 
00070 char DO_L(void)
00071 {
00072         char ret=0;
00073 
00074         I2C_SetSTO();
00075         if (SendSlaveAdress(MAX_7300AAI_W) != 0)
00076                 {
00077                 #if 0
00078                         printf("\n\rError DO_H");
00079                 #else
00080                         ;
00081                 #endif
00082                         return -1;
00083                 }
00084         WriteOnI2C(0x0C);//Write Configuration byte
00085         WriteOnI2C(0x55);//Write Port ConfiguratTIMER1_interruption matrix as output,page. 7 Table 1,2[5]
00086         I2C_SetSTO();
00087         SendSlaveAdress(MAX_7300AAI_W);
00088         WriteOnI2C(0x32);
00089         WriteOnI2C(0x00);       // as L
00090         I2C_SetSTO();
00091         SendSlaveAdress(MAX_7300AAI_W); 
00092         WriteOnI2C(0x31);       //Port P0.17, Page 13 Table 5 in [5]
00093         WriteOnI2C(0xFF);       // as L
00094         I2C_SetSTO();
00095         return ret;
00096 }
00097 
00098 short   DO_MAX_Init(short Val)
00099 {
00100         short   ret=0;
00101         #if 0
00102                 printf("\n\rAD_MAX_Read->SendSlaveAdress00, 0x%x",MAX_7300AAI_W);
00103         #endif
00104         disableIRQ();
00105         I2C_SetSTO();
00106         switch(Val)
00107         {
00108                 case 0:
00109                         #if 0
00110                                 ret=SendSlaveAdress(MAX_7300AAI_W);
00111                                 || (DO_MAX_Init(0) |=)if (ret != 0) {
00112                                         printf("\n\rMax,L init(0x%X) = 0x%X",MAX_7300AAI_W,ret);
00113                                         //return ret;
00114                                         }
00115                                 printf("\n\r0x%X = SetSlaveAddress(0x%X)",ret,MAX_7300AAI_W);
00116                         #else
00117                                 if (((ret = SendSlaveAdress(MAX_7300AAI_W)) != 0)||(I2C_I2STAT == 0x20)) {ret = 0xFF;return -1;}//Page 6
00118                                 WriteOnI2C(0x04);//Normal operation (Table 8)
00119                                 WriteOnI2C(0x01); 
00120                                 I2C_SetSTO();
00121                                 SendSlaveAdress(MAX_7300AAI_W);//Page 6
00122                                 WriteOnI2C(0x09);//Set not used port P4-7 as output 
00123                                 WriteOnI2C(0x55);
00124                                 I2C_SetSTO();   
00125                                 SendSlaveAdress(MAX_7300AAI_W);//Page 6
00126                                 WriteOnI2C(0x0A);//Set not used port P8-12 as output
00127                                 WriteOnI2C(0x55); 
00128                         #endif
00129                         break;
00130                 case 1:
00131                         #if 0
00132                                 ret=SendSlaveAdress(MAX_7300AAI_1_W);
00133                                 if (ret != 0) {
00134                                         printf("\n\rMax,L init = 0x%X",ret);
00135                                         return ret;
00136                                         }
00137                                 printf("\n\r0x%X = SetSlaveAddress(0x%X)",ret,MAX_7300AAI_1_W);
00138                         #else
00139                                 if (((ret = SendSlaveAdress(MAX_7300AAI_1_W)) != 0)||(I2C_I2STAT == 0x20)) {ret = 0xFF;return -1;}//Page 6
00140                                 WriteOnI2C(0x04);//Normal operation (Table 8)
00141                                 WriteOnI2C(0x01); 
00142                                 I2C_SetSTO();
00143                                 SendSlaveAdress(MAX_7300AAI_1_W);//Page 6
00144                                 WriteOnI2C(0x09);//Set not used port P4-7 as output 
00145                                 WriteOnI2C(0x55);
00146                                 I2C_SetSTO();   
00147                                 SendSlaveAdress(MAX_7300AAI_1_W);//Page 6
00148                                 WriteOnI2C(0x0A);//Set not used port P8-12 as output
00149                                 WriteOnI2C(0x55); 
00150                         #endif
00151                         break;
00152         }
00153         enableIRQ();
00154         I2C_SetSTO();
00155         return ret;
00156 }
00157 char    AD_MAX_Init(void)
00158 {
00159         char    ret=0;
00160         //I2C_I2DAT = 0;
00161         #if 0
00162                 printf("\n\rAD_MAX_Read->SendSlaveAdress00");
00163         #endif
00164         disableIRQ();
00165         #if 1
00166                 ret=SendSlaveAdress(MAX1039_W);
00167                 if (ret != 0) return -1;
00168                 printf("\n\r0x%X = SetSlaveAddress(0x%X)",ret,MAX1039_W);
00169                 printf("\n\r0x%X = WriteOnI2C(0x%X)",WriteOnI2C(AD_HS_MOD),AD_HS_MOD);
00170                 //printf("\n\r0x%X = WriteOnI2C(AD_RESET)",WriteOnI2C(AD_RESET));
00171                 printf("\n\r0x%X = WriteOnI2C(AD_SETUP)",WriteOnI2C(AD_SETUP));
00172                 printf("\n\r0x%X = WriteOnI2C(AD_CONF)",WriteOnI2C(AD_CONF));
00173         #else
00174                 ret=SendSlaveAdress(MAX1039_W);
00175                 if (ret != 0) return;
00176                 WriteOnI2C(AD_HS_MOD);
00177                 WriteOnI2C(AD_SETUP);
00178                 WriteOnI2C(AD_CONF);
00179         #endif
00180         enableIRQ();
00181         return ret;
00182 }
00183 #define INC     10
00184 uint32_t MStatistika(void)
00185 {
00186         int i=0;
00187         uint32_t sum=0;
00188         for(i=0;i<INC;i++)sum+=ReadOnI2C();
00189         return (sum/INC);
00190 }
00191 unsigned int AD_MAX_Read(char channel) //Channel nefunguje
00192 {
00193         unsigned int ret=0;
00194         ret = SendSlaveAdress(MAX1039_W);
00195         #if 0
00196         printf("\n\r0x%X = WriteOnI2C(AD_SETUP)",WriteOnI2C(AD_CONF));
00197         #else
00198         switch(channel)
00199         {
00200                 case 0: WriteOnI2C(AD_CONF);break;
00201                 case 1: WriteOnI2C((AD_CONF+4));
00202                         break;
00203                 default:break;
00204         }
00205         #endif
00206         ret = SendSlaveAdress(MAX1039_R);
00207         if (ret |= 0) printf("\n\r0x%X = SetSlaveAddress(0x%X)",ret,MAX1039_R);
00208         ret = MStatistika();
00209         #if 0
00210                 printf("\n\r\tAD_MAX_Read(0)=0x%X",(ret));
00211         #endif
00212         return ret;
00213 }

Generated on Fri Sep 21 13:41:54 2007 for example1 by  doxygen 1.4.7