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

2020-02-04 09:14发布

在本论坛学了不少东西,谢谢各位大虾了,今天我抛点砖,希望引点诸位大虾的玉来
我的问题如下:
如何在单片机中实现像PLC中的定时器应用
(1)可以是多个定时器,并且相互不干扰
(2)可以实现当条件满足时定时器开始计时,计时时间到后输出;条件不满足时定时器关闭,且自动清零计数值
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
10条回答
huayuliang
1楼-- · 2020-02-04 11:47
 精彩回答 2  元偷偷看……
rclong
2楼-- · 2020-02-04 17:42
单片机里采用定时器+时标的办法
参考 时间触发嵌入式系统设计模式
jswd0810
3楼-- · 2020-02-04 22:06
我现在结构是这样的:
void step0()
{
    T1(100);
    if(flag_finish)
   {
        step++;
       flag_finish=0;
    }
}
void step1()
{
    T2(100);
    if(flag_finish1)
   {
        step++;
       flag_finish1=0;
    }
}
......
void T1(uchar i)
{
    if(flag_20ms)
   {
      flag_20ms=0;
     if(++count>=i)
    {
     flag_finish=1;
    counter=0;
    }  
}

T2也是如此,是不是可以这样理解两位老师
anvy178
4楼-- · 2020-02-05 02:35
用软件实现的话 会被中断的,中断时间短的话 没什么大碍 长一点 你都不行了 ,你1MS输出的东西  说不定2MS才能输出.
millwood0
5楼-- · 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.
jswd0810
6楼-- · 2020-02-05 07:22
 精彩回答 2  元偷偷看……

一周热门 更多>