最近一直在写单总线 和DS1990 iButton卡通讯的问题.在网上参考了很多例子都没有解决,烦死我了,在这里想请教一下各位,我的程序到底哪里出错了(本人初学,暂时先读出序列号前8位,看看到底怎么和IB卡通讯的.)拜托各位帮下忙.感激不尽啊.(使用ATMEGA48,内部8M晶振)#include<avr/io.h>
#include<util/delay.h>
#include<yanshi.h>
#include<avr/interrupt.h>
#define uchar unsigned char
#define uint unsigned int
#define set1(a,b) a|=(1<<b)
#define set0(a,b) a&=~(1<<b)
void w1_init(void) //复位
{
DDRD=set0(DDRD,PD2);
while(PIND&0X04);
DDRD=set1(DDRD,PD2);
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);//
_delay_us(550);//延时至少480us
DDRD=set0(DDRD,PD2);//释放数据线
_delay_us(60);//延时大于60us
while(PIND&0X04);
_delay_us(500);//延时保证时序完整性
DDRD=set1(DDRD,PD2);
PORTD=set1(PORTD,PD2);
PORTD=set0(PORTD,PD2);
}
void write1(void)
{
DDRD=set1(DDRD,PD2);//拉成低电平
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);
_delay_us(5);//保持至少1us
DDRD=set0(DDRD,PD2);//释放数据线
_delay_us(65);//等待足够时间使da1990采样数据线
DDRD=set1(DDRD,PD2);//拉成低电平
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);
}
void write0(void)
{
DDRD=set1(DDRD,PD2);//拉成低电平
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);
_delay_us(65);//等待足够时间使ds1990采样数据线
DDRD=set1(DDRD,PD2);//拉成低电平
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);
}
void write_ds1990(uchar command)
{
uchar n=0;
for(n=0;n<8;n++)
{
if(command&0x01)
write1();
else
write0();
command>>=1;
_delay_us(2);
}
}
uchar ds1990_bitread(void)
{
DDRD=set1(DDRD,PD2);
PORTD=set1(PORTD,PD2);
_delay_us(1);
PORTD=set0(PORTD,PD2);
_delay_us(2);//保持至少1us
DDRD=set0(DDRD,PD2);//释放数据线
_delay_us(8);//延时使主机再15us的后阶段读取数据线
if(PIND&0x04)
{
return 1;
}
else
{
return 0;
}
DDRD=(DDRD,PD2);
PORTD=set1(PORTD,PD2);
PORTD=set0(PORTD,PD2);
}
uchar ds1990_byteread(void)
{
uchar n,Readback=0;
for(n=0;n<8;n++)
{
Readback>>=1;
if(ds1990_bitread())
Readback|=0x80;
_delay_us(61);
}
return Readback;
}
uchar main(void)
{
while(1)
{
sei();
uchar a;
uchar b;
w1_init();
write_ds1990(0x33);
DDRD=set1(DDRD,PD0);
b=ds1990_byteread();
for(a=0;a<8;a++)
{
if((b&0x01))
{
PORTD=set1(PORTD,PD0);
delay_nms(1000);
PORTD=set0(PORTD,PD0);
delay_nms(1000);
}
else
{
PORTD=set1(PORTD,PD0);
delay_nms(100);
PORTD=set0(PORTD,PD0);
delay_nms(100);
PORTD=set1(PORTD,PD0);
delay_nms(100);
PORTD=set0(PORTD,PD0);
delay_nms(1000);
}
b>>=1;
}
}
} |