2323#include "wiring_private.h"
2424
2525// the prescaler is set so that timer4 ticks every 64 clock cycles, and the
26- // the overflow handler is called every 256 ticks.
27- #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256 ))
26+ // the overflow handler is called every 250 ticks.
27+ #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 250 ))
2828
2929// the whole number of milliseconds per timer4 overflow
3030#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
@@ -46,18 +46,25 @@ INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, ITC_IRQ_TIM4_OVF) /* TIM4 UPD/OVF */
4646 // copy these to local variables so they can be stored in registers
4747 // (volatile variables must be read from memory on every access)
4848 unsigned long m = timer4_millis ;
49+ #if (FRACT_INC != 0 )
4950 unsigned char f = timer4_fract ;
51+ #endif
5052
5153 m += MILLIS_INC ;
54+ #if (FRACT_INC != 0 )
5255 f += FRACT_INC ;
5356 if (f >= FRACT_MAX ) {
5457 f -= FRACT_MAX ;
5558 m += 1 ;
5659 }
5760
5861 timer4_fract = f ;
62+ #endif
5963 timer4_millis = m ;
6064 timer4_overflow_count ++ ;
65+
66+ /* Clear Interrupt Pending bit */
67+ TIM4_ClearITPendingBit (TIM4_IT_UPDATE ); // TIM4->SR1 = (uint8_t)(~0x01);
6168}
6269
6370unsigned long millis ()
@@ -249,6 +256,9 @@ void delayMicroseconds(unsigned int us)
249256
250257void init ()
251258{
259+ // set the clock to 16 MHz
260+ CLK_HSIPrescalerConfig (CLK_PRESCALER_HSIDIV1 );
261+
252262 GPIO_DeInit (GPIOA );
253263 GPIO_DeInit (GPIOB );
254264 GPIO_DeInit (GPIOC );
@@ -259,10 +269,14 @@ void init()
259269
260270 // set timer 0 prescale factor to 64, period 0 (=256)
261271 TIM4_DeInit ();
262- TIM4_TimeBaseInit (TIM4_PRESCALER_64 , 0 );
263-
264- // enable timer 0 overflow interrupt
265- TIM4_ITConfig (TIM4_IT_UPDATE , ENABLE );
272+ TIM4_TimeBaseInit (TIM4_PRESCALER_64 , 249 );
273+ /* Clear TIM4 update flag */
274+ TIM4_ClearFlag (TIM4_FLAG_UPDATE ); // TIM4->SR1 = (uint8_t)(~0x01);
275+ /* Enable update interrupt */
276+ TIM4_ITConfig (TIM4_IT_UPDATE , ENABLE ); // TIM4->IER |= (uint8_t)TIM4_IT;
277+ /* Enable TIM4 */
278+ TIM4_Cmd (ENABLE ); // TIM4->CR1 |= TIM4_CR1_CEN;
279+
266280
267281#if 0 //FIXME
268282 // timers 1 and 2 are used for phase-correct hardware pwm
0 commit comments