int Wii_Data( nunchuck_data_t *data ) { int i = 0x21; if (data == 0) return -1; // send 0x00 to let nunchuck know we are going to request data i2c_start(); // send start i2c_sendByte(WII_ADDR); i2c_sendByte(0x00); i2c_stop(); // switch to read mode by adding 1 to WII_ADDR _delay_ms(1); i2c_start(); // send start i2c_sendByte(WII_ADDR | 1); uint8_t *packet = (uint8_t*)data; for(i = 0; i < 6; i++) { if(i < 5) { i2c_ack(); } else { i2c_nack(); } *(packet+i) = i2c_getByte(); } i2c_stop(); return i; }
/* Name: i2c_write * Description: Write buff_len number of bytes to the addressed slave device. */ void i2c_write(uint8_t address, uint8_t *buffer, int buff_len) { int state = 0; int i = 0; address = (address << 1)| WRITE; //7 bits address and one 0 to indicate we want to write on de CODEC i2c_start(); while(state != 0x08){ //Wait until the start signal have been recived state = LPC_I2C0->STAT ; } i2c_tx(address); LPC_I2C0->CONCLR = 0x28; //Clear SI and start flag while(state != 0x18){ //Wait until the addres has been recived state = LPC_I2C0->STAT; } i2c_tx(buffer[0]); LPC_I2C0->CONCLR = 0x18; //Clear SI and start flag while(state != 0x28){ //Wait until the register address has been recived state = LPC_I2C0->STAT; } for (i = 1; i < buff_len; i++) { i2c_tx(buffer[i]); i2c_ack(); LPC_I2C0->CONCLR = 0x18; //Clear SI and start flag state = 0; while(state != 0x28){ //Wait until the data has been transmited state = LPC_I2C0->STAT; } } i2c_stop(); // send Stop signal delay_ms(10); //wait }
static void i2c_sendack(char ack) { if (!ack) i2c_ack(); else i2c_nack(); }
void i2c_read() { iic_Pull(); i2c_stop(); i2c_start(); i2c_sendbyte(0xa9); //addr if (ack) i2c_stop(); else { rdat1=i2c_recbyte(); //dat1 ack=0; i2c_ack(); delays(20); rdat2=i2c_recbyte(); //dat2 ack=1; i2c_ack(); delays(20); } delays(100); i2c_stop(); i2c_start(); i2c_sendbyte(0xa8); //addr if (ack) i2c_stop(); else { // i2c_sendbyte(g_u8LedTxDAT1); // if(ack) // i2c_stop(); // else // { // i2c_sendbyte(g_u8LedTxDAT2); // ack=1; // i2c_ack(); // delays(20); // } i2c_sendbyte(g_u8LedTxDAT1); ack=1; i2c_ack(); delays(20); } }
/* * Function: measureRH() * -------------------- * Used to send the RH measurement command and read the result */ void measureRH() { i2c_start(); send_i2c_byte(SI7020ADR_W); // address +0 write send_i2c_byte(MEASURE_HUM_HOLD_CMD); //0xE5 =0b11100101 i2c_repeatedStart(); send_i2c_byte(SI7020ADR_R); //adresss +1 read i2c_mIdleI2C1(); hhLSB = i2c_read_ack(); i2c_ack(); i2c_mIdleI2C1(); i2c_ack(); hhMSB = i2c_read_ack(); i2c_mIdleI2C1(); /////////////////////// maybe? i2c_nack(); i2c_stop(); humidityCode.b[0] = hhLSB; humidityCode.b[1] = hhMSB; }
unsigned char i2c_receive_byte_with_ack() { // Receive Enalbe unsigned char ch; SSPCON2bits.RCEN = 1; while (!BF); ch = SSPBUF; i2c_wait(); i2c_ack(); return ch; }
/* * Function: measureT() * -------------------- * Used to send the temperature measurement command and read the result */ void measureT() { i2c_start(); send_i2c_byte(SI7020ADR_W); // address +0 write send_i2c_byte(MEASURE_TEMP_HOLD_CMD); //0xE3 =0b11100011 i2c_repeatedStart(); //i2c_start(); send_i2c_byte(SI7020ADR_R); //adresss +1 read i2c_mIdleI2C1(); ttLSB = i2c_read_ack(); i2c_ack(); i2c_mIdleI2C1(); i2c_ack(); ttMSB = i2c_read_ack(); i2c_mIdleI2C1(); /////////////////////// maybe? i2c_nack(); i2c_stop(); temperatureCode.b[0] = ttLSB; temperatureCode.b[1] = ttMSB; }
/* * Function: getLastTemp() * -------------------- * Used to retrieve last temperature measurement, idea is to save some time on * conversion-> because SI7020 does relative humidity(RH) measurement it also measures * temperature. */ void getLastTemp() { i2c_start(); send_i2c_byte(SI7020ADR_W); // address +0 write send_i2c_byte(READ_LAST_TEMP_CMD); i2c_repeatedStart(); //i2c_start(); send_i2c_byte(SI7020ADR_R); //adresss +1 read i2c_mIdleI2C1(); ttLSB = i2c_read_ack(); i2c_ack(); i2c_mIdleI2C1(); i2c_ack(); ttMSB = i2c_read_ack(); i2c_mIdleI2C1(); /////////////////////// maybe? i2c_nack(); i2c_stop(); temperatureCode.b[0] = ttLSB; temperatureCode.b[1] = ttMSB; }
unsigned char mpu_read_byte(unsigned char addr){ unsigned char dat; i2c_start(); i2c_send_byte(SlaveAddress); //Address for mpu6050 i2c_send_byte(addr); //Address for register i2c_start(); i2c_send_byte(SlaveAddress+1); //Address for mpu6050 dat=i2c_recv_byte(); i2c_ack(); i2c_stop(); return dat; }
int i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack) { int i, ack; I2C_SET(bus,0,0); for (i=7; i>=0; i--) (data&(1<<i)) ? i2c_one(bus) : i2c_zero(bus); if (wait_for_ack) udelay(wait_for_ack); ack=i2c_ack(bus); I2C_DEBUG(printk("%02x%c ",(int)data,ack?'-':'+')); return ack; }
unsigned char i2c_inb(int ack) { int i; unsigned char byte = 0; /* clock in each bit, MSB first */ for ( i=0x80; i; i>>=1 ) { #ifdef HAVE_I2C_LOW_FIRST /* Tricky business. Here we discharge the data line by driving it low and then set it to input to see if it stays low or goes high */ SDA_LO; /* First, discharge the data line */ #endif SDA_INPUT; /* And set to input */ SCL_HI; if ( SDA ) byte |= i; SCL_LO; SDA_OUTPUT; } i2c_ack(ack); return byte; }