Here is an example using the PWM on the RF200 module. This example drives the LED or device active high on GPIO 0.
Announcement
Collapse
No announcement yet.
PWM on Atmel RF200 Engine
Collapse
X
-
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 modesAttached FilesLast edited by IbarraE; 01-12-2012, 06:21 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,
AngeloLast edited by ARQuattr; 09-27-2011, 09:36 PM.
Comment
-
You can control the clock frequency by controlling the timer divisor
Here is the code snippet that controls it
Code:# Setup Counter 1 value = 0x18 # Fast PWM, TOP=ICR1 value |= 0x01 # Setup clock source fosc/1 poke(0x81,value)
PWM frequency = 16 MHz / (prescaler * 256) in 8 bit mode.
Comment
-
Originally posted by IbarraE View PostYou can control the clock frequency by controlling the timer divisor
Here is the code snippet that controls it
Code:# Setup Counter 1 value = 0x18 # Fast PWM, TOP=ICR1 value |= 0x01 # Setup clock source fosc/1 poke(0x81,value)
PWM frequency = 16 MHz / (prescaler * 256) in 8 bit mode.
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?
Comment
-
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.
Comment
-
Code:def init_OC0A(): # Set pin direction setPinDir(GPIO_0,True) # Setup clock source value = peek(0x45) value |= 0x01 # clk/1 poke(0x45,value)
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.
Comment
-
I am still learning Python language and I did small project with Raspberry Pi with Python. It was successful. But when I come here to try out with SM200. I feel like I go backward on everything I learned. I am kind of lost here.
I tried to upload this AtmelPWM.py to SM200 and test it. Nothing happens. I don't know what I am missing.
I connected LED from PWM (Pin 1) and GND through Solarbotics Synapse RF Engine Interface v1.0 breakout board. No light. Polarity of LED is right as I tested it.
Please help me.
Comment
-
Originally posted by Darthvazor View PostI tried to upload this AtmelPWM.py to SM200 and test it. Nothing happens. I don't know what I am missing.
I connected LED from PWM (Pin 1) and GND through Solarbotics Synapse RF Engine Interface v1.0 breakout board. No light. Polarity of LED is right as I tested it..
AtmelPWM.py file is intended to be an import file into the actual application script.
It lacks the initialization of the I/O needed to actually start the PWM process
Ex. Add this two your own script...
Code:from synapse.AtmelPWM import * brightness = 50 @setHook(HOOK_STARTUP) def startup(): heartBeat_En() init_OC0A() setOC0A_duty(brightness)
- Files under the "synapse" directory are usually not standalone scripts, but helper files to be imported into main scripts.
- You make mention of the SM200. However, the Solarbotics board with only accept RF200 module footprints. The only reason I bring it up is that the pinouts and other information will differ between the two types of modules.
Comment
-
Thank you for quick response. I added:
from synapse.AtmelPWM import *
brightness = 50
@setHook(HOOK_STARTUP)
def startup():
heartBeat_En()
init_OC0A()
setOC0A_duty(brightness)
SNAPpy Image Manager Error: An error occurred on line 213 while creating the SNAPpy image: name 'setHook' is not defined
@setHook(HOOK_10MS)
Comment
Comment