求大神解答 下面那程序是关于DS1302的 1602的我就没写出来来了
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 8000000UL
#define uint unsigned int
#define uchar unsigned char
#define io 0
#define sclk 1
#define ce 2
#define second 0x80
#define minutes 0x82
#define hour 0x84
#define date 0x86
#define month 0x88
#define week 0x8a
#define year 0x8c
#define rsecond 0x81
#define rminutes 0x83
#define rhour 0x85
#define rdate 0x87
#define rmonth 0x89
#define rweek 0x8b
#define ryear 0x8d
uchar d[12];
uchar c[10]="0123456789";
uint j=0;
uchar read1302_bit() //ds130单字节读
{
uchar i, k;
DDRA=0xfe;
PORTA&=(~(1<<io));
PORTA&=(~(1<<sclk));
for (k=0;k<8;k++)
{
i>>=1;
if(PINA&0x01)
{
i|=0x80;
}
else
{
i&=~(0x80);
}
PORTA|=(1<<sclk);
_delay_us(10);
PORTA&=(~(1<<sclk));
_delay_us(10);
}
return i;
}
void write1302_bit(uchar i) //1302单字节写
{
uchar k;
for (k=0;k<8;k++)
{
if(i&0x01)
PORTA|=(1<<io);
else
PORTA&=(~(1<<io));
PORTA|=(1<<sclk);
i>>=1;
PORTA&=(~(1<<sclk));
}
}
uchar read1302_data(uchar com) //读1302数据
{
uchar i=0;
DDRA=0xff;
PORTA&=(~(1<<ce));
PORTA&=(~(1<<sclk));
PORTA|=(1<<ce);
write1302_bit(com);
DDRA=0xfe;
PORTA|=0x01;
_delay_us(10);
i=read1302_bit();
PORTA|=(1<<sclk);
PORTA&=(~(1<<ce));
return i;
}
void write1302_data(uchar com,uchar data) //写1302指令和数据
{
DDRA=0xff;
PORTA&=(~(1<<ce));
PORTA&=(~(1<<sclk));
PORTA|=(1<<ce);
write1302_bit(com);
write1302_bit(data);
PORTA&=(~(1<<ce));
}
void ds1302_init()
{
write1302_data(0x8e,0x00);
write1302_data(second,0x00);
write1302_data(minutes,0x00);
write1302_data(hour,0x80);
write1302_data(date,0x01);
write1302_data(month,0x01);
write1302_data(year,0x14);
write1302_data(week,0x01);
write1302_data(0x8e,0x80);
}
void read() //读数据
{
uchar i;
i=read1302_data(rsecond); //1
d[0]=i&0x0f;
d[1]=((i&0x70)>>4);
i=read1302_data(rminutes); //2
d[2]=i&0x0f;
d[3]=((i&0x70)>>4);
i=read1302_data(rhour); //3
d[4]=i&0x0f;
d[5]=((i&0x10)>>4);
i=read1302_data(rdate); //4
d[6]=i&0x0f;
d[7]=((i&0x30)>>4);
i=read1302_data(rmonth); //5
d[8]=i&0x0f;
d[9]=((i&0x10)>>4);
i=read1302_data(rweek); //6
d[10]=i&0x07;
d[11]=0;
}
void display() //显示函数
{
write_data(' ');
write_data(' ');
write_data(' ');
write_data(' ');
write_data(c[d[1]]);
write_data(c[d[0]]);
write_data('-');
write_data(c[d[3]]);
write_data(c[d[2]]);
write_data('-');
write_data(c[d[5]]);
write_data(c[d[4]]);
write_com(0x80+0x40);
write_data(' ');
write_data(' ');
write_data(' ');
write_data(' ');
write_data(c[d[7]]);
write_data(c[d[6]]);
write_data('-');
write_data(c[d[9]]);
write_data(c[d[8]]);
write_data('-');
write_data(c[d[11]]);
write_data(c[d[10]]);
}
int main(void)
{
uchar i;
DDRB=0x00;
init();
ds1302_init();
while(1)
{
read();
display();
//TODO:: Please write your application code
}
}
[img]file:///C:\Users\chen.PC-PC\AppData\Roaming\Tencent\Users\852603159\QQ\WinTemp\RichOle\_]N(_4CX0{S7W}O%4@GYBQG.jpg[/img]
为什么我怎么就是一直显示那个数就不动了
|