void sleep_ms(uint32_t ms) { __sleep(ms_to_ticks(ms)); }
void main( void ) { unsigned char messageBuf[TWI_BUFFER_SIZE]; unsigned char TWI_slaveAddress, TWI_slaveAddress2, TWI_slaveAddressMask; // Feedback (opcional) /* DDRB = 0xFF; // Set to output PORTB = 0x55; // Startup pattern */ DDRC = ~((1 << 0) | (1 << 1) | (1 << 2)); //Bits 0-2 como entrada // Own TWI slave address TWI_slaveAddress = (0x10<<TWI_ADR_BITS); TWI_slaveAddress2 = (0x11<<TWI_ADR_BITS); // Alternativ slave address to respond to. TWI_slaveAddressMask = TWI_slaveAddress ^ TWI_slaveAddress2; // XOR the addresses to get the address mask. // Initialise TWI module for slave operation. Include address and/or enable General Call. TWI_Slave_Initialise( (unsigned char)(TWI_slaveAddress | (TRUE<<TWI_GEN_BIT)), TWI_slaveAddressMask); __enable_interrupt(); // Start the TWI transceiver to enable reseption of the first command from the TWI Master. TWI_Start_Transceiver(); // This example is made to work together with the AVR315 TWI Master application note. In adition to connecting the TWI // pins, also connect PORTB to the LEDS. The code reads a message as a TWI slave and acts according to if it is a // general call, or an address call. If it is an address call, then the first byte is considered a command byte and // it then responds differently according to the commands. // This loop runs forever. If the TWI is busy the execution will just continue doing other operations. for(;;) { #ifdef POWER_MANAGEMENT_ENABLED // Sleep while waiting for TWI transceiver to complete or waiting for new commands. // If we have data in the buffer, we can't enter sleep because we have to take care // of it first. // If the transceiver is busy, we enter idle mode because it will wake up by all TWI // interrupts. // If the transceiver not is busy, we can enter power-down mode because next receive // should be a TWI address match and it wakes the device up from all sleep modes. if( ! TWI_statusReg.RxDataInBuf ) { if(TWI_Transceiver_Busy()) { MCUCR = (1<<SE)|(0<<SM2)|(0<<SM1)|(0<<SM0); // Enable sleep with idle mode } else { MCUCR = (1<<SE)|(0<<SM2)|(1<<SM1)|(0<<SM0); // Enable sleep with power-down mode } __sleep(); } else { __no_operation(); // There is data in the buffer, code below takes care of it. } #else // No power management // Here you can add your own code that should be run while waiting for the TWI to finish __no_operation(); // Put own code here. #endif // Check if the TWI Transceiver has completed an operation. if ( ! TWI_Transceiver_Busy() ) { // Check if the last operation was successful if ( TWI_statusReg.lastTransOK ) { // Check if the last operation was a reception if ( TWI_statusReg.RxDataInBuf ) { TWI_Get_Data_From_Transceiver(messageBuf, 2); // Check if the last operation was a reception as General Call if ( TWI_statusReg.genAddressCall ) { // Put data received out to PORTB as an example. PORTB = messageBuf[0]; } else // Ends up here if the last operation was a reception as Slave Address Match { // Example of how to interpret a command and respond. // TWI_CMD_MASTER_WRITE stores the data to PORTB if (messageBuf[0] == TWI_CMD_MASTER_WRITE) { PORTB = messageBuf[1]; } // TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch. if (messageBuf[0] == TWI_CMD_MASTER_READ) { messageBuf[0] = PINB; TWI_Start_Transceiver_With_Data( messageBuf, 1 ); } } } else // Ends up here if the last operation was a transmission { __no_operation(); // Put own code here. } // Check if the TWI Transceiver has already been started. // If not then restart it to prepare it for new receptions. if ( ! TWI_Transceiver_Busy() ) { TWI_Start_Transceiver(); } } else // Ends up here if the last operation completed unsuccessfully { TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() ); } } } }