## Pulse Detection

6 posts / 0 new
Author
Message

Hi,

I have recently been given an assignment to run a program on ATMEGA16 when a pulse is given. I know how to detect high on a pin, but how do I detect 1 pulse?

I have 1 switch and 1 pulse. So when I turn on the switch and give 1 pulse, my simple led blinking code should execute. I am detecting the switch with (PINA & 0x01) code but what about the pulse?

Please suggest me and some code would be really nice since I am very new to this. Thank you.

Raquib wrote:
I know how to detect high on a pin, but how do I detect 1 pulse?
Detect a high on the pin and then go to a piece of code that detects low on the pin. When the low is detected, you have detected a pulse...

Martin, thank you for your reply. But suppose the duration between high and low on my pulse is large. How do I actually detect high and low in a certain duration? Suppose I want to detect if there is 1 complete pulse (1 High and 1 low) in any 1 second. Can you please help me on this? Thank you.

so run a timer so that you can see how long the pulse took.

Quote:
Please suggest me and some code would be really nice since I am very new to this.

But NOT so new that you can't be ASSIGNED something to do with an MCU.
Quote:
I have recently been given an assignment to run a program...

So i hope nobody's "nice"(sucker) enough to give you anything more than a few clues so YOU ALONE can write your own code. If you're an engineering student you BETTER get used to solving problems, esp. the simple ones.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

If you want to limit the length of a pulse, it will be necessary for you to decide just what those time constraints are. Is a pulse valid if it goes high for greater than 1 ms, greater than 100 ms, less than 1 s. Once you know the times you can build a simple state machine ( FSM ) and check when each state begins against a timer. Note, however, that the pulse detection logic is the same regardless - validating based on time is just added functionality to the basic pulse detection FSM.

It can be something as simple as the following pseudo-code,

```state = Wait
start_time = timer_value
loop forever:
switch state:
case Wait:
if input goes high:
start_time = timer_value
state = MeasureHigh

case MeasureHigh:
if input goes low:
high_time = timer_value - start_time
pulse is valid if high_time is in range
state = Wait
end loop```

Depending on the rest of the program, the state machine can be integrated into the rest of the main loop, or kept seperate as above, placed in a function that acts on global variables, or in a function that requires you to pass in the variables - design is entirely up to your project's requirements.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."