Trying to get a PWM to work

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

I have been trying for a few days now to get a PWM to work on an AT32uc3a1512 with zero success.  Well that's not totally true, I can get my code to compile without errors, but still no PWM.

 

I have looked at numerous examples people have posted, but either they do not have everything included, or I am too dense to realize it!

 

In my previous programs I have not used the Power Manager (PM) to control the few clocks I use (pll0 for cpu, fba for timers and usarts).  Is it absolutely necessary to use the PM for PWM?

 

Is there a complete example for PWM available for the UC3A1 that someone knows of, or would willing to post.

 

My code is so scattered with all the things I have tried, it is not worth posting.

 

Kind regards,

David

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

Are you using the ASF ?, or are you writing your own interface to the PWM ?


"Is it absolutely necessary to use the PM for PWM?"
No., the reset-defaults are that the PWM clock is enabled (PBAMASK), however, your main-clock will be the internal 115 kHz (MCCTRL)


Studio 6/7 has several example projects that uses the ASF.


If the GPIO multiplexing is not correctly configured to suit your chip/hardware then you will not see any PWM output.

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

Hi Mikech - thanks for the response.  Yes, I am using ASF.  My PLL is running at 66MHz using a 12MHz external oscillator.

 

I am using Studio 7.  I tried the one example for the UC3, but ASF would not let me change processor types.  So I created a new project and copied  the example PWM4 into it.  I do not have the SCIF library in my version of Studio &, so I copied it into the new project - got lots of compile errors.

 

I am using PWM0, which I read as only available on PB19.

 

Here is my last try at intitializing the PWM - anything you could point out as a problem would be greatly appreciated!

Kind regards,

David

 

void board_init(void)
{
    int c;
        avr32_pwm_channel_t pwm_channel = { .ccnt = 0 };
    
    
    
    pwm_channel.CMR.cpre = AVR32_PWM_CPRE_MCK_DIV_256; /* Channel prescaler. */    
    
    pm_gc_enable(&AVR32_PM,0);  // apparently not needed - PWM defaults to PBA
    sysclk_init();
    
    // Enable the clock to the selected example Timer/counter peripheral module.
//    sysclk_enable_peripheral_clock(BASE_TC);
    gpio_local_init()
    delay_init(66000000UL);
//    gpio_configure_pin(Timer_0_OUT,OUT);
//    gpio_configure_pin(PulsGen_1_OUT,OUT);    
    io_init();
    initialize_serial_port0(38400);
//    initialize_serial_port3(57000);
    gpio_enable_module(AVR32_PIN_PB19,AVR32_PWM_0_FUNCTION);
    
    pwm_opt_t pwm_opt =
    {
        .diva = AVR32_PWM_DIVA_CLK_OFF,
        .divb = AVR32_PWM_DIVB_CLK_OFF,
        .prea = AVR32_PWM_PREA_MCK,
        .preb = AVR32_PWM_PREB_MCK
    };
    
     pwm_init(&pwm_opt);
    /* With these settings, the output waveform period will be:
     * (115200/256)/20 == 22.5Hz == (MCK/prescaler)/period, with
     * MCK == 115200Hz, prescaler == 256, period == 20. */
    pwm_channel.cdty = 5; /* Channel duty cycle, should be < CPRD. */
    pwm_channel.cprd = 20; /* Channel period. */
    pwm_channel.cupd = 0; /* Channel update is not used here. */
    pwm_channel.CMR.calg = PWM_MODE_LEFT_ALIGNED; /* Channel mode. */
    pwm_channel.CMR.cpol = PWM_POLARITY_LOW;      /* Channel polarity. */
    pwm_channel.CMR.cpd = PWM_UPDATE_DUTY;        /* Not used the first time. */
    
     pwm_channel_init(FPR_PWM_CHANNEL_ID ,&pwm_channel);
     pwm_start_channels(1 << FPR_PWM_CHANNEL_ID );
    return;
}

 

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

Well, I seem to have a working PWM.  I copied the example program from http://simplemachines.it/martin/mizar32/1.6.0-AT32UC3/DRIVERS/PWM/EXAMPLE/DOC/html/a00008.html and was able to get a working PWM going.  Now to figure out how to change frequency and duty cycle!

 

Thanks again Mikech

David

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

dfansler wrote:
Now to figure out how to change frequency and duty cycle!

For the d/c it's more or less explicit in the code:

 

    pwm_channel.cdty = 5; /* Channel duty cycle, should be < CPRD. */
    pwm_channel.cprd = 20; /* Channel period. */

There the d/c is 5/20, or 25%. For the period, more knowledge re the clock system(s) on the AT32s is required than I possess. But there are good hints in the comment...

    /* With these settings, the output waveform period will be:
     * (115200/256)/20 == 22.5Hz == (MCK/prescaler)/period, with
     * MCK == 115200Hz, prescaler == 256, period == 20. */

 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Apr 25, 2017 - 05:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi JohanEkdahl - i had discovered the duty cycle and played with the period - then discovered, dawned me, that the processor was running on the internal clock.  So right now I am trying to figure out how to run the PWM off of PBA while running the main clock off the 66MHZ PPL1.

 

If you have any suggestions on how to do that without resorting to PM, they would be appreciated.  Right now I use sysclk_init() to change form the internal clock to the PPL1.  I do no dividing of PBA, so it should be running at 66MHz as well.  But I am not getting any output when I switch to PPL1.  If I comment out sysclk_init(), I get PWM output.

 

Kind regards,

David

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

Sorry.. As I  hinted at above, I'm already over the limit of my knowledge about the AT32 family. The AT32 clock selections system is way beyond my knowledge, but surely there must be data sheets with specs on how to select clock sources etc?

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Apr 25, 2017 - 09:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh well - did not hurt to ask!  I am sitting here reading through the docs and trying anything that looks promising.  Have not found it yet!

Kind regards,

David

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

If you look at the "Power Manager block diagram", and the definition of the bits in PM MCCTRL, you will discover that PLL1 cannot be selected as the main system clock on the UC3Axxx processors.

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

Opps, I meant PLL0!