38332a180a64a9eec360a56d3f095eb79d50dbd8
[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 #include "haptic.h"
11         
12 ISR(SIG_COMPARATOR)
13 {
14         if (ACSR & (1 << ACO)) {
15                 PORTB |= (1 << PB4);
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         /* enable interal reference for 
35          * comparator 
36          * When the bandgap reference is connected to the Analog Comparator (by setting the ACBG bit in ACSR). */
37 }
38
39 static void set_ref_voltage(void)
40 {
41         DDRB |= (1 << DDB1); /* set PB0 as output */
42         TCCR0A = TCCR0B = 0;
43         TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
44         TCCR0B |= (1 << CS00); /* clk/1024 prescaler */
45         OCR0B = 0x20; /* 25% duty cycle */
46         TCNT0 = 0;
47         TIMSK = (1 << OCIE0B); /* enable interrupts for overflow and compare */
48 }
49
50 static void config_comparator(void)
51 {
52         ADCSRA = 0;
53         ADCSRB |= (1 << ACME); /* multiplex enable */
54         
55         //ADMUX |= (1 << MUX0); /* ADC1 as negative input */
56         
57         ACSR &= ~(1 << ACD); /* enable comparator */
58         ACSR |= (1 << ACBG);
59         //ACSR |= (1 << ACIS1) | (1 << ACIS0);
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         //PORTB |= (1 << PB1);
72
73         cli();
74         //set_ref_voltage();
75         config_comparator();
76         sei();
77         switch_idle();
78
79         while(1) {
80                 _delay_ms(100);
81         }
82
83 }