cjr82123 发表于 2009-10-15 20:46:01

前后台系统的多任务运行的疑问?

用C语言编程已经1年多,一直以来都有一个疑问:
在大循环+中断(前后台系统)中,如何能使多个任务得到及时的响应?

举例:(编译器:GCC+AVRStudio Ver 4.14)

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

/*********************全局变量区********************/
volatile unsigned char Temp_Flag = 0; //温度采集标志位
volatile unsigned char Key_Flag= 0; //键盘扫描标志位
volatile unsigned char Time_Flag = 0; //时间刷新标志位

/*************系统初始化子函数*************/
void System_Initial(void)
{
.........; //省略
}

/*************中断服务子函数*************/
ISR(...) //系统每10ms产生一次中断请求,即时基为10ms.
{
++Temp_Flag;
++Key_Flag;
++Time_Flag;
}

/*************主函数*************/
int main(void)
{
cli(); //关全局中断
System_Initial(); //系统初始化
sei();
while(1)
{

/**********************任务1***********************/
if(tempFlag == 10) //每100ms调用一次温度采集
{
   tempFlag = 0;
   Fun1(); //读取传感器温度大概花费800ms的时间.
   }

/**********************任务2***********************/
   if(keyFlag == 1) //每10ms调用一次键盘扫描(状态机)
{
   keyFlag = 0;
   switch(KeyScan())
   {
    case 1:
             Fun2(); //此函数大约花费100ms时间.
                   break;
    case 2:
             Fun3(); //此函数大约花费200ms时间.
                   break;
    default: break;   
    }
   }

   /**********************任务3***********************/
   if(timeFlag == 100) //每1s更新时间并在LCD1602显示
{
   timeFlag = 0;
   Fun4(); //此函数大约花费400ms时间.
   }
}
}

有时候,系统运行到Fun1()函数的时候按下键盘,此时无法得到相应,当然以上多任务采用RTOS可以方便的解决,但是我想知道在前后台系统中遇到2个或以上的任务时,有什么更好的方法使各个任务得到及时的响应?

NJ8888 发表于 2009-10-15 20:56:54

那你可以在中断中处理按键

snoopyzz 发表于 2009-10-15 21:01:12

把func都拆成一步一步,状态机

jackiezeng 发表于 2009-10-15 21:03:40

每个任务尽量减小运行时间,,,,不要在任务里等,,,

cjr82123 发表于 2009-10-15 21:05:08

To LS:
      先谢谢,你的方法我觉得不行,Fun2()和Fun3()函数执行大概花费100、200ms的时间,在ISR中执行的键盘处理的时候,其他的任务也是无法得到及时的响应。

cjr82123 发表于 2009-10-15 21:23:34

To snoopyzz:
像你所说的,就等于利用定时器轮询各个任务,这样对吗?

To jackiezeng:
当然如果各个任务的运行时间都很小的话最好不过了。
当我以前遇到过读取DB18B20温度时(12-Bit)Conversion Time大概花费750-800ms,这样的情况是无法减少任务的时间。

windy__xp 发表于 2009-10-15 22:24:26

第一:
LZ的程序与注释有些矛盾的地方,比如任务1

if(++tempFlag == 10) //每100ms调用一次温度采集

这个不是100ms执行一次,因为 ++tempFlag 在主循环中自增,中断中自增的变量不知道用来做什么的。


第:
    Fun1(); //读取传感器温度大概花费800ms的时间.
    看到这样的程序结构就头大,前后台系统一个函数处理800ms不释放MCU,用多任务系统算了。

第三:看了你的回复,一个程序在单核的MCU中运行,再多任务也是分时使用MCU的,都想保证立马执行,可能吗?这个要根据任务的轻重缓急,以及允许响应延迟的时间来安排,如果使用前后台系统,那么就尽量不要出现执行800ms的函数,并且把事实性要求高的任务使用中断处理.

    看了你的函数,我肯定地说,你程序中很多地方是死等,而不是使用状态查询的方式。800ms算算1MPIS的单片机,执行了多少条指令了? 2L的DX说得很明白,“把func都拆成一步一步,状态机 ”。

windy__xp 发表于 2009-10-15 22:40:00

什么叫状态机,我举个例子说明吧。一个函数,你要执行 Setp1 2 3 ,但是每步之间,都需要等待某个状态100ms,才能执行setp2。

   你的程序结构:

   step1;            // 第一步开始
   delay100ms;       // 第一步执行完毕,注意:这个期间MCU是浪费来等外设执行STEP1完毕的
   step2;            // 第二步开始
   delay100ms;       // 第二步执行完毕,注意:这个期间MCU是浪费来等外设执行STEP2完毕的
   step3;


   而状态机,你可以简单的分为三个状态,设置一个status 变量,表示当前执行的状态。

   int status = 0;

   switch( status ) {

       case 0: {
            step1;                // 第一步开始
            status++;             // 到下一个状态
            break;                //
       }                        //
      
       case 1: {
            if( 第一步完毕 ){
                step2;            // 第二步开始
                status++;         // 到下一个状态
            }
            break;

       case 2: {
            if( 第二步完毕 ){
                step3;            // 第三步开始
                status = 0;       // 整个任务完毕,回到开始
            }
            break;
      }
      default:
            status = 0;         // 这里,说明状态出错了,这里可以加些错误的报警提示
            break;                //
    }


    这样以来,就不用死等了。

fangmcu 发表于 2009-10-15 22:44:13

受教了,期待更精彩的讨论了!!

hoverlin 发表于 2009-10-15 23:11:34

这样的设计可以参考UIP的Protothreads的方法,对前后台系统来说,是一个相当不错的抽象方案。基本原理是用状态机,或者地址laber在while(1)系统中模拟多任务行为。

/*
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: pt.h,v 1.2 2006/06/12 08:00:30 adam Exp $
*/

/**
* \addtogroup pt
* @{
*/

/**
* \file
* Protothreads implementation.
* \author
* Adam Dunkels <adam@sics.se>
*
*/

#ifndef __PT_H__
#define __PT_H__

#include "lc.h"

struct pt {
lc_t lc;
};

#define PT_WAITING 0
#define PT_EXITED1
#define PT_ENDED   2
#define PT_YIELDED 3

/**
* \name Initialization
* @{
*/

/**
* Initialize a protothread.
*
* Initializes a protothread. Initialization must be done prior to
* starting to execute the protothread.
*
* \param pt A pointer to the protothread control structure.
*
* \sa PT_SPAWN()
*
* \hideinitializer
*/
#define PT_INIT(pt)   LC_INIT((pt)->lc)

/** @} */

/**
* \name Declaration and definition
* @{
*/

/**
* Declaration of a protothread.
*
* This macro is used to declare a protothread. All protothreads must
* be declared with this macro.
*
* \param name_args The name and arguments of the C function
* implementing the protothread.
*
* \hideinitializer
*/
#define PT_THREAD(name_args) char name_args

/**
* Declare the start of a protothread inside the C function
* implementing the protothread.
*
* This macro is used to declare the starting point of a
* protothread. It should be placed at the start of the function in
* which the protothread runs. All C statements above the PT_BEGIN()
* invokation will be executed each time the protothread is scheduled.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)

/**
* Declare the end of a protothread.
*
* This macro is used for declaring that a protothread ends. It must
* always be used together with a matching PT_BEGIN() macro.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
                   PT_INIT(pt); return PT_ENDED; }

/** @} */

/**
* \name Blocked wait
* @{
*/

/**
* Block and wait until condition is true.
*
* This macro blocks the protothread until the specified condition is
* true.
*
* \param pt A pointer to the protothread control structure.
* \param condition The condition.
*
* \hideinitializer
*/
#define PT_WAIT_UNTIL(pt, condition)                \
do {                                                \
    LC_SET((pt)->lc);                                \
    if(!(condition)) {                                \
      return PT_WAITING;                        \
    }                                                \
} while(0)

/**
* Block and wait while condition is true.
*
* This function blocks and waits while condition is true. See
* PT_WAIT_UNTIL().
*
* \param pt A pointer to the protothread control structure.
* \param cond The condition.
*
* \hideinitializer
*/
#define PT_WAIT_WHILE(pt, cond)PT_WAIT_UNTIL((pt), !(cond))

/** @} */

/**
* \name Hierarchical protothreads
* @{
*/

/**
* Block and wait until a child protothread completes.
*
* This macro schedules a child protothread. The current protothread
* will block until the child protothread completes.
*
* \note The child protothread must be manually initialized with the
* PT_INIT() function before this function is used.
*
* \param pt A pointer to the protothread control structure.
* \param thread The child protothread with arguments
*
* \sa PT_SPAWN()
*
* \hideinitializer
*/
#define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))

/**
* Spawn a child protothread and wait until it exits.
*
* This macro spawns a child protothread and waits until it exits. The
* macro can only be used within a protothread.
*
* \param pt A pointer to the protothread control structure.
* \param child A pointer to the child protothread's control structure.
* \param thread The child protothread with arguments
*
* \hideinitializer
*/
#define PT_SPAWN(pt, child, thread)                \
do {                                                \
    PT_INIT((child));                                \
    PT_WAIT_THREAD((pt), (thread));                \
} while(0)

/** @} */

/**
* \name Exiting and restarting
* @{
*/

/**
* Restart the protothread.
*
* This macro will block and cause the running protothread to restart
* its execution at the place of the PT_BEGIN() call.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_RESTART(pt)                                \
do {                                                \
    PT_INIT(pt);                                \
    return PT_WAITING;                        \
} while(0)

/**
* Exit the protothread.
*
* This macro causes the protothread to exit. If the protothread was
* spawned by another protothread, the parent protothread will become
* unblocked and can continue to run.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_EXIT(pt)                                \
do {                                                \
    PT_INIT(pt);                                \
    return PT_EXITED;                        \
} while(0)

/** @} */

/**
* \name Calling a protothread
* @{
*/

/**
* Schedule a protothread.
*
* This function shedules a protothread. The return value of the
* function is non-zero if the protothread is running or zero if the
* protothread has exited.
*
* \param f The call to the C function implementing the protothread to
* be scheduled
*
* \hideinitializer
*/
#define PT_SCHEDULE(f) ((f) == PT_WAITING)

/** @} */

/**
* \name Yielding from a protothread
* @{
*/

/**
* Yield from the current protothread.
*
* This function will yield the protothread, thereby allowing other
* processing to take place in the system.
*
* \param pt A pointer to the protothread control structure.
*
* \hideinitializer
*/
#define PT_YIELD(pt)                                \
do {                                                \
    PT_YIELD_FLAG = 0;                                \
    LC_SET((pt)->lc);                                \
    if(PT_YIELD_FLAG == 0) {                        \
      return PT_YIELDED;                        \
    }                                                \
} while(0)

/**
* \brief      Yield from the protothread until a condition occurs.
* \param pt   A pointer to the protothread control structure.
* \param cond The condition.
*
*             This function will yield the protothread, until the
*             specified condition evaluates to true.
*
*
* \hideinitializer
*/
#define PT_YIELD_UNTIL(pt, cond)                \
do {                                                \
    PT_YIELD_FLAG = 0;                                \
    LC_SET((pt)->lc);                                \
    if((PT_YIELD_FLAG == 0) || !(cond)) {        \
      return PT_YIELDED;                        \
    }                                                \
} while(0)

/** @} */

#endif /* __PT_H__ */

/** @} */

ShangGuan 发表于 2009-10-15 23:13:25

Protothreads用好了,确实很不错。

cjr82123 发表于 2009-10-16 00:22:45

1.非常感谢windy__xp 龙啸的精彩回复,你在回复中所提到的错误是对的,我已经修改过了,应该是:
/**********************任务1***********************/
if(tempFlag == 10) //每100ms调用一次温度采集
{
   tempFlag = 0;
   Fun1(); //读取传感器温度大概花费800ms的时间.
   }

/**********************任务2***********************/
   if(keyFlag == 1) //每10ms调用一次键盘扫描(状态机)
{
   keyFlag = 0;
   switch(KeyScan())
   {
    case 1:
       Fun2(); //此函数大约花费100ms时间.
   break;
    case 2:
       Fun3(); //此函数大约花费200ms时间.
   break;
    default: break;   
    }
   }

   /**********************任务3***********************/
   if(timeFlag == 100) //每1s更新时间并在LCD1602显示
{
   timeFlag = 0;
   Fun4(); //此函数大约花费400ms时间.
   }
}
}

2.另外在你所说的程序结构:

step1;            // 第一步开始
delay100ms;       // 第一步执行完毕,注意:这个期间MCU是浪费来等外设执行STEP1完毕的
step2;            // 第二步开始
delay100ms;       // 第二步执行完毕,注意:这个期间MCU是浪费来等外设执行STEP2完毕的
step3;

呵呵,经常用状态机键盘,从来没有用过基于状态机的函数,明天试试!

cjr82123 发表于 2009-10-16 00:26:44

非常感谢hoverlin所提到的UIP的Protothreads的方法!
等试验完状态机函数后,详细研究一下Protothreads.

ba_wang_mao 发表于 2009-10-16 08:50:02

其实多任务编程思路和状态机差不多。
  多任务编程:无非是由操作系统来调度。(从就绪表中选中一个优先级最高的任务执行)

yzlyear 发表于 2009-10-16 10:24:45

不错的文章

Edesigner 发表于 2009-10-16 17:02:54

读取传感器温度大概花费800ms的时间,要这么久吗?你得检讨一下。30 us就足够有余。
键盘是硬件层和系统层做的做的不是用到按键时才去扫描。

wukaka 发表于 2010-7-21 10:19:20

精彩的帖子!感谢【7楼】 windy__xp 龙笑和【9楼】 hoverlin 所说的方法!

ilovezeno 发表于 2010-7-21 11:02:48

pt是个好东西.....
状态机的状态定义最好用enum而不是用define,这样可以方便的设置和加入偏移量,还能防止出错.....

ringan865 发表于 2010-7-21 13:45:40

mark

joni 发表于 2010-7-22 02:18:15

mark

zhaojun_xf 发表于 2010-7-22 06:01:51

最近也用到类似的方法,感觉不错。

liningsky 发表于 2012-1-4 00:22:47

amrk

eddia2012 发表于 2012-11-16 11:19:53

这样的设计可以参考UIP的Protothreads的方法,对前后台系统来说,是一个相当不错的抽象方案。基本原理是用状态机,或者地址laber在while(1)系统中模拟多任务行为。

------------------------------------------

10楼讲的很是。

jz701209李 发表于 2013-4-8 13:31:37

学习一下....

gaobao_1 发表于 2013-11-22 12:21:19

学习了下状态的原理,谢谢!


湛泸骏驰 发表于 2014-4-1 08:39:43

学习了一下。谢谢。

Free_Bird 发表于 2014-4-5 21:50:14

正在研究10楼的内容

jack_yu 发表于 2014-4-5 22:44:39

学习了谢谢!1

hejie126 发表于 2014-7-27 20:45:13

3楼正解
页: [1]
查看完整版本: 前后台系统的多任务运行的疑问?