单片机高手们 帮我看看程序 DS1302在protues中仿真
用的是PIC16f877a单片机读不出来数据啊,帮我看看啊很急啊!这是我的程序:#include<pic.h>#defineucharunsignedchar#defin...
用的是PIC16f877a单片机 读不出来数据啊,帮我看看啊 很急啊!
这是我的程序:
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
uchar fen,miao,shi_miao,ge_miao,shi_fen,ge_fen,date;
uint t;
#define rst RB0
#define sclk RB1
#define DATE RB2
#define sclk_HIGH() TRISB2=1
#define sclk_LOW() TRISB2=0
__CONFIG(0X3B31);
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};
void delay(uchar z);
void display_fm(uchar date,uchar date1);
uchar get_f1302sj();
uchar get_m1302sj();
void init();
uchar read_1302z(uchar add);
void write_1302z(uchar add,uchar date);
uchar read_1302();
void write_1302(uchar date);
void main()
{
while(1)
{
init();
get_f1302sj();
get_m1302sj();
display_fm(miao,fen);
}
}
void write_1302(uchar date)
{
uint i;
sclk_LOW();
NOP();
NOP();
for(i=8;i>0;i--)
{
DATE=date&0x01;
NOP();
NOP();
sclk_HIGH();
NOP();
NOP();
sclk_LOW();
date=date>>1;
}
}
uchar read_1302()
{
uchar date;
NOP();
NOP();
for(t=8;t>0;t--)
{
date=date>>1;
if(DATE==1)
{
date=date|0x80;
}
sclk_HIGH();
NOP();
NOP();
sclk_LOW();
NOP();
NOP();
}
return(date);
}
void write_1302z(uchar add,uchar date)
{
rst=0;
NOP();
NOP();
rst=1;
write_1302(add);
write_1302(date);
NOP();
NOP();
rst=0;
}
uchar read_1302z(uchar add)
{
rst=0;
NOP();
NOP();
rst=1;
write_1302(add);
read_1302();
rst=1;
return(date);
}
void init()
{
TRISB0=0;
TRISB1=0;
TRISD=0;
TRISC=0;
write_1302z(0x8e,0x00);
write_1302z(0x8e,0xa3);
write_1302z(0x8e,0x80);
}
uchar get_m1302sj()
{
uchar date;
read_1302z(0x81);
miao=((date&0x70)>>4)*10+(date&0x0f);
return(date);
}
uchar get_f1302sj()
{
read_1302z(0x83);
fen=((date&0x70)>>4)*10+(date&0x0f);
return(date);
}
void display_fm(uchar date,uchar date1)
{
shi_miao=miao/10;
ge_miao=miao%10;
shi_fen=fen/10;
ge_fen=fen%10;
PORTD=0XEF;
PORTC=table[shi_fen];
delay(5);
PORTD=0XdF;
PORTC=table[ge_fen];
delay(5);
PORTD=0XbF;
PORTC=table[shi_miao];
delay(5);
PORTD=0X7F;
PORTC=table[ge_miao];
}
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
} 展开
这是我的程序:
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
uchar fen,miao,shi_miao,ge_miao,shi_fen,ge_fen,date;
uint t;
#define rst RB0
#define sclk RB1
#define DATE RB2
#define sclk_HIGH() TRISB2=1
#define sclk_LOW() TRISB2=0
__CONFIG(0X3B31);
const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};
void delay(uchar z);
void display_fm(uchar date,uchar date1);
uchar get_f1302sj();
uchar get_m1302sj();
void init();
uchar read_1302z(uchar add);
void write_1302z(uchar add,uchar date);
uchar read_1302();
void write_1302(uchar date);
void main()
{
while(1)
{
init();
get_f1302sj();
get_m1302sj();
display_fm(miao,fen);
}
}
void write_1302(uchar date)
{
uint i;
sclk_LOW();
NOP();
NOP();
for(i=8;i>0;i--)
{
DATE=date&0x01;
NOP();
NOP();
sclk_HIGH();
NOP();
NOP();
sclk_LOW();
date=date>>1;
}
}
uchar read_1302()
{
uchar date;
NOP();
NOP();
for(t=8;t>0;t--)
{
date=date>>1;
if(DATE==1)
{
date=date|0x80;
}
sclk_HIGH();
NOP();
NOP();
sclk_LOW();
NOP();
NOP();
}
return(date);
}
void write_1302z(uchar add,uchar date)
{
rst=0;
NOP();
NOP();
rst=1;
write_1302(add);
write_1302(date);
NOP();
NOP();
rst=0;
}
uchar read_1302z(uchar add)
{
rst=0;
NOP();
NOP();
rst=1;
write_1302(add);
read_1302();
rst=1;
return(date);
}
void init()
{
TRISB0=0;
TRISB1=0;
TRISD=0;
TRISC=0;
write_1302z(0x8e,0x00);
write_1302z(0x8e,0xa3);
write_1302z(0x8e,0x80);
}
uchar get_m1302sj()
{
uchar date;
read_1302z(0x81);
miao=((date&0x70)>>4)*10+(date&0x0f);
return(date);
}
uchar get_f1302sj()
{
read_1302z(0x83);
fen=((date&0x70)>>4)*10+(date&0x0f);
return(date);
}
void display_fm(uchar date,uchar date1)
{
shi_miao=miao/10;
ge_miao=miao%10;
shi_fen=fen/10;
ge_fen=fen%10;
PORTD=0XEF;
PORTC=table[shi_fen];
delay(5);
PORTD=0XdF;
PORTC=table[ge_fen];
delay(5);
PORTD=0XbF;
PORTC=table[shi_miao];
delay(5);
PORTD=0X7F;
PORTC=table[ge_miao];
}
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询