void sim_check_interrupts() { unsigned int ticks = SDL_GetTicks(); int tickDiff = ticks - prevTicks; prevTicks = ticks; if (!(SREG & _BV(SREG_I))) return; #ifdef ENABLE_ULTILCD2 if ((TWCR & _BV(TWEN)) && (TWCR & _BV(TWINT)) && (TWCR & _BV(TWIE))) { //Relay the TWI interrupt by 25ms one time till it gets disabled again. This fakes the LCD refresh rate. if (twiIntStart == 0) twiIntStart = SDL_GetTicks(); if (SDL_GetTicks() - twiIntStart > 25) { cli(); TWI_vect(); _sei(); } } if (!(TWCR & _BV(TWEN)) || !(TWCR & _BV(TWIE))) { twiIntStart = 0; } #endif //if (tickDiff > 1) // printf("Ticks slow! %i\n", tickDiff); if (tickDiff > 0) { ms_callback(); cli(); for(int n=0;n<tickDiff;n++) { if (TIMSK0 & _BV(OCIE0B)) TIMER0_COMPB_vect(); if (TIMSK0 & _BV(TOIE0)) TIMER0_OVF_vect(); } //Timer1 runs at 16Mhz / 8 ticks per second. unsigned int waveformMode = ((TCCR1B & (_BV(WGM13) | _BV(WGM12))) >> 1) | (TCCR1A & (_BV(WGM11) | _BV(WGM10))); unsigned int clockSource = TCCR1B & (_BV(CS12) | _BV(CS11) | _BV(CS10)); unsigned int tickCount = F_CPU * tickDiff / 1000; unsigned int ticks = TCNT1; switch(clockSource) { case 0: tickCount = 0; break; case 1: break; case 2: tickCount /= 8; break; case 3: tickCount /= 64; break; case 4: tickCount /= 256; break; case 5: tickCount /= 1024; break; case 6: tickCount = 0; break; case 7: tickCount = 0; break; } tickCount *= 4;//For some reason the stepper speed is to slow, so cheat the timer routine. if (tickCount > 0 && OCR1A > 0) { ticks += tickCount; while(ticks > int(OCR1A)) { ticks -= int(OCR1A); if (TIMSK1 & _BV(OCIE1A)) TIMER1_COMPA_vect(); } TCNT1 = ticks; } _sei(); }
void callTimer(int times = 1){ // if(!(SREG & _BV(7))) // return; // interrupts not enabled for(int i=0; i<times; ++i) TIMER0_OVF_vect(); }