timer.c
#include
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=ET0=1;
}
timer.h
#ifndef _TIMER_H_
#define _TIMER_H_
#include
void Timer0Init(void);
#endif
smg.c
#include
unsigned char code smgduan[13]={
0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
0XBF,0XFF,0XC6};
//10:- 11:灭 12:C
unsigned char dspbuf[8];
void smg_display(void)
{
static unsigned char i=0;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
P2&=0X1F;
P2=(P2&0X1F)|0XC0;
P0=1<<i;
P2&=0X1F;
P2=(P2&0X1F)|0XE0;
P0=smgduan[dspbuf[i]];
P2&=0X1F;
if(++i==8)
i=0;
}
smg.h
#ifndef _SMG_H_
#define _SMG_H_
#include
extern unsigned char dspbuf[8];
void smg_display(void);
#endif
key.c
#include
unsigned char key_sum=0,k4_press,k5_press,k6_press,k7_press;
void key_read(void)
{
static unsigned char k4_time=0,k5_time=0,k6_time=0,k7_time=0;
key_sum=0;
if(k4==0)
{
k4_time++;
if(k4_time==1)
key_sum=4;
else if(k4_time==20)
{
key_sum=0;
k4_time=1;
}
}
else
k4_time=0;
if(k5==0)
{
k5_time++;
if(k5_time==1)
key_sum=5;
else if(k5_time==20)
{
key_sum=0;
k5_time=1;
}
}
else
k5_time=0;
if(k6==0)
{
k6_time++;
if(k6_time==1)
key_sum=6;
else if(k6_time==20)
{
key_sum=0;
k6_time=1;
}
}
else
k6_time=0;
if(k7==0)
{
k7_time++;
if(k7_time==1)
key_sum=7;
else if(k7_time==20)
{
key_sum=0;
k7_time=1;
}
}
else
k7_time=0;
}
void key_control(void)
{
if(key_sum==4) k4_press=1;
else if(key_sum==5) k5_press=1;
else if(key_sum==6) k6_press=1;
else if(key_sum==7) k7_press=1;
}
key.h
#ifndef _KEY_H_
#define _KEY_H_
#include
sbit k4=P3^3;
sbit k5=P3^2;
sbit k6=P3^1;
sbit k7=P3^0;
extern unsigned char k4_press;
extern unsigned char k5_press;
extern unsigned char k6_press;
extern unsigned char k7_press;
void key_read(void);
void key_control(void);
#endif
ds18b20.c
#include
#include
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t*=12;
while(t--);
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int ds18b20_temp_read(void)
{
unsigned char TH=0,TL=0;
unsigned int dat=0;
init_ds18b20();
Delay1ms();
Write_DS18B20(0XCC);
Write_DS18B20(0X44);
init_ds18b20();
Delay1ms();
Write_DS18B20(0XCC);
Write_DS18B20(0XBE);
TL= Read_DS18B20();
TH= Read_DS18B20();
dat=TH;
dat<<=8;
dat|=TL;
dat*=0.0625;
return dat;
}
ds18b20.h
#ifndef _DS18B20_H_
#define _DS18B20_H_
#include
unsigned int ds18b20_temp_read(void);
#endif
ds1302.c
#include
#include
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
unsigned char code addr_write[3]={
0X80,0X82,0X84};
unsigned char code addr_read[3]={
0X81,0X83,0X85};
unsigned char time[3]={
0X50,0X59,0X23};
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
void ds1302_write(void)
{
unsigned char i=0;
EA=0;
Write_Ds1302(0X8E,0X00);
for(i=0;i<3;i++)
Write_Ds1302(addr_write[i],time[i]);
Write_Ds1302(0X8E,0X80);
EA=1;
}
void ds1302_read(void)
{
unsigned char i=0;
EA=0;
for(i=0;i<3;i++)
time[i]=Read_Ds1302(addr_read[i]);
EA=1;
}
ds1302.h
#ifndef _DS1302_H_
#define _DS1302_H_
#include
void ds1302_write(void);
void ds1302_read(void);
void Write_Ds1302( unsigned char address,unsigned char dat ) ;
unsigned char Read_Ds1302 ( unsigned char address );
extern unsigned char time[3];
extern unsigned char code addr_write[3];
#endif
main.c
看起来很多的样子,其实很多都是重复的,调时间,调闹钟,闪烁之类的基本都是复制,然后稍微改一下就行
#include
#include
#include
#include
#include
#include
unsigned char flag_smg=0;
unsigned char flag_smg_time=0,flag_smg_alarm=0;
unsigned char flag_key;
unsigned char flag_500ms=1;
unsigned char k7_count=1,k6_count=1;
unsigned char flag_temp;
unsigned char time_set,alarm_set;
unsigned char flag_float,flag_alarm,flag_close_alarm,float_time=0;
unsigned char flag_led;
unsigned char time_alarm[3]={
0X00,0X00,0X00};
unsigned int temp;
void bee_init(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2&=0X1F;
}
void main(void)
{
ds1302_write();
bee_init();
Timer0Init();
while(1)
{
/**********key**********/
if(flag_key)
{
flag_key=0;
key_read();
key_control();
}
/*****k4*****/
if(k4_press)
{
k4_press=0;
if(flag_smg==0)
{
switch(flag_smg_time) //K4调时间
{
case 0:
flag_temp=1;flag_smg=2;break;
case 1:
time_set=time[2]/16*10+time[2]%16;
time_set--;
if(!(time_set<=24&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[2],time_set);
Write_Ds1302(0X8E,0X80);
break;
case 2:
time_set=time[1]/16*10+time[1]%16;
time_set--;
if(!(time_set<=60&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[1],time_set);
Write_Ds1302(0X8E,0X80);
break;
case 3:
time_set=time[0]/16*10+time[0]%16;
time_set--;
if(!(time_set<=60&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[0],time_set);
Write_Ds1302(0X8E,0X80);
break;
}
}
if(flag_smg==1) //K4调闹钟时间
{
switch(flag_smg_alarm)
{
case 1:
alarm_set=time_alarm[2]/16*10+time_alarm[2]%16;
alarm_set--;
if(!(alarm_set>=0&&alarm_set<=24))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[2]=alarm_set;
break;
case 2:
alarm_set=time_alarm[1]/16*10+time_alarm[1]%16;
alarm_set--;
if(!(alarm_set>=0&&alarm_set<=60))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[1]=alarm_set;
break;
case 3:
alarm_set=time_alarm[0]/16*10+time_alarm[0]%16;
alarm_set--;
if(!(alarm_set>=0&&alarm_set<=60))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[0]=alarm_set;
break;
}
}
}
if(k4==1&&k5==1&&k6==1&&k7==1) //温度
{
if(flag_temp)
{
flag_temp=0;
flag_smg=0;
}
}
/*****k5*****/
else if(k5_press)
{
k5_press=0;
if(flag_smg==0) //K5调时间
{
switch(flag_smg_time)
{
case 1:
time_set=time[2]/16*10+time[2]%16;
time_set++;
if(!(time_set<=24&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[2],time_set);
Write_Ds1302(0X8E,0X80);
break;
case 2:
time_set=time[1]/16*10+time[1]%16;
time_set++;
if(!(time_set<60&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[1],time_set);
Write_Ds1302(0X8E,0X80);
break;
case 3:
time_set=time[0]/16*10+time[0]%16;
time_set++;
if(!(time_set<60&&time_set>=0))
time_set=0;
time_set=time_set/10*16+time_set%10;
Write_Ds1302(0X8E,0x00);
Write_Ds1302(addr_write[0],time_set);
Write_Ds1302(0X8E,0X80);
break;
}
}
else if(flag_smg==1) //K5调闹钟时间
{
switch(flag_smg_alarm)
{
case 1:
alarm_set=time_alarm[2]/16*10+time_alarm[2]%16;
alarm_set++;
if(!(alarm_set>=0&&alarm_set<=24))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[2]=alarm_set;
break;
case 2:
alarm_set=time_alarm[1]/16*10+time_alarm[1]%16;
alarm_set++;
if(!(alarm_set>=0&&alarm_set<=60))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[1]=alarm_set;
break;
case 3:
alarm_set=time_alarm[0]/16*10+time_alarm[0]%16;
alarm_set++;
if(!(alarm_set>=0&&alarm_set<=60))
alarm_set=0;
alarm_set=alarm_set/10*16+alarm_set%10;
time_alarm[0]=alarm_set;
break;
}
}
}
/*****k6*****/
else if(k6_press)
{
k6_press=0;
flag_smg=1;
if(++k6_count==5) k6_count=1;
switch(k6_count)
{
case 1:flag_smg=0;flag_smg_time=0;break;
case 2:flag_smg_alarm=1;flag_500ms=1;break;
case 3:flag_smg_alarm=2;flag_500ms=1;break;
case 4:flag_smg_alarm=3;flag_500ms=1;break;
}
}
/*****k7*****/
else if(k7_press)
{
k7_press=0;
flag_smg=0;
if(++k7_count==5) k7_count=1;
switch(k7_count)
{
case 1:flag_smg_time=0;break;
case 2:flag_smg_time=1;flag_500ms=1;break;
case 3:flag_smg_time=2;flag_500ms=1;break;
case 4:flag_smg_time=3;flag_500ms=1;break;
}
}
/**********ds18b20**********/
temp=ds18b20_temp_read();
/**********ds1302**********/
ds1302_read();
/**********alarm**********/
if(time[2]==time_alarm[2])
{
if(time[1]==time_alarm[1])
{
if(time[0]==time_alarm[0])
flag_alarm=1;
else
flag_alarm=0;
}
}
if(flag_alarm)
{
flag_alarm=0;
flag_led=1;
flag_close_alarm=0;
}
if(flag_led==1&&flag_close_alarm==0)
{
if(flag_float)
{
flag_float=0;
float_time++;
if(float_time<=25)
{
if(float_time%2)
{
P2=(P2&0X1F)|0X80;
P0=0XFE;
P2&=0X1F;
}
else
{
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2&=0X1F;
}
}
else
{
float_time=0;
flag_led=0;
flag_close_alarm=0;
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2&=0X1F;
}
}
if(k4==0||k5==0||k6==0||k7==0) //按任意按键取消闹钟
{
flag_close_alarm=1;
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2&=0X1F;
}
}
/**********smg**********/
if(flag_smg==0)
{
if(flag_smg_time==0)
{
dspbuf[0]=time[2]/16;
dspbuf[1]=time[2]%16;
dspbuf[2]=10;
dspbuf[3]=time[1]/16;
dspbuf[4]=time[1]%16;
dspbuf[5]=10;
dspbuf[6]=time[0]/16;
dspbuf[7]=time[0]%16;
}
else if(flag_smg_time==1)
{
if(flag_500ms%2) //500ms闪烁
dspbuf[0]=time[2]/16;
dspbuf[1]=time[2]%16;
}
else
{
dspbuf[0]=11;
dspbuf[1]=11;
}
dspbuf[2]=10;
dspbuf[3]=time[1]/16;
dspbuf[4]=time[1]%16;
dspbuf[5]=10;
dspbuf[6]=time[0]/16;
dspbuf[7]=time[0]%16;
}
else if(flag_smg_time==2)
{
dspbuf[0]=time[2]/16;
dspbuf[1]=time[2]%16;
dspbuf[2]=10;
if(flag_500ms%2)
{
dspbuf[3]=time[1]/16;
dspbuf[4]=time[1]%16;
}
else
{
dspbuf[3]=11;
dspbuf[4]=11;
}
dspbuf[5]=10;
dspbuf[6]=time[0]/16;
dspbuf[7]=time[0]%16;
}
else if(flag_smg_time==3)
{
dspbuf[0]=time[2]/16;
dspbuf[1]=time[2]%16;
dspbuf[2]=10;
dspbuf[3]=time[1]/16;
dspbuf[4]=time[1]%16;
dspbuf[5]=10;
if(flag_500ms%2)
{
dspbuf[6]=time[0]/16;
dspbuf[7]=time[0]%16;
}
else
{
dspbuf[6]=11;
dspbuf[7]=11;
}
}
}
if(flag_smg==1)
{
if(flag_smg_alarm==0)
{
dspbuf[0]=time_alarm[2]/16;
dspbuf[1]=time_alarm[2]%16;
dspbuf[2]=10;
dspbuf[3]=time_alarm[1]/16;
dspbuf[4]=time_alarm[1]%16;
dspbuf[5]=10;
dspbuf[6]=time_alarm[0]/16;
dspbuf[7]=time_alarm[0]%16;
}
else if(flag_smg_alarm==1)
{
if(flag_500ms%2)
{
dspbuf[0]=time_alarm[2]/16;
dspbuf[1]=time_alarm[2]%16;
}
else
{
dspbuf[0]=11;
dspbuf[1]=11;
}
dspbuf[2]=10;
dspbuf[3]=time_alarm[1]/16;
dspbuf[4]=time_alarm[1]%16;
dspbuf[5]=10;
dspbuf[6]=time_alarm[0]/16;
dspbuf[7]=time_alarm[0]%16;
}
else if(flag_smg_alarm==2)
{
dspbuf[0]=time_alarm[2]/16;
dspbuf[1]=time_alarm[2]%16;
dspbuf[2]=10;
if(flag_500ms%2)
{
dspbuf[3]=time_alarm[1]/16;
dspbuf[4]=time_alarm[1]%16;
}
else
{
dspbuf[3]=11;
dspbuf[4]=11;
}
dspbuf[5]=10;
dspbuf[6]=time_alarm[0]/16;
dspbuf[7]=time_alarm[0]%16;
}
else if(flag_smg_alarm==3)
{
dspbuf[0]=time_alarm[2]/16;
dspbuf[1]=time_alarm[2]%16;
dspbuf[2]=10;
dspbuf[3]=time_alarm[1]/16;
dspbuf[4]=time_alarm[1]%16;
dspbuf[5]=10;
if(flag_500ms%2)
{
dspbuf[6]=time_alarm[0]/16;
dspbuf[7]=time_alarm[0]%16;
}
else
{
dspbuf[6]=11;
dspbuf[7]=11;
}
}
}
else if(flag_smg==2)
{
dspbuf[0]=11;
dspbuf[1]=11;
dspbuf[2]=11;
dspbuf[3]=11;
dspbuf[4]=11;
dspbuf[5]=temp/10;
dspbuf[6]=temp%10;
dspbuf[7]=12;
}
}
}
void timer0_isr(void) interrupt 1
{
static unsigned char flag_key_time=0;
static unsigned char flag_200ms_time=0;
static unsigned int flag_500ms_time=0;
if(++flag_key_time==30)
{
flag_key_time=0;
flag_key=1;
}
if(++flag_200ms_time==200)
{
flag_200ms_time=0;
flag_float=1;
}
if(++flag_500ms_time==500)
{
flag_500ms_time=0;
flag_500ms++;
}
smg_display();
}