1
0
Fork 0
This repository has been archived on 2019-12-23. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
arduinisten/projekte/protothreads/example.c
2010-04-03 03:23:33 +02:00

95 lines
2.1 KiB
C

#include "avr/io.h"
#include "util/delay.h"
#include "pt.h"
/* Two flags that the two protothread functions use. */
static int protothread1_flag, protothread2_flag;
static int
protothread1(struct pt *pt)
{
/* A protothread function must begin with PT_BEGIN() which takes a
* pointer to a struct pt. */
PT_BEGIN(pt);
/* We loop forever here. */
while(1) {
/* Wait until the other protothread has set its flag. */
PT_WAIT_UNTIL(pt, protothread2_flag != 0);
PORTB |= _BV(PB3);
_delay_ms(200);
PORTB &= ~_BV(PB3);
_delay_ms(200);
/* We then reset the other protothread's flag, and set our own
flag so that the other protothread can run. */
protothread2_flag = 0;
protothread1_flag = 1;
/* And we loop. */
}
/* All protothread functions must end with PT_END() which takes a
* pointer to a struct pt. */
PT_END(pt);
}
static int
protothread2(struct pt *pt)
{
/* A protothread function must begin with PT_BEGIN() which takes a
pointer to a struct pt. */
PT_BEGIN(pt);
/* We loop forever here. */
while(1) {
/* Let the other protothread run. */
protothread2_flag = 1;
/* Wait until the other protothread has set its flag. */
PT_WAIT_UNTIL(pt, protothread1_flag != 0);
PORTB |= _BV(PB2);
_delay_ms(100);
PORTB &= ~_BV(PB2);
_delay_ms(100);
/* We then reset the other protothread's flag, and set our own
flag so that the other protothread can run. */
protothread1_flag = 0;
protothread2_flag = 1;
/* And we loop. */
}
/* All protothread functions must end with PT_END() which takes a
* pointer to a struct pt. */
PT_END(pt);
}
static struct pt pt1, pt2;
int main() {
// pin 11 = PB3
DDRB |= _BV(PB3);
// pin 10 = PB2
DDRB |= _BV(PB2);
/* 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);
}
}