I have a problem when i run the code given below. Here i have a 1000 Hz timer and the periodic work is done in main loop. But when i measure the duration between the successive timer interrupts, there is an additional 2 ms time shift for every 11th measurement. This problem disappears when i comment out sine creation line. What could be the problem here?
Code: Select all
import pyb import utime from math import pi, sin Timer2 = pyb.Timer(2, freq = 1, mode=pyb.Timer.UP) sample_flag = 0 def isr_sampling_timer(timer): global sample_flag sample_flag = 1 def main(): global sample_flag time_new = 0 time_old = 0 cnt_signal = 0 time_arr = [0.0 for i in range(100)] cnt = 0 signal_input = 0 Timer2.freq(1000) Timer2.callback(isr_sampling_timer) while True: if sample_flag == 1: if cnt_signal%50 == 0: time_new = utime.ticks_us() time_arr[cnt] = utime.ticks_diff(time_new, time_old) time_old = time_new cnt = cnt + 1 signal_input = 3*sin((2*pi*0.2*cnt_signal/1000)+0) + 7 cnt_signal = cnt_signal + 1 if cnt == 100: Timer2.callback(None) for i in range(100): print(time_arr[i]) sample_flag = 0 main()