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

89 lines
1.8 KiB
C
Raw Normal View History

2010-04-03 22:50:45 +02:00
/* based on example-small.c in the protothreads tarball */
2010-04-03 03:23:33 +02:00
#include "avr/io.h"
#include "util/delay.h"
#include "pt.h"
2010-04-03 23:22:56 +02:00
#include "clock.h"
#include "timer.h"
2010-04-03 03:23:33 +02:00
2010-04-03 22:47:06 +02:00
/* TIMER_DELAY macro for convenience */
2010-04-03 22:39:13 +02:00
#define TIMER_DELAY(pt, timer, t) \
do { \
timer_set(&timer, t); \
PT_WAIT_UNTIL(pt, timer_expired(&timer)); \
2010-04-03 23:45:59 +02:00
} while(0)
2010-04-03 03:23:33 +02:00
2010-04-03 20:51:54 +02:00
/* Two timers for the two protothreads. */
static struct timer timer1, timer2;
2010-04-03 03:23:33 +02:00
static int
protothread1(struct pt *pt)
{
/* A protothread function must begin with PT_BEGIN() which takes a
2010-04-03 22:40:24 +02:00
pointer to a struct pt. */
2010-04-03 03:23:33 +02:00
PT_BEGIN(pt);
/* We loop forever here. */
while(1) {
PORTB |= _BV(PB3);
2010-04-03 22:39:13 +02:00
TIMER_DELAY(pt, timer1, 20);
2010-04-03 03:23:33 +02:00
PORTB &= ~_BV(PB3);
2010-04-03 22:39:13 +02:00
TIMER_DELAY(pt, timer1, 20);
2010-04-03 03:23:33 +02:00
/* And we loop. */
}
/* All protothread functions must end with PT_END() which takes a
2010-04-03 22:40:24 +02:00
pointer to a struct pt. */
2010-04-03 03:23:33 +02:00
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) {
PORTB |= _BV(PB2);
2010-04-03 22:39:13 +02:00
TIMER_DELAY(pt, timer2, 60);
2010-04-03 03:23:33 +02:00
PORTB &= ~_BV(PB2);
2010-04-03 22:39:13 +02:00
TIMER_DELAY(pt, timer2, 60);
2010-04-03 03:23:33 +02:00
/* And we loop. */
}
/* All protothread functions must end with PT_END() which takes a
2010-04-03 22:40:24 +02:00
pointer to a struct pt. */
2010-04-03 03:23:33 +02:00
PT_END(pt);
}
static struct pt pt1, pt2;
2010-04-03 18:38:55 +02:00
int main(void) {
2010-04-03 03:23:33 +02:00
// pin 11 = PB3
DDRB |= _BV(PB3);
// pin 10 = PB2
DDRB |= _BV(PB2);
2010-04-03 20:51:54 +02:00
/* Initialize clock */
clock_init();
2010-04-03 03:23:33 +02:00
/* 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);
}
}