SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By rayhall
#164195
Hello everyone,

I want to create single square wave signal on a output pin, based on the edge of another signal. This is an example of what I want to do.

1. Interrupt on the rising edge of a square wave signal using Input Capture ICP1.
2. In the ICP interrupt function, schedule another pin to be set high after 1ms, and then low after 2ms.

The captured and created signal are not a PWM. They are random frequency single square wave signals.

How can this be done. Does output compare do this ?

Ray.
By jremington
#164202
Why do you want to use interrupts? To accomplish this task without interrupts would take 5 lines of code.

1. wait for high on some input
2. wait 1 ms
3. output high on some output
4. wait 1 ms
5 . output low on that output

In any case you do not want to put steps 2-5 in an interrupt routine. Interrupt routines should accomplish only what they absolutely need to do, without delays and exit as quickly as possible.
By rayhall
#164256
jremington wrote:Why do you want to use interrupts?
The input capture interrupt must also messure the time period between rising edges. This is used to calculate rpm. Accurate timing is needed as the edge is also used to control fuel injector timing and pulse width. As this is part of a much bigger project, there is a lot going on in the main loop now, and the reading of the edge could be delayed.

Ray.
By jremington
#164281
Then, the input capture interrupt routine should set a global flag that tells the main loop to execute the response.
It would be quite complicated to use a timer to output one cycle of a square wave and then stop. I can't think of a way to do it that doesn't involve either wait loops, two timers or external logic circuitry.
By Valen
#164311
Let's say you start with a long period count stored in a (global) variable. As I presume the timerclock is counting much faster than 256 ticks in a milisecond. A 16 bit timer with 65535 ticks might suffice for a 1ms depending on the clock rate. Every time the timer counter overflow interupt happens, this long remaining period gets deducted by 255 (or 65535), until what is remaining is less than 255 (or 65535). Then you set the output compare register of the timer to the remaining of the counts that are left (minus some timer clock ticks needed to change the register). Only then enable the outputcompare interupt. Then when the output compare interupt happens the time is right to flip an output pin. But yeah, this is a bit more complex than your average delay loop. But it has minimal MCU processing load.