简体   繁体   English

8051 的同时任务

[英]simultaneous tasks with 8051

Is there any way to run two tasks with the 8051 μC simultaneously?有什么方法可以同时使用 8051 μC 运行两个任务? For example,例如,

Task one任务一

Delay 1 sec
P2.B2 = 1
Delay 1 sec
P2.B2 = 0

Task 2任务 2

If P1.B0 = 1
P2.B3=1

So at any time, press the switch connected to P2.0 is 1, LED at P2.3=ON, and P2.2 keeps LED at P2.2 blinking.所以在任何时候,按下连接P2.0的开关为1,P2.3的LED=ON,P2.2的LED保持在P2.2的闪烁。

A task is something what is typically provided by the underlying OS. task是通常由底层操作系统提供的东西。 If you are running on a bare metal system without any OS, you have no tasks at the first point.如果您在没有任何操作系统的裸机系统上运行,那么您一开始就没有任务。

But your application can build its own tasks.但是您的应用程序可以构建自己的任务。 The job is more or less easy.这项工作或多或少很容易。 You have to build a scheduler, typically triggered by a hardware clock for task switching, create stacks for each of the tasks and some control structures for the maintenance of the tasks.您必须构建一个调度程序,通常由用于任务切换的硬件时钟触发,为每个任务创建堆栈以及一些用于维护任务的控制结构。 As you have no MMU and no memory protection on bare metal systems like 8051, you simply can modify stack pointers to do the task switching.由于在 8051 等裸机系统上没有 MMU 和 memory 保护,您只需修改堆栈指针即可进行任务切换。

That is exactly what a library like FreeRTos can do for you.这正是像 FreeRTos 这样的库可以为您做的事情。 There is a port for 8051 available as I know.据我所知,有一个 8051 端口可用。 Searching on the web returns a lot of links to 8051 FreeRtos.在 web 上搜索会返回大量指向 8051 FreeRtos 的链接。 Maybe there are some more libraries which offering tasks for you.也许还有更多的图书馆可以为您提供任务。

But mostly the overhead of scheduling and all the administration effort is much to high.但主要是调度的开销和所有的管理工作都非常高。 Running an endless loop which is doing some jobs by reading some kind of queues or flags is much easier and often the more efficient solution.运行一个通过读取某种队列或标志来完成某些工作的无限循环要容易得多,而且通常是更有效的解决方案。 Also running some jobs in interrupt service routines fits well to bare metal requirements.在中断服务例程中运行一些作业也非常适合裸机要求。

I assume you are running on bare metal with no battery saving requirements.我假设您在没有节电要求的裸机上运行。 I assume you can now write a program, load it to your device and run it.我假设您现在可以编写一个程序,将其加载到您的设备并运行它。 What I suggest you do is roughly this.我建议你做的大致是这样的。

This program should have a main loop, which in its most simple would be like this:这个程序应该有一个主循环,最简单的应该是这样的:

MAX_TIME is the largest possible value of system clock, should never be reached

task_table is table with 
    next execution time as system clock time (MAX_TIME means disabled)
    function pointer

initialize task-table with the three tasks below

forever
  for each task with time 0
    set task time MAX_TIME (disable)
    call task function (task probably enables itself or other task)

  find a task with lowest non-zero time in task_queue 
  if task time is in past or now
    set task time MAX_TIME (disable)
    call task function (task probably enables itself or other task)

Time 0 tasks are checked separately, and then tasks with time, so that the time 0 tasks don't block each others or the tasks with time from ever being called.时间 0 的任务被单独检查,然后是时间任务,这样时间 0 的任务就不会互相阻塞,也不会阻止时间任务被调用。 Same could be achieved in different ways, this is just an example.同样可以通过不同的方式实现,这只是一个例子。

Then your requirements really call for 3 "tasks":那么您的要求确实需要 3 个“任务”:

task_p2_b2_0:
  P2.B2 = 0
  enable task task_p2_b2_1 at current_time + 1 second


task_p2_b2_1:
  P2.B2 = 1
  enable task task_p2_b2_0 at current_time + 1 second


task_p1_b0_poll:
  If P1.B0 = 1
    P2.B3=1
  enable task task_p1_b0_poll at time 0 (or current time + 10 ms or whatever)

Future development: Above is for a small number of static tasks.未来发展:以上是针对少量static任务。 Iterating up to... 5-10 item table is so fast that there is no point trying to optimize it.迭代到... 5-10 项表是如此之快,以至于没有必要尝试对其进行优化。 Once you have more tasks than that, you should consider using a priority heap to store the tasks.一旦你有更多的任务,你应该考虑使用优先级堆来存储任务。 Then you could also consider making main loop sleep when it has nothing to do, and use interrupt to wake it up (timer interrupt, serial port interrupt, pin activation interrupt etc).然后你也可以考虑让主循环在它无事可做的时候休眠,并使用中断来唤醒它(定时器中断、串口中断、引脚激活中断等)。 Also, you could have different task types, such as tasks which are activated when there is some IO (button press, byte from serial port, whatever).此外,您可能有不同的任务类型,例如当有一些 IO(按钮按下,来自串行端口的字节等)时激活的任务。 Etc. At the upper end of this adding features is a complete operating system really, but for simple things what I wrote above is really enough.等等。在这个添加功能的上端确实是一个完整的操作系统,但对于简单的事情,我上面写的真的足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM