本帖最后由 zhong2463 于 2018-9-25 09:04 编辑
就正常是一天加一,现在是只能通过按键加一。
#include <REGX51.H>
#include<intrins.h> //包含_nop_();定义的头文件
#include<LS595driver.h>
#include<LS595_2driver.h>
#include<ds1302driver.h>
SYSTEMTIME today; //2018.1.2 12:00:00 2
unsigned int T0loop; //记录进入T0的次数
unsigned int flag; //约10秒1次,
unsigned int countupday; //记录正计时天数的参数
unsigned char olddaytemp,newdaytemp; //用来比较是否已经过一天
unsigned char keyvalue;//记录按键扫描函数的返回值
//******************
//unsigned int test[10]={0,111,222,333,444,555,666,777,888,999}; //记录倒计时天数的参数
unsigned char i;
//*******************
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit LEDr=P1^3;
sbit LEDg=P1^4;
sbit LEDy=P1^5;
void Init_89C52(); //初始化89C52
void InitT0();
char Keyscan(); //按键扫描并返回键值
void Setcountupday(); //按键设置正计时天数
void main()
{
Init_89C52();
Delay_ms(1000);
countupday = 1;
LEDr = 1;
LEDg = 1;
LEDy = 1;
//*****************************
Init_1302(); //初始化时钟
InitT0(); //初始化T0定时器
Displayround();
while(1)
{
Displaysecond_595(&today);
Displayday_595(&today);
Displayhour_595(&today);
Displayminute_595(&today);
Setcountupday(unsigned int chu=888);
Displaycountupnum(countupday);
if(flag==1)
{
flag = 0;
if(olddaytemp=!newdaytemp)
{
countupday++;
}
}
}
}
//*************************************************************************
//Time0中断函数
//功能:定时出10秒/50毫秒=200个循环
//参数:无
//*************************************************************************
void Time0(void) interrupt 1 using 1
{
TR0 = 0;
T0loop++;
if(T0loop==200)
{
Gettime_1302(&today);
olddaytemp = today.day;
}
if(T0loop==400)
{
Gettime_1302(&today);
newdaytemp = today.day;
flag = 1;
T0loop = 0;
}
TH0 = (65536-46083)/256; //重新给T0赋初始值
TL0 = (65536-46083)%256; //
TR0 = 1; //启动T0
}
//*************************************************************************
//Time0中断函数
//功能:每8秒进入中断,执行开门或关门操作,并重设Time0
//参数:无
//*************************************************************************
void InitT0()
{
TMOD=0x01; //使用定时器T0,11.0592MHz晶振的12分频,16位定时器65536-1
TH0 =(65536-46083)/256; //将定时器计时时间设定为46083*1.085微秒=50000微秒=50毫秒
TL0 =(65536-46083)%256; //
EA = 1; //开启总中断
ET0 = 1; //T0中断允许
TR0 = 1; //启动定时器T0
T0loop = 0; //记录进入T0的次数
flag = 0; //10秒一个周期
}
/*89C52初始化*/
void Init_89C52()
{
P0 = 0;
P1 = 1;
P2 = 0;
T0loop = 0;
}
/*按键扫描函数*/
char Keyscan()
{
if(key1==0)
{
Delay_ms(200);
if(key1==0)
{
return 1;
}
}
if(key2==1)
{
Delay_ms(200);
if(key2==1)
{
return 2;
}
}
if(key3==1)
{
Delay_ms(200);
if(key3==1)
{
return 2;
}
}
else
return 0;
}
/*按键调整函数*/
void Setcountupday()
{
switch(Keyscan())
{
case 1:
countupday++;
Displaycountupnum(countupday);
break;
case 2:
countupday--;
Displaycountupnum(countupday);
break;
default:
Displaycountupnum(countupday);
break;
}
}
头文件595
/*===============================================================*/
/* 第二组级联74LS595_2数码管驱动程序 */
/*===============================================================*/
#ifndef _LS595_2driver_H_
#define _LS595_2driver_H_
sbit RCK_595_2 = P0^4; //第二组级联595的并行输出时钟
sbit DS_595_2 = P0^3; //第二组级联595数据线
sbit CLK_595_2 = P0^5; //第二组级联595移位输入时钟
unsigned char code countupseg[] = //第二组595中定义0~9,高亮低灭
{// 0 1 2 3 4 5 6 7 8 9
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
};
unsigned char code roundseg[6]={0x01,0x02,0x04,0x08,0x10,0x20}; //花式旋转
/*延时函数 1ms为单位*/
void Delay_ms(unsigned int t) //延时函数,t/ms
{
unsigned int a,b;
for(a=0;a<t;a++)
{
for(b=0;b<100;b++)
{
_nop_();
}
}
}
/* 向1个LS595_2写入1个字节*/
void Send1byte_595_2(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(dat&0x80)
DS_595_2 = 1;
else
DS_595_2 = 0;
dat <<=1;
CLK_595_2 = 0;
_nop_();
CLK_595_2 = 1;
}
}
/* 向3个级联LS595_2写入3个字节*/
void Send3byte_595_2(unsigned char dat1,unsigned char dat2,unsigned char dat3)
{
Send1byte_595_2(dat1);
Send1byte_595_2(dat2);
Send1byte_595_2(dat3);
}
/*允许将LS595输出并行数据*/
void Out_595_2()
{
RCK_595_2 = 0;
_nop_();
RCK_595_2 = 1;
}
/*显示倒计时 3位*/
void Displaycountupnum(unsigned int num)
{
unsigned int units,tens,hundreds;
units = num%10;
tens = (num/10)%10;
hundreds = num/100;
Send3byte_595_2(countupseg[hundreds],countupseg[tens],countupseg[units]); //显示倒计时的个、十、百位
Out_595_2();
}
/*大字花式转圈*/
void Displayround()
{
unsigned char j;
for(j=0;j<5;j++)
{
Send3byte_595_2(roundseg[j],roundseg[j],roundseg[j]);
Out_595_2();
Delay_ms(800);
}
}
#endif
|