projekty/Source/example1/src/I2C.c

Go to the documentation of this file.
00001 /*
00002  * $Id: I2C.c,v 1 2004/11/23 23:32: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] ARM-based Microcontroller LPC2119/2129/2194/2292/9994, "Preliminary User Manual", February 03, 2004
00022  * [2] 8XC552_562OVERVIEW_2.pdf
00023  * Written by Boris Kralik <kralikbo@yahoo.com>, 2004
00024  *
00025  */
00026 #include "I2C.h"
00027 
00028 #include "lpc229x.h"
00029 #include "Ports.h"
00030 #include "types.h"
00031 #include "lpcVIC.h"
00032 
00033 /***********************************************************************************
00034         Name:           EINT1_interrupt
00035         Description:    EXT1 interrupt routines
00036         Parameters:     void
00037         Return:         void
00038 ************************************************************************************/
00039 void 
00040 EINT1_interrupt(void)
00041 {
00042         disableIRQ();
00043         #if DBG
00044                 UARTWriteStr("\n\rExt 1 Interrupt");
00045         #endif
00046         PCB_PINSEL1     =       0;
00047         GPIO1_IODIR     |=      (1 << 18);//Test LED    
00048         #if DBG
00049                 UARTWriteStr("\n\rI2C Off");
00050         #endif  
00051         I2C_off();
00052         SCB_EXTINT = 0x00000002;//Must be, clear interrupt
00053         VICVectAddr = 0;       // Acknowledge Interrupt
00054         enableIRQ();
00055 }
00056 
00057 /************************************************************************************
00058         Name:           I2C_SetSTA
00059         Description:    Send start bit
00060         Parameters:     void
00061         Return:         void
00062 ************************************************************************************/
00063 void 
00064 I2C_SetSTA(){
00065         I2C_I2CONSET    |=      (1 << 5);       // Set STA bit in I2CONSET register
00066         I2C_I2CONCLR    |=      I2C_FLAG_STO;
00067         WD_Reset();
00068 }
00069 /************************************************************************************
00070         Name:           I2C_Status
00071         Description:    Return status of I2C bus
00072         Parameters:     void
00073         Return:         boolean
00074 ************************************************************************************/
00075 boolean I2C_Status(void)
00076 {
00077         return Status_I2C;
00078 }
00079 /************************************************************************************
00080         Name:           I2C_SetSTO
00081         Description:    Send stop bit
00082         Parameters:     void
00083         Return:         void
00084 ************************************************************************************/
00085 void I2C_SetSTO(void)
00086 {
00087         I2C_I2CONCLR    |=      I2C_FLAG_STA;
00088         I2C_I2CONSET    |=      I2C_FLAG_STO;
00089         WD_Reset();
00090         #if 0
00091                 UARTWriteStr("\n\rI2C_SetSTO");
00092         #endif
00093 }
00094 
00095 /************************************************************************************
00096         Name:           I2C_off
00097         Description:    I2C switch off
00098         Parameters:     void
00099         Return:         void
00100 ************************************************************************************/
00101 #if I2C_IRQ 
00102 void
00103 I2C_off(void)
00104 {
00105         Status_I2C      =       False;
00106         #if I2C_DBG
00107                 UARTWriteStr("\n\rI2C_Off");
00108         #endif
00109         I2C_I2CONSET    |=      I2C_FLAG_AA | I2C_FLAG_STO;
00110         I2C_I2CONCLR    |=      (1 << 6);       //I2C off
00111         I2C_I2CONCLR    |=      I2C_FLAG_SI;
00112         PCB_PINSEL0     &=      0xAF;
00113         GPIO0_IODIR     |=      (1 << 2);GPIO0_IODIR    |=      (1 << 3);//Set SCL as GPIO and set to L
00114         GPIO0_IOCLR     |=      (1 << 2);GPIO0_IOCLR    |=      (1 << 3);//Set SDA as GPIO and set to L
00115         WD_Reset();
00116 }
00117 #else
00118 
00119 void 
00120 I2C_off(void){
00121         Status_I2C      =       False;
00122         #if 0
00123                 UARTWriteStr("\n\rI2C_Off");
00124         #endif
00125         I2C_I2CONCLR = SIC;
00126         I2C_I2CONSET = STO;
00127         while((I2C_I2CONSET&STO)) ;         // wait for Stopped bus I2C
00128         PCB_PINSEL0     &=      0xAF;
00129         GPIO0_IODIR     |=      (1 << 2);GPIO0_IODIR    |=      (1 << 3);//Set SCL as GPIO and set to L
00130         GPIO0_IOCLR     |=      (1 << 2);GPIO0_IOCLR    |=      (1 << 3);//Set SDA as GPIO and set to L 
00131         WD_Reset();
00132 }
00133 #endif
00134 /************************************************************************************
00135         Name:           I2C_Initialize
00136         Description:    I2C initialize
00137         Parameters:     void
00138         Return:         short
00139 ************************************************************************************/
00140 short 
00141 I2C_Initialize(void)
00142 {
00143         short ret=0;
00144         
00145         Status_I2C      =       False;
00146         I2C_I2CONSET    =       0;
00147         I2C_I2STAT      =       0;
00148         I2C_I2DAT       =       0;
00149         I2C_I2ADR       =       0;
00150         I2C_I2SCLH      =       0;
00151         I2C_I2SCLL      =       0;
00152         I2C_I2CONCLR    =       0xFFFFFFFF;     
00153         #if I2C_DBG
00154                 UARTWriteStr("\n\rI2C_Initialize");
00155         #endif
00156         #if I2C_IRQ
00157                 disableIRQ();
00158         #endif  
00159         I2C_I2CONCLR    =       0xFF;           //Clear all flags in I2CONSET register
00160         PCB_PINSEL0     &= (uint32_t)0xFFFFFF5F;//Set 7, 5 bit to L
00161         PCB_PINSEL0     |=      0x50;           //Set P0.2 and P0.3 as SCL and SDA
00162         #if I2C_I2C_IRQ
00163                 I2C_I2CONCLR    |=      I2C_FLAG_SI;    //Clear SI(3) flag in I2CONSET
00164         #else
00165                 ;//I2C_I2CONSET |=      I2C_FLAG_SI;    //Clear SI(3) flag in I2CONSET
00166         #endif
00167         I2C_I2SCLL      =       19;     
00168         I2C_I2SCLH      =       19;//50;        //BR=24 000kHz/100=240kBd
00169         I2C_I2CONSET    |=      0x40;           //Set Master Mode
00170         #if I2C_IRQ
00171                 enableIRQ();
00172         #endif
00173         Status_I2C      =       True;
00174         WD_Reset();
00175         return ret;
00176 }
00177 /************************************************************************************
00178         Name:           INT_Initialize
00179         Description:    Initialization Interrupt vectors
00180         Parameters:     void
00181         Return:         void
00182 ************************************************************************************/
00183 #if I2C_IRQ
00184         void 
00185         INT_Initialize(void)
00186         {
00187                 VICIntEnClear = 0xFFFFFFFF;           // clear all interrupts
00188                 VICIntSelect = 0x00000000;            // clear all FIQ selections
00189                 VICDefVectAddr = 0x00000000;        // point unvectored IRQs to reset()
00190                 
00191                 VICVectCntl0 = 0x00000029;
00192                 VICVectAddr0 = (unsigned)I2C_interrupt;
00193                 VICVectCntl0 = VIC_I2C ;//| VIC_ENABLE; 
00194         
00195                 VICVectAddr1 = (unsigned long)EINT1_interrupt;   // set interrupt vector in 1
00196                 VICVectCntl1 = VIC_ENABLE | VIC_EINT1; //VICVectCntl1 ENABLE | VIC_Channel_EXTINT1;
00197         
00198                 VICIntEnable = ((1 << VIC_EINT1) | (1 << VIC_I2C));
00199                 SCB_EXTMODE  |= (1 << 1);       // edge sensitive EXT_INT1
00200                 
00201                 PCB_PINSEL0     |=      (1 << 29);      //P0.14 as ext1 interrupt
00202                 printf("\n\rINT_Initialize");
00203                 WD_Reset();
00204         }
00205 #endif
00206 /************************************************************************************
00207         Name:           I2C_interrupt
00208         Description:    i2c interrupt routines
00209         Parameters:     void
00210         Return:         void
00211 ************************************************************************************/
00212 #if I2C_IRQ
00213         void 
00214         I2C_interrupt(void)
00215         {
00216                 switch(I2C_I2STAT)
00217                 {
00218                         case (0x08):    //START Condition has been transmitted
00219                                 I2C_I2CONCLR    =       0x20;   //Clear start bit
00220                                 I2C_I2DAT       =       I2CAddress;     //Send address and write bit
00221                                 #if I2C_DBG
00222                                         UARTWriteStr("\n\rI2C_Int.,I2STAT=0x08");
00223                                 #endif
00224                                 break;
00225                         case (0x18):                            //Slave adress + W, ACK
00226                                 I2C_I2DAT       =       I2CData;//Write data to tx register
00227                                 #if I2C_DBG
00228                                         UARTWriteStr("\n\rI2C_Interrupt,I2C_I2STAT=0x18");
00229                                 #endif
00230                                 break;
00231                         case (0x20):                    //Slave address +w, not ACK
00232                                 I2C_I2DAT = I2CAddress;//Resend address and write bit
00233                                 #if I2C_DBG
00234                                         UARTWriteStr("\n\rI2C_Interrupt,I2C_I2STAT=0x20");
00235                                 #endif
00236                                 break;
00237                         case (0x28):                            //Data sent, Ack
00238                                 I2C_I2CONSET    =       0x10;   //Stop condition
00239                                 #if I2C_DBG
00240                                         UARTWriteStr("\n\rI2C_Interrupt,I2C_I2STAT=0x28");
00241                                 #endif
00242                                 break;
00243                         default:
00244 /*                              I2C_I2CONCLR    =       0x20;   //Clear start bit
00245                                 I2C_I2DAT       =       I2CAddress;     //Send address and write bit
00246                                 I2C_I2CONCLR    =       0x8;    //Clear I2C interrupt flag
00247                                 #if I2C_DBG
00248                                         UARTWriteStr("\n\rI2C_Interrupt,default,I2C_I2STAT=0x");
00249                                         UARTWriteStr(itoa_16(I2C_I2STAT));
00250                                         UARTWriteStr(",I2C_I2CONSET=0x");
00251                                         UARTWriteStr(itoa_16(I2C_I2CONSET));
00252                                 #endif*/
00253                                 break;
00254                 }
00255 //              printf("\n\r\t1");
00256                 VICVectAddr     =       0;       // Acknowledge Interrupt (rough?)
00257                 I2C_I2CONCLR    =       0x8;    //Clear I2C interrupt flag
00258                 #if 0
00259                         printf("\n\rI2C_I2CONSET:0x%X  I2C_I2DAT:0x%X I2C_I2STAT:0x%X",I2C_I2CONSET,I2C_I2DAT,I2C_I2STAT);
00260                 #else
00261                         ;
00262                 #endif
00263                 WD_Reset();
00264         }
00265 #endif
00266 
00267 #if !I2C_IRQ
00268 /***********************************************************************************
00269         Name:           SendSlaveAdress
00270         Description:    Send addredd via I2C
00271         Parameters:     uint32_t
00272         Return:         short status
00273 ************************************************************************************/
00274         short 
00275         SendSlaveAdress(uint32_t Addr_S)        
00276         {
00277                 int i=80;
00278                 unsigned int r=0;
00279                 WD_Reset();
00280                 if (I2C_Status() == False){
00281                         #if 1
00282                                 printf("\n\rI2C_Is off !!!");
00283                         #endif
00284                         WD_Reset();
00285                         return -1;
00286                         }
00287                 I2C_I2CONCLR = 0xFF;// clear I2C - included if User forgot to "StopI2C()"
00288                                     // else this function would hang.
00289                 I2C_I2CONSET = 0x40;         // Active Master Mode on I2C bus
00290                 if((Addr_S & 0x01))          // test if it's reading
00291                 {
00292                         I2C_I2CONSET = STA | AA;  // set STA - allow master to acknowlege slave;
00293                         WD_Reset();
00294                         #if 0
00295                                 UARTWriteStr("\n\rSSA(0)");
00296                         #endif
00297                 }else
00298                         {
00299                         I2C_I2CONSET = STA; // set STA don't allow acknowledges;
00300                         WD_Reset();
00301                         #if 0
00302                                 UARTWriteStr("\n\rSSA(1)");
00303                         #endif
00304                         }
00305                 while(I2C_I2STAT!=0x08)// Wait for start to be completed 
00306                 {
00307                         if ((i--)==0) {WD_Reset();return -1;}
00308                         #if 0
00309                                 UARTWriteStr("\n\rSSA(2)");
00310                                 WD_Reset();
00311                         #else
00312                                 WD_Reset();
00313                         #endif
00314                 }
00315                 I2C_I2DAT    = Addr_S;           // Charge slave Address
00316                 I2C_I2CONCLR = SIC | STAC;       // Clear i2c interrupt bit to send the data
00317                 i=80;
00318                 while( ! ( I2C_I2CONSET & SI))  // wait till status available
00319                 {
00320                         if ((i--)==0) {WD_Reset();return -1;}
00321                         #if 0
00322                                 printf("\n\rSSA(3):0x%X",Addr_S);// wait till status available");
00323                         #else
00324                                 WD_Reset();
00325                         #endif
00326                 }
00327                 #if 0
00328                         printf("\n\rSSA(4)Addr_s:0x%X I2C_I2STAT=0x%X",Addr_InitS,I2C_I2STAT);//read Status.See standard error codes in [2]
00329 //                      printf("\n\rSSA(5)T0S1CON(I2CONSET)=0x%X",I2C_I2CONSET);
00330                 #endif
00331                 if ((I2C_I2CONSET ==0x48)&(I2C_I2STAT == 0x18))
00332                         #if 0
00333                                 printf("\n\rData will be transmitted, ACK will be received");
00334                                 WD_Reset();     
00335                         #else
00336                                 WD_Reset();
00337                         #endif
00338                 
00339                 if(!(Addr_S & 0x01)) {       // if we are doing a write
00340                 #if I2C_DBG
00341                         UARTWriteStr("\n\rSSA(6) if we are doing a write");
00342                 #endif  
00343                         if (r == 0x18) {// look for "SLA+W has been transmitted; ACK has been received"
00344                                         #if 1
00345                                                 printf("\n\rSSA(7) look for SLA+W has been transmitted AND ACK has been received Status==0x%X",r);
00346                                         #endif
00347                                         WD_Reset();
00348                                         return 0;
00349                                         }
00350                         else if ( r==0x20 )// check for "SLA+W has been transmitted; NOT ACK has been received"
00351                                         {
00352                                         #if 0
00353                                                 UARTWriteStr("\n\rno acknowlege(0)(probably no device there)");
00354                                         #endif
00355                                         WD_Reset();
00356                                         return -1;
00357                                         }
00358                                 }else{
00359                                         if (r != 0x40) {             // look for "SLA+R has been transmitted; ACK has been received"
00360                                                 if ( r==0x48 ){ // check for "SLA+R has been transmitted; NOT ACK has been received"
00361                                                 #if 0
00362                                                         printf("\n\rno acknowlege(1) - probably no device there\n\r\tr=0x%X.",r);
00363                                                 #endif
00364                                                 WD_Reset();
00365                                                 return -1;
00366                                                 }
00367 /*                                              #if 1
00368                                                         UARTWriteStr("\n\rother error(0)");
00369                                                 #endif
00370                                                 WD_Reset();
00371                                                 return -1;*/
00372                                         }
00373                                 }
00374         #if 0
00375                 UARTWriteStr("\n\rSSA(8) end\n\r");
00376         #endif
00377         WD_Reset();
00378         return 0;
00379         }
00380 #else
00381         short //IRQ
00382         SendSlaveAdress(uint32_t Addr_S)
00383         {
00384                 if (I2C_Status() == False){printf("I2C is off !!!");return -1;}
00385                 if(!(Addr_S&0x01)) //test if it's a master adress
00386                 {
00387                         //while(I2C_I2STAT!=0xF8);
00388                         //UARTWriteStr("\n\rif it's a master adress");
00389                         I2C_I2CONSET = STA;     //Send START bit
00390                         while(I2C_I2STAT!=0x08)         //Set and wait the start
00391                         #if I2C_DBG
00392                                 printf("\n\rIRQ\n\rSSA:Set and wait the start (STAT):0x%X",I2C_I2STAT);//0x08-start condition was transmitted
00393                         #else
00394                                 WD_Reset(); //wait the ACK
00395                         #endif
00396                         I2CAddress = I2C_I2DAT    = Addr_S; // Charge slave Address
00397                         I2C_I2CONCLR = SIC|STAC; //Clear i2c interrupt bit to send the data
00398                         while(I2C_I2STAT!=0x18 && !(I2C_I2CONSET & SI))//ACK was received
00399                         #if I2C_DBG
00400                         {
00401                                 UARTWriteStr("\n\rSSA:I2C_I2STAT=0x");
00402                                 UARTWriteStr(itoa_16(I2C_I2STAT));//0x20 SL+W has been transmitted, NOT ACK has been received
00403                                 UARTWriteStr("\n\rSSA:I2C_I2CONSET=0x");
00404                                 UARTWriteStr(itoa_16(I2C_I2CONSET));//0x48
00405                         }
00406                         #else
00407                                 WD_Reset();
00408                                 return -1;
00409                         #endif
00410                 }
00411                 else                    //it's a slave adress
00412                 {
00413                 #if 0
00414                         UARTWriteStr("\n\rSSA:if it's a slave adress");
00415                 #endif
00416                         I2C_I2CONSET |= I2C_FLAG_STA;
00417                         I2C_I2CONCLR |= I2C_FLAG_SIC;
00418                         while((I2C_I2STAT!=0x10) && !(I2C_I2CONSET&I2C_FLAG_SI))
00419                         #if 0
00420                         {
00421                                 UARTWriteStr("\n\rSSA::I2C_I2STAT=0x");
00422                                 UARTWriteStr(itoa_16(I2C_I2STAT));//0x18
00423                                 UARTWriteStr("\n\rSSA::I2C_I2CONSET=0x");
00424                                 UARTWriteStr(itoa_16(I2C_I2CONSET));//0x48
00425                         }
00426                         #else
00427                                 WD_Reset();
00428                         #endif
00429                         I2C_I2DAT    = Addr_S; // Charge slave Address
00430                         I2C_I2CONCLR = I2C_FLAG_SIC|I2C_FLAG_STAC;//Clear i2c interrupt bit to send the data
00431                         while((I2C_I2STAT!=0x40)&&!(I2C_I2CONSET&I2C_FLAG_SI))
00432                         #if 0
00433                         {
00434                                 UARTWriteStr("\n\rSSA: - slave1 - wait the ACK");//wait the ACK
00435                                 UARTWriteStr("\n\rSTAT(1)=0x");
00436                                 UARTWriteStr(itoa_16(I2C_I2STAT));
00437                                 WD_Reset();
00438                         }
00439                         #else
00440                                 WD_Reset();
00441                         #endif
00442                 }
00443                 #if 1
00444                         printf("\n\rRet SetSlaveAddress");
00445                 #endif
00446         }
00447         WD_Reset();
00448         return 0;
00449 #endif
00450 #if I2C_IRQ
00451         void 
00452         WriteOnI2C(uint32_t Data)
00453         {
00454                 if (I2C_Status() == False){
00455                 #if I2C_DBG
00456 //                      UARTWriteString("I2C is off !!!");
00457                 #endif
00458                 return;}
00459                 #if 0
00460                         UARTWriteStr("\n\rWriteOnI2C (0)");
00461                 #endif
00462                 I2C_I2DAT    = Data; // Charge Data
00463                 I2C_I2CONCLR = 0x000000ff;//I2C_FLAG_SIC; // Clear i2c interrupt bit to send the data
00464                 I2C_I2CONSET = 0x00000040;
00465                 I2C_I2CONSET = 0x00000020;
00466                 while(I2C_I2STAT!=0x28&&!(I2C_I2CONSET&I2C_FLAG_SI))
00467                 #if 0
00468                         {
00469                         UARTWriteStr("\n\rWait the ACK, WriteOnI2C I2C_I2STAT=0x");
00470                         UARTWriteStr(itoa_16(I2C_I2STAT));
00471                         UARTWriteStr("\n\rI2C_I2CONSET=0x");
00472                         UARTWriteStr(itoa_16(I2C_I2CONSET));
00473                         }
00474                 #else
00475                         ;    //wait the ACK
00476                 #endif
00477                 #if 0
00478                         UARTWriteStr("\n\rWriteOnI2C (1)");
00479                 #endif
00480                 WD_Reset();
00481         }
00482 #else
00483         unsigned char 
00484         WriteOnI2C(unsigned char Data) {
00485                 unsigned char r;
00486                 unsigned int ret=0;
00487                 unsigned int i=80;
00488                                         if (I2C_Status() == False)
00489                                         {
00490                                         #if 1
00491                                                 UARTWriteStr("I2C is off !!!");
00492                                         #endif
00493                                         WD_Reset();
00494                                         return 0xFF;
00495                                         }
00496         I2C_I2DAT    = Data;                // Charge Data
00497         I2C_I2CONCLR = 0x8;                 // SIC; Clear i2c interrupt bit to send the data
00498         while( ! (I2C_I2CONSET & 0x8)){// wait till status available
00499                 #if 0
00500                         printf("\n\r WriteOnI2C(0):wait till status available I2C_I2CONSET=0x%X",I2C_I2CONSET);
00501                 #else
00502                         WD_Reset();
00503                         if ((i--) == 0) {return -1;}
00504                 #endif
00505         }
00506         #if 0
00507                 UARTWriteStr("\n\rWriteOnI2C (0)");
00508         #endif
00509         if (I2C_I2STAT != 0x28){// look for "Data byte in S1DAT has been transmitted; ACK has been received"
00510                         #if 1
00511                                 printf("\n\rwrite fail:I2C_I2STAT=0x%X",I2C_I2STAT);WD_Reset(); return I2C_I2STAT;
00512                         #else
00513                                 WD_Reset(); return I2C_I2STAT;
00514                         #endif
00515                         }
00516         #if 0
00517                 printf("\n\rWriteOnI2C (1) I2C_I2STAT==0x28 and I2C_I2CONSET=0x$%X",I2C_I2CONSET);
00518         #endif
00519         WD_Reset();
00520         return 0;
00521         }
00522 #endif
00523 #if I2C_IRQ
00524         uint32_t ReadOnI2C(void)
00525         {
00526                 if (I2C_Status() == False)
00527                 {
00528                 #if I2C_DBG
00529                         UARTWriteStr("I2C is off !!!");
00530                 #endif
00531                         WD_Reset();
00532                         return 0xFF;
00533                 }
00534                 I2C_I2CONCLR = I2C_FLAG_SIC;//ACK from the master
00535                 while(I2C_I2STAT!=0x50&&!(I2C_I2CONSET&I2C_FLAG_SI))
00536                 {
00537                 #if 0
00538                         UARTWriteStr("\n\rwait the data");      
00539                 #else
00540                         ; //wait the data
00541                 #endif
00542                 }
00543         // Read the data...
00544                 #if I2C_DBG
00545                         UARTWriteStr("\n\rI2C_I2DAT=0x");UARTWriteStr(itoa_16(I2C_I2DAT));
00546                 #endif
00547                 WD_Reset();
00548                 return I2C_I2DAT;
00549         }
00550 #else
00551         unsigned char ReadOnI2C(void) {
00552         if (I2C_Status() == False){UARTWriteStr("I2C is off !!!");return 0;}
00553         #if 0
00554                 UARTWriteStr("\n\rReadOnI2C");  
00555         #endif  
00556         unsigned char r;
00557         I2C_I2CONCLR = SIC;                  // clear SIC;
00558         while( ! (I2C_I2CONSET & 0x8))      // wait till status available
00559         #if 0
00560                 UARTWriteStr("\n\rwait the data");      
00561         #else
00562                 ; //wait the data
00563         #endif
00564         r=I2C_I2STAT;                        // check for error
00565         if (r != 0x50){                  // look for "Data byte has been received; ACK has been returned"
00566                 printf("\n\rread fail 0x%X",r);
00567         }
00568         #if 0
00569                 UARTWriteStr("\n\rTemperature:0x");UARTWriteStr(itoa_16(I2C_I2DAT));
00570         #endif
00571         WD_Reset();
00572         return I2C_I2DAT;
00573         }
00574 #endif
00575 void I2CTranserByte(unsigned Addr,unsigned Data)
00576 {
00577         if (I2C_Status() == False){
00578                 #if I2C_DBG
00579                         UARTWriteStr("I2C is off !!!");
00580                 #endif
00581                 WD_Reset();
00582                 return;
00583         }
00584         #if I2C_DBG
00585                 UARTWriteStr("\n\rI2CTranserByte(Addr:0x");
00586                 UARTWriteStr(itoa_16(Addr));
00587                 UARTWriteStr(")\n\rI2CTranserByte(Data:0x");
00588                 UARTWriteStr(itoa_16(Data));
00589                 UARTWriteStr(")");
00590         #endif  
00591         I2CAddress      =       Addr;
00592         I2CData         =       Data;
00593         I2C_I2CONCLR    =       0x000000FF;//Clear all I2C Settings
00594         I2C_I2CONSET    =       0x00000040;//Enable I2C interface
00595         I2C_I2CONSET    =       0x00000020;//Start condition
00596         WD_Reset();
00597 }

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