S8 I2C1_WRITE_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { char ComRes; DevAdd = (DevAdd<<1)|M_I2C_Write; StartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(DevAdd); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(RegAdd); // Write the chip ID register loaction for read IdleI2C1(); // Send the start bit while(I2C1STATbits.ACKSTAT); //wait for acknowledgement while(len--) { ComRes=MasterWriteI2C1(*RegValue); // Write the chip ID register loaction for read IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement RegValue++; } StopI2C1(); // Stop the I2C communication IdleI2C1(); // Wait to complete return ComRes; }
bool I2C1dev_writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) { // S IdleI2C1(); StartI2C1(); // Device write address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x00); // Register address IdleI2C1(); WriteI2C1(regAddr); for (i_accel = 0; i_accel < length; i_accel++) { // Data byte IdleI2C1(); WriteI2C1(data[i_accel]); } // P IdleI2C1(); StopI2C1(); return 1; }
void main(void) { unsigned char slave7bitAddr = 0x2A; //7-bit address of Slave. MSB=Don't care. unsigned char slaveAddrWrite = (slave7bitAddr << 1); //LSB=0, Master Write request. signed char writeStat; unsigned char message[11] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'}; OSCCONbits.IRCF = 0b111; //Set internal oscillator to 16mHz //Must set SCL(pin RC3)and SDA(pin RC4) as inputs and enable digital buffers. TRISCbits.TRISC3 = 1; //set input TRISCbits.TRISC4 = 1; ANSELCbits.ANSC3 = 0; //enable digital buffer ANSELCbits.ANSC4 = 0; OpenI2C1(MASTER, SLEW_OFF); //If you switch to 400kHz (see below) set SLEW_ON /* You can ignore all I2C "unable to resolve identifier" errors assuming you didn't make any typos. Don't forget the "1"'s. */ /* Now set the I2C clock speed. I2C Master always controls clock. For 400kHz use 1k pullup resistors and for 100kHz use 2.2k ohms */ SSP1ADD = 0x27; //100Khz = FOSC/(4 * (SSPADD + 1)) = 16E6/((39 + 1) * 4)note:39=0x27 //SSP1ADD = 0x09; //400Khz = FOSC/(4 * (SSPADD + 1)) = 16E6/((9 + 1) * 4) while (1) { IdleI2C1(); //Wait for bus to become idle. StartI2C1(); //Begin I2C communication IdleI2C1(); //send slave address (w/write request bit) and wait for slave to reply. do { writeStat = WriteI2C1(slaveAddrWrite); //Send address with LSB=Write if (writeStat == -1) { //Detected bus collision - More than one master? unsigned char data = SSP1BUF; //clear the buffer by reading it. SSPCON1bits.WCOL = 0; //clear the bus collision status bit } else if (writeStat == -2) { //NACK (no acknowledge rx'd) //Is the slave on and ready? Did we send the correct address? } } while (writeStat != 0); //Keep repeating until slave acknowledges. //Slave has Ack'd so we can send our Hello World message now. for (int x = 0; x <= 10; x++) { do { writeStat = (WriteI2C1(message[x])); if (writeStat == -2) { //NACK (no acknowledge rx'd) //Is the slave on and ready? Using the correct pullups? } } while (writeStat != 0); //Keep repeating until slave acknowledges. } IdleI2C1(); StopI2C1(); //Delay about 1 sec and then repeat. 1sec = ((10K*200*2)/(16E6/4) Delay10KTCYx(200); Delay10KTCYx(200); } }
int RcvData(unsigned int address) { StartI2C1(); //Send line start condition IdleI2C1(); //Wait to complete MasterWriteI2C1((address << 1) | 1); //Write out slave address OR 1 (read command) IdleI2C1(); //Wait to complete int rcv = MasterReadI2C1(); //Read in a value StopI2C1(); //Send line stop condition IdleI2C1(); //Wait co complete return rcv; //Return read value }
void LDByteWriteI2C(unsigned char SlaveAddress, unsigned char reg, unsigned char data){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress); //transmit write command IdleI2C1(); //Wait to complete MasterWriteI2C1(reg); IdleI2C1(); MasterWriteI2C1(data); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void ClearAll(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(Clear); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void SecondLine(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(Line2); IdleI2C1(); StopI2C1(); IdleI2C1(); }
void FirstLine(void) { StartI2C1(); IdleI2C1(); MasterWriteI2C1(Slave); IdleI2C1(); MasterWriteI2C1(Comsend); IdleI2C1(); MasterWriteI2C1(0x80); IdleI2C1(); StopI2C1(); IdleI2C1(); }
/** * @brief Write to device using generic i2c protocol * @param[in] slave_addr - slave address * @param[in] reg_addr - register address * @param[in] length - number of bytes to read * @param[in] *data - pointer to where register data is to be transfered * @return 0 if sucessfull, 1 otherwise */ int i2c_read(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char *data) { BYTE i=2; StartI2C1(); //Send the Start Bit IdleI2C1(); if (MasterWriteI2C1(((slave_addr<<1)|(0x00)))) return 1; IdleI2C1(); if (MasterWriteI2C1(reg_addr)) return 1; IdleI2C1(); StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete if (MasterWriteI2C1(((slave_addr<<1)|(0x01)))) return 1; IdleI2C1(); I2CReceiverEnable ( I2C1, TRUE); for(i=0;i<length;i++) { data[i] = MasterReadI2C1(); if(i<(length-1)) { I2CAcknowledgeByte(MPU_I2C, TRUE); IdleI2C1(); } else { I2CAcknowledgeByte(MPU_I2C, FALSE); IdleI2C1(); } } StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete return 0; }
S8 I2C1_READ_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { int DataWait = len*100; char ComRes,DevAddr; DevAddr = (DevAdd<<1)|M_I2C_Write; StartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C1(RegAdd); // Write the chip ID register loaction for read IdleI2C1(); // Send the start bit while(I2C1STATbits.ACKSTAT); //wait for acknowledgement RestartI2C1(); // Send the start bit IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement DevAddr = (DevAdd<<1)|M_I2C_Read; MasterWriteI2C1(DevAddr); // Adress with write mode IdleI2C1(); // Wait to complete while(I2C1STATbits.ACKSTAT); //wait for acknowledgement MastergetsI2C1(len, RegValue, DataWait); // Read the Register value IdleI2C1(); // Wait to complete NotAckI2C1(); // Not Acknowledge I2C IdleI2C1(); // Wait to complete StopI2C1(); // Stop I2C communication IdleI2C1(); // Wait to complete; return ComRes; }
/*************************************************** * SendData(int data, unsigned int address) * * * * Sends a byte of data (DATA) over the I2C line * * to I2C address ADDRESS * * * * Returns: nothing * ***************************************************/ void SendData (int data, unsigned int address){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1((address << 1)); //Sends the slave address over the I2C line. This must happen first so the //proper slave is selected to receive data. IdleI2C1(); //Wait to complete MasterWriteI2C1(data); //Sends data byte over I2C line IdleI2C1(); //Wait to complete StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete } //end function
/* * send data to a slave device * takes the slave address, writes the given data byte */ int sendSpeed(unsigned int *slaveAddr, unsigned int *speed) { // get the data from global variable char newSpeed[3] = {*speed, 'X', '\0'}; // wait until idle - not actually needed for single-master bus IdleI2C1(); StartI2C1(); // send start data = SSP1BUF; WriteI2C1(*slaveAddr & 0xFE); // do { // send address until ack'd // status = WriteI2C1(*slaveAddr || 0x00); // if (!(0 == status)) { // write collision // data = SSP1BUF; // SSP1CON1bits.WCOL = 0; // } // } while (!(0 == status)); // send bytes WriteI2C1(newSpeed[0]); // TODO: send multiple bytes StopI2C1(); // stop transmission return 1; }
uint8_t I2C_WriteToReg(uint8_t I2CAddress, uint8_t deviceRegister, uint8_t data) { StartI2C1(); while(I2CCONbits.SEN); IFS1bits.MI2C1IF = 0; MasterWriteI2C1(I2CAddress << 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(deviceRegister); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(data); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag StopI2C1(); // Write stop sequence. while(I2CCONbits.PEN); IdleI2C1(); return(1); }
void Init_mcp(void) { unsigned int config2, config1; config2 = 0x190; // Baud rate is set for 100 kHz config1 = 0b1000001000100000; /* * (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD & I2C_IPMI_DIS & I2C_7BIT_ADD & * I2C_SLW_DIS & I2C_SM_DIS & I2C_GCALL_DIS & I2C_STR_DIS & I2C_NACK & * I2C_ACK_DIS & I2C_RCV_DIS & I2C_STOP_DIS & I2C_RESTART_DIS & * I2C_START_DIS) */ // Configure I2C for 7 bit address mode OpenI2C1(config1, config2); IdleI2C1(); //Initialize MCP23017 i2c I/O expander Write23X08_17(IOCONA, 0b01111000, 0x00); Write23X08_17(GPPUA, 0x00, 0x00); // Set Pull-up resistors Write23X08_17(GPPUB, 0x1F, 0x00); // Set Pull-up resistors Write23X08_17(IPOLA, 0x00, 0x00); // I/O polarity normal Write23X08_17(IPOLB, 0x00, 0x00); // I/O polarity normal Write23X08_17(GPIOA, 0x00, 0x00); // Row is kept LOW while changing I/O Write23X08_17(GPIOB, 0x00, 0x00); // Row is kept LOW while changing I/O Write23X08_17(IODIRA, 0x00, 0x00); // Rows = inputs; Columns = outputs Write23X08_17(IODIRB, 0x1F, 0x00); // Rows = inputs; Columns = outputs }
void I2C_WriteToReg(uint8_t address, uint8_t deviceRegister, uint8_t data) { // Assert the start condition StartI2C1(); while (I2C1CONbits.SEN); IFS1bits.MI2C1IF = 0; // Send the 7-bit I2C device address MasterWriteI2C1(address << 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the register address MasterWriteI2C1(deviceRegister); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the data MasterWriteI2C1(data); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Assert the stop condition StopI2C1(); while (I2C1CONbits.PEN); // Sit idle on the bus IdleI2C1(); }
signed char WriteI2C1( unsigned char data_out ) { SSPBUF = data_out; // write single byte to SSPBUF if ( SSPCON1bits.WCOL ) // test if write collision occurred return ( -1 ); // if WCOL bit is set return negative # else { if( ((SSPCON1&0x0F)!=0x08) && ((SSPCON1&0x0F)!=0x0B) ) //Slave mode only { SSPCON1bits.CKP = 1; // release clock line while ( !PIR1bits.SSPIF ); // wait until ninth clock pulse received if ( ( !SSPSTATbits.R_W ) && ( !SSPSTATbits.BF ) )// if R/W=0 and BF=0, NOT ACK was received { return ( -2 ); //return NACK } else { return ( 0 ); //return ACK } } else if( ((SSPCON1&0x0F)==0x08) || ((SSPCON1&0x0F)==0x0B) ) //master mode only { while( SSPSTATbits.BF ); // wait until write cycle is complete IdleI2C1(); // ensure module is idle if ( SSPCON2bits.ACKSTAT ) // test for ACK condition received return ( -2 ); // return NACK else return ( 0 ); //return ACK } } }
uint8_t I2C_ReadFromReg(uint8_t I2CAddress, uint8_t deviceRegister) { uint8_t data = 0; StartI2C1(); while(I2CCONbits.SEN); MasterWriteI2C1(I2CAddress << 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag MasterWriteI2C1(deviceRegister); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag RestartI2C1(); // Second start. while(I2CCONbits.RSEN); MasterWriteI2C1((I2CAddress << 1 )+ 1); while(I2CSTATbits.TBF); // 8 clock cycles while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag data = MasterReadI2C1(); NotAckI2C1(); // Read stop sequence. while(I2C1CONbits.ACKEN == 1); StopI2C1(); while(I2CCONbits.PEN); IdleI2C1(); return(data); }
void WaitI2C(I2cBus* i2cBus) { if (i2cBus == NULL) { IdleI2C1(); } else { enum I2cPort portIndex = i2cBus->port; if (portIndex == I2C_BUS_PORT_1) { while(I2C1CONbits.SEN || I2C1CONbits.PEN || I2C1CONbits.RSEN || I2C1CONbits.RCEN || I2C1CONbits.ACKEN || I2C1STATbits.TRSTAT); } #if defined _I2C2 else if (portIndex == I2C_BUS_PORT_2) { while(I2C2CONbits.SEN || I2C2CONbits.PEN || I2C2CONbits.RSEN || I2C2CONbits.RCEN || I2C2CONbits.ACKEN || I2C2STATbits.TRSTAT); } #endif #if defined _I2C3 else if (portIndex == I2C_BUS_PORT_3) { while(I2C3CONbits.SEN || I2C3CONbits.PEN || I2C3CONbits.RSEN || I2C3CONbits.RCEN || I2C3CONbits.ACKEN || I2C3STATbits.TRSTAT); } #endif #if defined _I2C4 else if (portIndex == I2C_BUS_PORT_4) { while(I2C4CONbits.SEN || I2C4CONbits.PEN || I2C4CONbits.RSEN || I2C4CONbits.RCEN || I2C4CONbits.ACKEN || I2C4STATbits.TRSTAT); } } #endif }
/* * Function reads I2C device from slaveAdd * inputs: slaveAdd * registerAdd * returns: int recByte -> received byte from slave device * -1 -> reading error */ unsigned char I2C1ReadByte1(UINT8 slaveAdd, UINT8 registerAdd) { unsigned char recByte = 0; UINT8 address; StartI2C1(); IdleI2C1(); //Wait to complete address = (slaveAdd<<1) & 0xFE; //write MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } //Send register register location MasterWriteI2C1(registerAdd); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } RestartI2C1(); //Send start condition again IdleI2C1(); address = (slaveAdd<<1) | 0x01; //Read MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return 0; } recByte = MasterReadI2C1(); //NotAckI2C1(); IdleI2C1(); StopI2C1(); IdleI2C1(); return recByte; }
/* * sends a start and writes a char */ int startAndWrite(char *c) { char data = 0x9A; //0b10011010; IdleI2C1(); StartI2C1(); WriteI2C1(*c); // just write a char WriteI2C1(data); StopI2C1(); }
unsigned char Read23X08_17(unsigned char reg, unsigned char gAddrPins) { unsigned char num; StartI2C1(); // Wait till start sequence is completed while (I2C1CONbits.SEN); MasterWriteI2C1(gControlByte | WrtCmd | gAddrPins); WaitForACK(); MasterWriteI2C1(reg); WaitForACK(); RestartI2C1(); IdleI2C1(); MasterWriteI2C1(gControlByte | RdCmd | gAddrPins); WaitForACK(); num = MasterReadI2C1(); NotAckI2C1(); StopI2C1(); IdleI2C1(); return (num); }
void SetupI2C(void) { unsigned int I2C1CONvalue, I2C1BRGvalue; I2C1CONvalue = I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_HLD & I2C1_IPMI_DIS & I2C1_7BIT_ADD & I2C1_SLW_DIS & I2C1_SM_DIS & I2C1_GCALL_DIS & I2C1_STR_DIS & I2C1_NACK & I2C1_ACK_DIS & I2C1_RCV_DIS & I2C1_STOP_DIS & I2C1_RESTART_DIS & I2C1_START_DIS; I2C1BRGvalue = 363; // Fcy(1/Fscl - 1/1111111)-1 OpenI2C1(I2C1CONvalue, I2C1BRGvalue); IdleI2C1(); }
void Show(unsigned char *text) { // Assert a start condition StartI2C1(); while(_SEN); // Write the address of the slave MasterWriteI2C1(Slave); while(I2C1STATbits.TBF); //Wait till address is transmitted while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle IdleI2C1(); MasterWriteI2C1(Datasend); IdleI2C1(); while(*text) { MasterWriteI2C1(*text); IdleI2C1(); ++text; } StopI2C1(); delay(2); }
int8_t I2C1dev_readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) { count_accel = 0; // S IdleI2C1(); StartI2C1(); // Device write address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x00); // Register address IdleI2C1(); WriteI2C1(regAddr); // R IdleI2C1(); RestartI2C1(); // Device read address IdleI2C1(); WriteI2C1(devAddr << 1 | 0x01); for (count_accel = 0; count_accel < length; count_accel++) { // Data byte IdleI2C1(); data[count_accel] = ReadI2C1(); if (count_accel == length - 1) { // NACK IdleI2C1(); NotAckI2C1(); } else { // ACK IdleI2C1(); AckI2C1(); } } // P IdleI2C1(); StopI2C1(); return count_accel; }
void I2C_ReadFromReg_Burst(uint8_t address, uint8_t deviceRegister, uint8_t* data, uint8_t burstNum) { // Assert the start condition StartI2C1(); while (I2C1CONbits.SEN); // Send the 7-bit device address MasterWriteI2C1(address << 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Send the register address MasterWriteI2C1(deviceRegister); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Start a new I2C transaction RestartI2C1(); while (I2C1CONbits.RSEN); // Send the second address MasterWriteI2C1((address << 1) + 1); while (I2C1STATbits.TBF); // 8 clock cycles while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle IFS1bits.MI2C1IF = 0; // Clear interrupt flag // Read the data data[0] = MasterReadI2C1(); if (burstNum > 1) { uint8_t i; for (i = 1; i < burstNum; i++) { AckI2C1(); while (I2C1CONbits.ACKEN == 1); data[i] = MasterReadI2C1(); } } // No need to ack reception of this data NotAckI2C1(); while (I2C1CONbits.ACKEN == 1); // Stop the I2C transaction StopI2C1(); while (I2C1CONbits.PEN); // Go idle on the bus IdleI2C1(); }
/** * @brief Write to device using generic i2c protocol * @param[in] slave_addr - slave address * @param[in] reg_addr - register address * @param[in] length - number of bytes to write * @param[in] *data - pointer for data to write * @return 0 if sucessfull, 1 otherwise */ int i2c_write(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char const *data) { BYTE i; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete if (MasterWriteI2C1(((slave_addr<<1)|(0x00)))) return 1; IdleI2C1(); if (MasterWriteI2C1(reg_addr)) return 1; IdleI2C1(); for(i=0;i<length;i++){ if (MasterWriteI2C1(data[i])) return 1; } StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete return 0; }
int RcvIRTemp(unsigned int address){ rcv = 0; StartI2C2(); //Send line start condition IdleI2C2(); //Wait to complete MasterWriteI2C2(0xb4); //Write slave addr WRITE (OR 0) IdleI2C2(); MasterWriteI2C2(0xf0); //command // //Command to read Tobj1 on ir sensor's ram: 000 = read ram; 0x07 = tobj1 IdleI2C2(); //Wait to complete // StopI2C2(); //restart // IdleI2C2(); // StartI2C2(); // IdleI2C2(); // // MasterWriteI2C2(0xb5); //Write the slave address, READ (OR 1) // IdleI2C2(); MasterReadI2C2(); //Read in LSB IdleI2C2(); AckI2C2(); //Acknowledge LSB IdleI2C2(); MasterReadI2C2(); //Read in MSB IdleI2C1(); AckI2C2(); //Acknowledge MSB IdleI2C2(); MasterReadI2C2(); //Read in PEC IdleI2C1(); AckI2C2(); //Acknowledge MSB IdleI2C2(); StopI2C2(); //Send line stop condition IdleI2C2(); //Wait co complete return rcv; //Return read value }
char MasterWriteI2C1(unsigned char data_out) { I2C1TRN = data_out; if(I2C1STATbits.IWCOL) /* If write collision occurs,return -1 */ return -1; else { while(I2C1STATbits.TRSTAT); // wait until write cycle is complete IdleI2C1(); // ensure module is idle if ( I2C1STATbits.ACKSTAT ) // test for ACK condition received return ( -2 ); else return ( 0 ); // if WCOL bit is not set return non-negative # } }
void LDByteReadI2C(unsigned char SlaveAddress, unsigned char reg, unsigned char *data, int num){ StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress); //transmit write command IdleI2C1(); //Wait to complete MasterWriteI2C1(reg); IdleI2C1(); StopI2C1(); IdleI2C1(); StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1(SlaveAddress|0x01); //transmit read command IdleI2C1(); //Wait to complete MastergetsI2C1(num, data, 30); StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete }
// Blocking encoder read. This will fail if the encoders are in a weird state void amsEncoderBlockingRead(unsigned char num) { unsigned char enc_data[2]; CRITICAL_SECTION_START i2cStartTx(ENC_I2C_CHAN); //Setup to burst read both registers, 0xFE and 0xFF i2cSendByte(ENC_I2C_CHAN, encAddr[2*num+1]); //Write address i2cSendByte(ENC_I2C_CHAN, AMS_ENC_ANGLE_REG); i2cEndTx(ENC_I2C_CHAN); i2cStartTx(ENC_I2C_CHAN); i2cSendByte(ENC_I2C_CHAN, encAddr[2*num]); //Read address i2cReadString(1,2,enc_data,10000); i2cEndTx(ENC_I2C_CHAN); IdleI2C1(); _MI2C1IF = 0; CRITICAL_SECTION_END amsEncoderUpdatePos(num,((enc_data[1] << 6)+(enc_data[0] & 0x3F))); }