5ff04c791b846c571606a90adef96a5cd0020aa7
[lra-as-sensor] / hbc.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <avr/io.h>
4 #include <avr/wdt.h>
5 #include <avr/sleep.h>
6 #include <avr/power.h>
7 #include <avr/interrupt.h>
8 #include <util/delay.h>
9 #include "haptic.h"
10         
11 volatile int xvar = 1;
12
13 /* idle mode will wake up board and will
14  * return PC after latest sleep_mode() call */
15 EMPTY_INTERRUPT(SIG_COMPARATOR);
16
17 ISR(TIM0_COMPA_vect)
18 {       
19         xvar++;
20 }
21 ISR(TIM0_COMPB_vect)
22 {
23         xvar++;
24 }
25
26 ISR(TIM0_OVF_vect)
27 {
28         //xvar++;
29 }
30
31
32 static void switch_idle(void)
33 {
34         sei();
35         set_sleep_mode(SLEEP_MODE_IDLE);
36         sleep_mode();
37 }
38
39 static void init_board(void)
40 {
41         power_adc_disable();
42         wdt_disable();
43         /* enable interal reference for 
44          * comparator 
45          * When the bandgap reference is connected to the Analog Comparator (by setting the ACBG bit in ACSR). */
46 }
47
48 int main(void)
49 {
50         init_board();
51         xvar = 1;       
52         cli();
53         DDRB |= (1 << DDB0); /* set PB0 as output */
54         TCCR0A = TCCR0B = 0;
55         TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0A1);
56         TCCR0B |= (1 << CS02) | (1 << CS00); /* clk/1024 prescaler */
57         OCR0A = 0x7F; /* 50% duty cycle */
58         TCNT0 = 0;
59         TIMSK = (1 << OCIE0A) | (1 << OCIE0B) | (1 << TOIE0); /* enable interrupts for overflow and compare */
60         sei();
61
62         while(1) {
63                 _delay_ms(100);
64                 /* set LRA as sensor and switch to idle mode
65                  * check back-EMF of LRA: if treshold is surpassed
66                  * comparator intterupt is triggered */
67 //              set_lra_as_sensor();
68 //              switch_idle(); 
69 //
70 //              /* board wakes up, set LRA as actuator and
71 //               * plays haptic effect */
72 //              set_lra_as_actuator();
73 //              play_haptic();
74
75         }
76
77 }