Interrupts...

Hi, Is there any way of generating internal interrupts, by this I mean not an externally generated one from one of the digital pins? I'd like to generate a precise frequency of 1000Hz on one of the digital pins going to a circuit that feeds back a corresponding voltage. While in the process of generating the frequency I'd like also to be able read the voltage and when a particular value is met, to then light up an LED. The only way I can see of doing this accurately is to have an internal interrupt at twice the frequency needed to be generated. This then invokes a routine to toggle the frequency output pin and then hopefully there will be enough time left to read the analogue input, make a decision on whether to light the LED and finally exit before the next interrupt.

Hi, you can use some of the timer libraries available. For example: http://www.arduino.cc/playground/Code/Timer1 There are equivalent libs for Timer0 and Timer2 (they are 8 bit timers, while Timer1 is 16 bit). Also search arduino.cc for timer in google - there are a lot of topics about them. (do this search: site:arduino.cc timer) Basically what those libs provide as functionality is to call back some function defined by you with some frequency, and internally they already use timer interrupts so you don't need to worry about that (and also prescalers and overflows). And the usual warning - when you start controlling the timers yourself you loose the "built in" arduino PWM functionality on some pins. If you don't intend to do anything with PWM then it does not matter.

Hi, Thanks for the pointer, I shall investigate further..! :)

when you start controlling the timers yourself you loose the "built in" arduino PWM functionality on some pins

Could I not save the values of all the timers and then reset them after use? Ideally, I will need to disable all external interrupts and all timers except the one I'll be using...

Thanks.

Most probably you could :). But that is something the AVR masters around here can answer. I’ve played with timer2 until it worked, did not care about saving states.

Ideally, I will need to disable all external interrupts and all timers except the one I'll be using...

If I'm understanding your goals right, you won't need to disable the other timers. While in an interrupt, no other interrupts can run, so nothing can interfere with your 1000Hz timer.

As far as generating a 1000Hz square wave, though, you could probably get that just by manually configuring the PWM frequency. That way, it would be set and forget, and you would have 100% of your processor speed left over to do other things.

Also, beware of trying to do an analogRead() inside an interrupt-- it takes a long time to run, and so you risk locking up your program.

FrequencyTimer2 will toggle pin 11 at a rate of your choosing in hardware (without using interrupts) See: http://www.arduino.cc/playground/Code/FrequencyTimer2

It also has functionality to call a function using interrupts but this is neither needed nor desirable.

You can test the state of the voltage in your loop function. It won't matter if you turn the LED on a few milliseconds after the voltage changes and you won't be overtaxing an interrupt handler.

If you are using an ATmega328 chip then see this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239820770/4#4

Thanks for the info guys, looks like FrequencyTimer2 code will do just fine..! :) I've just recieved my brand spanking new Duemilanove through the post, literally an hour ago, I'm off to have some fun..! ;D

Thanks again!