73 lines
1.3 KiB
C
73 lines
1.3 KiB
C
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/sleep.h>
|
|
#include <stdint.h>
|
|
#include "sleep.h"
|
|
|
|
uint16_t adc_schlafen() {
|
|
uint8_t value;
|
|
uint16_t sum;
|
|
//sleep_long(1000);
|
|
ADCSRA |= (1<<ADEN) | (1<<ADSC);
|
|
while(ADCSRA & (1<<ADSC)) { /* Habe Fertig */ };
|
|
value = ADCL; //LOWER First
|
|
value = ADCH; //THEN upper
|
|
sum = value;
|
|
ADCSRA &= ~(1<<ADEN);
|
|
sum *= 30;
|
|
sum += 1000;
|
|
return sum;
|
|
}
|
|
|
|
void mosfet_an() {
|
|
uint16_t sleep = adc_schlafen();
|
|
PORTB |= (1<<PB0);
|
|
sleep_long(sleep);
|
|
PORTB &= ~(1<<PB0);
|
|
}
|
|
|
|
ISR(INT0_vect)
|
|
{
|
|
_delay_ms(100);
|
|
if (!(PINB & (1<<PINB1)) ) { //Ein
|
|
PORTB |= (1<<PB3);
|
|
mosfet_an();
|
|
}
|
|
return;
|
|
//} else { //Aus
|
|
while(1) {
|
|
PORTB |= (1<<PB3);
|
|
_delay_ms(1000);
|
|
PORTB &= ~(1<<PB3);
|
|
_delay_ms(1000);
|
|
}
|
|
//}
|
|
//set_sleep_mode(SLEEP_MODE_IDLE);
|
|
//sleep_mode();
|
|
|
|
}
|
|
|
|
int main (void) {
|
|
cli();
|
|
DDRB |= (1<<PB0) | (1<<PB3);
|
|
PORTB |= (1<<PB1);
|
|
GIMSK |= (1<<INT0);
|
|
MCUCR |= (1<<ISC01);
|
|
|
|
ADMUX = 0x00;
|
|
ADMUX |= (1<<REFS0) | (1<<ADLAR) | (1<<MUX1);
|
|
sei();
|
|
while(1){
|
|
PORTB &= ~(1<<PB0);
|
|
//set_sleep_mode(SLEEP_MODE_IDLE);
|
|
//sleep_mode();
|
|
//_delay_ms(2000);
|
|
PORTB |= (1<<PB3);
|
|
_delay_ms(100);
|
|
PORTB &= ~(1<<PB3);
|
|
_delay_ms(1500);
|
|
}
|
|
return 0;
|
|
}
|