帮忙看看我的DS1302程序 无法读写
等.........................................#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define RST PA0
#define IO PA1
#define SCL PA2
/***************************************************************/
//写1302一字节
/***************************************************************/
void ds1302x(unsigned char x)
{
unsigned char y;
for(y=0;y<8;y++)
{
if(x&0x01)
{
PORTA|=_BV(IO);
}
else
{
PORTA&=~_BV(IO);
}
_delay_us(10);
PORTA&=~_BV(SCL);
_delay_us(10);
PORTA|=_BV(SCL);
_delay_us(10);
x=x>>1;
}
}
/***************************************************************/
//读1302一字节
/***************************************************************/
unsigned char ds1302d(void)
{
unsigned char n,d;
DDRA&=~_BV(IO);
PORTA&=~_BV(IO);
for(n=0;n<8;n++)
{
d=d>>1;
PORTA|=_BV(SCL);
_delay_us(10);
PORTA&=~_BV(SCL);
_delay_us(10);
if(PINA&0X02)
{
d=d|0x80;
}
_delay_us(10);
}
DDRA|=_BV(IO);
return d;
}
/***************************************************************/
//写1302
/***************************************************************/
void ds1302w(unsigned char a,unsigned char b)
{
PORTA&=~_BV(RST);
_delay_us(10);
PORTA|=_BV(RST);
_delay_us(10);
ds1302x(a);
_delay_us(10);
ds1302x(b);
PORTA&=~_BV(RST);
}
/***************************************************************/
//读1302
/***************************************************************/
unsigned char ds1302r(unsigned char c)
{
unsigned char d;
PORTA&=~_BV(RST);
_delay_us(10);
PORTA|=_BV(RST);
_delay_us(10);
ds1302x(c);
_delay_us(10);
d=ds1302d();
PORTA&=~_BV(RST);
return d;
}
void main(void)
{
unsigned char p;
DDRA|=0XFF;
DDRB|=0XFF;
PORTA|=_BV(7);
ds1302w(0x8e,0x00);
ds1302w(0x80,0x00);
//ds1302w(0x82,0x00);
//ds1302w(0x84,0x00);
//ds1302w(0x86,0x00);
//ds1302w(0x88,0x00);
//ds1302w(0x8a,0x00);
//ds1302w(0x8c,0x00);
while(1)
{
PORTB=ds1302r(0x8e);
for(p=0;p<50;p++)
_delay_ms(10);
}
} ..................................
页:
[1]