📄 app.c
字号:
/* This is simple test application.
* It starts some tasks and use LEDs on Microchip
* dsPIC development board with dsPIC30F6014 */
#include "includes.h"
/* prototypes */
void error_found(void);
extern void dummy_task(void *pdata);
/* globals */
OS_STK StartTaskStk[100];
OS_STK stk1[200];
OS_STK stk2[200];
OS_STK stk3[200];
OS_STK dummyStk[100];
OS_EVENT *pevent;
/* this is highest priority task in the system */
void StartTask (void *pdata) {
pdata = pdata;
/* start periodic timer */
TMR1 = 0x0000;
T1CONbits.TCS = 0; /* internal clk. source */
T1CONbits.TGATE = 0; /* internal clk. source */
T1CONbits.TCKPS = 2; /* clk/64 - prescaler */
PR1 = 1152; /* num. of cycles per interrupt */
IPC0bits.T1IP = 4; /* int. priority */
IFS0bits.T1IF = 0; /* clear int. flag */
IEC0bits.T1IE = 1; /* enable int. */
SRbits.IPL = 0; /* enable CPU prio. levels 1-7 */
T1CONbits.TON = 1; /* start timer */
while (1) {
if (KEY1 == 0) {
LED4 = 0; /* turn ON led, don't turn it off until reset */
}
OSTimeDly(OS_TIME_100ms*3);
}
}
void error_found(void) {
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr;
#endif
/* set led status */
LED1 = 1;
LED2 = 0; /* ON */
LED3 = 0; /* ON */
LED4 = 1;
OS_ENTER_CRITICAL(); /* disable interrupt */
while(1); /* stay here ontil the end of time */
}
void check(
unsigned int x1,
unsigned int x2,
unsigned int x3,
unsigned int x4,
unsigned int y1,
unsigned int y2,
unsigned int y3,
unsigned int y4) {
if (x1!=y1) error_found();
if (x2!=y2) error_found();
if (x3!=y3) error_found();
if (x4!=y4) error_found();
}
/* flash LEDs and check that variables have not changed */
void flasher(void *data) {
unsigned int instance;
unsigned int i;
volatile unsigned int x1, x2, x3, x4;
volatile unsigned char s[20]; /* dummy */
volatile unsigned int y1, y2, y3, y4;
INT8U err;
instance = (unsigned int)data;
s[0] = 1;
i = 0; /* jitter */
switch(instance) {
case 1:
x1=11; x2=12; x3=13; x4=14;
y1=x1; y2=x2; y3=x3; y4=x4;
while(1) {
LED1 = 0;
check(x1,x2,x3,x4,y1,y2,y3,y4);
OSSemPend(pevent, 0, &err); /* triger task-level context switch */
check(x1,x2,x3,x4,y1,y2,y3,y4);
LED1 = 1;
OSSemPend(pevent, 0, &err);
check(x1,x2,x3,x4,y1,y2,y3,y4);
i++; if (i>=15) i=0;
}
break;
case 2:
x1=21; x2=22; x3=23; x4=24;
y1=x1; y2=x2; y3=x3; y4=x4;
while(1) {
LED2 = 0;
check(x1,x2,x3,x4,y1,y2,y3,y4);
OSTimeDly(OS_TIME_10ms*5+i);
OSSemPost(pevent);
OSTimeDly(OS_TIME_10ms*3+i);
check(x1,x2,x3,x4,y1,y2,y3,y4);
LED2 = 1;
OSTimeDly(OS_TIME_10ms*7+i);
check(x1,x2,x3,x4,y1,y2,y3,y4);
i++; if (i>=15) i=0;
}
break;
case 3:
x1=31; x2=32; x3=33; x4=34;
y1=x1; y2=x2; y3=x3; y4=x4;
while(1) {
LED3 = 0;
check(x1,x2,x3,x4,y1,y2,y3,y4);
OSTimeDly(OS_TIME_10ms*19+i);
check(x1,x2,x3,x4,y1,y2,y3,y4);
LED3 = 1;
OSTimeDly(OS_TIME_10ms*19+i);
check(x1,x2,x3,x4,y1,y2,y3,y4);
i++; if (i>=15) i=0;
}
break;
case 4:
while (1) {
LED3 = 0;
OSTimeDly(OS_TICKS_PER_SEC);
LED3 = 1;
OSTimeDly(OS_TICKS_PER_SEC);
}
default:
break;
}
/* should not be here */
while (1) {
OSTimeDly(OS_TICKS_PER_SEC);
}
}
int main ( void ) {
/* init I/O pins */
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
TRISDbits.TRISD2 = 0;
TRISDbits.TRISD3 = 0;
/* turn off all LEDs */
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 1;
OSInit();
OSTaskCreate(StartTask, NULL, &StartTaskStk[0], 0);
OSTaskCreate(flasher, (void *)1, &stk1[0], 1);
OSTaskCreate(flasher, (void *)2, &stk2[0], 2);
OSTaskCreate(flasher, (void *)3, &stk3[0], 3);
OSTaskCreate(dummy_task, NULL, &dummyStk[0], 20);
pevent = OSSemCreate(0);
OSStart();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -