标题:
定时器应用C语言程序
[打印本页]
作者:
苹果也疯狂
时间:
2015-3-15 21:45
标题:
定时器应用C语言程序
#Include "Stdio.H"
#Include "Reg51.H"
// 0 - 9 的编码
Unsigned Char Code Table[] = { 0x11,0xb7,0x29,0x23,0x87,0x43,0x41,0x37,0x01,0x03,0x00 };
Unsigned Int Count2;
//第1-6 位的片选线
Unsigned Char Code Dig[] = { 0x7f,0xfd,0xdf,0xf7,0xef,0xfb };
Unsigned Char Buffer[8],Val[6];
Unsigned Char Count1,Scan,I,Ch,KeyVal,Modify,OverTime;
Bit FKey,FSec,Key1,Key2,Key3,OldKey1,OldKey2,OldKey3,Blink,FTimer,FSTOP;
Unsigned Long Int Cn,Tmp;
Void Init(Void);
Void Display(Void);
Void Cal_cn(Void);
Void Cal_val(Void);
Main()
{
Init();
While(1) {
If(FSec) { // 秒标志被置位?
FSec = 0; // 清除秒标志
If(!FSTOP && Modify==0) {
If(Cn!=0) Cn--; Else { FTimer = 1; FSTOP = 1; }
}
Display();
}
If(FKey) { // 有按键按下?
FKey = 0; // 清除按件标志,并处理该按键
If(KeyVal==1) { // 是调整按钮?
FSTOP = 0; Modify++; // 进入相应状态,并重置超时定时器
If(Modify==7) { Modify = 0; OverTime = 0; } Else OverTime = 10;
}
If(KeyVal==2 && Modify!=0) { // +1按钮,相应位+1
Cal_val();
If(Val[Modify-1]<9) Val[Modify-1]++;
Cal_cn();
Display(); OverTime = 10; // 刷新显示,并重置超时定时器
}
If(KeyVal==3 && Modify!=0) { // -1按钮,相应位-1
Cal_val();
If(Val[Modify-1]>0) Val[Modify-1]--;
Cal_cn();
Display(); OverTime = 10; // 刷新显示,并重置超时定时器
}
}
}
}
Void Init(Void) {
Count1 = Count2 = 0; Scan = 0; KeyVal = 0;
Cn = 0; For(I=0;I<6;I++) Val[I] = 0;
FKey = 0; Modify = 0; Blink = 0; OverTime = 0; FTimer = 0; FSTOP = 0;
Key1 = Key2 = Key3 = 0;
OldKey1 = OldKey2 = OldKey3 = 1;
For(I=0;I<6;I++) Buffer[I] = 0;
TCON &= 0xCF; // 初始化Timer0
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = TL0 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
Timerint () Interrupt 1 {
TR0 = 0;
TL0 = 0x12;
TH0 = 0xf7; // 0xf712 For 2.5ms
TR0 = 1;
If(Scan==1 && !FKey) { // 扫描调整按钮
Key1 = P30;
If(!Key1 && OldKey1) { FKey = 1; KeyVal = 1; }
}
If(Scan==3 && !FKey) { // 扫描+1按钮
Key2 = P30;
If(!Key2 && OldKey2) { FKey = 1; KeyVal = 2; }
}
If(Scan==4 && !FKey) { // 扫描-1按钮
Key3 = P30;
If(!Key3 && OldKey3) { FKey = 1; KeyVal = 3; }
}
OldKey1 = Key1; OldKey2 = Key2; OldKey3 = Key3;
P1 = 0xff; P3 = Dig[Scan];
Ch = Buffer[Scan]; // 依次显示6位
If(Modify==0) {
If(FTimer) P1 = Table[Ch] & 0xfe; // 倒计时结束,启动P10(P10低电平)
Else P1 = Table[Ch];
}
Else If(Modify==1 && Blink && Scan==0) P1 = 0xff;
Else If(Modify==2 && Blink && Scan==1) P1 = 0xff;
Else If(Modify==3 && Blink && Scan==2) P1 = 0xff;
Else If(Modify==4 && Blink && Scan==3) P1 = 0xff;
Else If(Modify==5 && Blink && Scan==4) P1 = 0xff;
Else If(Modify==6 && Blink && Scan==5) P1 = 0xff;
Else P1 = Table[Ch];
Scan++; If(Scan==6) Scan = 0;
Count1++;
If(Count1==200) { Count1 = 0; Blink = ~Blink; } // 0.5s闪烁定时器
Count2++;
If(Count2==400) {
Count2 = 0; FSec = 1; FTimer = 0; // 秒脉冲标志置位,并关闭P10输出
If(OverTime) { OverTime--; If(OverTime==0) Modify = 0; } // 10s超时定时器
}
}
Void Cal_cn(Void) {
Cn = 0;
For(I=0;I<6;I++) {
Cn *= 10; Cn += Val[I];
}
}
Void Cal_val(Void) {
Tmp = Cn;
For(I=0;I<6;I++) {
Val[5-I] =(Unsigned Char) ( Tmp % 10 );
Tmp = Tmp / 10;
}
}
Void Display(Void) {
Tmp = Cn;
For(I=0;I<6;I++) {
Buffer[5-I] =(Unsigned Char) ( Tmp % 10 );
Tmp = Tmp / 10;
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0