Tiny RTOS 学习第一篇
完全按照楼主的教程,使用自制学习机验证简单流水灯测试程序:
流水灯原理图:
按键原理图:
测试功能:
程序初始功能为左到右的流水灯。
按键1: 左向右的流水灯;
按键2: 右向左的流水灯;
按键3: 三个灯闪烁;
代码:
Task0:
- /* LED控制任务 */
- void Task0(void)
- {
- for(;;)
- {
- if(mode == 1) {
- P35 = 0; //LED On
- OSDelay(t) ; // 任务延时
- P35 = 1; //LED Off
- P36 = 0; //LED On
- OSDelay(t) ;
- P36 = 1; //LED Off
- P37 = 0; //LED On
- OSDelay(t) ;
- P37 = 1; //LED Off
- } else if (mode == 2) {
- P37 = 0;
- OSDelay(t) ; // 任务延时
- P37 = 1;
- P36 = 0; //LED On
- OSDelay(t) ;
- P36 = 1; //LED Off
- P35 = 0; //LED On
- OSDelay(t) ; // 任务延时
- P35 = 1; //LED Off
- } else {
- P35 = 0;
- P36 = 0;
- P37 = 0;
- OSDelay(t) ; // 任务延时
- P35 = 1;
- P36 = 1;
- P37 = 1;
- OSDelay(t) ; // 任务延时
-
- }
-
-
- }
- }
-
- /* 按键检测 任务 */
- void Task1(void)
- {
- static unsigned char Key1Cnt ;
- unsigned char newKey;
- for(;;)
- {
- if(!P64 || !P65 || !P66)
- {
- if(!P64) {
- newKey = KEY1;
- } else if (!P65) {
- newKey = KEY2;
- } else {
- newKey = KEY3;
- }
-
- if(oldKey != newKey) {
- Key1Cnt = 1;
- oldKey = newKey;
- }
-
- if(Key1Cnt < 0xff) Key1Cnt ++ ;
- }
- else
- {
- Key1Cnt = 0 ;
- }
-
- if(Key1Cnt == 250)
- {
- OSEnterCritical() ;
- mode = newKey;
- OSExitCritical() ;
- }
-
- OSTaskSuspend() ; // 任务挂起
- }
- }
复制代码
实验效果:
|