protothreads有啥优点?这样使用有啥意义?
刚刚看protothreads,各位帮忙说说,这个东西到底有啥优点啊?我写了个程序,不知道这样用有没有意义?
程序功能:每隔1秒亮灭一次D口LED
每隔10秒亮灭一次b口LED
#include"pt.h"
#include"avr/io.h"
#include"util/delay.h"
static int protothread1_flag, protothread2_flag,counter,counter1;
static int
protothread1(struct pt *pt)
{
PT_BEGIN(pt);
while(1) {
PT_WAIT_UNTIL(pt, counter==1);
PORTD=~PORTD;
counter=0;
}
PT_END(pt);
}
static int
protothread2(struct pt *pt)
{
PT_BEGIN(pt);
while(1) {
protothread2_flag = 1;
PT_WAIT_UNTIL(pt, counter1==10);
counter1=0;
PORTB = ~PORTB;
protothread1_flag = 0;
}
PT_END(pt);
}
static struct pt pt1, pt2;
int
main(void)
{
DDRD =0XFF;
DDRB =0XFF;
PORTD=0XFF;
PORTB=0XFF;
/* Initialize the protothread state variables with PT_INIT(). */
PT_INIT(&pt1);
PT_INIT(&pt2);
/*
* Then we schedule the two protothreads by repeatedly calling their
* protothread functions and passing a pointer to the protothread
* state variables as arguments.
*/
while(1) {
protothread1(&pt1);
protothread2(&pt2);
_delay_ms(1000);
counter++;
counter1++;
}
} 个人很喜欢这样写法,一直感叹,老外怎么想出来的。这个是简单而有效的方法 楼主写过eeprom的读写程序吧,写延时你用什么? 回复【2楼】mysde
-----------------------------------------------------------------------
写过,那时候用的是pic单片机。延时用的是gcc自带的库函数。 在使用rtos的时候,不使用延时函数,应该挂起去执行其它任务,到时间再回来执行原来的任务。 回复【4楼】mysde
-----------------------------------------------------------------------
我想也对啊,不然加上延时就失去使用它的意义了。
其实我觉得protothreads的意义就是使当前没有必要的任务暂时不要运行,等时机到了的时候再去运行。
好像是这个样子,有时候我们不用它的时候也用过类似的方法,可能是pt做的比我们更合理,对吧? 是的,没用这个之前我用switch语句散转。最好还是使用ucos。
最近公司要开发低成本仪表才想起用这个。 回复【6楼】mysde
-----------------------------------------------------------------------
我用过uC/OS-II,挺好玩的。不过据说很耗资源。我也有点感触,很消耗flash,呵呵
现在你在用protothreads呢? protothread刚开始用,正到处找例程做实验呢,
用ucos时消耗ram很大,我的芯片只有4Kram,rom无所谓,我的64K 回复【8楼】mysde
-----------------------------------------------------------------------
我刚接触protothread,你们有什么好的资料和例程,可以教下我吗?
你用的是STC的芯片吧,我也正在用。
页:
[1]