本帖最后由 4758866 于 2012-12-2 12:41 编辑
主要在最后一个子函数。fm不能置1
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,
0x79,0x71
};
sbit duan=P2^6;
sbit wei=P2^7;
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define WRITE_PROTECT 0x8E
sbit ACC_7=ACC^7;//位寻址寄存器定义
sbit SCLK=P3^2; // DS1302时钟信号 6脚
sbit DIO=P2^4; // DS1302数据信号 7脚
sbit CE=P3^3; // DS1302片选 5脚
sbit s1=P3^4;
sbit s2=P3^5;
sbit s3=P3^6;
sbit s4=P3^7;
sbit fm=P2^3;
uchar miao,fen,shi,fun,s2a,lshi,lfen,kai,ee;
void smgxs(uchar, uchar, uchar);
void delay(uchar z);
void Write1302 ( uchar addr,dat );
uchar Read1302 (uchar addr );
void Initial( );
void set1302( );
void xsxz( );
void naozhong( );
void main( )
{
fun=0;
s2a=0;
ee=0;
Initial();//时钟初始化
while(1)
{
miao=Read1302(READ_SECOND);//读取秒
fen=Read1302(READ_MINUTE);//读取分
shi=Read1302(READ_HOUR);//读取时
xsxz( );
set1302();
if(kai==11)
naozhong( );
}
}
void smgxs(uchar shi, uchar fen, uchar miao)//
{
uchar w1,w2,w3,w4,w5,w6;
w1=shi/10;
w2=shi%10;
w3=fen/10;
w4=fen%10;
w5=miao/10;
w6=miao%10;
duan=1;//打开段选
P0=table[w1];//段数据
duan=0;//关闭段选
P0=0xff;//消隐
wei=1;//打开位选
P0=0xfe;//位数据
wei=0;//关闭位选
delay(5);//延时
duan=1;
P0=table[w2];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(5);
duan=1;
P0=table[w3];
duan=0;
P0=0xff;
wei=1;
P0=0xfb;
wei=0;
delay(5);
duan=1;
P0=table[w4];
duan=0;
P0=0xff;
wei=1;
P0=0xf7;
wei=0;
delay(5);
duan=1;
P0=table[w5];
duan=0;
P0=0xff;
wei=1;
P0=0xef;
wei=0;
delay(5);
duan=1;
P0=table[w6];
duan=0;
P0=0xff;
wei=1;
P0=0xdf;
wei=0;
delay(5);
}
void delay(uchar z)//延时子程序ms
{
uchar x,y;
for (x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Write1302 ( uchar addr,dat ) //地址、数据发送子程序
{
uchar i,temp;
CE=0; //CE引脚为低,数据传送中止
SCLK=0; //清零时钟总线
CE=1; //CE引脚为高,逻辑控制有效//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);//每次传输低字节
addr>>= 1;//右移一位
SCLK=1;//发送数据
}
for ( i=8; i>0; i-- )
{
SCLK=0;
temp=dat;
DIO=(bit)(temp&0x01);
dat>>=1;
SCLK=1;
}
CE=0;
}
uchar Read1302 (uchar addr )//数据读取子程序
{
uchar i,temp,dat1,dat2;
CE=1;
for ( i=8; i>0; i-- ) //发送地址 //循环8次移位
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);//每次传输低字节
addr>>=1;//右移一位
SCLK=1; //读取数据
}
for ( i=8;i>0;i--)
{
ACC_7=DIO;
SCLK=1;//注意时序
ACC>>=1;
SCLK=0;
}
CE=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}
void Initial( )//初始化DS1302
{
Write1302(WRITE_PROTECT,0X00); //禁止写保护
Write1302(WRITE_SECOND,0x30); //秒位初始化
Write1302(WRITE_MINUTE,0x59); //分钟初始化
Write1302(WRITE_HOUR,0x23); //小时初始化
Write1302(WRITE_PROTECT,0x80); //允许写保护
}
void set1302()
{
uchar aa,bb,cc,dd;
if(s1==0)//功能键
{
delay(20);
if(s1==0)
while(!s1);
fun++;
if(fun==3)
{
fun=0;
}
}
if(s2==0)//时分调整切换键
{
delay(20);
if(s2==0)
{
while(!s2);
s2a++;
if (s2a==3)
{
s2a=0;
}
}
}
if((fun==1)&&(s2a==0))//调整分钟
{
if(s3==0)
{
delay(20);
if(s3==0)
{
while(!s3);
Write1302(WRITE_PROTECT,0X00);
fen++;
if(fen==60)
{
fen=0;
}
}
}
if(s4==0)
{
delay(20);
if(s4==0)
{
while(!s4);
Write1302(WRITE_PROTECT,0X00);
if(fen==0)
fen=60;
fen--;
}
}
aa=fen/10;
bb=fen%10;
aa<<=4;
fen=aa+bb;
Write1302(WRITE_MINUTE,fen);
Write1302(WRITE_PROTECT,0x80);//允许写保护
}
if((fun==1)&&(s2a==1))//调整小时
{
if(s3==0)
{
delay(20);
if(s3==0)
{
while(!s3);
Write1302(WRITE_PROTECT,0X00);
shi++;
if(shi==24)
{
shi=0;
}
}
}
if(s4==0)
{
delay(20);
if(s4==0)
{
while(!s4);
Write1302(WRITE_PROTECT,0X00);
if(shi==0)
shi=24;
shi--;
}
}
cc=shi/10;
dd=shi%10;
cc<<=4;
shi=cc+dd;
Write1302(WRITE_HOUR,shi);
Write1302(WRITE_PROTECT,0x80);//允许写保护
}
if((fun==2)&&(s2a==0))//闹钟1的分钟设定
{
if(s3==0)
{
delay(20);
if(s3==0)
{
while(!s3);
lfen++;
if(lfen==60)
lfen=0;
}
}
if(s4==0)
{
delay(20);
if(s4==0)
{
while(!s4);
if(lfen==0)
lfen=60;
lfen--;
}
}
}
if((fun==2)&&(s2a==1))//闹钟1的小时设定
{
if(s3==0)
{
delay(20);
if(s3==0)
{
while(!s3);
lshi++;
if(lshi==24)
lshi=0;
}
}
if(s4==0)
{
delay(20);
if(s4==0)
{
while(!s4);
if(lshi==0)
lshi=24;
lshi--;
}
}
}
if((fun==2)&&(s2a==2))
{
kai=11;
}
}
void xsxz()//显示选择
{
if (fun==0)
smgxs(shi,fen,miao);
if (fun==1)
smgxs(shi,fen,miao);
if (fun==2)
smgxs(lshi,lfen,kai);
}
void naozhong ( )
{
if((shi==lshi)&&(fen==lfen))
{
if(ee==0)
{
fm=0;
}
if(ee==1)
{
fm=1;
}
}
if(s2==0)
{
delay(20);
if(s2==0)
{
ee=!ee;
}
}
}
加了还是不行啊。我的要求是s2=0的时候fm=1.
一周热门 更多>