SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By aludra_55
#74494
I'm not that familiar with Atmega168/328 so I need help...
I've also read the datasheet. It is education but still kinda
confusing.

I've enabled hardware PWM generator using Timer1 on two pins to
control two servos. From what I've researched in the internet so far, Timer1 uses ICR1.

Now, I've used the pulseIn command (Arduino) to capture an incoming RC signal from a receiver but it's pretty much noisy. Probably because the resolution of the timer of the pulseIn command is not high enough. That's why I want to use the 16bit Timer1, which is pretty much occupied with PWM generation for the two servos.

Now the question. In the same microcontroller, I would like to know if it's
possible to use ICR1 to capture a signal to calculate the width of the pulse while still being used for PWM generation? Thanks in advance.

-AludrA
By Lajon
#74529
For slow signals like servo pulses you can use the "Normal" timer mode, this makes ICP a lot simpler. The pulse generation is then no longer automatic but can still make use of the output switching HW (so it will not jitter). You need one output compare ISR for each output. The ISR needs to step the time and setup the next switching operation. So it will have code like
Code: Select all
OCR1A += next_duration;
and it needs to set and clear bits in TCCR1A (the COM1xx bits that control if the output should go low or high).
If you give up the HW switching and just use the output compare interrupt (just one) you can control multiple servos with fairly simple code (and still in the Normal timer mode so ICP is simple to use on the same timer).
I have made a code example of this at avrfreaks:
http://www.avrfreaks.net/index.php?name ... c&start=24
/Lars
By aludra_55
#74546
holy cow!... I was checking AVR Freaks and saw your posts but I haven't really seen anything in the forum about using both capture and PWM in the same Timer.

Anyway, I don't have any experience in tweaking the HW but I've been
reading the datasheet. From what I've read so far, it seems that it is
possible to do capture and PWM on the same timer but just like what you
said the output signal of PWM must be disabled then enabled so that the
Timer could be used for capturing. This is great. At least I know that it's possible now and all I have to do is further my research. I will check your code. Thank you very much.

Right now I'm basically breaking down every single steps in using the Timer for PWM and capturing. I'm even studying how to do the bitmath. I try to take it slow starting from the basic as my foundation. Thanks again so much.

-AludrA