Esempio n. 1
0
  /**
  * 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;
}
Esempio n. 2
0
File: main.c Progetto: GBert/misc
void init_MCP()
{
     //IODIR A configuration
     MCP_addr();
     WriteI2C(MCP_IODIR_A);
     WriteI2C(0x00);
     StopI2C();

     //IPOL A configuration
     MCP_addr();
     WriteI2C(MCP_IPOL_A);
     WriteI2C(0x00);
     StopI2C();

     //OLAT A configuration
     MCP_addr();
     WriteI2C(MCP_OLAT_A);
     WriteI2C(0xFF);
     StopI2C();

     //IOCON A configuration
     MCP_addr();
     WriteI2C(MCP_IOCON_A);
     WriteI2C(0x00);
     StopI2C();

     //GPINTEN A configuration
     MCP_addr();
     WriteI2C(MCP_GPINTEN_A);
     WriteI2C(0x00);
     StopI2C();
}
Esempio n. 3
0
// 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);
}
Esempio n. 4
0
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);
}
Esempio n. 5
0
void MCP3422_config(uint8_t config_byte)
{
    StartI2C();
    WriteI2C(0xD0); // I2C address (for writing)
    WriteI2C(config_byte); // CODE_LOAD command
    StopI2C();
}
Esempio n. 6
0
void ITG3200_write(unsigned char reg, unsigned char val) {
	StartI2C();
	WriteI2C(ITG3200_ADDR_WRITE);
	WriteI2C(reg);
	WriteI2C(val);
	StopI2C();
}
Esempio n. 7
0
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);
}
Esempio n. 8
0
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
}
Esempio n. 9
0
static int
i2c_write(struct i2c_platform_data *adap, unsigned char *buf,
		     unsigned int len)
{
	int	i;
	u32	data;

	pr_debug("i2c_write\n");

	i = 0;
	while (i < len) {
		data = buf[i];
		if (MasterWriteI2C(adap, data))
			return -EIO;
		IdleI2C(adap);
		if (pic32_bus_readl(adap->base + PIC32_I2CxSTAT) &
		    I2CSTAT_ACKSTAT)
			return -EIO;
		i++;
	}

	StopI2C(adap);
	IdleI2C(adap);
	return 0;
}
Esempio n. 10
0
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;
}
Esempio n. 11
0
/*********************************************************************
* 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);
}
Esempio n. 12
0
/*********************************************************************
 * Function:        XEE_RESULT XEEEndWrite(void)
 *
 * PreCondition:    XEEInit() && XEEBeginWrite() are already called.
 *
 * Input:           None
 *
 * Output:          XEE_SUCCESS if successful
 *                  other value if failed.
 *
 * Side Effects:    None
 *
 * Overview:        Instructs EEPROM to begin write cycle.
 *
 * Note:            Call this function after either page full of bytes
 *                  written or no more bytes are left to load.
 *                  This function initiates the write cycle.
 *                  User must call for XEEWait() to ensure that write
 *                  cycle is finished before calling any other
 *                  routine.
 ********************************************************************/
XEE_RESULT XEEEndWrite(void)
{
    IdleI2C();
    StopI2C();
    while(EEPROM_SPICON2bits.PEN);
    return XEE_SUCCESS;
}
Esempio n. 13
0
/*
*------------------------------------------------------------------------------
* 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();
}
Esempio n. 14
0
/*!
  \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();

}
Esempio n. 16
0
XEE_RESULT XEEClose(void)
{
    IdleI2C();
    StopI2C();
    while( EEPROM_SPICON2bits.PEN );           // wait until stop condition is over.
    if ( PIR2bits.BCLIF )
        return XEE_BUS_COLLISION;
    return XEE_SUCCESS;
}
Esempio n. 17
0
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();
}
Esempio n. 18
0
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();
}
Esempio n. 19
0
/*
*------------------------------------------------------------------------------
* 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();
}
Esempio n. 20
0
/*!
  \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();
}
Esempio n. 21
0
/*
*------------------------------------------------------------------------------
* 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();
}
Esempio n. 22
0
// 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);
}
Esempio n. 23
0
void internalChangeAddressLine(unsigned char oldAddress, unsigned char value) {
  IdleI2C();
  StartI2C();
  MasterWriteI2C(oldAddress);
  IdleI2C();
  MasterWriteI2C(0);
  IdleI2C();
  MasterWriteI2C(value);
  IdleI2C();
  StopI2C();
  IdleI2C();
}
Esempio n. 24
0
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;
}
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");
}
Esempio n. 26
0
/*********************************************************************
* 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);
}
Esempio n. 27
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();
}
Esempio n. 28
0
/*********************************************************************
 * Function:        XEE_RESULT XEEEndRead(void)
 *
 * PreCondition:    XEEInit() && XEEBeginRead() are already called.
 *
 * Input:           None
 *
 * Output:          XEE_SUCCESS if successful
 *                  other value if failed.
 *
 * Side Effects:    None
 *
 * Overview:        Ends sequential read cycle.
 *
 * Note:            This function ends seuential cycle that was in
 *                  progress.  It releases I2C bus.
 ********************************************************************/
XEE_RESULT XEEEndRead(void)
{
    getcI2C();
    while( EEPROM_SPICON2bits.RCEN );  // check that receive sequence is over.

    NotAckI2C();
    while( EEPROM_SPICON2bits.ACKEN );

    StopI2C();
    while( EEPROM_SPICON2bits.PEN );

    return XEE_SUCCESS;
}
Esempio n. 29
0
/*********************************************************************
 * 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
}
Esempio n. 30
0
/*
*------------------------------------------------------------------------------
* void WriteRtcTimeAndDate(UINT8 *buff)
*
* Summary	: Write time and date related values into RTC
*
* Input		: UINT8 *buff - pointer to the buffer having data to be write.
*
* Output	: None
*
*------------------------------------------------------------------------------
*/
void WriteRtcTimeAndDate(UINT8 *buff)
{
	StartI2C();
	WriteI2C(DEV_ADDR_RTC);
	WriteI2C(RTC_REG_SEC);
	WriteI2C(*(buff+0));
	WriteI2C(*(buff+1));
	WriteI2C(*(buff+2));
	WriteI2C(*(buff+3));
	WriteI2C(*(buff+4));
	WriteI2C(*(buff+5));
	WriteI2C(*(buff+6));
	StopI2C();
}