本帖最后由 久之1990 于 2011-11-26 13:44 编辑
#include<iom88.h>
#include<avr_macros.h>
#define uchar unsigned char
#define uint unsigned int
#define DDR_WR_IO DDRC |= 0X10
#define DDR_RE_IO DDRC &= 0XEF
#define SCLK_1 PORTC_Bit5=1
#define SCLK_0 PORTC_Bit5=0
#define RST_1 PORTC_Bit3=1
#define RST_0 PORTC_Bit3=0
#define WR_IO_1 PORTC_Bit4 = 1
#define WR_IO_0 PORTC_Bit4 = 0
#define RE_IO (PINC&0X10)
uchar table[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};//0~9
uchar duanma[] = {0x7f,0xbf,0xdf,0xef};
uchar shu[] = {0,0,0,0};
void delay(uchar x)
{
uchar kk;
for(;x>0;x--)
for(kk=200;kk>0;kk--);
}
void display()
{
uchar i;
for(i=0;i<4;i++)
{
PORTD = 0xff;
PORTD = duanma;
PORTB = table[shu];
delay(2);
}
}
uchar read_byte_ds1302()
{
uchar i;
uchar dat=0;
DDR_RE_IO;
for(i=0;i<8;i++)
{
dat = dat>>1;
SCLK_1;
SCLK_0;
if(RE_IO)
dat |= 0x80;
}
return (dat/16*10 + dat%16);
}
void write_byte_ds1302(uchar dat)
{
uchar i;
DDR_WR_IO ;
for(i=0;i<8;i++)
{
SCLK_0;
if(dat&0x01)
WR_IO_1;
else
WR_IO_0;
SCLK_1;
dat = dat>>1;
}
}
uchar read_data(uchar add)
{
uchar dat;
RST_1;
write_byte_ds1302(add);
dat = read_byte_ds1302();
RST_0;
return dat;
}
void write_data(uchar add,uchar dat)
{
RST_1;
write_byte_ds1302(add);
write_byte_ds1302(dat);
RST_0;
}
void init_ds1302()
{
write_data(0x8e,0x00);
write_data(0x80,0x56);
write_data(0x82,0x36);
write_data(0x8e,0x80);
}
void gettime()
{
shu[0] = read_data(0x81)%10;
shu[2] = read_data(0x83)%10;
}
void main()
{
DDRD = 0XFF;
DDRB = 0XFF;
DDRC = 0XFF;
init_ds1302();
while(1)
{
gettime();
display();
}
}
为什么总显示两个5呢? |