📄 disp.c
字号:
/*用rtos编写LED数码管显示程序,其中有用于计算的程序*/
/*另加LED流水灯*/
//须加入conf_tny.a51将周期数改为1000(原为10000),即将报时周期改为1ms,将TimeSharing由5改为1,即将时间片改为1ms
/*测试结果:1)可很稳定地显示,LED的闪烁也正常
2)如果将Timer1的定时中断改为1000即1ms发送一次,可以看到LED流水灯速度时显示加快,但同时数码管出现闪烁,系统超载
*/
#include "reg51.h"
#include "intrins.h"
#include <rtx51tny.h> /* RTX51 tiny functions & defines */
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#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 code BitTab[]={0x7F,0xBF,0xDF,0xEF,0xF7,0xFB};
uchar code DispTab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
uchar DispBuf[6];
ulong Count=0; //计数值
void job0 (void ) _task_ Init {
TH1=-(10000/256);
TL1=-(10000%256); //12M晶振定时10毫秒
TMOD|=0x10; //T/C1工作于定时器方式1
ET1=1; //定时器T1允许中断
TR1=1; //定时器T1开始运行
PT1=1; //高优先级中断
os_create_task (Disp); /* start task Disp */
os_create_task (Led); /* start task Led flash */
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); //12M晶振定时10毫秒
isr_send_signal(Led);
}
void LedFlash(void) _task_ Led{
static uchar FlashLed=0xfe;
static uchar LedCount;
while(1)
{ LedCount++;
Count++; //计数值不断加1
if(LedCount>=100) //如果计数次数等于或超过100,由于定时器每10ms送一次信号,故意味着1s为一个周期
{
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{
ulong tmp;
while(1){
tmp=Count;
DispBuf[5]=tmp%10;
tmp/=10;
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) //11011111 P3.2位被按下
{ 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -