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; }
void sendUpdateUpperRange(uint8_t currentNumber) { USI_TWI_Master_Initialise(); uint8_t buffer[UPPER_RANGE_CMD_SIZE]; buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = UPPER_RANGE_CMD; buffer[2] = currentNumber; /* Remember that you are only transmitting it 10 times, and that you can * transmit multiple times if it makes trouble... */ for (int i = 0; i < RETRANSMIT_COUNT; i++) { _delay_ms(10); /* Is this necessary? */ USI_TWI_Start_Transceiver_With_Data(buffer, sizeof(buffer)); /* keep retransmitting until it is successful */ } _delay_ms(10); /* reinitialize ourself as a slave */ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); }
void I2C_masterBegin(I2C_Config* config) { #ifdef USI_TWI USI_TWI_Master_Initialise(); #else TWSR = (TWSR & 0xFC) | config->psc; TWBR = config->twbr_val; #endif }
//initialize I2C bus and power on the sensor void init_sensor(){ unsigned char message[2]; USI_TWI_Master_Initialise(); message[0] = 0x90; message[1] = 0x03; I2C_write(LIGHT_SENSOR_ADDR, message, 2, TRUE); }
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; }
static void setup() { set_output(DDRD, BLUE_LED); output_low(PORTD, BLUE_LED); set_output(DDRB, RED_LED); output_low(PORTB, RED_LED); set_output(DDRB, PWM_DIGIT_1); TCCR0A = _BV(COM0A1) | _BV(WGM00); OCR0A = 0xFF; TCCR0B = _BV(CS01) | _BV(CS00); set_input(DDRB, BUTTON); USI_TWI_Master_Initialise(); init_DHT22(); }
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; }
/** * Broadcast a command to all peers on the I2C bus. * * @param[in] cmd * @param[in] prevNumber * The previous value of the token. This is sent out * only to cause messages to be unique; it helps * generate collisions. * @param[in] newNumber * The actual new value of the token based on the command. * * @return * 'true' if at least one of the broadcasts was successful. The * typical cause for failure would be a multi-master collision in * the case where multiple counter modules decide to send their * counter updates at the same time; i.e. if their buttons are * pressed at roughly the same time. */ bool sendUpdate(CommandType cmd, uint8_t prevNumber, uint8_t newNumber) { USI_TWI_Master_Initialise(); uint8_t buffer[CMD_SIZE]; buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = (uint8_t)cmd; buffer[2] = prevNumber; /* We send the previous number along * with the command to make every broadcast unique--if we * did not do so then there would be no difference in the * transmissions between two counter moudules wanting to * update the current-token at nearly the same time. * * Sending the previous number is like sending the * identity of the transmitter; and it helps generate * collisions when multiple counter-modules choose to send * out the CUR_NUM command at the same time. Collisions * can be detected by all but one of the transmitters; and * hence all but one of them back off and retry later. */ buffer[3] = newNumber; bool transmitted = false; for (int i = 0; i < CMD_RETRANSMIT_COUNT; i++) { if (USI_TWI_Start_Transceiver_With_Data(buffer, sizeof(buffer))) { transmitted = true; /* transmitted at least once */ } else { if (COLLISION_DETECTED()) { return (false); /* we've noticed a transmit collision; * this arises when two distinct masters attempt * to send on the I2C at the same time. All but * one of the colliding transmitters will notice * the collision and return false from this * function. */ } } } /* reinitialize ourself as a slave */ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); return (transmitted); }
/* Inform the other modules that we are now serving currentNumber */ void sendUpdate(uint8_t currentNumber) { USI_TWI_Master_Initialise(); uint8_t buffer[CUR_NUM_CMD_SIZE]; buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = CUR_NUM_CMD; /* we are transmitting the current number */ buffer[2] = currentNumber; /* Remember that you are only transmitting it 25 times, and that you can * transmit multiple times if it makes trouble... */ for (int i = 0; i < RETRANSMIT_COUNT; i++) { USI_TWI_Start_Transceiver_With_Data(buffer, sizeof(buffer)); } /* reinitialize ourself as a slave */ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); }
void USI_TWI::begin(){ // initialize I2C lib USI_TWI_Master_Initialise(); }
int main(void) { avr_init(); /* all devices initialize themselves as slaves in TWI*/ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); /* main loop */ while(1) { if (uart_dataReceived) { // if we have received anything over UART USI_TWI_Master_Initialise(); // Initialize ourselves as TWI master uint8_t buffer[2]; buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = uart_data; /* The data we received over UART has to be sent * over TWI. */ sendUart(uart_data); while (!USI_TWI_Start_Transceiver_With_Data(buffer, 2)); /* Transmit, and * re-transmit * till * successful. */ sendUart(uart_data); uart_dataReceived = false; /* we've consumed the data from UART.. */ /* reinitialize ourself as a slave in TWI*/ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); } else if (USI_TWI_Data_In_Receive_Buffer()) { /* have we * received * anything * over TWI? */ // temporary: USI_TWI_Master_Initialise(); // Initialize ourselves as TWI master uint8_t buffer[2]; buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = 0xf3; /* The data we received over UART has to be sent * over TWI. */ while (!USI_TWI_Start_Transceiver_With_Data(buffer, 2)); /* Transmit, and * re-transmit * till * successful. */ buffer[0] = (TWI_BROADCAST_ADDRESS << TWI_ADR_BITS) | /* address */ (FALSE << TWI_READ_BIT); /* write operation */ buffer[1] = 0x08; /* The data we received over UART has to be sent * over TWI. */ while (!USI_TWI_Start_Transceiver_With_Data(buffer, 2)); /* Transmit, and * re-transmit * till * successful. */ // temporary till here uint8_t TWI_data = USI_TWI_Receive_Byte(); // Receive that data sendUart(TWI_data); // Transmit it over UART } } }