dit projekt
k04periodic.ino
Go to the documentation of this file.
1 //220216
2 #include <krnl.h>
3 
4 #define STK 110
5 struct k_t *p1, *p2, *sem1, *sem2;
6 
7 char st1[STK], st2[STK];
8 
9 void tperiodic()
10 {
11  // simple periodic sampler/controller/...
12  k_set_sem_timer(sem1, 100);
13 
14  while (1) {
15  k_wait(sem1, 0);
16  k_eat_msec(70); // we eat 70% of cpu time
17  }
18 }
19 
20 
21 void tnoise()
22 {
23  while (1) {
24  k_eat_msec( random(30, 200) ); // we eat between 30 and 200 msec of time
25  k_sleep(500); // and sleep for 500 msec
26  }
27 }
28 
29 int err;
30 
31 void setup()
32 {
33  for (int i = 8; i < 14; i++) // using PORTB 0..5: d8..d13
34  pinMode(i, OUTPUT);
35  Serial.begin(115200);
36 
37  k_init(2, 1, 0); // init with space for three tasks
38 
39  // priority low number higher priority than higher number
40  //Task 1
41  p1 = k_crt_task(tperiodic, 10, st1, STK); // t1 as task, priority 10, 100 B stak
42  //Task 2
43  p2 = k_crt_task(tnoise, 11 , st2, STK); // t1 as task, priority 10, 100 B stak
44 
45  sem1 = k_crt_sem(0,5);
46 
47  err = k_start(); // 1 milli sec tick speed
48  Serial.print("start error ");
49  Serial.print(err);
50 
51 }
52 
53 void loop() {}
54 
55 
56 extern "C" {
57 
58  void k_breakout() // called every task shift from dispatcher
59  {
60  PORTB = (1 << pRun->nr);
61  // on MEGA use PORTA
62  }
63 }
64 
65 // D8 ON dummy eating cpu
66 // D9 ON Task 1 (the first created
67 // D10 ON Task 2
68 // ...
69 // only one led ON at a time
volatile int i
Definition: k02twotasks.ino:29
void tnoise()
Definition: k04periodic.ino:21
struct k_t * sem1
Definition: k04periodic.ino:5
char st2[STK]
Definition: k04periodic.ino:7
void setup()
Definition: k04periodic.ino:31
void k_breakout()
Definition: k04periodic.ino:58
int err
Definition: k04periodic.ino:29
struct k_t * p1
Definition: k04periodic.ino:5
void tperiodic()
Definition: k04periodic.ino:9
#define STK
Definition: k04periodic.ino:4
struct k_t * p2
Definition: k04periodic.ino:5
struct k_t * sem2
Definition: k04periodic.ino:5
char st1[STK]
Definition: k04periodic.ino:7
void loop()
Definition: k04periodic.ino:53
void k_eat_msec(unsigned int eatTime)
Definition: krnl.c:292
JDN SIGER NOGO IN SIMPLIFY VRS struct k_t * pRun
Definition: krnl.c:200
int k_sleep(int time)
Definition: krnl.c:491
struct k_t * k_crt_sem(int init_val, int maxvalue)
Definition: krnl.c:589
int k_wait(struct k_t *sem, int timeout)
Definition: krnl.c:728
int k_set_sem_timer(struct k_t *sem, int val)
Definition: krnl.c:631
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
Definition: krnl.c:374
int k_init(int nrTask, int nrSem, int nrMsg)
Definition: krnl.c:1152
int k_start()
Definition: krnl.c:1192
Definition: krnl.h:334
unsigned char nr
Definition: krnl.h:338