Hello,
For some reason I cannot get Channel 1 of TC0 timer to work. Channel 0 works fine though so I don't understand what is happening.
Based upon checking the register it looks like the clock of channel 1 never starts running.
int RPM_update_frequency = 0;
uint32_t RPM_tick_max = 0;
float RPM_period = 0;
void InitializeTC0(void)
{
//enable power to ID_TC0
PMC->PMC_PCER0 = PMC_PCER0_PID27;
// set TC0 channel 0 to handle computing motor RPM
RPM_update_frequency = 20;
RPM_tick_max = floor((uint32_t)32000/(float)RPM_update_frequency);
RPM_period = 1/(float)RPM_update_frequency;
// select SLCK which operates at 32khz, and enable compare trigger
TC0->TC_CHANNEL[0].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK5|TC_CMR_CPCTRG;
// set timer to cause interrupt when timer counter reaches RC value
TC0->TC_CHANNEL[0].TC_IER = TC_IER_CPCS;
// set period number to trigger,
TC0->TC_CHANNEL[0].TC_RC = RPM_tick_max;
// Start channel 0
TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKEN|TC_CCR_SWTRG;
// set TC0 channel 1 to handle sending current RPM through serial
int broadcast_frequency = 0;
uint32_t broadcast_ticks = 0;
float broadcast_period = 0;
broadcast_frequency = 1;
broadcast_ticks = floor((uint32_t)32000/broadcast_frequency);
broadcast_period = 1/broadcast_frequency;
// select SLCK which operates at 32khz, and enable compare trigger
TC0->TC_CHANNEL[1].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK5|TC_CMR_CPCTRG;
// set timer to cause interrupt when timer counter reaches RC value
TC0->TC_CHANNEL[1].TC_IER = TC_IER_CPCS;
// set period number to trigger, 1 second
TC0->TC_CHANNEL[1].TC_RC = broadcast_ticks;
// start channel 1
TC0->TC_CHANNEL[1].TC_CCR = TC_CCR_CLKEN|TC_CCR_SWTRG;
// set priority and enable interrupt
NVIC_SetPriority(TC0_IRQn, 4);
NVIC_EnableIRQ(TC0_IRQn);
}
int encoder_1_last_position = 0;
int encoder_2_last_position = 0;
float encoder_1_RPM = 0;
float encoder_2_RPM = 0;
void TC0_Handler(void)
{
int channel_0_status = 0;
int channel_1_status = 0;
int value = 0;
value = TC0->TC_CHANNEL[1].TC_CV;
channel_0_status = TC0->TC_CHANNEL[0].TC_SR;
if((channel_0_status & TC_SR_CPCS) != 0)
{
encoder_1_RPM = 60*(encoder_1_position - encoder_1_last_position)*RPM_update_frequency; // multiplication is faster, speed_update_frequency = 1/period
encoder_1_last_position = encoder_1_position;
encoder_2_RPM = 60*(encoder_2_position - encoder_2_last_position)*RPM_update_frequency; // multiplication is faster, speed_update_frequency = 1/period
encoder_2_last_position = encoder_2_position;
}
channel_1_status = TC0->TC_CHANNEL[1].TC_SR;
if((channel_1_status & TC_SR_CPCS) != 0)
{
char buffer[200];
int message_length;
message_length = sprintf(buffer, "Encoder 1 RPM: %f, Encoder 2 RPM: %f \n", encoder_1_RPM, encoder_2_RPM);
send_message(buffer);
}
}