Announcement

Collapse
No announcement yet.

PWM on Atmel RF200 Engine

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PWM on Atmel RF200 Engine

    Here is an example using the PWM on the RF200 module. This example drives the LED or device active high on GPIO 0.
    Last edited by IbarraE; 12-01-2010, 05:41 PM.

  • #2
    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
    Attached Files
    Last edited by IbarraE; 01-12-2012, 06:21 PM.

    Comment


    • #3
      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
      Last edited by ARQuattr; 09-27-2011, 09:36 PM.

      Comment


      • #4
        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)
        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.

        Comment


        • #5
          Thanks again IbarraE,
          Do I need to initialize both timers 1 and 3 to use only PWM on GPIO 1 and 2?

          Comment


          • #6
            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.

            Comment


            • #7
              Thank you.

              Comment


              • #8
                Originally posted by IbarraE View Post
                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)
                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?
                Last edited by Del Tapparo; 01-07-2012, 04:07 PM. Reason: More info:

                Comment


                • #9
                  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)
                  sigpic
                  Proven Solutions for the Internet of Things
                  www.synapse-wireless.com

                  Comment


                  • #10
                    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.
                    Last edited by Jheath; 01-07-2012, 04:23 PM. Reason: MUCH better wording
                    sigpic
                    Proven Solutions for the Internet of Things
                    www.synapse-wireless.com

                    Comment


                    • #11
                      Code:
                      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.

                      Comment


                      • #12
                        I tried changing the system clock pre-scaler to run it at 4 MHZ with no success:
                        Code:
                        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.

                        Comment


                        • #13
                          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


                          • #14
                            Originally posted by Darthvazor View Post
                            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..

                            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)
                            Side Notes:
                            - 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.
                            sigpic
                            Proven Solutions for the Internet of Things
                            www.synapse-wireless.com

                            Comment


                            • #15
                              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)
                              to my blank script. I upload it to my sm200 but error came up. It said:

                              SNAPpy Image Manager Error: An error occurred on line 213 while creating the SNAPpy image: name 'setHook' is not defined
                              Line 213 in AtmelPWM.py said:

                              @setHook(HOOK_10MS)
                              Do I have to add @setHook(HOOK_STARTUP) at top of the code in AtmelPWM.py? Or what?

                              Comment

                              Working...
                              X