Arduino Due Timer question

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int main(void)
{
    /* Initialize the SAM system */
    SystemInit();

	InitPIO();
	InitTimer();
	
    while (1) 
    {
        //TODO:: Please write your application code 
    }
}

void InitPIO()
{
	//Disable PIO Control on PB25 and set up for Peripheral B TIOA0
	PIOB->PIO_PDR = PIO_PB25;
	PIOB->PIO_ABSR = PIO_PB25;
	
	//Enable output on B25
	PIOB->PIO_OER = PIO_PB25;
	
	//Enable pull-up on pin
	PIOB->PIO_PUER = PIO_PB25;
}

void InitTimer()
{
	// Disable TC clock
	TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKDIS;
	
	//Disable interrupts
	TC0->TC_CHANNEL[0].TC_IDR = 0xfffffff;
	
	//Clear status register by reading it.
	TC0->TC_CHANNEL[0].TC_SR;
	
	//We need to associate clock with Timer 0
	PMC->PMC_PCER0 = _BV(ID_TC0);
	
	//TIMER_CLOCK1 = MCK / 2, Wave with UP mode with automatic trigger on RC Compare and
	//  RA Compare Effect on TIOA = SET
	TC0->TC_CHANNEL->TC_CMR = TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVSEL_UP_RC | TC_CMR_ACPA_SET;
	//Set compare value in Register A
	TC0->TC_CHANNEL[0].TC_RA = 200;
	
	//Enables the clock a software trigger is performed: the counter is reset and the clock is started 
	TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}

Hmmm I wrote a long bit of text that when I added code disappeared.  Crap

 

Anyway I am trying to use Timer 0 in the Wave mode to produce PWM on PWM Pin2 (B25) and it looks like it would be pretty easy but apparently there is something I'm missing.  I'll also be using the PWM Controller to do this next but want to learn the timer and how to program the various modes.

 

Any help would be much appreciated...Thanks

This topic has a solution.

Happy Trails,

Mike

JaxCoder.com

Last Edited: Tue. Sep 30, 2014 - 10:40 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Most ARM peripherals usually need their clock domain enabled before they are used. Maybe it's there but are you doing that?

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

Thanks clawson for you response.  I finally got the code below to work, it was as you said the clock PIO clock was not being set.  The data sheet is so hard to read and contradicts itself in places.  When I went back and read it it said that some peripherals may need the clock to operate, I guess this is one that does. :)

 

void InitPIO()
{	
	PMC->PMC_PCER0 = _BV(ID_PIOB);
	
	//Disable PIO Control on PB25 and set up for Peripheral B TIOA0
	PIOB->PIO_PDR = PIO_PB25 | PIO_PB27;
	PIOB->PIO_ABSR = PIO_PB25 | PIO_PB27;
	
	//Enable output on B25
	PIOB->PIO_OER = PIO_PB25 | PIO_PB27;
	
	//Enable pull-up on pin
	PIOB->PIO_PUDR = PIO_PB25 | PIO_PB27;
}

void InitTimer()
{
//	PMC->PMC_WPMR = PMC_WPMR_WPKEY_VALUE | PMC_WPMR_WPEN;

	//We need to associate clock with Timer 0
	PMC->PMC_PCER0 = _BV(ID_TC0);
	
	//TODO This works but at a constant 50% duty and changing RC changes frequency
	//  Try TC_CMR_WAVSEL_UP | TC_CMR_CPCTRG
	//	and see if this allows RA to adjust duty while RC set freq.
	
	//TIMER_CLOCK1 = MCK / 2, Wave with UP mode with automatic trigger on RC Compare and
	//  RA Compare Effect on TIOA = SET
	TC0->TC_CHANNEL[0].TC_CMR = TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVSEL_UP_RC | TC_CMR_ACPA_TOGGLE; 
	//Set compare value in Register A
	TC0->TC_CHANNEL[0].TC_RA = 1000;
	TC0->TC_CHANNEL[0].TC_RC = 4000;
	
	//Enables the clock a software trigger is performed: the counter is reset and the clock is started 
	TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}

 

Happy Trails,

Mike

JaxCoder.com

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

it said that some peripherals may need the clock to operate

My experience of ARM/Cortex is that is pretty much ALL peripherals need a clock before you can use them - even something as simple as GPIO! (so this catches your very first LED flasher too).

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

You are pretty much right, the PIO configuration that doesn't need the clock is outputs.  All Input lines and peripherals lines need the clock, lesson well learned.

 

Thanks for you input, much appreciated.

Mike

Happy Trails,

Mike

JaxCoder.com