i am developing an application that uses timer registers in DA14580 kit with 32KHZ clok frequency,
i set TIMER0_ON_REG register with counter opposite to time period which needed to fire after.
this is the equation used to convert period to counter
on_counter = (period_ms * 32)- 1
aftet loading TIMER0_ON_REG with on_counter i need to get current counter through timer is running
example timer is needed to fire after 1000 millisecond after 500 millisecond i will read TIMER0_ON_REG
and convert reading number with equation (((on_counter ) + 1) / 32 to get elapsed time
but my problem here reading value from this register is not accurate and a lot of time is zero !!
and i need to know if this conter is counting up or down
Hi Eslam
can you show us your project and code?
Regards!
PY
this code is supposed to set timer to fire after 300 millisecond use 32 KHZ clock but my problem is that it fires after 309.20794 millisecond
which is not accurate, I need necessarily more accuracy so how can I get that???
The following is the code that do what described above:
---------------------------------------------------------------
SetBits16(CLK_PER_REG, TMR_DIV, 0);
SetBits16(CLK_PER_REG, TMR_ENABLE ,1);
SetBits16(TIMER0_CTRL_REG,TIM0_CLK_SEL,0); // Select 32KHz input clock
SetBits16(TIMER0_CTRL_REG,TIM0_CLK_DIV,1);
SetBits16(TIMER0_CTRL_REG,TIM0_CTRL,0);
u32_tick=300 * 32
SetWord16(TIMER0_ON_REG,u32_tick);
NVIC_EnableIRQ(SWTIM_IRQn);
NVIC_ClearPendingIRQ(SWTIM_IRQn);
SetBits16(TIMER0_CTRL_REG,TIM0_CTRL,1); // Enable timer
-------------------------------------------------------------------------------------------
my second problem is to get current counter reading to get current time from it after starting the timer
I use this code to get it, but it's also not accurate and a lot of times it gets zero so how can I know the current time ??
u16_on_reg = GetWord16(TIMER0_ON_REG);
current_time = ( (u16_on_reg) / 32 );
Thanks,
Eslam
Hi Eslam,
I can't test your full project but here is some suggestion for you.
1. the 32KHz crystal(dont know whether you use external or internal) can't be exactly 32000HZ. So 300*32 can't give your exactly 300ms interval.
2. I dont know how you tested the timeout interval. If you use software approach(debug or enter ISR and set IO signal) it could be some system overhead. I will suggest you use PWM way to test whether the interval is steady. you can refer to peripheral_examples in our SDK.
3. I will not recommend you read TIMER0_ON_REG. For any generic timer, the counter is volatile and you dont know when it will change so the reading is not safe. For your purpose, maybe consider to reduce counter threshold and use your own code to accumulate. like below:
u32_tick=300;
and in isr
current_time ++;
Regards!
PY
1. I don't know which 32KHz crystal used internal or external but i configure it as mentioned in datasheet, i clear TIM0_CLK_SEL bit in TIMER0_CTRL_REG register
so please tell me which 32KHz crystal can i use ? and how get 300 ms exactly using 32KHZ ??
2——我测试通过切换在计时器r and show toggling periods using oscilloscope , if you recommend use pwm instead please tell me how can i cheek the intervals between timer firing ?? already i checked peripheral_examples and he told me that use buzzer but i don't have it
3- if it's not safe to read TIMER0_ON_REG, if there any other way to get timer value from registers as i forced from my system requirements to do it hardwaly.and already mentioned in datasheet that i can read TIMER0_ON_REG and actual counter value ON_CNTer is returned
would you please acknowledge that you recevied the message ?