关于多定时器的实现问题请教

2020-02-04 09:14发布

在本论坛学了不少东西,谢谢各位大虾了,今天我抛点砖,希望引点诸位大虾的玉来
我的问题如下:
如何在单片机中实现像PLC中的定时器应用
(1)可以是多个定时器,并且相互不干扰
(2)可以实现当条件满足时定时器开始计时,计时时间到后输出;条件不满足时定时器关闭,且自动清零计数值
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
11条回答
millwood0
2020-02-05 06:26
it will depend on your chip's hardware capabilities.

with a fast chip, you can simply use one counter and perform numerous test within the isr:

tmr_isr() {
  SysTick+=1; //increment systick counter
  for (i=0; i<TMR_MAX; i++) tmr[i].flag = (SysTick % tmr[i].period)?0:1; //set the flag if overflow
}

in your application, tmr.flag will be set if its period has passed.

Because of the '%' operator, this approach is not workable on a slow mcu to drive lots of software timers.

the 2nd approach would be to increment and test individual timers in the isr:

tmr_isr() {
  for (i=0; i<TMR_MAX; i++) {
    tmr[i].counter+=1; //increment timer;
    tmr[i].flag=(tmr[i].counter == tmr[i].period)?1:0; //set the flag
  }
}

if even the testing is too much, and your main loop is fast enough, you can put the testing portion of it over there.

generally, it will take you about 10 ticks to perform the various steps so if you are talking about 1ms isr period, you can do at most 100 software timers on a 1MIPS mcu, and practically 10 - 20 software timers if you want your mcu to perform other tasks.

一周热门 更多>