cleanup: makefile, hbc and removed unused files
[lra-as-sensor] / hbc.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <avr/io.h>
5 #include <avr/wdt.h>
6 #include <avr/sleep.h>
7 #include <avr/power.h>
8 #include <avr/interrupt.h>
9 #include <util/delay.h>
10         
11 ISR(SIG_COMPARATOR)
12 {
13         if (ACSR & (1 << ACO)) {
14                 PORTB |= (1 << PB4);
15                 /* turn of comparator on detection */
16                 ACSR &= ~(1 << ACIE);
17         } else {
18                 PORTB &= ~(1 << PB4);
19         }
20 }
21
22 EMPTY_INTERRUPT(TIM0_COMPA_vect);
23
24 static void switch_idle(void)
25 {
26         set_sleep_mode(SLEEP_MODE_IDLE);
27         sleep_mode();
28 }
29
30 static void init_board(void)
31 {
32         power_adc_disable();
33         wdt_disable();
34 }
35
36 static void set_ref_voltage(void)
37 {
38         DDRB |= (1 << DDB1); /* set PB0 as output */
39         TCCR0A = TCCR0B = 0;
40         TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
41         TCCR0B |= (1 << CS00); /* clk/1024 prescaler */
42         OCR0B = 0x20; /* 25% duty cycle */
43         TCNT0 = 0;
44         TIMSK = (1 << OCIE0B); /* enable interrupts for overflow and compare */
45 }
46
47 static void config_comparator(void)
48 {
49         ADCSRA = 0;
50         ADCSRB |= (1 << ACME); /* multiplex enable */
51         
52         /* Use admux to set alternative negative pin */
53         //ADMUX |= (1 << MUX0);
54         
55         /* enable interal reference for 
56          * comparator 
57          * When the bandgap reference is connected to the Analog Comparator (by setting the ACBG bit in ACSR). */
58         ACSR &= ~(1 << ACD); /* enable comparator */
59         ACSR |= (1 << ACBG);
60
61         DIDR0 |= (1 << AIN1D) | (1 << AIN0D);
62
63         ACSR |= (1 << ACIE); /* enable interrupts */
64 }
65
66 int main(void)
67 {
68         init_board();
69
70         DDRB |= (1 << DDB4);
71
72         cli();
73         config_comparator();
74         sei();
75         switch_idle();
76
77         while(1) {
78                 _delay_ms(100);
79         }
80
81 }