__interrupt void USCI0RX_ISR(void) { switch (working_mode) { case MODE_PC_CONN_WAIT: switch (UCA0RXBUF) { case PC_REQUEST_CONNECT: sendUart(DEVICE_RESPONCE_CONNECTED); working_mode = MODE_PC_CONN_ACTIVE; sendUartString("Connected...\r\n", 14); break; default: sendUartString("Awaiting connection...\r\n", 24); break; } break; case MODE_PC_CONN_ACTIVE: switch (UCA0RXBUF) { case PC_REQUEST_GET_DATA: sendUart('T'); break; case PC_REQUEST_DISCONNECT: working_mode = MODE_PC_CONN_WAIT; sendUartString("Disconnected.\r\n", 15); break; default: sendUartString("Unknown request code.\r\n", 23); break; } break; default: break; } }
int main(void) { ioinit(); timer_init(); /* all devices initialize themselves as slaves */ USI_TWI_Slave_Initialise(TWI_BROADCAST_ADDRESS); #if WE_NEED_TO_TEST_UART /* for debugging only */ uint8_t counter = 0; while (true) { ++counter; sendUart('a' + (counter % 26)); _delay_ms(1000); } #endif /* #if WE_NEED_TO_TEST_UART */ /* loop forever */ for (;; ) { uint8_t byte = USI_TWI_Receive_Byte(); sendUart(byte); } /* we will never come here. */ return (0); }
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 } } }