File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1088,6 +1088,10 @@ STATIC mp_obj_t pyb_timer_freq(mp_uint_t n_args, const mp_obj_t *args) {
10881088 uint32_t prescaler = compute_prescaler_period_from_freq (self , args [1 ], & period );
10891089 self -> tim .Instance -> PSC = prescaler ;
10901090 __HAL_TIM_SetAutoreload (& self -> tim , period );
1091+ // Reset the counter to zero. Otherwise, if counter >= period it will
1092+ // continue counting until it wraps (at either 16 or 32 bits depending
1093+ // on the timer).
1094+ __HAL_TIM_SetCounter (& self -> tim , 0 );
10911095 return mp_const_none ;
10921096 }
10931097}
@@ -1118,6 +1122,10 @@ STATIC mp_obj_t pyb_timer_period(mp_uint_t n_args, const mp_obj_t *args) {
11181122 } else {
11191123 // set
11201124 __HAL_TIM_SetAutoreload (& self -> tim , mp_obj_get_int (args [1 ]) & TIMER_CNT_MASK (self ));
1125+ // Reset the counter to zero. Otherwise, if counter >= period it will
1126+ // continue counting until it wraps (at either 16 or 32 bits depending
1127+ // on the timer).
1128+ __HAL_TIM_SetCounter (& self -> tim , 0 );
11211129 return mp_const_none ;
11221130 }
11231131}
You can’t perform that action at this time.
0 commit comments