老规矩,建好工程后直接将将程序复制粘贴到里面就可以用了。
用的是CT107D15开发板,CT107S的估计也能用
不会用的话问我!女士优先,我快两个月没跟女生说过话了!
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xc6,0xff,0x86,0x8c};
unsigned char yi,er,san,si,wu,liu,qi,ba;
long wendu;
unsigned char shijian[]={50,59,16,0,0,0,0};
unsigned char num[7];
bit jiemian=0;
unsigned char tiao1=0;
unsigned char tiao2=0;
int shuju;
bit an;
unsigned char yuzhi1=17;
unsigned char yuzhi2=25;
unsigned char yuzhi3=4;
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SBA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
sbit DQ = P1^4; //单总线接口
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
//总线启动条件
void IIC_Start(void)
{
SBA = 1;
SCL = 1;
somenop;
SBA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SBA = 0;
SCL = 1;
somenop;
SBA = 1;
}
//等待应答
bit IIC_WaitAck(void)
{
SBA = 1;
somenop;
SCL = 1;
somenop;
if(SBA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SBA = 1;
}
else
{
SBA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SBA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char AD()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return temp;
}
void chushihua()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void Delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
void shumaguan1(unsigned char yi,unsigned char er)
{
P2=0XC0;
P0=0X01;
P2=0XFF;
P0=tab[yi];
Delayms(1);
P2=0XC0;
P0=0X02;
P2=0XFF;
P0=tab[er];
Delayms(1);
}
void shumaguan2(unsigned char san,unsigned char si)
{
P2=0XC0;
P0=0X04;
P2=0XFF;
P0=tab[san];
Delayms(1);
P2=0XC0;
P0=0X08;
P2=0XFF;
P0=tab[si];
Delayms(1);
}
void shumaguan3(unsigned char wu,unsigned char liu)
{
P2=0XC0;
P0=0X10;
P2=0XFF;
P0=tab[wu];
Delayms(1);
P2=0XC0;
P0=0X20;
P2=0XFF;
P0=tab[liu];
Delayms(1);
}
void shumaguan4(unsigned char qi,unsigned char ba)
{
P2=0XC0;
P0=0X40;
P2=0XFF;
P0=tab[qi];
Delayms(1);
P2=0XC0;
P0=0X80;
P2=0XFF;
P0=tab[ba];
Delayms(1);
}
void juzhen()
{
P44=0;P42=1;P35=1;P34=1;
if((P42==1)&&(P35==1)&&(P34==1))
{
if(P32==0)
{
Delayms(5);
if(P32==0)
{
while(!P32)
{
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
if(jiemian==0)
{
if(tiao1==0)
{
tiao1=1;
}
else
if(tiao1==1)
{
tiao1=2;
}
else
if(tiao1==2)
{
tiao1=0;
}
}
if(jiemian==1)
{
if(tiao2==0)
{
tiao2=1;
}
else
if(tiao2==1)
{
tiao2=2;
}
else
if(tiao2==2)
{
tiao2=0;
}
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(!P33)
{
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
if(jiemian==0)
{
jiemian=1;
}
else
if(jiemian==1)
{
jiemian=0;
}
}
}
P44=1;P42=0;P35=1;P34=1;
if((P44==1)&&(P35==1)&&(P34==1))
{
if(P32==0)
{
Delayms(5);
if(P32==0)
{
while(!P32)
{
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
if(jiemian==1)
{
if(tiao2==0)
{
yuzhi1=yuzhi1+1;
}
if(tiao2==1)
{
yuzhi2=yuzhi2+1;
}
if(tiao2==2)
{
yuzhi3=yuzhi3+1;
if(yuzhi3==9)
{
yuzhi3=yuzhi3-1;
}
}
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(!P33)
{
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
if(jiemian==1)
{
if(tiao2==0)
{
yuzhi1=yuzhi1-1;
}
if(tiao2==1)
{
yuzhi2=yuzhi2-1;
}
if(tiao2==2)
{
yuzhi3=yuzhi3-1;
if(yuzhi3==3)
{
yuzhi3=yuzhi3+1;
}
}
}
}
}
}
//单总线延时函数
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0; i<8; 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;
}
long zhuchengxu()
{
long di,gao,temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(5);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(5);
di=Read_DS18B20();
gao=Read_DS18B20();
gao=gao&0x0f;
gao=gao<<8;
temp=(di|gao)*625;
return temp;
}
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/10)<<4)|(dat%10));
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
unsigned char dat1,dat2;
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_();
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void chushihua1()
{
unsigned char i,add;
Write_Ds1302(0x8e,0x00);
add=0x80;
for(i=0;i<7;i++)
{
Write_Ds1302(add,shijian[i]);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
void DS1302()
{
unsigned char i,add;
Write_Ds1302(0x8e,0x00);
add=0x81;
for(i=0;i<7;i++)
{
num[i]=Read_Ds1302(add);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
bit add=0,bdd=0,ddd=1;
bit ap=0;
bit au=1;
bit al=1;
bit ao=1;
bit am=1;
bit ml=1;
void LED()
{
if(yuzhi1>=num[2])
{
add=1;
}
else
{
add=0;
}
if(yuzhi2>wendu/10000)
{
bdd=1;
}
else
{
bdd=0;
}
if(an==1)
{
ap=0;
}
else
{
ap=1;
}
if(yuzhi3==4)
{
au=0;
al=1;
ao=1;
am=1;
ml=1;
}
if(yuzhi3==5)
{
au=1;
al=0;
ao=1;
am=1;
ml=1;
}
if(yuzhi3==6)
{
au=1;
al=1;
ao=0;
am=1;
ml=1;
}
if(yuzhi3==7)
{
au=1;
al=1;
ao=1;
am=0;
ml=1;
}
if(yuzhi3==8)
{
au=0;
al=1;
ao=1;
am=1;
ml=0;
}
P00=add;
P01=bdd;
P02=ddd;
P03=au;
P04=al;
P05=ao;
P06=am;
P07=ml;
P2=0X80;
}
int b;
int k;
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;ET0=1;
}
void tm0_isr() interrupt 1
{
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
if(ap==0)
{
b++;
if(b==3000)
{
if(ap==0)
{
k=0;
ddd=0;
}
}
}
if(ap==1)
{
k++;
if(k==3000)
{
b=0;
ddd=1;
}
}
}
void main()
{
chushihua();
chushihua1();
Timer0Init();
while(1)
{
DS1302();
if(jiemian==0)
{
if(tiao1==0)
{
yi=num[2]/10;er=num[2]%10;san=20;
si=num[1]/10;wu=num[1]%10;liu=20;
qi=num[0]/10;ba=num[0]%10;
}
if(tiao1==1)
{
wendu=zhuchengxu();
yi=21;er=22;san=22;si=22;wu=22;liu=wendu/100000;qi=wendu%100000/10000+10;ba=wendu%10000/1000;
}
if(tiao1==2)
{
shuju=AD()*100/51;
if(shuju>50)
{
an=0;
}
if((shuju<50)&&(shuju>0))
{
an=1;
}
yi=23;er=22;san=shuju/100+10;si=shuju%100/10;wu=shuju%10;liu=22;qi=22;ba=an;
}
}
if(jiemian==1)
{
if(tiao2==0)
{
yi=24;er=1;san=22;si=22;wu=22;liu=22;qi=yuzhi1/10;ba=yuzhi1%10;
}
if(tiao2==1)
{
yi=24;er=2;san=22;si=22;wu=22;liu=22;qi=yuzhi2/10;ba=yuzhi2%10;
}
if(tiao2==2)
{
yi=24;er=3;san=22;si=22;wu=22;liu=22;qi=22;ba=yuzhi3;
}
}
if(jiemian==0)
{
LED();
}
shuju=AD()*100/51;
if(shuju>50)
{
an=0;
}
if((shuju<50)&&(shuju>0))
{
an=1;
}
juzhen();
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}