这个源程序是一个四位共阳数码管时钟程序,时间和温度自动切换的(源程序是时间显示10秒左右然后自动切换到温度显示10秒左右,如此循环),现在我想改一下切换时时间和温度的显示时间(比如改成时间显示10秒后自动切换成温度显示2秒,如此循环),之前找过好几个人帮忙分析过,可是都说看不懂,看看这里有那位高手帮忙看看应该是改哪一段程序。
/******************************************************************************************
//四位数码管
电子钟
//应用程序 C
//neo
//V1.1 2011-7-16
//MCS-51 12MHZ C语言
//接口说明: DS1302、DS18B20
*********************************************************************************************/
#include <reg52.h>
#include <intrins.h>
#define uint8 unsigned char
#define uchar unsigned char
#define uint unsigned int
uchar Disp_Buff[4];
char SB=4; // clock set
time bit
//bit Set_Flag; // set time flag
bit T=0; // display Temperature and Time flag
bit TT=0; // 10S Auto change display Temperature and Time flag
//#define T0_H 0xf0 // 4ms
//#define T0_L 0x60
#define T0_H 0xf5 // 2666us
#define T0_L 0x96
#define ST 20
#define T1_H 0xb1 //20ms
#define T1_L 0xe0
#define NumLed_Bit P2
#define Disp P1
#define Key P3
#define Key1 1
#define Key2 2
#define Key3 3
#define Key4 4
#define Key_NULL 0
sbit Beep=P0^7;
void Play(uchar t);
sbit Red_Led=P3^7;
// Driver DS1302
/********************************************************************************************/
sbit RST=P0^0;
sbit IO=P0^1;
sbit SCLK=P0^2;
#define second_write 0x80
#define minute_write 0x82
#define hour_write 0x84
#define week_write 0x8a
#define day_write 0x86
#define month_write 0x88
#define year_write 0x8c
#define second_read 0x81
#define minute_read 0x83
#define hour_read 0x85
#define week_read 0x8b
#define day_read 0x87
#define month_read 0x89
#define year_read 0x8d
#define sclk_high SCLK=1;
#define sclk_low SCLK=0;
#define io_high IO=1;
#define io_low IO=0;
#define io_read IO
#define rst_high RST=1;
#define rst_low RST=0;
struct {uint8 second;
uint8 minute;
uint8 hour;
uint8 day;
uint8 week;
uint8 month;
uint8 year; } current_time;
/********************************************************************************************/
static void ds1302write(uint8 content)
{
uint8 i;
for(i=8;i>0;i--) { if(content&0x01) io_high
else io_low
content>>=1; sclk_high sclk_low
}
}
/********************************************************************************************/
static uint8 ds1302read(void)
{
uint8 i,readvalue;
io_high
for(i=8;i>0;i--) { readvalue>>=1; if(io_read) readvalue|=0x80;
else readvalue&=0x7f;
sclk_high sclk_low }
return readvalue;
}
/********************************************************************************************/
void ds1302write_byte (uint8 address,uint8 content)
{
rst_low sclk_low rst_high ds1302write(address);
ds1302write(content); rst_low sclk_high
}
uint8 ds1302read_byte(uint8 address)
{
uint8 readvalue;
rst_low sclk_low rst_high
ds1302write(address);
readvalue=ds1302read();
rst_low sclk_high
return readvalue;
}
/********************************************************************************************/
void Clock_Init(void)
{
uchar R;
if(ds1302read_byte(0xc1)!=0xf0) {
ds1302write_byte(0x8e,0x00);
ds1302write_byte(year_write,0x11);
ds1302write_byte(week_write,0x06);
ds1302write_byte(month_write,0x04);
ds1302write_byte(day_write,0x09);
ds1302write_byte(hour_write,0x23);
ds1302write_byte(minute_write,0x59);
ds1302write_byte(second_write,0x30);
ds1302write_byte(0x90,0xa5);
ds1302write_byte(0xc0,0xf0);
for (R=0;R<10;R+=2)
{
ds1302write_byte(0xc2+R,0);
}
ds1302write_byte(0x8e,0x80);
}
}
/********************************************************************************************/
void Clock_Updata(void)
{
current_time.second=ds1302read_byte(second_read);
current_time.minute=ds1302read_byte(minute_read);
current_time.hour= ds1302read_byte(hour_read);
current_time.day= ds1302read_byte(day_read);
current_time.month= ds1302read_byte(month_read);
current_time.week= ds1302read_byte(week_read);
current_time.year= ds1302read_byte(year_read);
}
/********************************************************************************************/
void Write_Time(void)
{
ds1302write_byte(0x8e,0x00);
ds1302write_byte(0x82,((Disp_Buff[2]<<4)|Disp_Buff[3]));
ds1302write_byte(0x84,((Disp_Buff[0]<<4)|Disp_Buff[1]));
ds1302write_byte(0x8e,0x80);
}
/********************************************************************************************/
void Write_Al
ARM(void)
{
ds1302write_byte(0x8e,0x00);
ds1302write_byte(0xca,((Disp_Buff[2]<<4)|Disp_Buff[3])); // alarm minute
ds1302write_byte(0xc8,((Disp_Buff[0]<<4)|Disp_Buff[1])); // alarm hour
ds1302write_byte(0xc6,1);
ds1302write_byte(0x8e,0x80);
}
/********************************************************************************************/
void Alarm(void)
{
if(ds1302read_byte(0xc7)==1&&
current_time.minute==ds1302read_byte(0xcb)&&
current_time.hour==ds1302read_byte(0xc9) )
{
ds1302write_byte(0x8e,0x00);
ds1302write_byte(0xc6,0);
ds1302write_byte(0x8e,0x80);
Play(3);
}
}
/********************************************************************************************/
// DS18B20 driver
sbit io_DQ=P0^6;
#define DQ_HIGH io_DQ=1;
#define DQ_LOW io_DQ=0;
#define DQ_READ io_DQ
unsigned char Temperature[4];
void Delay_10Us (unsigned int Count)
{
while(--Count)
{
_nop_();
}
}
/********************************************************************************************/
unsigned char Ds18b20_Init(void)
{
unsigned char Flag;
DQ_HIGH Delay_10Us(3);
DQ_LOW Delay_10Us(80);
DQ_HIGH Delay_10Us(15);
Flag=DQ_READ;
return Flag;
}
/********************************************************************************************/
void Write_f(unsigned char cmd)
{
unsigned char i;
for(i=8;i>0;i--) {
EA=0;
DQ_LOW
DQ_READ=cmd&0x01;
Delay_10Us(5);
DQ_HIGH
EA=1;
cmd>>=1;
}
}
/********************************************************************************************/
unsigned char Read_f(void)
{
unsigned char Read_Value,i;
for (i=8;i>0;i--) {
EA=0;
DQ_LOW
Read_Value>>=1;
DQ_HIGH
if (DQ_READ==1) Read_Value|=0x80;
EA=1;
Delay_10Us(3);
}
return Read_Value;
}
/********************************************************************************************/
unsigned int Read_Temp(void)
{
unsigned char Temp_H, Temp_L;
unsigned int Return_Temp;
EA=0; // close interrupt;
Ds18b20_Init();
Write_f(0xcc);
Write_f(0x44);
Ds18b20_Init();
Write_f(0xcc);
Write_f(0xbe);
Temp_L=Read_f();
Temp_H=Read_f();
EA=1;
Return_Temp=Temp_H;
Return_Temp<<=8;
Return_Temp|=Temp_L;
return Return_Temp;
}
/********************************************************************************************/
void Temperature_Update(void)
{
unsigned char T_flag=0;
unsigned int Temp_Dat;
float Temp;
Temp_Dat=Read_Temp();
if(Temp_Dat&0xf000) {T_flag=1; Temp_Dat=~Temp_Dat+1; }
Temp=Temp_Dat*0.0625;
Temp_Dat=Temp*10;
Temperature[0]=T_flag;
Temperature[1]=Temp_Dat/100;
Temperature[2]=Temp_Dat%100/10;
Temperature[3]=Temp_Dat%10;
}
/********************************************************************************************/
void Timer0_Init(void)
{
TMOD|=0x01;
TH0=T0_H;
TL0=T0_L;
EA=1;
ET0=1;
TR0=1;
}
/********************************************************************************************/
bit T_10S,T_1S;
bit S; // shining flag
bit pp=1; // open or close :
unsigned int ms1,ms2,ms3;
uchar Bit;// Number Led select
uchar code Disp_NumCode[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90, // dp-a P07-P00 0-9,
0xa7, //c 10
0xc8,0x86,0xc0, //NEO 11-13
0x92,0xc6,0x88, // S,C,A 14-16
0xa1,0x8c,0x8e, // d,p,F 17-19
0xff, // " " 20
0xc7,0xf7 // L 21 _ 22
};
/********************************************************************************************/
uchar code Open_Logo[]={11,12,13,20};
uchar code S_C[]={14,22,15,20};
uchar code S_A[]={14,22,16,20};
uchar code d_p[]={17,22,18,20};
uchar code BELL[]={17,12,21,21};
uchar code ON[]= {20,13,11,20};
uchar code OFF[]={20,13,19,19};
void Put_In(uchar *P)
{
uchar a;
for (a=0;a<4;a++)
{
Disp_Buff[a]=P[a];
}
}
/********************************************************************************************/
void Timer0_Isr(void) interrupt 1
{
TH0=T0_H; //4ms
TL0=T0_L;
Disp=0xff;
/*
switch(Bit) {
case 0 : NumLed_Bit=0xfe; break;
case 1 : NumLed_Bit=0xfd; break;
case 2 : NumLed_Bit=0xfb; break;
case 3 : NumLed_Bit=0xf7; break;
case 4 : NumLed_Bit=0xf3; break; // display :
default : break;
}
*/
switch(Bit) {
case 0 : {
if(SB==0)
{
if (S) NumLed_Bit=0xfe;
if(S==0) NumLed_Bit=0xff;
}
else NumLed_Bit=0xfe;
}break;
case 1 : {
if(SB==1)
{
if (S) NumLed_Bit=0xfd;
if(S==0) NumLed_Bit=0xff;
}
else NumLed_Bit=0xfd;
}break;
case 2 : {
if(SB==2)
{
if (S) NumLed_Bit=0xfb;
if(S==0) NumLed_Bit=0xff;
}
else NumLed_Bit=0xfb;
} break;
case 3 : {
if(SB==3)
{
if (S) NumLed_Bit=0xf7;
if(S==0) NumLed_Bit=0xff;
}
else NumLed_Bit=0xf7;
} break;
case 4 : {
if(T==0||TT==0) NumLed_Bit=0xf3; // display :
if( T || TT ) NumLed_Bit=0xfb; // display .
} break;
default : break;
}
if(Bit<4) {
Disp=Disp_NumCode[Disp_Buff[Bit]];
}
else
{
if(pp==1)
{
if (T==0||TT==0)
{
if(current_time.second%2) Disp=0x7f;
else Disp=0xff;
}
if( T || TT ) Disp=0x7f;
}
else Disp=0xff; // close :
}
if(++Bit>4) Bit=0;
}
/********************************************************************************************/
void Timer1_Init(void)
{
TMOD|=0x10;
TH1=T1_H; //20ms
TL1=T1_L;
EA=1;
ET1=1;
TR1=1;
}
/********************************************************************************************/
uchar Key_Time;
void Timer1_Isr(void) interrupt 3
{
TH1=T1_H;
TL1=T1_L;
if(Key!=0xff) Key_Time++;
if(++ms1>ST) { ms1=0; S=~S; } //when set time Shining bit
if(++ms2>=50) { ms2=0; T_1S=1; }
if(++ms3>500) { ms3=0; T_10S=1;}
}
/********************************************************************************************/
void DelayM(unsigned int a){//-延时函数 1MS/次
unsigned char i;
while(a--)
{
for(i = 0; i < 125; i++)
{}
} //i 从0加到125,CPU大概就耗时1毫秒
}
/********************************************************************************************/
void Play(uchar t)
{
uchar i;
switch(t) {
case 1:
{
Beep=0;
DelayM(100);
} break;
case 2:
{
Beep=0;
DelayM(400);
Beep=1;
DelayM(500);
Beep=0;
DelayM(100);
} break;
case 3:
{
for(i=0;i<20;i++)
{
Beep=0;
DelayM(100);
Beep=1;
DelayM(100);
Beep=0;
DelayM(100);
Beep=1;
DelayM(500);
}
} break;
default : break;
}
Beep=1;
}
/********************************************************************************************/
uchar Key_Scan(void)
{
static bit Key_Down;
static uchar K;
uchar K_Value=0;
if(Key_Time>250) Key_Time=3;
if(Key_Time>=3&&Key_Down==0)
{
Key_Time=0;
Key_Down=1;
switch(Key)
{
case 0xfe : K=1; break;
case 0xfd : K=2; break;
case 0xfb : K=3; break;
case 0xf7 : K=4; break;
default : K=0; break;
}
}
if(Key==0xff&&Key_Down==1)
{
Key_Down=0;
K_Value=K;
Play(1);
}
return K_Value;
}
/********************************************************************************************/
void Disp_Time(void)
{
Disp_Buff[3]=current_time.minute%16;
Disp_Buff[2]=current_time.minute/16;
Disp_Buff[1]=current_time.hour%16;
Disp_Buff[0]=current_time.hour/16;
}
/********************************************************************************************/
void Disp_Temperature_3bit(void)
{
Disp_Buff[3]=20;
Disp_Buff[2]=10; // display 'c'
Disp_Buff[1]=Temperature[2];
Disp_Buff[0]=Temperature[1];
}
/********************************************************************************************
void Disp_Temperature_4bit(void)
{
Disp_Buff[3]=Temperature[3];
Disp_Buff[2]=10; // display c
Disp_Buff[1]=Temperature[2];
Disp_Buff[0]=Temperature[1];
}
********************************************************************************************/
uchar Select_M=4;
void MENU (void)
{
uchar K;
char Menu=0;
pp=0; // close ':'
while(Menu!=4)
{
K=Key_Scan();
switch (Menu)
{
case 0 : { Put_In(S_C); } break;
case 1 : { Put_In(S_A); } break;
case 2 : { Put_In(d_p); } break;
case 3 : { Put_In(BELL);}
default : break;
}
if(K==Key3) Menu=4;
if(K==Key2) Menu=(Menu+1)%4;
if(K==Key1) {
Select_M=Menu; Menu=4;
}
}
pp=1;
}
/********************************************************************************************/
void Set_Time(void)
{
bit Set_Flag=1; // set time flag
uchar K;
SB=0;
Disp_Time();
T=0;
TT=0;
while(Set_Flag)
{
K=Key_Scan();
if(K==Key1)
{
switch(SB)
{
case 0 : {
if(Disp_Buff[1]>3)
Disp_Buff[0]=(Disp_Buff[0]+1)%2;
else
Disp_Buff[0]=(Disp_Buff[0]+1)%3;
} break;
case 1 : {
if(Disp_Buff[0]==2)
Disp_Buff[1]=(Disp_Buff[1]+1)%4;
else
Disp_Buff[1]=(Disp_Buff[1]+1)%10;
} break;
case 2 : {
Disp_Buff[2]=(Disp_Buff[2]+1)%6;
} break;
case 3 : {
Disp_Buff[3]=(Disp_Buff[3]+1)%10;
} break;
default : break;
}
}
if(K==Key2) { SB= (SB+1)%4; }
if(K==Key3) { Set_Flag=0; SB=4; }
if(K==Key4) {
Set_Flag=0; SB=4; Write_Time(); T_10S=0;
}
}
}
/********************************************************************************************/
void Write_Ds1302Ram(uchar address,uchar dat)
{
ds1302write_byte(0x8e,0x00);
ds1302write_byte(address,dat);
ds1302write_byte(0x8e,0x80);
}
/********************************************************************************************/
uchar dp; // display modle select bit
void Set_Dp(void)
{
bit dp_Set=1;
uchar K;
pp=0; // close ':'
//dp=ds1302read_byte(0xc3);
while(dp_Set)
{
K=Key_Scan();
switch(dp)
{
case 1 : Put_In(ON); break;
case 0 : Put_In(OFF); break;
default : break;
}
if(K==Key3) dp_Set=0;
if(K==Key2) dp=(dp+1)%2;
if(K==Key1) { dp_Set=0; Write_Ds1302Ram(0xc2,dp); T=0; TT=0; T_10S=0; }
}
pp=1;
}
/********************************************************************************************/
uchar Bell_State;
void Set_Bell(void)
{
bit Bell_Set=1;
uchar K;
pp=0;
//Bell_State=ds1302read_byte(0xc5);
while(Bell_Set)
{
K=Key_Scan();
switch(Bell_State)
{
case 1 : Put_In(ON); break;
case 0 : Put_In(OFF); break;
default : break;
}
if(K==Key3) Bell_Set=0;
if(K==Key2) Bell_State=(Bell_State+1)%2;
if(K==Key1) {
Write_Ds1302Ram(0xc4,Bell_State); Bell_Set=0; T=0; TT=0; T_10S=0;
}
}
pp=1;
}
/********************************************************************************************/
void Set_Alarm(void)
{
static bit First_Alarm_Set=0;
bit Set_Flag=1; // set time flag
uchar K;
SB=0;
T=0;
TT=0;
if(First_Alarm_Set)
{
Disp_Buff[0]=ds1302read_byte(0xc9)/16; //alarm hour
Disp_Buff[1]=ds1302read_byte(0xc9)%16;
Disp_Buff[2]=ds1302read_byte(0xcb)/16; //alarm minute
Disp_Buff[3]=ds1302read_byte(0xcb)%16;
}
else Disp_Time();
while(Set_Flag)
{
K=Key_Scan();
if(K==Key1)
{
switch(SB)
{
case 0 : {
if(Disp_Buff[1]>3)
Disp_Buff[0]=(Disp_Buff[0]+1)%2;
else
Disp_Buff[0]=(Disp_Buff[0]+1)%3;
} break;
case 1 : {
if(Disp_Buff[0]==2)
Disp_Buff[1]=(Disp_Buff[1]+1)%4;
else
Disp_Buff[1]=(Disp_Buff[1]+1)%10;
} break;
case 2 : {
Disp_Buff[2]=(Disp_Buff[2]+1)%6;
} break;
case 3 : {
Disp_Buff[3]=(Disp_Buff[3]+1)%10;
} break;
default : break;
}
}
if(K==Key2) { SB= (SB+1)%4; }
if(K==Key3) { Set_Flag=0; SB=4; }
if(K==Key4) {
Set_Flag=0; SB=4;
First_Alarm_Set=1;
Write_Alarm(); T_10S=0;
}
}
}
/********************************************************************************************/
/*
void Ds1302Ram_Init(void)
{
uchar i;
//for(i=0xc3;i<0xc7;i+=2)
i=ds1302read_byte(0xc3);
if(i!=0||i!=1)
{
Write_Ds1302Ram(0xc2,0);
}
i=ds1302read_byte(0xc5);
if(i!=0||i!=1)
{
Write_Ds1302Ram(0xc4,0);
}
} */
/********************************************************************************************/
void main()
{
uchar K;
Key=0xff;
Timer0_Init();
Timer1_Init();
Clock_Init();
//Ds1302Ram_Init();
Put_In(Open_Logo);
Play(1);
DelayM(3000);
dp=ds1302read_byte(0xc3);
Bell_State=ds1302read_byte(0xc5);
Clock_Updata();
while(1)
{
//if(T_1S) { T_1S=0; Clock_Updata(); Temperature_Update(); }
Clock_Updata();
if(current_time.minute==0&&
current_time.second==0&&
Bell_State==1)
{ DelayM(1000); Play(2); }
K=Key_Scan();
if(K==4) MENU();
if(K==1&&dp==0) T=~T;
switch (Select_M)
{
case 0 : { Set_Time(); Select_M=4; } break;
case 1 : { Set_Alarm(); Select_M=4; } break;
case 2 : { Set_Dp(); Select_M=4; } break;
case 3 : { Set_Bell(); Select_M=4; };break;
default : break;
}
if(T_10S==1&&dp) { TT=~TT; T_10S=0; }
if(TT==0&&dp)
{
Disp_Time();
}
if(TT&&dp)
{
Temperature_Update();
Disp_Temperature_3bit();
}
if(T==0&&dp==0)
{
 
一周热门 更多>