ATSAM3S4C PWM Example

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

Hi all,

 

I am new here and new to ASF. And to be quite honest - I have found it quite hard work. That being said, I feel I am making head way. I have pulled a number of examples from various sources, each seem to do things a little differently. I have tried all those I can find and modifed as necessary, but to no prevail. Just trying to get a basic PWM setup, that's all. I've spent time diving into the code and all is making sense, so I don't know what I am missing/doing wrong. If anyone has any suggestions... it would be most appreciated. Thanks in advance. This is an example I pulled from 'All About Circuits', project is attached.

 

#include <asf.h>

//Create Pin PA1
#define PWM_Pin IOPORT_CREATE_PIN(PIOA, PIO_PA1_IDX)

pwm_channel_t pwm_channel_instance;

int main (void)
{
	sysclk_init();
	board_init();

	//pio_set_peripheral(PWM,PIO_PERIPH_A, PIO_PA1A_PWMH1); I have also tried this instead of pio_configure_pin

	//connect peripheral A to pin PA1
	pio_configure_pin(PWM_Pin, PIO_TYPE_PIO_PERIPH_A);

	//enable the peripheral clock for the PWM hardware
	pmc_enable_periph_clk(ID_PWM);

	//disable the channel until it is properly configured
	pwm_channel_disable(PWM, PWM_CHANNEL_0);

	//PWM clock configuration
	pwm_clock_t PWM_clock_config =
	{
		.ul_clka = 1000000,
		.ul_clkb = 0,
		.ul_mck = sysclk_get_cpu_hz()
	};

	//apply the clock configuration
	pwm_init(PWM, &PWM_clock_config);

	//see the article for details
	pwm_channel_instance.channel = PWM_CHANNEL_0;
	pwm_channel_instance.ul_prescaler = PWM_CMR_CPRE_CLKA;
	pwm_channel_instance.polarity = PWM_HIGH;
	pwm_channel_instance.alignment = PWM_ALIGN_LEFT;
	pwm_channel_instance.ul_period = 100;
	pwm_channel_instance.ul_duty = 50;

	//apply the channel configuration
	pwm_channel_init(PWM, &pwm_channel_instance);

	//configuration is complete, so enable the channel
	pwm_channel_enable(PWM, PWM_CHANNEL_0);

	while(1);
}

Attachment(s): 

AntMAN

Last Edited: Wed. Jul 7, 2021 - 11:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quick note - PA1 pin is driving a motor with an external pull-down resistor.

AntMAN

Last Edited: Wed. Jul 7, 2021 - 11:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, I worked this out some time ago. Just wanted to provide an update incase anyone encounters this problem. FYI, I did contact support, they could not fiond a problem with my code... however, there is a problem. The pin I am using to output my PWM is PA1, on my device (sam3s4c) that is peripheral A (PWMH1). I was trying to output PWM channel 0 onto this pin, where as I should have output PWM channel 1. 

AntMAN