本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
在数码管中显示分和秒,可是读不出来数据,就是一直就是4个0,急啊 用的是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--);
} |