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; }
/* * 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 mti_tcn75a_set_resolution(MtiTcn75aResolution resolution) { uint8_t tmp; uint8_t rc; /* start */ StartI2C1(); /* send slave address (write) */ rc = WriteI2C1(0b10010000); if (rc != 0x00) { rc = CH_ERROR_I2C_SLAVE_ADDRESS; goto out; } /* set config pointer */ rc = WriteI2C1(0b00000001); if (rc != 0x00) { rc = CH_ERROR_I2C_SLAVE_CONFIG; goto out; } /* send config */ tmp = 0x00; tmp |= resolution << 5; WriteI2C1(tmp); /* stop */ StopI2C1(); out: return rc; }
/* * 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(); }
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); } }
void I2CWriteByte(unsigned char add,unsigned char subadd,unsigned char data) { StartI2C1(); WriteI2C1(add<<1); //while(!SSP1CON2bits.ACKSTAT); WriteI2C1(subadd); //while(!SSP1CON2bits.ACKSTAT); WriteI2C1(data); //while(!SSP1CON2bits.ACKSTAT); StopI2C1(); }
unsigned char I2CReadByte(unsigned char add,unsigned char subadd) { unsigned char _temp=0; StartI2C1(); while(WriteI2C1(add<<1)!=0); while(WriteI2C1(subadd)!=0); RestartI2C1(); while(WriteI2C1(1+(add<<1))!=0); _temp=ReadI2C1(); NotAckI2C1(); StopI2C1(); return _temp; }
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; }
uint8_t mti_tcn75a_get_temperature(int32_t *result) { uint16_t tmp; uint8_t rc; uint8_t tmp_lsb = 4; uint8_t tmp_msb = 2; /* start */ StartI2C1(); /* send slave address (write) */ rc = WriteI2C1(0b10010000); if (rc != 0x00) { rc = CH_ERROR_I2C_SLAVE_ADDRESS; goto out; } /* set ambient temperature pointer */ rc = WriteI2C1(0b00000000); /* reset the bus */ RestartI2C1(); /* send slave address (read) */ WriteI2C1(0b10010001); /* read the temperature */ tmp_msb = ReadI2C1(); AckI2C1(); tmp_lsb = ReadI2C1(); NotAckI2C1(); /* stop */ StopI2C1(); /* format result */ *result = (((int32_t) tmp_msb) << 16) + (((int32_t) tmp_lsb) << 8); out: return rc; }
/* * Simple test case, just try to write a char */ int writeChar(char *c) { IdleI2C1(); WriteI2C1(*c); // just write a char }
signed int EERandomRead1( unsigned char control, unsigned char address ) { IdleI2C1(); // ensure module is idle StartI2C1(); // initiate START condition while ( SSP1CON2bits.SEN ); // wait until start condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } else { if ( WriteI2C1( control ) ) // write 1 byte { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT ) // test for ACK condition, if received { if ( WriteI2C1( address ) ) // WRITE word address for EEPROM { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT ) // test for ACK condition, if received { RestartI2C1(); // generate I2C1 bus restart condition while ( SSP1CON2bits.RSEN );// wait until re-start condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } if ( WriteI2C1( control+1 ))// write 1 byte - R/W bit should be 1 { StopI2C1(); return ( -3 ); // return with write collision error } //IdleI2C1(); // ensure module is idle if ( !SSP1CON2bits.ACKSTAT )// test for ACK condition, if received { SSP1CON2bits.RCEN = 1; // enable master for 1 byte reception while ( SSP1CON2bits.RCEN ); // check that receive sequence is over NotAckI2C1(); // send ACK condition while ( SSP1CON2bits.ACKEN ); // wait until ACK sequence is over StopI2C1(); // send STOP condition while ( SSP1CON2bits.PEN ); // wait until stop condition is over if ( PIR2bits.BCL1IF ) // test for bus collision { return ( -1 ); // return with Bus Collision error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } else { StopI2C1(); return ( -2 ); // return with Not Ack error } } return ( (unsigned int) SSP1BUF ); // return with data }
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; }
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; }
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; }