unsigned char rtc_get_time (unsigned char address) { unsigned char time; StartI2C(); WriteI2C(0xD0); WriteI2C(address); RestartI2C(); WriteI2C(0xD1); time = ReadI2C(); return (((time & 0xF0) >> 4) * 10) + (time & 0x0F); // BCD to decimal }
/********************************************************************* * Function: XEE_RESULT XEEIsBusy(unsigned char control) * * PreCondition: XEEInit() is already called. * * Input: control - EEPROM control and address code. * * Output: XEE_READY if EEPROM is not busy * XEE_BUSY if EEPROM is busy * other value if failed. * * Side Effects: None * * Overview: Requests ack from EEPROM. * * Note: None ********************************************************************/ XEE_RESULT XEEIsBusy(unsigned char control) { XEE_RESULT r; IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( EEPROM_SPICON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCLIF ) // test for bus collision { return XEE_BUS_COLLISION; // return with Bus Collision error } else { if ( WriteI2C( control ) ) // write byte - R/W bit should be 0 return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); // ensure module is idle if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error if ( !EEPROM_SPICON2bits.ACKSTAT ) r = XEE_READY; else r = XEE_BUSY; #if 0 while ( EEPROM_SPICON2bits.ACKSTAT ) // test for ACK condition received { RestartI2C(); // initiate Restart condition while ( EEPROM_SPICON2bits.RSEN ); // wait until re-start condition is over if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error if ( WriteI2C( control ) ) // write byte - R/W bit should be 0 return XEE_BUS_COLLISION; // set error for write collision IdleI2C(); // ensure module is idle } #endif } StopI2C(); // send STOP condition while ( EEPROM_SPICON2bits.PEN ); // wait until stop condition is over if ( PIR2bits.BCLIF ) // test for bus collision return XEE_BUS_COLLISION; // return with Bus Collision error return r; //return XEE_READY; // return with no error }
unsigned char ITG3200_read(unsigned char reg) { unsigned char buf; StartI2C(); WriteI2C(ITG3200_ADDR_WRITE); WriteI2C(reg); RestartI2C(); WriteI2C(ITG3200_ADDR_READ); buf = ReadI2C(); NotAckI2C(); StopI2C(); return buf; }
char readEEPROMexterna (char endereco) { //while(!DataRdyI2C()); IdleI2C(); StartI2C(); IdleI2C(); WriteI2C(0xA0); IdleI2C(); WriteI2C(0x00); IdleI2C(); //HB WriteI2C(endereco); IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0xA1); IdleI2C(); value = ReadI2C(); IdleI2C(); //Dados NotAckI2C(); IdleI2C(); StopI2C();IdleI2C(); return value; }
/********************************************************************* * Function: LDSequentialReadI2C() * Input: ControlByte, address, *rdptr, length. * Overview: Performs a sequential read of length bytes starting at address * and places data in array pointed to by *rdptr ********************************************************************/ unsigned int LDSequentialReadI2C(unsigned char ControlByte, unsigned char address, unsigned char *rdptr, unsigned char length) { IdleI2C(); //Ensure Module is Idle StartI2C(); //Initiate start condition WriteI2C(ControlByte); //write 1 byte IdleI2C(); //Ensure module is Idle WriteI2C(address); //Write word address IdleI2C(); //Ensure module is idle RestartI2C(); //Generate I2C Restart Condition WriteI2C(ControlByte | 0x01); //Write 1 byte - R/W bit should be 1 for read IdleI2C(); //Ensure bus is idle getsI2C(rdptr, length); //Read in multiple bytes NotAckI2C(); //Send Not Ack StopI2C(); //Send stop condition return(0); }
/********************************************************************* * Function: EEAckPolling() * Input: Control byte. * Output: Error state. * Overview: Polls the bus for an Acknowledge from device ********************************************************************/ unsigned int EEAckPolling(unsigned char control) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start condition if(I2C1STATbits.BCL) { return(-1); //Bus collision, return } else { if(WriteI2C(control)) { return(-3); //error return } IdleI2C(); //wait for bus idle if(I2C1STATbits.BCL) { return(-1); //error return } while(ACKStatus()) { RestartI2C(); //generate restart if(I2C1STATbits.BCL) { return(-1); //error return } if(WriteI2C(control)) { return(-3); } IdleI2C(); } } StopI2C(); //send stop condition if(I2C1STATbits.BCL) { return(-1); } return(0); }
/********************************************************************* * Function: LDByteReadI2C() * Input: Control Byte, Address, *Data, Length. * Overview: Performs a low density read of Length bytes and stores in *Data array * starting at Address. ********************************************************************/ unsigned int LDByteReadI2C(unsigned char ControlByte, unsigned char Address, unsigned char *Data, unsigned char Length) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C(ControlByte); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C(ControlByte | 0x01); //Write control byte for read IdleI2C(); //wait for bus Idle getsI2C(Data, Length); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop }
/* * Gyro i2c interface, Read/Write */ UINT8 Gyro_I2C_R(UINT8 Address) { UINT8 Data; IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C(Gyro_MPU6050_Address); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C(Gyro_MPU6050_Address | 0x01); //Write control byte for read IdleI2C(); //wait for bus Idle Data = getI2C(); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop return Data; }
/********************************************************************* * Function: HDByteReadI2C() * * Input: Control Byte, HighAdd, LowAdd, *Data, Length. * * Output: None. * * Overview: Performs a low density read of Length bytes and stores in *Data array * starting at Address formed from HighAdd and LowAdd. * * Note: None ********************************************************************/ unsigned int HDByteReadI2C(unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char *Data, unsigned char Length) { IdleI2C(); //Wait for bus Idle StartI2C(); //Generate Start condition WriteI2C(ControlByte); //send control byte for write IdleI2C(); //Wait for bus Idle WriteI2C(HighAdd); //Send High Address IdleI2C(); //Wait for bus Idle WriteI2C(LowAdd); //Send Low Address IdleI2C(); //Wait for bus Idle RestartI2C(); //Generate Restart WriteI2C(ControlByte | 0x01); //send control byte for Read IdleI2C(); //Wait for bus Idle getsI2C(Data, Length); //Read Length number of bytes to Data NotAckI2C(); //send Not Ack StopI2C(); //Send Stop Condition return(0); }
int i2ReadData(char SlaveAddress, unsigned char Address, int Bytes, unsigned char *Dades) { // Pre: Bytes >= 0; Dades t‚ espai per a NumBytes // Pre: El bit m‚s baix de Address no t‚ informaci¢ (ser… read o write) // Post: retorna 0 si tot OK o 1 si hi ha un timeout d'un msegon // en un ack IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start Condition WriteI2C((SlaveAddress<<1)); //Write Control Byte IdleI2C(); //wait for bus Idle WriteI2C(Address); //Write start address IdleI2C(); //wait for bus Idle RestartI2C(); //Generate restart condition WriteI2C((SlaveAddress<<1 | 0x01)); //Write control byte for read IdleI2C(); //wait for bus Idle getsI2C(Dades, Bytes); //read Length number of bytes NotAckI2C(); //Send Not Ack StopI2C(); //Generate Stop return 0; }
void ADXL345_multiByteRead(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); for (i = 0; i < size; i++) { buffer[i] = ReadI2C(); //keep the clock pulsing // if not last byte, send ack // if last byte, send nack if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); // the &0x3F restricts reading from only the XYZ data registers unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send address to start reading from. for (i = 0; i < size; i++) { buffer[i] = ReadSPI(); //keep the clock pulsing } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
unsigned char HDByteReadI2C( unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char *data, unsigned char length ) { IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( ControlByte ); // write 1 byte IdleI2C(); // ensure module is idle WriteI2C( HighAdd ); // WRITE word address to EEPROM IdleI2C(); // ensure module is idle while ( SSPCON2bits.RSEN ); // wait until re-start condition is over WriteI2C( LowAdd ); // WRITE word address to EEPROM IdleI2C(); // ensure module is idle RestartI2C(); // generate I2C bus restart condition while ( SSPCON2bits.RSEN ); // wait until re-start condition is over WriteI2C( ControlByte | 0x01 ); // WRITE 1 byte - R/W bit should be 1 for read IdleI2C(); // ensure module is idle getsI2C( data, length ); // read in multiple bytes NotAckI2C(); // send not ACK condition while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over StopI2C(); // send STOP condition while ( SSPCON2bits.PEN ); // wait until stop condition is over return ( 0 ); // return with no error }
unsigned char ADXL345_oneByteRead(unsigned char address) { unsigned char data = 0; #if I2C StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(address); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); data = ReadI2C(); NotAckI2C(); StopI2C(); return data; #elif SPI SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns address = address | ADXL345_SPI_READ; WriteSPI(address); // read bit, multibyte bit, A5-A0 address data = ReadSPI(); SPI_CS_PIN = 1; //CS pin high, ie disable chip Delay1TCY(); // delay at least 5 ns return data; #endif }
void getTemperaturaHumidade (void) { unsigned char TEMPL=0, TEMPH=0, HUMIDL=0, HUMIDH=0; unsigned char DUMMY=0, OP=0, BT=0; float humidade, temperatura; char msg[55]; LED_AMAR=1; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) StartI2C(); // ACORDAR DEVICE __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(135); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) // com clock de 4 mhz: // 10K (100) = 1000 ms // 1K (100) = 100 ms // 1K (10) = 10 ms // 1K (2) = 2 ms // Delay100TCYx(); __delay_us(25); RestartI2C(); // REQUISITAR PEDIDO DE BYTES __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(60); // manual do AM2315 recomenda minimo de 30us WriteI2C(0x03); // byte que simboliza a temperatura __delay_us(60); WriteI2C(0x00); // start byte para leitura __delay_us(60); WriteI2C(0x04); // quantidades de bytes a serem lidos; //AckI2C(); __delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) __delay_ms(10); // manual do AM2315 recomenda esperar no minimo 10ms RestartI2C(); WriteI2C(0xB9); // endereco Slave do AM2315 //AckI2C(); // retirado por nao necessitar (?) __delay_us(60); // manual do AM2315 recomenda minimo de 30us IdleI2C(); OP = ReadI2C(); // 1o byte AckI2C(); IdleI2C(); BT = ReadI2C(); // 2o byte AckI2C(); IdleI2C(); HUMIDL = ReadI2C(); // 3o byte AckI2C(); IdleI2C(); HUMIDH = ReadI2C(); // 4o byte AckI2C(); IdleI2C(); TEMPL = ReadI2C(); // 5o byte AckI2C(); IdleI2C(); TEMPH = ReadI2C(); // 6o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 7o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 8 byte //__delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM=0; LED_AMAR=0; LED_VERD=1; // Calculos obtidos do exemplo do Arduino humidade = HUMIDL; humidade *= 256; humidade += HUMIDH; humidade /= 10; temperatura = TEMPL; temperatura *= 256; temperatura += TEMPH; temperatura /= 10; /* ou ainda RH = RHH << 8; RH |= RHL; TEMP = TEMPH << 8; TEMP |= TEMPL; */ sprintf (msg, "Temp= %0.2f, Humid= %0.2f .", temperatura, humidade); while(BusyUSART()); putsUSART(msg); while(BusyUSART()); putrsUSART("\n\r"); LED_VERD=0; }
int LCDClearData(BYTE add1) { #ifdef _WINDOWS UNUSED(add1); #else BYTE i; BYTE j; BYTE data; BYTE status; for(j=0;j<7;j++){ LCDSetXY(add1,0,j); RestartI2C(); IdleI2C(); //****write the address of the device for communication*** data = SSPBUF; //read any previous stored content in buffer to clear buffer full status do { status = WriteI2C( add1 | 0x00 ); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //R/W BIT IS '0' FOR FURTHER WRITE TO SLAVE do { status = WriteI2C(modeLCD_DATA_TILL_STOP); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //***WRITE THE THE DATA TO BE SENT FOR SLAVE*** //write string of data to be transmitted to slave for (i=0;i<128;i++ ) // transmit data { if ( SSP1CON1bits.SSPM3 ) // if Master transmitter then execute the following { //temp = putcI2C1 ( *wrptr ); //if (temp ) return ( temp ); if ( WriteI2C1( 0 ) ) // write 1 byte { return ( -3 ); // return with write collision error } IdleI2C1(); // test for idle condition if ( SSP1CON2bits.ACKSTAT ) // test received ack bit state { return ( -2 ); // bus device responded with NOT ACK } // terminate putsI2C1() function } } //---TERMINATE COMMUNICATION FROM MASTER SIDE--- IdleI2C(); } #endif return 0; }
void getPressao(void) { #define BMP085_ADDRESS 0xEE // I2C address of BMP085 #define P_CORRECTION 1.5 // in mBars - factor to adjust for elevation to match local weather station pressure // this value for 14' above sea level (in Boca Raton, Florida) //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) LED_AMAR=1; IdleI2C(); StartI2C(); //IdleI2C(); __delay_us(16); WriteI2C( 0xD0 ); //IdleI2C(); __delay_us(60); WriteI2C( 0x00 ); IdleI2C(); __delay_us(16); //AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) //IdleI2C(); __delay_us(26); RestartI2C(); __delay_us(16); WriteI2C( 0xD1 ); __delay_us(1); IdleI2C(); /* segundo =ReadI2C(); AckI2C(); IdleI2C(); minuto =ReadI2C(); AckI2C(); IdleI2C(); hora =ReadI2C(); AckI2C(); IdleI2C(); diasemana=ReadI2C(); AckI2C(); IdleI2C(); dia =ReadI2C(); AckI2C(); IdleI2C(); mes =ReadI2C(); AckI2C(); IdleI2C(); ano =ReadI2C(); AckI2C(); IdleI2C(); dummy =ReadI2C(); //AckI2C(); //__delay_us(16); //IdleI2C(); //NotAckI2C(); //IdleI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM = 0; LED_AMAR=0; LED_VERD=1; sprintf(msg,"%xh:%xm:%xs _ dia %x/%x/%x _ ", hora,minuto,segundo,dia,mes,ano); */ while(BusyUSART()); // putsUSART( msg ); LED_VERD=0; }
void getDS1307(void) { int hora=0, minuto=0, segundo=0, diasemana=0, dia=0, mes=0, ano=0, dummy=0; char msg[40]; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) LED_AMAR=1; IdleI2C(); StartI2C(); //IdleI2C(); __delay_us(16); WriteI2C( 0xD0 ); //IdleI2C(); __delay_us(60); WriteI2C( 0x00 ); IdleI2C(); __delay_us(16); //AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) //IdleI2C(); __delay_us(26); RestartI2C(); __delay_us(16); WriteI2C( 0xD1 ); __delay_us(1); IdleI2C(); segundo =ReadI2C(); AckI2C(); IdleI2C(); minuto =ReadI2C(); AckI2C(); IdleI2C(); hora =ReadI2C(); AckI2C(); IdleI2C(); diasemana=ReadI2C(); AckI2C(); IdleI2C(); dia =ReadI2C(); AckI2C(); IdleI2C(); mes =ReadI2C(); AckI2C(); IdleI2C(); ano =ReadI2C(); AckI2C(); IdleI2C(); dummy =ReadI2C(); //AckI2C(); //__delay_us(16); //IdleI2C(); //NotAckI2C(); //IdleI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM = 0; LED_AMAR=0; LED_VERD=1; sprintf(msg,"%xh:%xm:%xs _ dia %x/%x/%x _ ", hora,minuto,segundo,dia,mes,ano); while(BusyUSART()); putsUSART( msg ); LED_VERD=0; }
int LCDSendData(BYTE add1,BYTE* wrptr, BYTE size) { #ifndef _WINDOWS BYTE data; BYTE status; BYTE i; BYTE_VAL d; BYTE_VAL d1; RestartI2C(); IdleI2C(); //****write the address of the device for communication*** data = SSPBUF; //read any previous stored content in buffer to clear buffer full status do { status = WriteI2C( add1 | 0x00 ); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //R/W BIT IS '0' FOR FURTHER WRITE TO SLAVE do { status = WriteI2C(modeLCD_DATA_TILL_STOP); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //***WRITE THE THE DATA TO BE SENT FOR SLAVE*** //write string of data to be transmitted to slave for (i=0;i<size;i++ ) // transmit data { if ( SSP1CON1bits.SSPM3 ) // if Master transmitter then execute the following { //temp = putcI2C1 ( *wrptr ); //if (temp ) return ( temp ); d.Val = *wrptr; // swapping bits d1.bits.b0 = d.bits.b7; d1.bits.b1 = d.bits.b6; d1.bits.b2 = d.bits.b5; d1.bits.b3 = d.bits.b4; d1.bits.b4 = d.bits.b3; d1.bits.b5 = d.bits.b2; d1.bits.b6 = d.bits.b1; d1.bits.b7 = d.bits.b0; // if ( WriteI2C1( d1.Val ) ) // write 1 byte { return ( -3 ); // return with write collision error } IdleI2C1(); // test for idle condition if ( SSP1CON2bits.ACKSTAT ) // test received ack bit state { return ( -2 ); // bus device responded with NOT ACK } // terminate putsI2C1() function } wrptr ++; // increment pointer } //---TERMINATE COMMUNICATION FROM MASTER SIDE--- IdleI2C(); #else UNUSED(add1); UNUSED(wrptr); UNUSED(size); #endif return 0; }
byte I2CReadByte ( byte Device, byte Address ) { byte Data = 0; // Ensure I2C module is idle IdleI2C(); // Transmit a Start condition StartI2C(); // Wait till Start sequence is completed while(I2CCONbits.SEN ); // Write Slave address and set master for transmission (R/W bit should be 0) MasterWriteI2C(Device); // Wait till address is transmitted while(I2CSTATbits.TBF); // Test for ACK condition received while(I2CSTATbits.ACKSTAT); // Ensure I2C module is idle //IdleI2C(); // Write word address for serial EEPROM MasterWriteI2C(Address); // Wait till address is transmitted while(I2CSTATbits.TBF); // Test for ACK condition received while(I2CSTATbits.ACKSTAT); // Ensure I2C module is idle //IdleI2C(); // Generate the I2C bus restart condition RestartI2C(); // Wait until re-start condition is over while (I2CCONbits.RSEN); // WWrite Slave address and set master for reception (R/W bit should be 1) MasterWriteI2C( Device+1 ); // Wait till address is transmitted while(I2CSTATbits.TBF); // Ensure I2C module is idle //IdleI2C(); // Test for ACK condition received while(I2CSTATbits.ACKSTAT); // Ensure I2C module is idle //IdleI2C(); // Read the data byte Data = MasterReadI2C(); // Ensure I2C module is idle //IdleI2C(); // send NACK condition back to the I2C slave indicating master received the data byte NotAckI2C(); // wait until NACK sequence is over while (I2CCONbits.ACKEN); // Ensure I2C module is idle //IdleI2C(); // send STOP condition StopI2C(); // Wait till Stop sequence is completed while(I2CCONbits.PEN); // Ensure I2C module is idle IdleI2C(); return ( Data ); // return with data }
void readTempSensor(unsigned char *byte1, unsigned char *byte2, unsigned char *slope, unsigned char *counter) { StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xAA); // read two bytes command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*byte1) = ReadI2C(); IdleI2C(); AckI2C(); IdleI2C(); (*byte2) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read counter StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA8); // read counter command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*counter) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read slope StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA9); // read slope command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*slope) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); }
unsigned int EERandomRead( unsigned char control, unsigned char address ) { IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( SSPCON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCLIF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } else { if ( WriteI2C( control ) ) // write 1 byte { StopI2C(); return ( -3 ); // return with write collision error } //IdleI2C(); // ensure module is idle if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received { if ( WriteI2C( address ) ) // WRITE word address for EEPROM { StopI2C(); return ( -3 ); // return with write collision error } //IdleI2C(); // ensure module is idle if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received { RestartI2C(); // generate I2C bus restart condition while ( SSPCON2bits.RSEN );// wait until re-start condition is over if ( PIR2bits.BCLIF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } if ( WriteI2C( control+1 ) )// write 1 byte - R/W bit should be 1 { StopI2C(); return ( -3 ); // return with write collision error } //IdleI2C(); // ensure module is idle if ( !SSPCON2bits.ACKSTAT )// test for ACK condition, if received { SSPCON2bits.RCEN = 1; // enable master for 1 byte reception while ( SSPCON2bits.RCEN ); // check that receive sequence is over NotAckI2C(); // send ACK condition while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over StopI2C(); // send STOP condition while ( SSPCON2bits.PEN ); // wait until stop condition is over if ( PIR2bits.BCLIF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } } else { StopI2C(); return ( -2 ); // return with Not Ack error } } else { StopI2C(); return ( -2 ); // return with Not Ack error } } else { StopI2C(); return ( -2 ); // return with Not Ack error } } return ( (unsigned int) SSPBUF ); // return with data }
int LCDSendCommand(BYTE add1,BYTE* wrptr, BYTE size) { #ifdef _WINDOWS UNUSED(add1); UNUSED(wrptr); UNUSED(size); #else BYTE i; BYTE data; BYTE status; //****write the address of the device for communication*** if(START){ RestartI2C(); } else { StartI2C(); START=1; } IdleI2C(); data = SSPBUF; //read any previous stored content in buffer to clear buffer full status do { status = WriteI2C( add1 | 0x00 ); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //R/W BIT IS '0' FOR FURTHER WRITE TO SLAVE do { status = WriteI2C(modeLCD_CMD_TILL_STOP); //write the address of slave if(status == -1) //check if bus collision happened { data = SSPBUF; //upon bus collision detection clear the buffer, SSPCON1bits.WCOL=0; // clear the bus collision status bit //LATAbits.LATA1 =1; } } while(status!=0); //write untill successful communication //***WRITE THE THE DATA TO BE SENT FOR SLAVE*** //write string of data to be transmitted to slave for (i=0;i<size;i++ ) // transmit data { if ( SSP1CON1bits.SSPM3 ) // if Master transmitter then execute the following { //temp = putcI2C1 ( *wrptr ); //if (temp ) return ( temp ); if ( WriteI2C1( *wrptr ) ) // write 1 byte { return ( -3 ); // return with write collision error } IdleI2C1(); // test for idle condition if ( SSP1CON2bits.ACKSTAT ) // test received ack bit state { return ( -2 ); // bus device responded with NOT ACK } // terminate putsI2C1() function } wrptr ++; // increment pointer } IdleI2C(); #endif return 0; }