add analog comparator

This commit is contained in:
King Kévin 2015-01-06 16:26:30 +01:00
parent 01dc62a713
commit 97f3283118
2 changed files with 30 additions and 18 deletions

View File

@ -30,7 +30,7 @@
#include "main.h" // main definitions
/* variables */
volatile uint8_t stateD; // the state of port D
volatile uint8_t state_portD; // the state of port D
/* switch off LED */
void led_off(void)
@ -51,30 +51,30 @@ void led_toggle(void)
}
/* scale on interrupt
PCI2 Interrupt Vector for PCINT[23:16]/PORTD
*/
* PCI2 Interrupt Vector for PCINT[23:16]/PORTD
*/
ISR(PCINT2_vect)
{
if ((stateD&(1<<SCALE_ON))!=(PIND&(1<<SCALE_ON))) { // scale on state changed
if ((state_portD&(1<<SCALE_ON))!=(PIND&(1<<SCALE_ON))) { // scale on state changed
if (PIND&(1<<SCALE_ON)) {
led_on();
} else {
led_off();
}
}
stateD = PIND; // save new state
state_portD = PIND; // save new state
}
/* timer 1 OCR1A match interrupt
* use it to toogle LED
/* PWM weight signal
* analog comparator interrupt
*/
ISR(TIMER1_COMPA_vect)
ISR(ANALOG_COMP_vect)
{
/*
if (blink_flag) {
led_toggle();
if (ACSR&(1<<ACO)) { // PWM high
led_on();
} else { // PWM low
led_off();
}
*/
}
/* disable watchdog when booting */
@ -99,14 +99,25 @@ void io_init(void)
/* LED */
DDRB |= (1<<LED); // LED is driven by pin (set as output)
led_off();
/* scale on signal */
DDRD &= ~(1<<SCALE_ON); // SIGNAL_ON is input (should be per default)
PCIFR &= ~(1<<PCIF2); // clear interrupt flag for SCALE_ON on PCINT[23:16]/PORTD
PCICR |= (1<<PCIE2); // enable interrupt for PCINT[23:16]/PORTD
PCMSK2 |= (1<<PCINT21); // enable interrupt for SCALE_ON
stateD = PIND; // save state to detect changes
state_portD = PIND; // save state to detect change
/* PWM scale weight signal on analog comparator */
DDRD &= ~((1<<SCALE_PWM)||(1<<REF_PWM)); // analog comparator is input (should be per default)
DDRD &= ~((1<<SCALE_PWM)|(1<<REF_PWM)); // analog comparator is input (should be per default)
ADCSRA &= ~(1<<ADEN); // switch off ADC
ADCSRB &= ~(1<<ACME); // disable analog comparator multiplexer, use AIN1 as negative input (should be per default)
ACSR &= ~(1<<ACD); // enable analog comparator (should be per default)
ACSR &= ~(1<<ACBG); // use AIN0 as positiv input (should be per default)
ACSR |= (1<<ACI); // clear analog comparator interrup flag
ACSR |= (1<<ACIC); // enable analog comparator input capture (on timer 1)
ACSR &= ~((1<<ACIS1)|(1<<ACIS0)); // comparator interrupt on output toggle
DIDR1 |= ((1<<AIN1D)|(1<<AIN0D)); // disable digital input buffer on AIN0 and AIN1
ACSR |= (1<<ACIE); // enable analog comparator interrupt
sei(); /* enable interrupts */
}

View File

@ -25,15 +25,16 @@
*/
#define SCALE_ON PD5
/* PWM weight signal
* high time defines the weight (propotionnally)
* low time is constant
* signal level is 500mV
* PWM frequency is ~3Hz
* high time increases with increasing weight (propotionnally)
* low time decreases with increasing weight
* signal level is 1V
* positive pin of analog comparator used to measure PWM
* pin: PD6, AIN0, D6
*/
#define SCALE_PWM PD6
/* negative pin of analog comparator
* set at 100-400mV using voltage divider
* set at 0.2-0.8V using voltage divider
* used to detect PWM signal change
* pin: PD7, AIN1, D7
*/