+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
#include <avr/io.h>
+#include <avr/wdt.h>
+#include <avr/sleep.h>
+#include <avr/power.h>
#include <avr/interrupt.h>
+#include <util/delay.h>
+
+ISR(SIG_COMPARATOR)
+{
+ if (ACSR & (1 << ACO)) {
+ PORTB |= (1 << PB4);
+ /* turn of comparator on detection */
+ ACSR &= ~(1 << ACIE);
+ } else {
+ PORTB &= ~(1 << PB4);
+ }
+}
-#include <stdio.h>
-#include <stdlib.h>
+EMPTY_INTERRUPT(TIM0_COMPA_vect);
-#include <util/delay.h>
+static void switch_idle(void)
+{
+ set_sleep_mode(SLEEP_MODE_IDLE);
+ sleep_mode();
+}
-int main(void) {
+static void init_board(void)
+{
+ power_adc_disable();
+ wdt_disable();
+}
+
+static void set_ref_voltage(void)
+{
+ DDRB |= (1 << DDB1); /* set PB0 as output */
+ TCCR0A = TCCR0B = 0;
+ TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
+ TCCR0B |= (1 << CS00); /* clk/1024 prescaler */
+ OCR0B = 0x20; /* 25% duty cycle */
+ TCNT0 = 0;
+ TIMSK = (1 << OCIE0B); /* enable interrupts for overflow and compare */
+}
+
+static void config_comparator(void)
+{
+ ADCSRA = 0;
+ ADCSRB |= (1 << ACME); /* multiplex enable */
+ /* Use admux to set alternative negative pin */
+ //ADMUX |= (1 << MUX0);
- DDRB |= (1 << DDB3);
- PORTB = (1 << PB3);
+ /* enable interal reference for
+ * comparator
+ * When the bandgap reference is connected to the Analog Comparator (by setting the ACBG bit in ACSR). */
+ ACSR &= ~(1 << ACD); /* enable comparator */
+ ACSR |= (1 << ACBG);
+
+ DIDR0 |= (1 << AIN1D) | (1 << AIN0D);
+
+ ACSR |= (1 << ACIE); /* enable interrupts */
+}
+
+int main(void)
+{
+ init_board();
+
+ DDRB |= (1 << DDB4);
+
+ cli();
+ config_comparator();
+ sei();
+ switch_idle();
while(1) {
- _delay_ms(2);
- PORTB = 0;
- _delay_ms(2);
- PORTB = (1 << PB3);
+ _delay_ms(100);
}
- return 0;
-
}