View Full Version : PWM on Atmel RF200 Engine
IbarraE
10-04-2010, 10:15 PM
Here is an example using the PWM on the RF200 module. This example drives the LED or device active high on GPIO 0.
IbarraE
12-01-2010, 06:45 PM
I've added support for all 6 PWMs available on the RF200 platform.
Init the timers and the init the PWM you want to use. Then you are ready to set the duty cycle. I included an example of a heart beat LED on GPIO_0
edit: I modified the script slightly to switch between 8-bit and 16-bit modes
ARQuattr
09-27-2011, 10:16 PM
IbarraE,
Thanks for this code. If I only need to use the PWMs on GPIO 1 and 2, do I still need to perform all the timer initializations in init_Timers? (i.e. are both timer 1 and 3 needed?)
Also, what does the PWM frequency work out to using this code?
Thanks,
Angelo
IbarraE
09-28-2011, 05:01 PM
You can control the clock frequency by controlling the timer divisor
Here is the code snippet that controls it
# Setup Counter 1
value = 0x18 # Fast PWM, TOP=ICR1
value |= 0x01 # Setup clock source fosc/1
poke(0x81,value)
The CPU clock is 16 MHz and the prescaler is set to 1 which means the PWM frequency would be 62.5kHz. This can be easily calculated by:
PWM frequency = 16 MHz / (prescaler * 256) in 8 bit mode.
ARQuattr
10-02-2011, 06:38 PM
Thanks again IbarraE,
Do I need to initialize both timers 1 and 3 to use only PWM on GPIO 1 and 2?
IbarraE
10-03-2011, 10:36 AM
No, you won't need to initialize both timers in that case. You only need to initialize the timer for the PWMs that you are using. In your case you are only using timer 1 PWMs so you'll only need to initialize timer 1.
ARQuattr
10-03-2011, 02:59 PM
Thank you.
Del Tapparo
01-07-2012, 05:02 PM
You can control the clock frequency by controlling the timer divisor
Here is the code snippet that controls it
# Setup Counter 1
value = 0x18 # Fast PWM, TOP=ICR1
value |= 0x01 # Setup clock source fosc/1
poke(0x81,value)
The CPU clock is 16 MHz and the prescaler is set to 1 which means the PWM frequency would be 62.5kHz. This can be easily calculated by:
PWM frequency = 16 MHz / (prescaler * 256) in 8 bit mode.
I have changed the value of line 2 (clock source) to 2, 3, and 4 with no resulting change in frequency. I keep getting 62.5KHZ. What am I doing wrong? I would like to end up close to 20KHZ (a value of 3).
I should add, I am using GPIO_0 (OC0A) for PWM. My code initializes the output pins and timer 1 !!!! Now I see that GPIO_0 uses timer 0, not 1. How do I change the frequency now?
Jheath
01-07-2012, 05:12 PM
How do you have your Prescaler values setup?
Note for those reading this thread: The Atmel specific PWM operation details can be found in the ATmega128RFA1 datasheet (the Timer/Counter register sections to be precise)
Jheath
01-07-2012, 05:17 PM
Yes, the 0x81 register is specific to Timer/Counter1.
The setup for Timer0 is similar, just done by a different set of registers.
The register related to what you reference above (except applicable to Timer/Counter0) is TCCR0B => Read/Write of 0x25/0x45.
The duty cycle is set by OCR0A => 0x47
Section 17.9 of the Atmel datasheet has more information.
Del Tapparo
01-07-2012, 05:46 PM
def init_OC0A():
# Set pin direction
setPinDir(GPIO_0,True)
# Setup clock source
value = peek(0x45)
value |= 0x01 # clk/1
poke(0x45,value)
OK. I think the pre-scaler value is contained in the init of the pin.
I get: 1 = 62.5KHZ, 2= 7.8KHZ, and 3 = 1 KHZ.
This corresponds to what I read in the data sheet for the Atmel.
Anyway I can get closer to 20 KHZ? (This may be beyond the scope of forum help. But any help would be much appreciated.
Sure would be nice if PWM was a built-in function. :)
Del Tapparo
01-07-2012, 06:41 PM
I tried changing the system clock pre-scaler to run it at 4 MHZ with no success:
poke (0x61,0x100) # Enable clock prescaler
poke (0x61,0x02) # 16MHZ / 4 = 4 MHZ
Am I on the right track? System clock of 4MHZ with a timer 0 prescaler of 1 should get me to 15.6 KHZ, which would be OK.
vBulletin® v3.8.0, Copyright ©2000-2012, Jelsoft Enterprises Ltd.