TC problem on ATSAML21J18B

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All,

 

I've run into a problem trying to use the TC peripheral that has me very confused.  I am basically trying to et up a timer with a frequency of 48kHz.  The problem I have is that it seems like if I have low values in the CC0 register, the timer doesn't behave correctly.  My setup code is as follows:

 

static void configure_tc(struct tc_module *tc_instance)
{
	struct tc_config config_tc;
	struct tc_events config_events;
	tc_get_config_defaults(&config_tc);
	config_tc.counter_size    = TC_COUNTER_SIZE_16BIT;
	config_tc.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ;
	config_tc.clock_source    = GCLK_GENERATOR_0;
	config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV8;
	config_tc.counter_16_bit.compare_capture_channel[0] =63;
	
	tc_init(tc_instance, CONF_TC_MODULE, &config_tc);
	config_events.generate_event_on_compare_channel[0]= true;
	tc_enable_events(tc_instance, &config_events);
	tc_enable(tc_instance);
}

GCLK_GENERATOR_0 is fed by the main clock which is running at 4MHz.  For testing purposes I am generating an event that is toggling a pin that I am looking at on scope.     As an example of some of the peculiar behavior I am seeing, if I change the prescaler value to DIV4, there is barely any change in the frequency I measuring.  With the setup provided, with the DIV8 prescaler I expect a roughly 3.96kHz output on the scope which I am getting.  When I change the prescaler to DIV4 I expect my measured frequency to double, but it doesn't I am only measuring about 4.4kHz.  If I start increasing the compare value in the CC0 register it will eventually start behaving as expected.  I'm unsure what to try next, everything seems in order.  If anyone has any suggestions or guidance they could offer I would appreciate it.