PWM in dffrent frequencies

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

hi,

i'm a new member at the ATMEL family.
i wasted 10 hours yesterday in finding a way to use the PWM (in AT90S4433)
in diffrent frequencies- I need to create an output square wave in 10 diffrent frequencies
(20kHz-100kHz, Duty cycle 50%) with the PWM.- can i do it? or the PWM only works at fck/2048
fck/1024 and fck/512 ???
can anyone help me? can anyone send me an example?
Thanks.
Noam.

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

The hardware PWM has a limited selection of frequencies so I think you may be out of luck.

One other approach would be to use the Output compare function to cause an interrupt and switch the polarity of a pin to generate semi-hardware PWM. I've used this before and it works well as long as you pay attention to interrupt timing and can stand a little bit of jitter in your generated frequency. Also you have to be careful about very short on and off pulses to make sure you have enough time to service the interrupts. 100 kHz may be hard to do i.e. only ~50 instructions at 10 MHz if your duty cycle is much greater or less.

The basic approach is this

1. Calculate the frequency you need in timer counts at as high a timer tick rate as possible. 16-bit timers are best for this.

2. Calculate the number of "on" ticks and the number of "off" ticks.

3. Use the OC register to bump the output pin and interrupt the processor when the number of "on" ticks go by, then re-load the register with the number of "off" ticks. Since you are at 50% duty cycle you could just set the OC mode to "toggle" and reset the timer to 0 after the second interrupt.

4. An alternative method would be to pre-load the timer to a value so that it overflows after 1/2 of your cycle and then toggle the pin in software. The interrupt routine would be very small.

Either of these should leave quite a bit of time for other processing.

admin's test signature