use timer1 icp

This commit is contained in:
Olaf Rempel 2008-06-22 23:46:52 +02:00
parent d05f1de3b1
commit e4f6fd2d36
2 changed files with 23 additions and 18 deletions

34
blmc.c
View File

@ -31,8 +31,8 @@ static uint8_t next_sense;
void trigger_adc(uint8_t channel) void trigger_adc(uint8_t channel)
{ {
/* Disable Analog Comperator */ /* Disable Analog Comperator Interrupt */
ACSR &= ~(1<<ACIE); TIMSK1 &= ~(1<<ICIE1);
/* set channel (external reference, 5V) */ /* set channel (external reference, 5V) */
ADMUX = (0<<REFS1) | (0<<REFS0) | channel; ADMUX = (0<<REFS1) | (0<<REFS0) | channel;
@ -50,8 +50,8 @@ void next_phase(void)
static uint8_t phase; static uint8_t phase;
static uint8_t phase_adc; static uint8_t phase_adc;
/* Disable Analog Comperator */ /* Disable Analog Comperator Interrupt */
ACSR &= ~(1<<ACIE); TIMSK1 &= ~(1<<ICIE1);
TCNT0 = 0x00; TCNT0 = 0x00;
TCNT2 = 0x00; TCNT2 = 0x00;
@ -63,7 +63,7 @@ void next_phase(void)
PORTD |= PHASE_B_EN; PORTD |= PHASE_B_EN;
/* C: falling edge */ /* C: falling edge */
ACSR = (1<<ACIS1); TCCR1B &= ~(1<<ICES1);
next_sense = SENSE_C; next_sense = SENSE_C;
break; break;
@ -73,7 +73,7 @@ void next_phase(void)
PORTD |= PHASE_C_EN; PORTD |= PHASE_C_EN;
/* A: rising edge */ /* A: rising edge */
ACSR = (1<<ACIS1) | (1<<ACIS0); TCCR1B |= (1<<ICES1);
next_sense = SENSE_A; next_sense = SENSE_A;
break; break;
@ -83,7 +83,7 @@ void next_phase(void)
PORTB |= PHASE_A_EN; PORTB |= PHASE_A_EN;
/* B: falling edge */ /* B: falling edge */
ACSR = (1<<ACIS1); TCCR1B &= ~(1<<ICES1);
next_sense = SENSE_B; next_sense = SENSE_B;
break; break;
@ -93,7 +93,7 @@ void next_phase(void)
PORTD |= PHASE_B_EN; PORTD |= PHASE_B_EN;
/* C: rising edge */ /* C: rising edge */
ACSR = (1<<ACIS1) | (1<<ACIS0); TCCR1B |= (1<<ICES1);
next_sense = SENSE_C; next_sense = SENSE_C;
break; break;
@ -103,7 +103,7 @@ void next_phase(void)
PORTD |= PHASE_C_EN; PORTD |= PHASE_C_EN;
/* A: falling edge */ /* A: falling edge */
ACSR = (1<<ACIS1); TCCR1B &= ~(1<<ICES1);
next_sense = SENSE_A; next_sense = SENSE_A;
break; break;
@ -113,7 +113,7 @@ void next_phase(void)
PORTB |= PHASE_A_EN; PORTB |= PHASE_A_EN;
/* B: rising edge */ /* B: rising edge */
ACSR = (1<<ACIS1) | (1<<ACIS0); TCCR1B |= (1<<ICES1);
next_sense = SENSE_B; next_sense = SENSE_B;
break; break;
} }
@ -136,8 +136,10 @@ void next_phase(void)
ADMUX = next_sense; ADMUX = next_sense;
/* enable Analog Comparator with Interrupts */ /* enable Analog Comparator with Interrupts */
if (blmc.flags & FLAG_COM_NORMAL) if (blmc.flags & FLAG_COM_NORMAL) {
ACSR |= (1<<ACIE) | (1<<ACI); TIFR1 |= (1<<ICF1);
TIMSK1 |= (1<<ICIE1);
}
} }
phase++; phase++;
@ -246,7 +248,7 @@ void setpwm(uint8_t pwm)
} }
} }
ISR(ANALOG_COMP_vect) ISR(TIMER1_CAPT_vect)
{ {
next_phase(); next_phase();
} }
@ -266,8 +268,10 @@ ISR(ADC_vect)
ADCSRA = 0x00; ADCSRA = 0x00;
/* enable Analog Comparator with Interrupts */ /* enable Analog Comparator with Interrupts */
if (blmc.flags & FLAG_COM_NORMAL) if (blmc.flags & FLAG_COM_NORMAL) {
ACSR |= (1<<ACIE) | (1<<ACI); TIFR1 |= (1<<ICF1);
TIMSK1 |= (1<<ICIE1);
}
if (channel == SENSE_CURRENT) { if (channel == SENSE_CURRENT) {
current_tmp += value; current_tmp += value;

7
main.c
View File

@ -45,13 +45,13 @@ extern struct blmc_ blmc;
static uint8_t led[2]; static uint8_t led[2];
ISR(TIMER1_OVF_vect) ISR(TIMER1_COMPB_vect)
{ {
static uint8_t rpm_cnt = 0; static uint8_t rpm_cnt = 0;
static uint8_t adc_chan = SENSE_CURRENT; static uint8_t adc_chan = SENSE_CURRENT;
/* Come back in 20ms */ /* Come back in 20ms */
TCNT1 = (0xFFFF - 20000); OCR1B = TCNT1 + 20000;
/* commutations during last 20ms */ /* commutations during last 20ms */
uint16_t diff = blmc.rpm_tmp - blmc.rpm_tmp_old; uint16_t diff = blmc.rpm_tmp - blmc.rpm_tmp_old;
@ -158,10 +158,11 @@ int main(void)
TCCR2B = (1<<CS20); TCCR2B = (1<<CS20);
/* enable Timer1 OVF Interrupt */ /* enable Timer1 OVF Interrupt */
TIMSK1 = (1<<TOIE1); TIMSK1 = (1<<OCIE1B);
/* Enable Analog Comparator Multiplexer */ /* Enable Analog Comparator Multiplexer */
ADCSRB |= (1<<ACME); ADCSRB |= (1<<ACME);
ACSR |= (1<<ACIC);
/* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */ /* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */
TWCR = (1<<TWEA) | (1<<TWEN) | (1<<TWIE); TWCR = (1<<TWEA) | (1<<TWEN) | (1<<TWIE);