我想要当采集时钟时间为晚上八点也就是20点时,蜂鸣器响,为什么从
仿真中看引脚一直是灰色的呢?
#include
#include
#define uint unsigned int
#define uchar unsigned char
***it SDA=P1^0; //1302引脚端
***it SCK=P1^1;
***it RST=P1^2;
***it lcden=P2^2; //1602引脚端
***it lcdrs=P2^0;
***it lcdrw=P2^1;
***it bemp=P3^7; //蜂鸣器
uchar code table[]={"0123456789-:"}; //显示
void delayus(uint t)
{
uchar i;
for(i=0;i
}
void write_1302_byte(uchar dat) //写一个字节
{
uchar i;
SCK=0;
delayus(2);
for(i=0;i<8;i++)
{
SDA=dat&0x01;
delayus(2);
SCK=1;
delayus(2);
SCK=0;
dat>>=1;
}
}
void write_1302(uchar add,uchar date) //写一条命令
{
RST=0;
SCK=0;
RST=1;
delayus(2);
write_1302_byte(add);
write_1302_byte(date);
SCK=1;
RST=0;
}
uchar read_1302_byte() //读一个字节
{
uchar i,dat;
delayus(2);
for(i=0;i<8;i++)
{
dat>>=1;
if(SDA==1)
dat|=0x80;
SCK=1;
delayus(2);
SCK=0;
delayus(2);
}
return dat;
}
uchar read_1302(uchar add) //读一条命令
{
uchar date;
RST=0;
SCK=0;
RST=1;
write_1302_byte(add);
date=read_1302_byte();
SCK=1;
RST=0;
return date;
}
void init_1302() //1302初始化
{
uchar flag;
flag=read_1302(0x81);
if(flag&0x80) //如果秒最高位为1,则此时始终振荡器停止
{
write_1302(0x8e,0x00); //写不保护指令打开
write_1302(0x80,((55/10)<<4|(55%10))); //写秒,55s
write_1302(0x82,((59/10)<<4|(59%10))); //写分,59min
write_1302(0x84,((21/10)<<4|(21%10))); //写时,21h
write_1302(0x86,((11/10)<<4|(11%10))); //写日,11日
write_1302(0x88,((4/10)<<4|(4%10))); //写月,4月
write_1302(0x8c,((13/10)<<4|(13%10))); //写年,13年
write_1302(0x8e,0x80); //打开写保护指令
}
}
void delayms(uint z) //1ms延时
{
uint x,y;
for(x=z;z>0;z--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //1602写命令
{
lcdrs=0;
lcdrw=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date) //1602写数据
{
lcdrs=1;
lcdrw=0;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void dispsecond(uchar x) //显示秒
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0xc0+0x0c);
write_date(table[i]);
write_date(table[j]);
delayms(50);
}
void dispminute(uchar x) //显示分
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0xc0+0x09);
write_date(table[i]);
write_date(table[j]);
write_date(table[11]);
delayms(50);
}
void disphour(uchar x) //显示时
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0xc0+0x06);
write_date(table[i]);
write_date(table[j]);
write_date(table[11]);
delayms(50);
}
void dispday(uchar x) //显示日
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0x80+0x0d);
write_date(table[i]);
write_date(table[j]);
delayms(50);
}
void dispmonth(uchar x) //显示月
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0x80+0x0a);
write_date(table[i]);
write_date(table[j]);
write_date(table[10]);
delayms(50);
}
void dispyear(uchar x) //显示年
{
uchar i,j;
i=x/10;
j=x%10;
write_com(0x80+0x07);
write_date(table[i]);
write_date(table[j]);
write_date(table[10]);
delayms(50);
}
void init_1602() //1602初始化
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void init()
{
TMOD=0X01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
uchar second,minute,hour,day,month,year;
uchar read_value;
init_1602();
init_1302();
write_com(0x80);
write_date('D');
write_date('a');
write_date('t');
write_date('e');
write_com(0x80+0x05);
write_date('2');
write_date('0');
write_com(0xc0);
write_date('T');
write_date('i');
write_date('m');
write_date('e');
init();
while(1)
{
read_value=read_1302(0x8d); //读年
year=((read_value&0x70)>>4)*10+(read_value&0x0f);
dispyear(year); //显示年
read_value=read_1302(0x89); //读月
month=((read_value&0x70)>>4)*10+(read_value&0x0f);
dispmonth(month); //显示月
read_value=read_1302(0x87); //读日
day=((read_value&0x70)>>4)*10+(read_value&0x0f);
dispday(day); //显示日
read_value=read_1302(0x85); //读时
hour=((read_value&0x70)>>4)*10+(read_value&0x0f);
disphour(hour); //显示小时
read_value=read_1302(0x83); //读分
minute=((read_value&0x70)>>4)*10+(read_value&0x0f);
dispminute(minute); //显示分
read_value=read_1302(0x81); //读秒
second=((read_value&0x70)>>4)*10+(read_value&0x0f);//将BCD码转换为十进制
dispsecond(second);//显示秒
/* if(hour/10==2&&(hour%10==2))
{
bemp=0;
delayms(5);
bemp=1;
delayms(5);
}*/
}
}
void xx() interrupt 1
{
uchar num,read_value,hour;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
num++;
if(num==20)
{
num=0;
read_value=read_1302(0x85); //读时
hour=((read_value&0x70)>>4)*10+(read_value&0x0f);
if(hour/10==2&&(hour%10==0))
bemp=~bemp;
}
}