Re: [问题] 让两颗LED用不同的频率闪烁

楼主: descent (“雄辩是银,沉默是金”)   2017-08-04 14:27:03
不好意思, 我搞错了。coroutine.h 从下面网址下载。
https://www.chiark.greenend.org.uk/~sgtatham/coroutine.h
我用了 coroutine 做了类似的模拟, 因为实做 multitask 太费工夫。
这是纯 c 的方式, 完全不相依任何平台。
你先跑看看是不是你要的样子, 需要在 linux 终端机上测试。
简单来说, task2 第一次被呼叫时, 点灯, 第二次被呼叫时, 关灯。
补上画面:
https://goo.gl/o21jWg
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include "coroutine.h"
using namespace std;
#define W 10
#define H 5
#define X(x) "\x1b[xA"
#define Y(y) "\x1b[yC"
void x(int x)
{
cout << "\x1b[" << x << "A";
}
void y(int y)
{
cout << "\x1b["<< y << "C";
}
void xy(int x, int y)
{
cout << "\x1b[" << x << ";" << y << "H";
}
int task2();
int task1()
{
xy(1,1);
cout << "*" << endl;
task2();
usleep(100000);
xy(1,1);
cout << " " << endl;
usleep(100000);
}
int task2()
{
scrBegin;
xy(3,3);
cout << "*" << endl;
scrReturnV;
xy(3,3);
cout << " " << endl;
scrLine=0;
scrFinishV(-1);
}
int main(int argc, char *argv[])
{
system("clear");
#if 1
while(1)
{
task1();
}
#endif
return 0;
}
※ 引述《zzss2003 (brotherD)》之铭言:
: 开发平台(Platform):C language in embedded system
: 问题(Question):想让两颗LED用不同的频率闪烁
: 程式码(Code):
: void task1(void)
: {
: J11: LED1 ON
: J12: Delay 1s
: J13: LED1 OFF
: J14: Delay 1s
: }
: void task2(void)
: {
: J21: LED2 ON
: J22: Delay 2s
: J23: LED2 OFF
: J24: Delay 2s
: }
:
: 补充说明(Supplement):目前的想法是想用中断达到此功能,先执行J11,中断触发执行
: J21,中断执行完毕再回去执行J12,中断又触发在执行J22....以此类推。看似很简单的功
: 能,但要写程式时却不知道如何下手。
: 问题1: 第一个中断触发后,要怎么样让中断副程式进入task2这个function?
: 问题2: 进入task2这个function后,如何让中断副程式"只"做一行后就结束?
: 问题3: 第二个中断触发后,如何让第二个中断从第一个中断结束的地方继续做?
: 问题4: 如3,第二个中断结束后,要如何返回J12,而不是重新从J11开始执行?
: 问题5: 这些功能是不是可以只靠"中断+if else"就达到?最多再多一个timer
: 问题6: 我该如何思考问题?虽然我现在有办法把问题列出来(并不完全),但最重要的是,
: 我要如何"思考"这些问题?
: 谢谢各位前辈的指点与指教,小弟感激不尽。

Links booklink

Contact Us: admin [ a t ] ucptt.com