Almost working version

master
Davide Bongiovanni 7 years ago
parent d3fb7aae27
commit cd61812c31

@ -3,30 +3,24 @@
#include <avr/io.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
uint16_t time_open = 0;
uint8_t check_challenge(uint8_t arg) {
return (arg >> 3) ^ (arg << 3);
return (arg >> 3);
}
void I2C_received(uint8_t received_data) {
if (challenge != 0) {
if (check_challenge(received_data) == check_challenge(challenge)) { // Check validity of response
// Open lock
PORTC |= 2 << 1;
_delay_ms(5000);
PORTC = 0;
}
if (received_data == check_challenge(challenge)) { // Check validity of response
// Open lock
time_open = 5000;
}
challenge = 0;
}
void I2C_requested() {
challenge = (uint8_t)(TCNT1 & 0xff); // Use content of timer as pseudo-random number
if (challenge == 0) // Prevent 'challenge' from being 0, which has a special meaning
challenge = 1;
challenge = (uint8_t)(TCNT1 & 0xff);
I2C_transmitByte(challenge);
}
@ -43,5 +37,13 @@ void setup() {
int main() {
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);
}
}

@ -5,7 +5,7 @@
# (GNU make, BSD make, SysV make)
MCU = atmega328p
MCU = atmega88
FORMAT = ihex
TARGET = main
SRC = $(TARGET).c \

Loading…
Cancel
Save