#include <reg51.h>
#include <INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
#define display8 0xfe //数码管1从左至右
#define display7 0xfd //数码管2从左至右
#define display6 0xfb //数码管3从左至右
#define display5 0xf7 //数码管4从左至右
#define display4 0xef //数码管5从左至右
#define display3 0xdf //数码管6从左至右
#define display2 0xbf //数码管7从左至右
#define display1 0x7f //数码管8从左至右
//===以下IO定义请根据您硬件的连接修改===
sbit T_RST=P1^4;//ds1302-5
sbit T_IO=P1^3;//ds1302-6
sbit T_CLK=P1^2;//ds1302-7
bit dula;//数码管段锁存器,wuyong
bit wela;//数码管位锁存器,wuyong
bit led;//LED流水灯锁存器,wuyong
bit JI;//wuyong
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit up=P3^6;
sbit down=P3^5;
sbit set=P3^7;
sbit did=P1^7;
sbit sda=P1^6;
sbit scl=P1^5;
sbit SDA_TEMP=P1^0;
sbit SCL_TEMP=P1^1;
unsigned char eepromdata[8];
unsigned char temperdata[2];
unsigned char timecount;
unsigned char displaycount;
bit secondflag=0;
unsigned char secondcount=0;
unsigned char retn;
unsigned int result;
unsigned char x;
unsigned int k;
unsigned int ks;
uchar a,b,clock_ss,clock_sg,clock_fs,clock_fg,clock_ms,clock_mg,clock_mg_temp;
uchar temp_disp=0;
unsigned char displaybuffer[4];
int tempdf,hour,mie,sei,c,nhour,nmie,shour,smie;
uchar clk_time[3]; //秒,分,时寄存器初始值
code uchar ledmap[]={
0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
0x88, 0x83, 0xC6, 0xa1 ,0x86 ,0x8e, 0xff};
//数码管段码
unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,25,28,31,34,38,41,44,48,50,53,56,59,63,66,69,72,75,78,81,84,88,91,94,97};
void delay_TEMP(void);
void delay10ms(void);
void i_start(void);
void i_stop(void);
void i_init(void);
void i_ack(void);
bit i_clock(void);
bit i_send(unsigned char i_data);
unsigned char i_receive(void);
bit start_temperature_T(void);
bit read_temperature_T(unsigned char *p);
void delay1()
{ ;; }
void delay_TEMP(void){_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
void delay10ms(void){unsigned int i;for(i=0;i<1000;i++){delay_TEMP();}}
void i_start(void){SCL_TEMP=1;delay_TEMP();SDA_TEMP=0;delay_TEMP();SCL_TEMP=0;delay_TEMP();}
void i_stop(void)
{SDA_TEMP=0;
delay_TEMP();
SCL_TEMP=1;
delay_TEMP();
SDA_TEMP=1;
delay_TEMP();
SCL_TEMP=0;
delay_TEMP();}
void i_init(void)
{SCL_TEMP=0;i_stop();}
void t0(void) interrupt 1 using 0 //T0用于定时1s时间到
{secondcount++;if(secondcount==100){secondcount=0;secondflag=1;}
TH0=55536/256;TL0=55536%256;}
void i_ack(void){SDA_TEMP=0;i_clock();SDA_TEMP=1;}
bit i_clock(void)
{bit sample;
SCL_TEMP=1;
delay_TEMP();
sample=SDA_TEMP;
_nop_();_nop_();
SCL_TEMP=0;
delay_TEMP();
return(sample);}
bit i_send(unsigned char i_data)
{unsigned char i;
for(i=0;i<8;i++){SDA_TEMP=(bit)(i_data & 0x80);
i_data=i_data<<1;
i_clock();}
SDA_TEMP=1;
return(~i_clock());}
unsigned char i_receive(void)
{unsigned char i_data=0;
unsigned char i;
for(i=0;i<8;i++){i_data*=2;if(i_clock()) i_data++;}return(i_data);}
bit start_temperature_T(void)
{i_start();
if(i_send(0x90))
{if(i_send(0xee))
{i_stop();delay_TEMP();return(1);}
else{i_stop();
delay_TEMP();return(0);}}
else{i_stop();delay_TEMP();return(0);}}
bit read_temperature_T(unsigned char *p)
{i_start();
if(i_send(0x90))
{ if(i_send(0xaa)){
i_start();
if(i_send(0x91))
{*(p+1)=i_receive();
i_ack();
*p=i_receive();
i_stop();
delay_TEMP();
return(1);}
else{i_stop();delay_TEMP();return(0);}}
else{i_stop();delay_TEMP();return(0);}}
else{i_stop();delay_TEMP();return(0);}}
void start() //开始信号
{
sda=1;
delay1();
scl=1;
delay1();
sda=0;
delay1();
}
void stop() //停止
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
delay1();
}
void respons() //应答
{
uchar i;
scl=1;
delay1();
while((sda==1)&&(i<250))i++;
scl=0;
delay1();
}
/******************初始化***************************/
void init()
{
sda=1;
delay1();
scl=1;
delay1();
}
/******************2401写1字节***************************/
void writ_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay1();
sda=CY;
delay1();
scl=1;
delay1();
// scl=0;
// delay();
}
scl=0;
delay1();
sda=1;
delay1();
}
/******************2401读1字节�***************************/
uchar rea_byte()
{
uchar i,k;
scl=0;
delay1();
sda=1;
delay1();
for(i=0;i<8;i++)
{
scl=1;
delay1();
k=(k<<1)|sda;
scl=0;
delay1();
}
return k;
}
/******************24C01:写入数据(先送地址,再写数据)***************************/
void write_add(uchar address,uchar date)
{
start();
writ_byte(0xa0);
respons();
writ_byte(address);
respons();
writ_byte(date);
respons();
stop();
}
/******************24c01:读入数据(先送地址,再读数据)***************************/
uchar read_add(uchar address)
{
uchar date;
start();
writ_byte(0xa0);
respons();
writ_byte(address);
respons();
start();
writ_byte(0xa1);
respons();
date=rea_byte();
stop();
return date;
}
/******************DS1302:写入操作(上升沿)*********************/
void write_byte(uchar da)
{
uchar i;
ACC=da;
for(i=8;i>0;i--)
{
T_IO=ACC0;
T_CLK=0;
T_CLK=1;
ACC=ACC>>1;
}
}
/******************DS1302:读取操作(下降沿)*****************/
uchar read_byte(void)
{
uchar i;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
T_CLK = 1;
T_CLK = 0;
ACC7 = T_IO;
}
return(ACC);
}
/******************DS1302:写入数据(先送地址,再写数据)***************************/
void write_1302(uchar addr,uchar da)
{
T_RST=0; //停止工作
T_CLK=0;
T_RST=1; //重新工作
write_byte(addr); //写入地址
write_byte(da);
T_RST=0;
T_CLK=1;
}
/******************DS1302:读取数据(先送地址,再读数据)**************************/
uchar read_1302(uchar addr)
{
uchar temp;
T_RST=0; //停止工作
T_CLK=0;
T_RST=1; //重新工作
write_byte(addr); //写入地址
temp=read_byte();
T_RST=0;
T_CLK=1; //停止工作
return(temp);
}
/***********************延时程序=a*1ms**************************************/
void delay(uchar a)
{
uchar i;
while(a-- !=0)
{
for(i=0;i<125;i++);
}
}
void fn()//喇叭
{
did=0;
delay(20);
did=1;
delay(20);
did=0;
/*delay(120);
did=1;
delay(120);
did=0;
delay(120);
did=1;
delay(120);
did=0;
delay(320);
did=1;
delay(400);*/
}
/***********************显示程序**********************************************/
/* DS1302秒,分,时寄存器是BCD码形式: 用16求商和余进行"高4位"和"低4位"分离 */
/****************************************************************************/
void led_disp()
{ uchar cant1,cant2;
if(b==0)
{cant1=0;cant2=0;
clock_mg_temp=clock_mg;//保存尚一次的秒钟
clock_ms=clk_time[0]/ 16; clock_mg=clk_time[0]%16;
clock_fs=clk_time[1]/ 16; clock_fg=clk_time[1]%16;
mie=clock_fs*10+ clock_fg;
clock_ss=clk_time[2]/ 16; clock_sg=clk_time[2]%16; //BCD*to*10
hour=clock_ss*10+ clock_sg;
}
if(b==1)
{
cant1++;
cant2=0;
clock_ss=hour/ 10; clock_sg=hour%10;
clock_fs=mie/ 10; clock_fg=mie%10;
clock_ms=sei/10; clock_mg=sei%10;
}
if(b==2)
{ cant1=0;
cant2++;
clock_ss=hour/ 10; clock_sg=hour%10;
clock_fs=mie/ 10; clock_fg=mie%10;
clock_ms=sei/10; clock_mg=sei%10;