Good day.
I'm trying to build my own programmable conditioning stage that can be added to simple CDI units for motorcycles.
Having an oscilloscope and signal generator is what I can think most useful tool for this project but Affordability is my main issue.
That is why I would like to have anyone's opinion/ideas/comments to further improve my code / algorithm.
I will be using ATTINY85 and my target RPM if possible is from 500 (or lower) to 16000 rpm (or higher).
Below is my code.
Regards,
Tony
Code: Select all
unsigned short int pulse=0;
unsigned long int tick=0, ovf=0; delay=0; //variables will be optimized later.
void main() {
DDRB = ( 1 << DDB3 ) | ( 0 << DDB2);
PORTB = ( 0 << PORTB3 );
MCUCR = ( 1 << ISC01 ) | ( 1 << ISC00 ) ; //detect rising edge
TCCR1 = ( 1 << CS13 ) | ( 1 << CS12 ) | ( 0 << CS11 ) | ( 1 << CS10 ) ;
GIMSK = ( 1 << INT0 ) ; //enable INTO interrupt
SREG_I_bit = 1; //enable Global Interrupt
while(1){
}
}
void interrupt_INT0() iv IVT_ADDR_INT0 ics ICS_OFF{ // Interrupt INT0 runtine
if(pulse==1){
tick=ovf+TCNT1;
TCNT1 = 0;
for( delay=0; delay <=tick; delay++){ // waiting time before spark
Delay_ms(10); // tick will be mapped to EEPROM table values or to Variable array..
}
PORTB = (1 << PORTB3); // spark on
Delay_us(200); // spark on time
PORTB = (0 << PORTB3); // spark off
tick=0;
ovf=0;
TCNT=0;
}
if(pulse==0){ // 1st sensor triggered
pulse=1;
tick=0;
TIMSK = ( 1 << TOIE1 ) ; // TIMER1 ENABLE AND START
TCNT1 = 0;
}
}
void interrupt_TIMER1() iv IVT_ADDR_TIMER1_OVF ics ICS_OFF{ // TIMER1 overflows. If sensor not being triggered.
ovf=ovf+255; //since TCNT1 is declared to 0.
TCNT1=0;
}