|
|
@ -3,30 +3,24 @@
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define I2C_ADDR 0x20
|
|
|
|
#define I2C_ADDR 0x30
|
|
|
|
|
|
|
|
|
|
|
|
volatile uint8_t challenge = 0; // Use 0 as 'challenge not sent' condition
|
|
|
|
volatile uint8_t challenge = 0; // Use 0 as 'challenge not sent' condition
|
|
|
|
|
|
|
|
uint16_t time_open = 0;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t check_challenge(uint8_t arg) {
|
|
|
|
uint8_t check_challenge(uint8_t arg) {
|
|
|
|
return (arg >> 3) ^ (arg << 3);
|
|
|
|
return (arg >> 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void I2C_received(uint8_t received_data) {
|
|
|
|
void I2C_received(uint8_t received_data) {
|
|
|
|
if (challenge != 0) {
|
|
|
|
if (received_data == check_challenge(challenge)) { // Check validity of response
|
|
|
|
if (check_challenge(received_data) == check_challenge(challenge)) { // Check validity of response
|
|
|
|
|
|
|
|
// Open lock
|
|
|
|
// Open lock
|
|
|
|
PORTC |= 2 << 1;
|
|
|
|
time_open = 5000;
|
|
|
|
_delay_ms(5000);
|
|
|
|
|
|
|
|
PORTC = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void I2C_requested() {
|
|
|
|
void I2C_requested() {
|
|
|
|
challenge = (uint8_t)(TCNT1 & 0xff); // Use content of timer as pseudo-random number
|
|
|
|
challenge = (uint8_t)(TCNT1 & 0xff);
|
|
|
|
if (challenge == 0) // Prevent 'challenge' from being 0, which has a special meaning
|
|
|
|
|
|
|
|
challenge = 1;
|
|
|
|
|
|
|
|
I2C_transmitByte(challenge);
|
|
|
|
I2C_transmitByte(challenge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -43,5 +37,13 @@ void setup() {
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int main() {
|
|
|
|
setup();
|
|
|
|
setup();
|
|
|
|
while (1); // All the work is done in the callbacks
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
if (time_open > 0) {
|
|
|
|
|
|
|
|
PORTC |= 2 << 1;
|
|
|
|
|
|
|
|
time_open -= 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
PORTC = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_delay_ms(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|