1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00
doorlockd-mirror/avr-code/main.c

144 lines
2.5 KiB
C
Raw Normal View History

2015-05-14 22:03:00 +02:00
/* 2015, Ralf Ramsauer
* ralf@binary-kitchen.de
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <string.h>
#define GREEN_ON (PORTB &= ~(1<<PB1))
#define GREEN_OFF (PORTB |= (1<<PB1))
#define RED_ON (PORTB &= ~(1<<PB0))
#define RED_OFF (PORTB |= (1<<PB0))
#define DOORLIGHT_ON (PORTD |= (1<<PD6))
#define DOORLIGHT_OFF (PORTD &= ~(1<<PD6))
2015-05-20 15:31:13 +02:00
2015-05-14 22:03:00 +02:00
#define OPEN_BOLZEN (PORTD &= ~(1<<PD4))
#define CLOSE_BOLZEN (PORTD |= (1<<PD4))
#define OPEN_SCHNAPPER (PORTD |= (1<<PD5))
#define CLOSE_SCHNAPPER (PORTD &= ~(1<<PD5))
#define IS_RESCUE (!(PIND & (1<<PD3)))
#define IS_SWITCH (!(PIND & (1<<PD2)))
2015-05-20 22:42:20 +02:00
static uint8_t open = 0;
static uint8_t klacker = 0;
2015-05-14 22:03:00 +02:00
void open_door()
{
RED_OFF;
GREEN_ON;
OPEN_BOLZEN;
2015-05-20 15:31:13 +02:00
DOORLIGHT_ON;
2015-05-20 22:42:20 +02:00
}
void close_door()
{
RED_ON;
GREEN_OFF;
DOORLIGHT_OFF;
CLOSE_SCHNAPPER;
CLOSE_BOLZEN;
2015-05-14 22:03:00 +02:00
}
void schnapper(unsigned int i)
{
2015-06-08 21:49:19 +02:00
OPEN_SCHNAPPER;
while (i--) {
_delay_ms(1000);
2015-05-14 22:03:00 +02:00
}
2015-06-08 21:49:19 +02:00
CLOSE_SCHNAPPER;
_delay_ms(100);
2015-05-14 22:03:00 +02:00
}
ISR(USI_OVERFLOW_vect)
{
2015-05-20 22:42:20 +02:00
// Clear interrupt flag
USISR |= (1<<USIOIF);
// Reset counter
2015-05-14 22:03:00 +02:00
TCNT1 = 0;
2015-05-20 22:42:20 +02:00
open = 1;
uint8_t in = USIDR;
// Depending on the clock, a klacker either can be 10101010 or 01010101
if (in == 0x55 || in == 0xAA)
{
klacker = 1;
}
2015-05-14 22:03:00 +02:00
}
ISR(TIMER1_OVF_vect)
{
open = 0;
}
int main(void) {
// disable all interrupts
cli();
2015-05-20 22:42:20 +02:00
// PD4-6 as Output
2015-05-20 15:31:13 +02:00
DDRD = (1<<PD5)|(1<<PD4)|(1<<PD6);
2015-05-20 22:42:20 +02:00
// PD2,3 Pullup
PORTD |= (1<<PD2)|(1<<PD3);
2015-05-14 22:03:00 +02:00
2015-05-20 22:42:20 +02:00
// PB0-1 as Output
2015-05-14 22:03:00 +02:00
DDRB = (1<<PB0)|(1<<PB1);
2015-05-20 22:42:20 +02:00
// PB5,7 Pullup
2015-05-14 22:03:00 +02:00
PORTB |= (1<<PB7)|(1<<PB5);
2015-05-20 22:42:20 +02:00
// first action: close door
close_door();
2015-05-14 22:03:00 +02:00
2015-05-20 22:42:20 +02:00
// Configure USI
2015-05-14 22:03:00 +02:00
USICR = (1<<USICS1) | (1<<USIOIE) | (1<<USIWM0);
2015-05-20 22:42:20 +02:00
// Configure Timer
2015-05-14 22:03:00 +02:00
TIMSK |= (1<<TOIE1);
TIFR |= (1<<TOV1);
TCNT1 = 0;
TCCR1A = 0;
TCCR1B = (1<<CS01)|(1<<CS00);
2015-05-20 22:42:20 +02:00
// wait a bit to settle down
2015-05-14 22:03:00 +02:00
_delay_ms(1000);
2015-05-20 22:42:20 +02:00
// enable interrupts
2015-05-14 22:03:00 +02:00
sei();
for(;;) {
if (open == 0) {
close_door();
} else if (open == 1) {
open_door();
2015-05-20 22:42:20 +02:00
if (klacker == 1)
{
2015-06-08 21:49:19 +02:00
schnapper(3);
2015-05-20 22:42:20 +02:00
klacker = 0;
}
}
2015-05-14 22:03:00 +02:00
if (IS_RESCUE) {
cli();
open_door();
2015-06-08 21:49:19 +02:00
schnapper(3);
2015-05-14 22:03:00 +02:00
sei();
} else if (IS_SWITCH && open == 0) {
_delay_ms(300);
if(IS_SWITCH && open == 0) {
cli();
open_door();
2015-06-08 21:49:19 +02:00
schnapper(5);
2015-05-14 22:03:00 +02:00
sei();
}
}
_delay_ms(50);
}
return 0;
}