单片机初学者求教各位程序问题啦~~谢谢
时间:10-02
整理:3721RD
点击:
以下是老师布置的电子钟程序,能不能改成有这些功能:1)早上七点时蜂鸣器会响一分钟,按任意键停。
2)蜂鸣器响时晶体管显示屏会闪烁,0.5秒闪一下
3)按下key6时显示年月日,再按一下重新显示时分秒
#include "reg52.h"
sbit Key=P3^2;
bit KeyDownFlg;
bit SecFlag;
unsigned char Count4ms;
unsigned char disp_buff[6];
unsigned char disp_cnt; //0..5
unsigned char hour,min,sec;
unsigned char Beep_cnt;
unsigned char P2_buff;
unsigned char KeyBuff;
code unsigned char Tab1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E};//;数码管显示字型表
//code unsigned char Tab[6]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
code unsigned char Tab[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //1110: P0.0 1101: p0.1 1011:p0.2
void T0_service(void) interrupt 1
{
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
Count4ms=Count4ms+1;
if (Count4ms==250)
{
Count4ms=0;
SecFlag=1;
}
P2_buff=Tab[disp_cnt];
if (KeyDownFlg)
{
P2_buff=P2_buff&0xbf; // Buzzer is on 1011 1111
Beep_cnt=Beep_cnt+1;
if (Beep_cnt==100) //Buzzer is off
KeyDownFlg=0;
}
//disp
// P2=P2_buff;
// P0=Tab1[disp_buff[disp_cnt]];
P0=P2_buff;
P2=Tab1[disp_buff[disp_cnt]];
disp_cnt=disp_cnt+1;
if (disp_cnt==6)
disp_cnt=0;
}
void FillDispBuffer(void)
{
disp_buff[0]=hour/10;
disp_buff[1]=hour%10;
disp_buff[2]=min/10;
disp_buff[3]=min%10;
disp_buff[4]=sec/10;
disp_buff[5]=sec%10;
}
void ChangeTime(void)
{
SecFlag=0;
sec=sec+1;
if(sec==60)
{
sec=0;
min=min+1;
if(min==60)
{
hour=hour+1;
min=0;
if(hour==24)
hour=0;
}
}
FillDispBuffer();
}
unsigned char CheckData(unsigned char dat,unsigned char th)
{
unsigned char ret_dat;
ret_dat=dat;
if (dat==255)
ret_dat=th-1;
if (dat==th)
ret_dat=0;
return(ret_dat);
}
void DealKey(void)
{
KeyBuff=P0;
if (KeyDownFlg)
return;
if ((KeyBuff&0x01)!=0x01) //
hour=hour+1;
if ((KeyBuff&0x02)!=0x02) //
hour=hour-1;
if ((KeyBuff&0x04)!=0x04) //
min=min+1;
if ((KeyBuff&0x08)!=0x08) //
min=min-1;
if ((KeyBuff&0x10)!=0x10) //
sec=sec+1;
if ((KeyBuff&0x20)!=0x20) //
sec=sec-1;
//check data
hour=CheckData(hour,24);
min=CheckData(min,60);
sec=CheckData(sec,60);
FillDispBuffer();
Beep_cnt=0;
KeyDownFlg=1;
}
void main()
{
//initial
disp_cnt=0;
Count4ms=0;
hour=0;
min=0;
sec=0;
//
TMOD=0x01;
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
TR0=1;
ET0=1;
EA=1;
while(1)
{
if (SecFlag)
ChangeTime();
if (!Key)
DealKey();
}
}
2)蜂鸣器响时晶体管显示屏会闪烁,0.5秒闪一下
3)按下key6时显示年月日,再按一下重新显示时分秒
#include "reg52.h"
sbit Key=P3^2;
bit KeyDownFlg;
bit SecFlag;
unsigned char Count4ms;
unsigned char disp_buff[6];
unsigned char disp_cnt; //0..5
unsigned char hour,min,sec;
unsigned char Beep_cnt;
unsigned char P2_buff;
unsigned char KeyBuff;
code unsigned char Tab1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E};//;数码管显示字型表
//code unsigned char Tab[6]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
code unsigned char Tab[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //1110: P0.0 1101: p0.1 1011:p0.2
void T0_service(void) interrupt 1
{
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
Count4ms=Count4ms+1;
if (Count4ms==250)
{
Count4ms=0;
SecFlag=1;
}
P2_buff=Tab[disp_cnt];
if (KeyDownFlg)
{
P2_buff=P2_buff&0xbf; // Buzzer is on 1011 1111
Beep_cnt=Beep_cnt+1;
if (Beep_cnt==100) //Buzzer is off
KeyDownFlg=0;
}
//disp
// P2=P2_buff;
// P0=Tab1[disp_buff[disp_cnt]];
P0=P2_buff;
P2=Tab1[disp_buff[disp_cnt]];
disp_cnt=disp_cnt+1;
if (disp_cnt==6)
disp_cnt=0;
}
void FillDispBuffer(void)
{
disp_buff[0]=hour/10;
disp_buff[1]=hour%10;
disp_buff[2]=min/10;
disp_buff[3]=min%10;
disp_buff[4]=sec/10;
disp_buff[5]=sec%10;
}
void ChangeTime(void)
{
SecFlag=0;
sec=sec+1;
if(sec==60)
{
sec=0;
min=min+1;
if(min==60)
{
hour=hour+1;
min=0;
if(hour==24)
hour=0;
}
}
FillDispBuffer();
}
unsigned char CheckData(unsigned char dat,unsigned char th)
{
unsigned char ret_dat;
ret_dat=dat;
if (dat==255)
ret_dat=th-1;
if (dat==th)
ret_dat=0;
return(ret_dat);
}
void DealKey(void)
{
KeyBuff=P0;
if (KeyDownFlg)
return;
if ((KeyBuff&0x01)!=0x01) //
hour=hour+1;
if ((KeyBuff&0x02)!=0x02) //
hour=hour-1;
if ((KeyBuff&0x04)!=0x04) //
min=min+1;
if ((KeyBuff&0x08)!=0x08) //
min=min-1;
if ((KeyBuff&0x10)!=0x10) //
sec=sec+1;
if ((KeyBuff&0x20)!=0x20) //
sec=sec-1;
//check data
hour=CheckData(hour,24);
min=CheckData(min,60);
sec=CheckData(sec,60);
FillDispBuffer();
Beep_cnt=0;
KeyDownFlg=1;
}
void main()
{
//initial
disp_cnt=0;
Count4ms=0;
hour=0;
min=0;
sec=0;
//
TMOD=0x01;
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
TR0=1;
ET0=1;
EA=1;
while(1)
{
if (SecFlag)
ChangeTime();
if (!Key)
DealKey();
}
}
应该可以的,我可以试试