找回密码
 立即注册
楼主: 王昱顺

【协程方式】实现【多任务调度 / 多线程】 仅需2byte的RAM 并带有详细视频讲解

[复制链接]
  • 打卡等级:以坛为家I
  • 打卡总天数:306
  • 最近打卡:2025-06-15 06:48:42

0

主题

206

回帖

1706

积分

金牌会员

积分
1706
发表于 2025-4-27 08:42:55 | 显示全部楼层
TASK_Test.hex文件。文件时间显示,没有同时编译成功呢
这算问题吗?
是不能更改内容了吗?

Rebuild target 'Target 1'
User command #1: .\Auto_Keil.exe
compiling main.c...
compiling set_task.c...
compiling set_io.c...
compiling set_timer.c...
linking...
L251 LINKER/LOCATER V4.66.93.0 - SN: Eval Version
COPYRIGHT ARM Germany GmbH 1995 - 2018
@.\HEXFIL~1\TASK_T~1.LNP ".\Hex File\main.obj",
".\AI8051U_32_MDU32.LIB",
".\AI8051U_32_TFPU.LIB",
".\Hex File\set_task.obj",
".\Hex File\set_io.obj",
".\Hex File\set_timer.obj"
TO ".\Hex File\TASK_Test"
PRINT(".\Listings\TASK_Test.map") CASE DISABLEWARNING (16,57,30)
REMOVEUNUSED
CLASSES (EDATA (0x0-0x7FF),
HDATA (0x0-0x7FF))
******************************************************************************
* RESTRICTED VERSION WITH 0800H BYTE CODE SIZE LIMIT; USED: 122EH BYTE (227%) *
******************************************************************************
Program Size: data=8.0 edata+hdata=452 xdata=0 const=191 code=6811
*** ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED
    LIMIT:   0800H BYTES
Target not created.
Build Time Elapsed:  00:00:01

截图202504270841103422.jpg
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:566
  • 最近打卡:2025-06-15 00:22:21

11

主题

1192

回帖

5516

积分

论坛元老

积分
5516
发表于 2025-4-27 09:16:45 | 显示全部楼层
大*** 发表于 2025-4-27 08:42
TASK_Test.hex文件。文件时间显示,没有同时编译成功呢
这算问题吗?
是不能更改内容了吗?

你的Keil C251 还没有注册。

Keil软件要注册才行。不注册只能编译小于2KByte。

和谐一下,找下注册机
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:306
  • 最近打卡:2025-06-15 06:48:42

0

主题

206

回帖

1706

积分

金牌会员

积分
1706
发表于 2025-4-28 08:55:07 | 显示全部楼层

我也奇怪,原先注册过了,一直用好好的,原来,
C251,过期了。
已经重新注册。
感谢,提醒!

回复 支持 反对

使用道具 举报 送花

  • 打卡等级:偶尔看看I
  • 打卡总天数:16
  • 最近打卡:2025-06-13 08:35:04

1

主题

25

回帖

145

积分

等待验证会员

积分
145
发表于 2025-5-6 14:37:39 | 显示全部楼层
例程是在C251下运行的8051U吗?

点评

是的哩, 实际上只要是c语言环境都可以使用  详情 回复 发表于 2025-5-6 14:58
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:498
  • 最近打卡:2025-06-15 10:18:37
已绑定手机
已实名认证

112

主题

2387

回帖

6004

积分

版主

积分
6004
发表于 2025-5-6 14:58:58 | 显示全部楼层
明月*** 发表于 2025-5-6 14:37
例程是在C251下运行的8051U吗?

是的哩, 实际上只要是c语言环境都可以使用
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:偶尔看看II
  • 打卡总天数:28
  • 最近打卡:2025-06-14 08:02:36

4

主题

78

回帖

646

积分

高级会员

积分
646
发表于 2025-5-19 18:09:36 | 显示全部楼层
Protothreads
希望这个世界让我装一次B
回复 支持 1 反对 0

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:4
  • 最近打卡:2025-05-20 11:42:07
已绑定手机

3

主题

13

回帖

97

积分

注册会员

积分
97
发表于 2025-5-29 10:31:13 | 显示全部楼层
也可以看看这个: https://dunkels.com/adam/pt/, 原理是一样的, 都是无堆栈协程, 这个通用库功能多一点

点评

Protothreads,非常简洁、精巧、实用,值得一读  发表于 2025-6-6 10:36
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:96
  • 最近打卡:2025-06-11 17:55:55

1

主题

26

回帖

51

积分

注册会员

积分
51
发表于 2025-5-30 15:55:03 | 显示全部楼层
可以的
回复

使用道具 举报 送花

  • 打卡等级:以坛为家II
  • 打卡总天数:540
  • 最近打卡:2025-06-14 14:56:28

1

主题

185

回帖

2001

积分

金牌会员

积分
2001
发表于 2025-6-6 11:14:57 | 显示全部楼层
hhh*** 发表于 2025-4-24 16:05
翻译了楼主首页的协程2:
_this_task=2;
if(task_num

这段代码确实没问题,有点令人意外,case 是可以放在 switch 中的代码块里面的。

Duff's device

The famous "Duff's device" in C makes use of the fact that a `case` statement is still legal within a sub-block of its matching `switch` statement. Tom Duff used this for an optimised output loop:

    switch (count % 8)
    {
        case 0:        do {  *to = *from++;
        case 7:              *to = *from++;
        case 6:              *to = *from++;
        case 5:              *to = *from++;
        case 4:              *to = *from++;
        case 3:              *to = *from++;
        case 2:              *to = *from++;
        case 1:              *to = *from++;
                       } while ((count -= 8) > 0);
    }
```

We can put it to a slightly different use in the coroutine trick. Instead of using a `switch` statement to decide which `goto` statement to execute, we can use the `switch` statement to perform the jump itself:

int function(void)
{
    static int i, state = 0;
    switch (state)
    {
        case 0: /* start of function */
        for (i = 0; i < 10; i++)
        {
            state = 1; /* so we will come back to "case 1" */
            return i;
            case 1:; /* resume control straight after the return */
        }
    }
}

Now this is looking promising. All we have to do now is construct a few well chosen macros, and we can hide the gory details in something plausible-looking:

```
#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void)
{
    static int i;
    crBegin;
    for (i = 0; i < 10; i++)  crReturn(1, i);
    crFinish;
}
```

(note the use of `do ... while(0)` to ensure that `crReturn` does not need braces around it when it comes directly between `if` and `else`)

This is almost exactly what we wanted. We can use `crReturn` to return from the function in such a way that control at the next call resumes just after the return. Of course we must obey some ground rules (surround the function body with `crBegin` and `crFinish`; declare all local variables `static` if they need to be preserved across a `crReturn`; *never* put a `crReturn` within an explicit `switch` statement); but those do not limit us very much.

The only snag remaining is the first parameter to `crReturn`. Just as when we invented a new label in the previous section we had to avoid it colliding with existing label names, now we must ensure all our state parameters to `crReturn` are different. The consequences will be fairly benign - the compiler will catch it and not let it do horrible things at run time - but we still need to avoid doing it.

Even this can be solved. ANSI C provides the special macro name `__LINE__`, which expands to the current source line number. So we can rewrite `crReturn` as

```
#define crReturn(x) do { state=__LINE__; return x; \
                         case __LINE__:; } while (0)
```

and then we no longer have to worry about those state parameters at all, provided we obey a fourth ground rule (never put two `crReturn` statements on the same line).
回复 支持 反对

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-6-16 04:16 , Processed in 0.669160 second(s), 109 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表