I'm trying to generate a 1kHz PWL and call a function every 1ms the code is below. My initial though was to set TASKS_CLEAR once the timer reached the max pwl value. Oddly this worked of the reset value was 32,000 but not 256. I think I have fixed it by using the "SHORTS" function to clear the timer, but I'd like to know why using clear did not work. The BLE was disabled during the test.
I want to use this chip to control a sensored brushless motor (with ble off when the motor is running, will I able to get enough through put).
Using this chip has become a real pain. So many poorly documented issues.
void timers_init2(void){
nrf_gpio_cfg_output(3);
// Start 16 MHz crystal oscillator .
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;
// Wait for the external oscillator to start up.
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
{
//Do nothing.
}
NRF_TIMER2->TASKS_STOP = 1;// Stop timer
NRF_TIMER2->MODE =TIMER_MODE_MODE_Timer;
NRF_TIMER2->TASKS_CLEAR = 1;
NRF_TIMER2->PRESCALER = 6; // max=9 6 gives 250khz
NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit; // 8 bit mode does not work
NRF_TIMER2->CC[0]=256; //reset value
NRF_TIMER2->CC[1]=m.pwm;
NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos)
| (TIMER_INTENSET_COMPARE1_Enabled<< TIMER_INTENSET_COMPARE1_Pos);
NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
NVIC_EnableIRQ(TIMER2_IRQn);
NRF_TIMER2->TASKS_START = 1;
}
void TIMER2_IRQHandler(void){
if((NRF_TIMER2->EVENTS_COMPARE[0] != 0)
&& ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) !=0)){
// reset timer and start pulse
NRF_TIMER2->EVENTS_COMPARE[0] = 0;
//NRF_TIMER2->TASKS_CLEAR; // needed to replace this with shorts call above
call_func_every_ms();
if(m.pwm> 0) {
NRF_TIMER2->CC[1]=m.pwm;
}
nrf_gpio_pin_set(3);
}
if((NRF_TIMER2->EVENTS_COMPARE[1] !=0)
&& ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE1_Msk) !=0)){
// End of pwm pulse
NRF_TIMER2->EVENTS_COMPARE[1] = 0;
nrf_gpio_pin_clear(3);
}