void SaySomething(const char* pMessage, uint8_t pWaitTillEnd){ //Just in my version, the /enable of the opamp is on D0 //force it on //PORTD &= 0xFE; USI_TWI_Master_Initialise(); uint8_t vLen = strlen(pMessage); //heading byte : address + is it read or write mBuff[0] = (AQTK_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); strncpy(mBuff +1, pMessage, vLen); mBuff[1+vLen] = 0; mBuff[1+vLen+1] = 13;//need the CR char mBuff[1+vLen+1+1] = 0; //send 4 bytes USI_TWI_Start_Read_Write( (unsigned char*) mBuff, vLen+4 ); //wait until the LSI is free if (pWaitTillEnd){ while (LsiIsBusy() == 1){_delay_ms(50);} } //Just in my version, the /enable of the opamp is on D0 //force it OFF //PORTD |= 0x01; }
int main(void) { // initialize uint8_t i2cMessageBuf[I2C_MAX_MSG_SIZE]; USI_TWI_Master_Initialise(); // reset the power mgmt register to wake it up i2cMessageBuf[0] = (uint8_t) 0xD0; // address 0x68, write i2cMessageBuf[1] = 0x6b; // low x-axis i2cMessageBuf[2] = 0x00; USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x03); long_delay_ms(50); while (TRUE) { // construct a command to high-order x value of a 9150 accelerometer // register 59 = 0x3b; low-order register is 0x3c, then put them // together i2cMessageBuf[0] = (uint8_t) 0xD0; i2cMessageBuf[1] = 0x3b; // high x-axis //i2cMessageBuf[2] = 0x3c; // send the message USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x02); // wait long_delay_ms(50); // read the msg i2cMessageBuf[0] = 0xD1; // lsb == 1 => read i2cMessageBuf[1] = 0x3b; //i2cMessageBuf[2] = 0x3c; USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x03); //USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x04); // turn on LED if not flat on table // flat on table: high is FA, low is FF if (i2cMessageBuf[1] != 0xFA) { DDRB |= 1 << PB3; PORTB &= ~(1<<PB3); } else PORTB |= 1 << PB3; // wait long_delay_ms(500); } // never reached return 0; }
uint8_t USI_TWI::endTransmission(){ // actually sends the buffer bool xferOK = false; uint8_t errorCode = 0; xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work USI_BufIdx = 0; if (xferOK) return 0; else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }
uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave bool xferOK = false; uint8_t errorCode = 0; USI_LastRead = 0; USI_BytesAvail = numBytes; // save this off in a global numBytes++; // add extra byte to transmit header USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work // USI_Buf now holds the data read if (xferOK) return 0; else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }
//Returns 1 if busy //basically recomposed on IsBusy from the AquesTalk's Arduino library uint8_t LsiIsBusy(){ //heading byte : address + is it read or write mBuff[0] = (AQTK_I2C_ADDR<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT); mBuff[1] = 0; USI_TWI_Start_Read_Write( mBuff, 2 ); //answer is in mBuff[1] ! if (mBuff[1]=='*' || mBuff[1]==0xFF){ //busy, come back later _delay_ms(10); return 1; } return 0; }
void AquestLSI_Init(){ USI_TWI_Master_Initialise(); //heading byte : address + is it read or write mBuff[0] = (AQTK_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); //from the next byte is the message mBuff[1] = '?'; mBuff[2] = 0; mBuff[3] = 13; //need the CR char mBuff[4] = 0; //send 4 bytes USI_TWI_Start_Read_Write( mBuff, 5 ); //Just in my version, the /enable of the opamp is on D0 //force it OFF //PORTD |= 0x01; }
uint8_t i2c_endTransmission(uint8_t stop) { // actually sends the buffer bool xferOK = false; uint8_t errorCode = 0; xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work USI_BufIdx = 0; if (xferOK) { if (stop) { errorCode = USI_TWI_Master_Stop(); if (errorCode == 0) { errorCode = USI_TWI_Get_State_Info(); return errorCode; } } return 0; } else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }