timer interrupt timer intervals

so i want the timer to go back to counting once the ISR starts , not at the end of the ISR . since ISR code sometimes takes more or less time depending on the conditions of the program and i want the ISR to be initiated in exact time intervals . is that possible ?

You can set the register of a timer in an ISR, and that timer will do that. Why would something wait for the end of the ISR ? This might be interesting : http://www.gammon.com.au/interrupts

Or do you mean that a timer generates an ISR ? You don't have to reset or restart the timer, the hardware timer can run continuously for example at 1000Hz, and generate an interrupt at 1000Hz.

Do you want to do something with hardware at specific intervals ? Then you can use the timer output (hardware timer output, super exact). An ISR is delayed by other ISRs, it is never at exact intervals (unless it is the only interrupt that ever occurs).

sir , i only have a single interrupt working at once , the thing is (and i checked this) the timer that triggers the interrupt goes back to tricking when the ISR is over . so if you have a ISR that takes a specific amount of time , that would work . but the time taken by my ISR is variable . so i checked and the intervals in which my interrupt was triggered were messed up

The Arduino uses TIMER0 for the millis(). That interrupt is also running. I don't understand what you mean that the timer is doing something if the ISR is over. That doesn't seem right, perhaps not even possible. The hardware timer runs independent of the software interrupt.

Can you explain what is happening ? And show your whole sketch ?

the code is long , but here is the case .
it’s kinda complicated … but since you asked …

i want the system to play a RAW sample of music (header-less 8bit) at a rate of 15660 khz
so then you must release a byte to a DAC every 64us , using an interrupt
now just finding the byte and outputting it takes no more than 1 microsecond after that the timer (TIMER1) goes back to ticking for 64us again . now the problem is that after 512 bytes outputted , i need to read a sector from the sd card . that takes about 2ms . so then the ISR will now take 3001us then the timer goes back to ticking at the end of the ISR ,and everything get’s distorted .
i can hear the music just fine but there happens to be an other interrupt (TIMER2) that is triggered every 30ms and lasts for 40ms that outputs some sort of video . within that interrupt i continue to supply the bytes but the one case where the first ISR has to read a sector must not go within this one .
here is an example . so ima devise a second into 60ms intervals . in each interval 30ms is used by the video … every 60ms requires one sector input wich takes 3ms and a video output that needs 30ms the other 1us goes every 64us and does not disturb the video .but the 3ms one disturbs it .
now the interrupt that takes 1us get’s triggered every 64us at the 512th time it must clear the sound array and refill it with a sector from the SD card the thing is the timer goes back to ticking after 3ms now not 1us wich would move the position the the 3ms sequence a bit in the 60ms period untill after a bit of time it becomes inside the 30ms used by the video . i did get the video and sound to work separately but this timer distortion seems to unallow them to work togeather

here is the ISR of timer1 ... the raw file starts at sector 512 and is over at sector 33400 exactly 16838656bytes

i can listen the the full tune but there is a liiiiittle distortion

volatile unsigned short b=512;
ISR(TIMER1_COMPA_vect)
{
PORTL=vBlock[i];
i++;
if(i==512) {i=0; sdc_readBlock(b); b++; if(b==33400) b=512;}
}

amine2: so i want the timer to go back to counting once the ISR starts , not at the end of the ISR . since ISR code sometimes takes more or less time depending on the conditions of the program and i want the ISR to be initiated in exact time intervals . is that possible ?

Related to: interrupt priority ?

Cross-post of: Quick question about interrupts (really short)

@amine2: Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

And stop starting new threads!

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

  • Moderator

done , ok ima keep it all in this thread , thanks sir

It is hard for me to understand why the timer starts 64us after outputting a byte. I might be to complicated for me as well.

I assume you know this : https://github.com/TMRh20/TMRpcm It seems to use a trick for nested interrupts, search for "nested" in TMRpcm.c

The VGAx can output a tone, but not audio from an SD card. https://github.com/smaffer/vgax

You need someone who knows a lot about these things. Perhaps the author of the TMRh20.

I wouldn't be reading from disk during an ISR.

i actually am doing that NICK , it works .

thank you very much Nick and Peter . i dont know how i can give back the credit . here you need to pay people for such help

amine2: i actually am doing that NICK , it works .

Apparently not very well, since the delay caused by reading the disk seems to be the root cause of your problem. Reading the disk should be done in the foreground task, so interrupts can continue to happen on-schedule AS the disk is being read.

Your problem is not the interrupts -it's the things you're doing IN the interrupts that should NOT be done in interrupts. Interrupts should only do tasks that can be done quickly. Any thing that takes a long time, should be done in the foreground, NOT an ISR.

Regards, Ray L.

yeah , but the task of reading the sector DATA must be done at an exact timing .