/** * Gets the software revision of the software in the RLY08 */ unsigned int getRLY08SoftwareRevision() { IdleI2C(); StartI2C(); IdleI2C(); while(I2CCONbits.SEN); // send the address MasterWriteI2C(writeAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_SOFTWARE_REVISION); IdleI2C(); StopI2C(); IdleI2C(); StartI2C(); while(I2CCONbits.SEN); // send the address again with read bit MasterWriteI2C(readAddr); IdleI2C(); // read the data unsigned char data = MasterReadI2C(); IdleI2C(); StopI2C(); return data; }
// Configure and start temperature sensor void configStartTempSensor(void) { // Set configuration register StartI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xAC); // access configuration IdleI2C(); WriteI2C(0x00); // set config IdleI2C(); StopI2C(); // Need at least a 10msec delay (from data sheet) Delay10KTCYx(40); // Start temperature conversion StartI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xEE); // start conversion IdleI2C(); StopI2C(); // Another delay Delay10KTCYx(40); }
void rtcInit(void) { StartI2C(); WriteI2C(0xD0); WriteI2C(0x00); WriteI2C(0x80); //CH = 1 Stop oscillator WriteI2C(0x48); //Minute WriteI2C(0x16); //Hour WriteI2C(0x03); //Tuesday WriteI2C(0x14); //14 WriteI2C(0x03); //March WriteI2C(0x17); //2017 StopI2C(); //Stop the I2C Protocol Delay10KTCYx(250); Delay10KTCYx(250); Delay10KTCYx(250); Delay10KTCYx(250); //Have to start the Clock again StartI2C(); WriteI2C(0xD0); WriteI2C(0x00); WriteI2C(0x00); //start Clock and set the second hand to Zero StopI2C(); Delay10KTCYx(250); Delay10KTCYx(250); Delay10KTCYx(250); Delay10KTCYx(250); }
void EEPROM_write(BYTE devaddr, DWORD addr, BYTE *data, BYTE size) { BYTE i = 0; IdleI2C(); StartI2C(); IdleI2C(); // Dev address WriteI2C( devaddr << 1 ); IdleI2C(); // Memory address WriteI2C( addr >> 8 ); IdleI2C(); WriteI2C( addr & 0xFF ); IdleI2C(); // data for(; i < size; ++i) { WriteI2C( data[i] ); IdleI2C(); } StopI2C(); DelayMs(5); }
void ITG3200_write(unsigned char reg, unsigned char val) { StartI2C(); WriteI2C(ITG3200_ADDR_WRITE); WriteI2C(reg); WriteI2C(val); StopI2C(); }
void ADXL345_multiByteWrite(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); for (i = 0; i < size; i++) { WriteI2C(buffer[i]); } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send starting write address. for (i = 0; i < size; i++) { WriteSPI(buffer[i]); } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
int16_t PutsMasterI2C (uint8_t adresse, uint8_t *donnees, uint8_t nb_octet) { uint8_t j; if (StartI2C () != 1) { return ERREUR_I2C_START; } if (WriteI2C ((adresse<<1)) != 1) { return ERREUR_I2C_WRITE; } for (j=0; j<nb_octet; j++) { if (WriteI2C (donnees[j]) != 1) { return ERREUR_I2C_WRITE; } } if (StopI2C () != 1) { return ERREUR_I2C_STOP; } return 1; }
/* *------------------------------------------------------------------------------ * void ReadRtcTimeAndDate(UINT8 * buff) * * Summary : Read time and date related registers from RTC * * Input : UINT8 *buff - buffer pointer to receive the data * * Output : None * *------------------------------------------------------------------------------ */ void ReadRtcTimeAndDate(UINT8 * buff) { StartI2C(); WriteI2C(DEV_ADDR_RTC); WriteI2C(RTC_REG_SEC); StartI2C(); WriteI2C(DEV_ADDR_RTC + 1); *(buff+0)=ReadI2C(ACK); // Second *(buff+1)=ReadI2C(ACK); // Minute *(buff+2)=ReadI2C(ACK); // hour *(buff+3)=ReadI2C(ACK); // Day *(buff+4)=ReadI2C(ACK); // date *(buff+5)=ReadI2C(ACK); // month *(buff+6)=ReadI2C(NO_ACK); // year StopI2C(); }
/********************************************************************* * Function: XEE_RESULT XEEBeginRead(unsigned char control, * XEE_ADDR address) * * PreCondition: XEEInit() is already called. * * Input: control - EEPROM control and address code. * address - Address at which read is to be performed. * * Output: XEE_SUCCESS if successful * other value if failed. * * Side Effects: None * * Overview: Sets internal address counter to given address. * Puts EEPROM in sequential read mode. * * Note: This function does not release I2C bus. * User must call XEEEndRead() when read is not longer * needed; I2C bus will released after XEEEndRead() * is called. ********************************************************************/ XEE_RESULT XEEBeginRead(unsigned char control, XEE_ADDR address ) { unsigned char r; r = XEESetAddr(control, address); if ( r != XEE_SUCCESS ) return r; r = XEEClose(); if ( r != XEE_SUCCESS ) return r; IdleI2C(); StartI2C(); while( EEPROM_SPICON2bits.SEN ); if ( PIR2bits.BCLIF ) return XEE_BUS_COLLISION; if ( WriteI2C(control+1) ) return XEE_BUS_COLLISION; IdleI2C(); if ( !EEPROM_SPICON2bits.ACKSTAT ) return XEE_SUCCESS; return XEE_NAK; }
/********************************************************************* * Function: LDByteWriteI2C() * Input: Control Byte, 8 - bit address, data. * Overview: Write a byte to low density device at address LowAdd ********************************************************************/ unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode1; unsigned int ErrorCode2; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(ControlByte); //Write Control byte IdleI2C(); ErrorCode1 = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); ErrorCode2 = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition //EEAckPolling(ControlByte); //Perform ACK polling if(ErrorCode1 == 0) { printf("ACK 1 not recieved"); } if(ErrorCode2 == 0) { printf("ACK 2 not recieved"); } //return(ErrorCode); }
void MCP3422_config(uint8_t config_byte) { StartI2C(); WriteI2C(0xD0); // I2C address (for writing) WriteI2C(config_byte); // CODE_LOAD command StopI2C(); }
/* *------------------------------------------------------------------------------ * void StoreSystemDay(UINT8 day) * * Summary : Set new day * * Input : UINT8 day - value related to day * * Output : None * * Nonte : Day value must be between 1 to 7 *------------------------------------------------------------------------------ */ void StoreSystemDay(UINT8 day) { StartI2C(); WriteI2C(DEV_ADDR_RTC); WriteI2C(RTC_REG_DAY); WriteI2C(day); //day StopI2C(); }
/*! \brief LCD IO Ports lesen \return LCD IO Port */ unsigned char GetIOLCD(void) { unsigned char data = 0x00; StartI2C(LCD_DEV+1); data = ReadI2C(0); StopI2C(); return data; }
// ****************************************************************************** // * Function Name: I2CWriteByte * // * Return Value: error condition status * // * Parameters: EE memory device control, address and data * // * bytes. * // * Description: Write single data byte to I2C EE memory * // * device. This routine can be used for any I2C * // * EE memory device, which uses 2 bytes of * // * address data as in the 24LC32A/64A/256A. * // * * // ****************************************************************************** void I2CWriteByte( byte Device, word Address, byte Data ) { // Ensure I2C module is idle IdleI2C(); // Transmit a Start condition StartI2C(); // Wait till Start sequence is completed while(I2CCONbits.SEN ); // Ensure I2C module is idle IdleI2C(); // 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(); // Write data byte to serial EEPROM MasterWriteI2C(Data); // Wait till address is transmitted while(I2CSTATbits.TBF); // 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(); }
void MCP3422_read3(uint8_t* byte1, uint8_t* byte2, uint8_t* config_byte) { StartI2C(); WriteI2C(0xD1); // I2C address (for reading) *byte1 = ReadI2C(); *byte2 = ReadI2C(); *config_byte = ReadLastI2C(); StopI2C(); }
void MAX5217_load_code(uint8_t value_hi, uint8_t value_lo) { StartI2C(); WriteI2C(0x3A); // I2C address (for writing) WriteI2C(0x01); // CODE_LOAD command WriteI2C(value_hi); // DAC value <15:8> WriteI2C(value_lo); // DAC value <7:0> StopI2C(); }
/* *------------------------------------------------------------------------------ * void StoreSystemDate(void) * * Summary : Set new RTC date * * Input : None * * Output : None * *------------------------------------------------------------------------------ */ void StoreSystemDate(UINT8 *databuffer) { StartI2C(); WriteI2C(DEV_ADDR_RTC); WriteI2C(RTC_REG_DATE); WriteI2C(((*(databuffer + 0) & 0x0F)<<4) | (*(databuffer + 1) & 0x0F)); //date WriteI2C(((*(databuffer + 2) & 0x0F)<<4) | (*(databuffer + 3) & 0x0F)); //month WriteI2C(((*(databuffer + 4) & 0x0F)<<4) | (*(databuffer + 5) & 0x0F)); //year StopI2C(); }
/* *------------------------------------------------------------------------------ * void StoreSystemTime(void) * * Summary : Set new RTC time * * Input : None * * Output : None * *------------------------------------------------------------------------------ */ void StoreSystemTime(UINT8 *databuffer) { StartI2C(); WriteI2C(DEV_ADDR_RTC); WriteI2C(RTC_REG_SEC); WriteI2C(((*(databuffer + 4) & 0x0F)<<4) | (*(databuffer + 5) & 0x0F)); //sec WriteI2C(((*(databuffer + 2) & 0x0F)<<4) | (*(databuffer + 3) & 0x0F)); //min WriteI2C(((*(databuffer + 0) & 0x0F)<<4) | (*(databuffer + 1) & 0x0F)); //hour StopI2C(); }
/*! \brief LCD IO Ports schreiben \param setCommand an - oder auschalten \param bits zu setzende bits */ void SetIOLCD(unsigned char setCommand, unsigned char bits) { if (setCommand == ON) portLCD |= bits; else portLCD &= ~bits; StartI2C(LCD_DEV); WriteI2C(portLCD); StopI2C(); }
void changeRLY08Address(int newAddress) { StartI2C(); IdleI2C(); while(I2CCONbits.SEN); MasterWriteI2C(writeAddr); IdleI2C(); // command register MasterWriteI2C(RLY08_REGISTER_COMMAND); IdleI2C(); StartI2C(); while(I2CCONbits.SEN); MasterWriteI2C(RLY08_CHANGE_ADDRESS_FIRST_COMMAND); IdleI2C(); MasterWriteI2C(RLY08_CHANGE_ADDRESS_SECOND_COMMAND); IdleI2C(); MasterWriteI2C(RLY08_CHANGE_ADDRESS_THIRD_COMMAND); MasterWriteI2C(newAddress); StopI2C(); }
unsigned char i2c_master_send(unsigned char length, unsigned char *msg) { for (ic_ptr->outbuflen = 0; ic_ptr->outbuflen < length; ic_ptr->outbuflen++) { ic_ptr->outbuffer[ic_ptr->outbuflen] = msg[ic_ptr->outbuflen]; } ic_ptr->outbuflen = length; ic_ptr->outbufind = 1; // point to the second byte to be sent ic_ptr->status = I2C_MASTER_SEND_ADDR; ic_ptr->slave_addr = ic_ptr->outbuffer[0]; StartI2C(); return (0); }
void internalChangeAddressLine(unsigned char oldAddress, unsigned char value) { IdleI2C(); StartI2C(); MasterWriteI2C(oldAddress); IdleI2C(); MasterWriteI2C(0); IdleI2C(); MasterWriteI2C(value); IdleI2C(); StopI2C(); IdleI2C(); }
unsigned char readSRF02(char addr, char reg) { StartI2C(); while (I2CCONbits.SEN); // send the address MasterWriteI2C(addr); IdleI2C(); // send the register MasterWriteI2C(reg); IdleI2C(); StopI2C(); IdleI2C(); StartI2C(); while (I2CCONbits.SEN); // send the address again with read bit MasterWriteI2C(addr + 1); IdleI2C(); // read the data unsigned char data = MasterReadI2C(); StopI2C(); return data; }
// Configure and start temperature sensor void stopTempSensor(void) { StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0x22); // stop conversion IdleI2C(); StopI2C(); // Another delay Delay10KTCYx(40); }
int getRelayStates() { StartI2C(); IdleI2C(); // send the address MasterWriteI2C(writeAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_RELAY_STATES); IdleI2C(); StartI2C(); // send the address again with read bit MasterWriteI2C(readAddr); IdleI2C(); // send the register MasterWriteI2C(RLY08_REGISTER_RELAY_STATES); // read the data - and send an ACK int data = MasterReadI2C(); StopI2C(); return(data); }
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 }
void i2c_setup(void) { //------------------- //Setup I2C //------------------- SSPADD = 0x78; //120 OpenI2C(MASTER, SLEW_OFF); //Init I2C module printf("\n\n\rConfiguring I2C..."); StartI2C(); WriteI2C(0x98); WriteI2C(0x07); //mode register WriteI2C(0x00); //Standby mode StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x05); //SPCNT register WriteI2C(0x00); //No sleep count StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x06); //interrupt register (INTSU) WriteI2C(0xE4); //shake INT on 3-axis, and tap detect StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x09); //PDET register WriteI2C(0x84); //tap detection on z-axis StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x08); //SR register WriteI2C(0x00); //Sample rate at 32/sec StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x0A); //PD register WriteI2C(0x2F); //test tap detection, debounce StopI2C(); IdleI2C(); StartI2C(); WriteI2C(0x98); WriteI2C(0x07); //select the mode register WriteI2C(0x41); //active mode, int push pull StopI2C(); printf("Finished!\r\n"); }
/********************************************************************* * Function: LDPageWriteI2C() * Input: ControlByte, LowAdd, *wrptr. * Overview: Write a page of data from array pointed to be wrptr * starting at LowAdd * Note: LowAdd must start on a page boundary ********************************************************************/ unsigned int LDPageWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char *wrptr) { IdleI2C(); //wait for bus Idle StartI2C(); //Generate Start condition WriteI2C(ControlByte); //send controlbyte for a write IdleI2C(); //wait for bus Idle WriteI2C(LowAdd); //send low address IdleI2C(); //wait for bus Idle putstringI2C(wrptr); //send data IdleI2C(); //wait for bus Idle StopI2C(); //Generate Stop return(0); }
void writeSRF02Command(char addr, char reg, char cmd) { StartI2C(); // Wait till Start sequence is completed IdleI2C(); MasterWriteI2C(addr); IdleI2C(); MasterWriteI2C(reg); IdleI2C(); // command MasterWriteI2C(cmd); IdleI2C(); StopI2C(); }
/********************************************************************* * 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 }