sec.c
来自「单片及c语言轻松入门的随书光盘」· C语言 代码 · 共 103 行
C
103 行
#include "intrins.h"
#include <rtx51tny.h>
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define Init 0 //初始化
#define Disp 1 //显示程序定为任务1
#define Led 2 //LED闪烁定为任务2
#define Data 3 //数值处理
#define Key 4 //键盘操作定义为任务3
bit StartLedFlash; //启停LED流动
bit LeftRight; //流动方向
uchar BitTab[]={0x7F,0xBF,0xDF,0xEf,0xF7,0xFB};
uchar DispTab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E,0xFF};
uchar DispBuf[6];
uint Count=0; //计数值
void job0 (void ) _task_ Init {
TH1=-(10000/256);
TL1=-(10000%256);
TMOD|=0x10;
ET1=1;
TR1=1;
PT1=1;
os_create_task (Disp);
os_create_task (Led);
os_create_task (Data);
os_create_task (Key);
os_delete_task (0);
}
void Display(void) _task_ Disp{
static uchar DispCount;
while(1){
P2=0xff; //全灭
P0=DispTab[DispBuf[DispCount]];
P2=BitTab[DispCount];
DispCount++;
if(DispCount==6)
DispCount=0;
os_wait2(K_IVL,4);
}
}
void Timer1() interrupt 3
{ TH1=-(10000/256);
TL1=-(10000%256);
isr_send_signal(Led);//送出信号
}
void LedFlash(void) _task_ Led{
static uchar FlashLed=0xfe;
static uchar LedCount;
while(1)
{ LedCount++;
Count++; //计数值不断加1
if(LedCount>=100)
{ if(StartLedFlash) //
{ P1=FlashLed;
if(LeftRight)
FlashLed=_crol_(FlashLed,1);
else
FlashLed=_cror_(FlashLed,1);
}
LedCount=0;
}
os_wait(K_SIG,1,0); //等待信号
}
}
void DataProcess(void) _task_ Data{
uint tmp;
while(1){ tmp=Count;
DispBuf[4]=tmp%10;
tmp/=10;
DispBuf[3]=tmp%10;
tmp/=10;
DispBuf[2]=tmp%10;
tmp/=10;
DispBuf[1]=tmp%10;
DispBuf[0]=tmp/10;
}
}
void KeyValue(uchar KeyV)
{ if((KeyV|0xfb)!=0xff)
StartLedFlash=1;
else if((KeyV|0xf7)!=0xff)
StartLedFlash=0;
else if((KeyV|0xef)!=0xff)
LeftRight=1;
else if((KeyV|0xdf)!=0xff)
LeftRight=0;
}
void KeyProcess(void) _task_ Key{
uchar tmp;
while(1)
{ P3|=0x3c; //中间4位置高电平
tmp=P3;
tmp|=0xc3; //两边4位置高电平
if(tmp!=0xff)
KeyValue(tmp);
os_wait(K_IVL,2,0);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?