From: Robin Krens Date: Fri, 20 May 2022 12:30:50 +0000 (+0200) Subject: hbc: sensor based on back emf X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=5a12804f1173a21db44a671e2c74be9c6e76f394;p=lra-as-sensor hbc: sensor based on back emf - voltage divider for ~1.6V - AIN0 and AIN1: AIN1 has LRA and checks if treshold is above 1.6 --- diff --git a/hbc.c b/hbc.c index 6080410..c5c2c0d 100644 --- a/hbc.c +++ b/hbc.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -8,17 +9,17 @@ #include #include "haptic.h" -volatile int xvar = 1; - -/* idle mode will wake up board and will - * return PC after latest sleep_mode() call */ -EMPTY_INTERRUPT(SIG_COMPARATOR); - -ISR(TIM0_COMPA_vect) -{ - xvar++; +ISR(SIG_COMPARATOR) +{ + if (ACSR & (1 << ACO)) { + PORTB |= (1 << PB4); + } else { + PORTB &= ~(1 << PB4); + } } +EMPTY_INTERRUPT(TIM0_COMPA_vect); + static void switch_idle(void) { sei(); @@ -35,33 +36,47 @@ static void init_board(void) * When the bandgap reference is connected to the Analog Comparator (by setting the ACBG bit in ACSR). */ } +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 */ + + //ADMUX |= (1 << MUX0); /* ADC1 as negative input */ + + ACSR &= ~(1 << ACD); /* enable comparator */ + //ACSR |= (1 << ACBG); + //ACSR |= (1 << ACIS1) | (1 << ACIS0); + + DIDR0 |= (1 << AIN1D) | (1 << AIN0D); + + ACSR |= (1 << ACIE); /* enable interrupts */ +} + int main(void) { init_board(); - xvar = 1; + + DDRB |= (1 << DDB4); + //PORTB |= (1 << PB1); + cli(); - DDRB |= (1 << DDB0); /* set PB0 as output */ - TCCR0A = TCCR0B = 0; - TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0A1); - TCCR0B |= (1 << CS02) | (1 << CS00); /* clk/1024 prescaler */ - OCR0A = 0x7F; /* 50% duty cycle */ - TCNT0 = 0; - TIMSK = (1 << OCIE0A); /* enable interrupts for overflow and compare */ + //set_ref_voltage(); + config_comparator(); sei(); while(1) { _delay_ms(100); - /* set LRA as sensor and switch to idle mode - * check back-EMF of LRA: if treshold is surpassed - * comparator intterupt is triggered */ -// set_lra_as_sensor(); -// switch_idle(); -// -// /* board wakes up, set LRA as actuator and -// * plays haptic effect */ -// set_lra_as_actuator(); -// play_haptic(); - } }