直接复制粘贴然后运行
然后打开stc烧录到开发板上面就能用 程序哪里不懂的话问我,我闲的蛋疼!
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char a=0,b=0;
unsigned char yuzhi=50;
bit panding,zuduan=0,fanzhuan=1;
int shuju;
unsigned char cuncu;
unsigned char shijian[]={0,30,8,0,0,0,0};
unsigned char num[7];
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
#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 SDBA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
void IIC_Start(void)
{
SDBA = 1;
SCL = 1;
somenop;
SDBA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDBA = 0;
SCL = 1;
somenop;
SDBA = 1;
}
//等待应答
bit IIC_WaitAck(void)
{
SDBA = 1;
somenop;
SCL = 1;
somenop;
if(SDBA)
{
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)
{
SDBA = 1;
}
else
{
SDBA = 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(SDBA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char AD()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
unsigned char EEPROM(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0XA1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void EEPROM2(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
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 dingshi()
{
unsigned char i,add;
add=0x80;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302(add,shijian[i]);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
void DS1302()
{
unsigned char i,add;
add=0x81;
Write_Ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
num[i]=Read_Ds1302 (add);
add=add+2;
}
Write_Ds1302(0x8e,0x80);
}
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 chushihua()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void anjian()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
while(~P30);
}
if(a==0)
{
if(b==0)
{
b++;
}
else if(b==1)
{
b--;
zuduan=0;
}
}
}
if(P31==0)
{
Delayms(5);
if(P31==0)
{
while(~P31);
}
if(b==0)
{
if(a==0)
{
a++;
}
else if(a==1)
{
a--;
}
}
if(b==1)
{
zuduan=1;
if(panding==1)
{
P2=0XA0;P04=0;P06=0; //如果蜂鸣器打开的话就断开蜂鸣器
panding=0; //判定一次后就不再判定
}
if(panding==0)
{
P2=0XA0;P04=0;P06=fanzhuan;
fanzhuan=~fanzhuan;
}
}
}
if(P32==0)
{
Delayms(5);
if(P32==0)
{
while(~P32);
}
if(a==1)
{
yuzhi++;
}
if(b==1)
{
P2=0XA0;P04=1;P06=0;
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(~P33);
}
if(a==1)
{
yuzhi--;
}
if(b==1)
{
P2=0XA0;P04=0;P06=0;
}
}
}
void jidianqi()
{
if(b==0)
{
if(shuju/100<yuzhi)
{
P2=0XA0;
P04=1;
P06=0;
}
else if(shuju/100>yuzhi)
{
P2=0XA0;P04=0;P06=0;
}
}
}
void fengmingqi()
{
if(b==1)
{
if(shuju/100<yuzhi)
{
P2=0XA0;P04=0;P06=1;
panding=1;
}
else if(shuju/100>yuzhi)
{
P2=0XA0;P04=0;P06=0;
panding=0;
}
}
}
void main()
{
chushihua();
dingshi();
while(1)
{
yuzhi=EEPROM(0X01);Delayms(2);
jidianqi();
if(zuduan==0)
{
fengmingqi();
}
anjian();
if(a==0)
{
DS1302();
shuju=AD()*39;
yi=num[2]/10;er=num[2]%10;san=10;
si=num[1]/10;wu=num[1]%10;liu=10;
qi=shuju/1000,ba=shuju%1000/100;
}
if(a==1)
{
yi=10;er=10;san=11;si=11;wu=11;liu=11;qi=yuzhi/10;ba=yuzhi%10;
}
if(b==0)
{
P2=0X80;P0=0XFE;
}
if(b==1)
{
P2=0X80;P0=0XFD;
}
EEPROM2(0X01,yuzhi);
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}