Setup a continuous arduino timer?

1 次查看(过去 30 天)
Hello! I'm essentially building a bike speedometer and i want to initialize a timer in matlab that begins when the arduino turns on. Preferably, I would have a variable t which updates every tenth of a milisecond. My idea was to take the value of this variable every time i had an external interupt (this interupt is produced by a magnetic field sensor which actuates every time a wheel makes a revolution) and take the time between interrupts in order to get the frequency of rotation of a wheel. My question ishow do I setup some sort of counter that records the time (or rather change in time since any arbitrary start) down to miliseconds or tenths of miliseconds and be able to take this value as it continuously updates for the rest of my code. any advice is appreciated, I'm having some trouble looking through online resources.
  1 个评论
Geoff Hayes
Geoff Hayes 2020-3-15
Declan - do you really mean you want to update the variable every tenth of a millisecond? Because that implies that the period of the timer would need to be 0.0001 which seems to be unsupported (see period description) as the minimum allowed value is one millisecond (0.001). If one millisecond is sufficient, then you could nest your timer callback within your main function so that the timer callback has access to the variable t. Perhaps something like
function myTimerTest
t = 0;
cumulativeTimerCallbackStart = 0;
periodSec = 0.001;
numTasksToExecute = 100;
myTimer = timer('Name','MyTimer', ...
'StartDelay', periodSec, ...
'ExecutionMode','fixedRate', ...
'TimerFcn',{@timerCallback}, ...
'TasksToExecute', numTasksToExecute, ...
'StartFcn', @startCallback, ...
'StopFcn', @stopCallback, ...
'Period', periodSec);
start(myTimer);
function timerCallback(hObject, eventdata)
t = toc(cumulativeTimerCallbackStart);
end
function startCallback(hObject, eventdata)
cumulativeTimerCallbackStart = tic;
end
function stopCallback(hObject, eventdata)
fprintf('Elapsed time after %d iterations: %.4f\n', numTasksToExecute, t);
end
where tic and toc are used to get the elapsed time between each timer callback and so "should" be a decent t. Note that the smaller the period, the greater the delay introduced between each callback firing (so you won't necessarily see the callback fire every 1 ms if that is what you have set the period to be).

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Clocks and Timers 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by