A bit awkward to explain perhaps but say I’ve got a NONLINEAR array of numbers like 1,2,3,5,7,10,15,31, etc (I just made those up) and they go up to like 66000 or thereabouts. I’m already using timer1 for something so I have to use timer 2 to basically check each element of the array in order and interrupt the program when there’s a match. So IF count = 31, interrupt. Then IF count = 59, interrupt. Then IF count = 2016, interrupt.
Obviously this is going to lead to overflows very quickly so I could do something like counting overflows and adding 256 for each of them and then the remainder is counted on the final cycle. However I don’t have a lot of time to do math like that in the ISR because first I have to clear TCNT2 so while I’m doing math to update the count, the timer meanwhile is back to counting again (this is at clock speed btw so very fast). So while at the end of the array I may have some time, at the beginning of the array I have very little time. In fact the lowest number in my array is 85 so 85 ticks is all the time I have to execute a code block that handles all the different possible scenarios for keeping track of this changing number. I’m afraid I’ll miss an interrupt event this way or the accuracy of triggering such an event won’t be centered on its target perfectly.
Ideally I would like to simply have TCNT2 do all the work for me all by itself because it’s got its own compare and overflow interrupts but with 8 bits it just won’t go very far. My numbers range from 85 to I think 66000’ish.
Just wondering if there’s an optimized or defacto way to do this sort of thing to avoid glitches and such. I would post code except there is none. Nothing in the main loop anyway. I just have my timer1 and timer2 setup code, both running with no prescaler and that’s it. Beyond that I just need to do my work in the ISRs.