Misunderstanding PWM

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm trying to control an RC servo with PWM...

Previously I was using compare matches on the 16-bit Timer1 on my Atmega8 and software interrupts to generate the PWM..

Basically, I prescaled my 8mhz clock to 1mhz...
When the timer was started, output went high and then the timer counted to OCR1B at which output went low..

Then when OCR1A matched (value = 20000, 20 milliseconds)... the timer's counter was reset and output was again set as HIGH..this looped

So I was left with a .5 to 2.5 millisecond HIGH pulse (though longer or shorter pulses could be generated) occuring in a 20 millisecond window..

So, for example, if I wanted to generate a 1.5ms pulse to set the servo to neutral position I'd send a 1.5 millisecond HIGH pulse, followed by 18.5 milliseconds of LOW...this would repeat until the position needed to change.

Unfortunately, I've added more interrupts and even when doing interrupts within interrupts I'm starting to notice artifacts in the servo's timing line (no oscillope, it just momentarily "wants" to move...even though it doesn't, it kinda "ticks")...

What I want to do is generate the same kind of signal using either the CTC output mode of Timer1 or one of the PWM modes..

Unfortunately, I'm very confused as to how to produce a variable-length HIGH pulse under a fixed 20 millisecond window this way...

So, help me out here... I tried using CTC mode and setting OCR1A to 20000 (20ms @ 1/8th prescaling) and setting OCR1B to my position (500-2500).. and setting the outputs for OC1A to toggle..

Unfortunately, I think that the output selection is mapped to the compare register, like COM1A(0:1) and COM1B(0:1) map to seperate pins OC1A and OC1B and to seperate compare registers OCR1A and OCR1B...

I'm very confused...I think it can be done by gating the output of the two output pins OC1 and OC2 (and I'm willing to go that route if it's the only way)...

Keep in mind what I'm ultimately trying to do, I'm trying to generate the signal described above without software interrupts.. I want to achieve this fully by setting registers...

So guys, it's probably a simple solution I'm not seeing. The PWM literature in my datasheet looks like greek at first glance.

Thanks for reading, hope you can help...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Many many many messages about this.. I think it can be done all in hw with a 16 bit timer set to give 20 ms on ch1a, ch1b generates the 500-2500usec pulse.. I have a couple programs that work ok, but shucks, they use hardware interrupts. The hc11s had a software interrupt instruction, but I dont think you can do this easily on an AVR

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You will find HW PWM for servos discussed in many threads. For example here:
https://www.avrfreaks.net/index.p...
In the data sheet you should have a look at timer 1 fast PWM (mode 14)
/Lars