#define F_CPU 8000000UL #include "../avr-i2c-slave/I2CSlave.h" #include #include #include #define BAUDRATE 9600 #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1) #define I2C_ADDR 0x20 void USART_init(void){ UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8); UBRR0L = (uint8_t)(BAUD_PRESCALLER); UCSR0B = (1<> 3); } void I2C_received(uint8_t received_data) { uint8_t solution; char message[100] = {0}; solution = check_challenge(challenge); sprintf(message, "Response: %d. Expected: %d\n", received_data, solution); USART_putstring(message); if (received_data == solution) { // Check validity of response // Open lock time_open = 5000; } } void I2C_requested() { challenge = (uint8_t)(TCNT1 & 0xff); I2C_transmitByte(challenge); char message[100] = {0}; sprintf(message, "Request received. Sending %d\n", challenge); USART_putstring(message); } void setup() { // set received/requested callbacks I2C_setCallbacks(I2C_received, I2C_requested); // init I2C I2C_init(I2C_ADDR); // init output gpio DDRC = 0b00000100; // PC2 TCNT1 = 0; TCCR1B |= (1 << CS10) | (1 << CS11); // No prescaling } int main() { int i = 0; setup(); USART_init(); while (1) { i++; if (time_open > 0) { PORTC |= 2 << 1; time_open -= 1; } else { PORTC = 0; } _delay_ms(1); if (i > 1000) { char message[] = "Alive\n"; USART_putstring(message); i = 0; } } }