本帖最后由 看见未来 于 2012-3-22 15:34 编辑
#include <pic.h>
#define uchar unsigned char
#define uint unsigned int
#define clk RB0
#define io RB1
#define CE RB2
//unsigned char time_rx @ 0x30; //定义接收寄存器
//static volatile bit time_rx7 @ (unsigned)&time_rx*8+7; //接收寄存器的最高位
uchar duan_sm[]={0X3f,0X06,0X5b,0X4f,0X66,0X6d,0X7d,0X07,0X7f,0X6f};
uchar wei_sm[]={0X00,0X01,0X02,0X03,0X04,0X05};
uchar time_dat[]={12,3,3,20,12,30,32};
uchar write_add[]={0X8C,0X8A,0X88,0X86,0X84,0X82,0X80};
uchar read_add[]={0X8D,0X8B,0X89,0X87,0X85,0X83,0X81};
uchar display[6];
void delay_ms(uchar ms)
{
uint i,j;
for (i=0;i<ms;i++)
{
for(j=0;j<10;j++)
{
}
}
}
void write_byte(uchar dat)
{
uchar i;
TRISB1=0;
clk=0;
for(i=0;i<8;i++)
{
clk=0;
io=dat&0X01;
dat=dat>>1;
clk=1;
}
}
void ds1302_write(uchar add,uchar dat)
{
CE=0;
CE=1;
write_byte(add);
write_byte(dat);
CE=0;
clk=1;
io=1;
}
void ds1302_get_time()
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_dat/10;
time_dat=time_dat%10;
time_dat=time_dat+j*16;
}
ds1302_write(0X8E,0X00);
for(i=0;i<7;i++)
{
ds1302_write(write_add,time_dat);
}
ds1302_write(0X8E,0X80);
}
uchar read_time(uchar add)//接收数据
{
uchar i,value;
CE=0;
clk=0;
CE=1;
write_byte(add);
TRISB1=1;
for(i=0;i<8;i++)
{
value=value>>1;
clk=0;
if(io)
value=value|0X80;
clk=1;
}
CE=0;
clk=0;
clk=1;
io=1;
return value;
}
void ds1302_read(void) //接收时间数据
{
uchar i;
for(i=0;i<7;i++)
{
time_dat=read_time(read_add);
}
}
void dui_time(void) //将BCD码数用十进制显示出来
{
display[0]=time_dat[6]%16;
display[1]=time_dat[6]/16;
display[2]=time_dat[5]%16;
display[3]=time_dat[5]/16;
display[4]=time_dat[4]%16;
display[5]=time_dat[4]/16;
}
void dplay() //数码管显示
{
uchar i;
for(i=0;i<6;i++)
{
PORTD=wei_sm;
PORTC=duan_sm[display];
delay_ms(1);
}
}
void main()
{
TRISB0=0;
TRISB2=0;
TRISC=0;
TRISD=0;
PORTC=0;
PORTD=0;
ds1302_get_time();
while(1)
{
ds1302_read();
dui_time();
dplay();
}
}
为什么秒走到34又返回32,如此循环下去呢?程序有什么问题啊? |