/***************************************************************************
作者:yupc
Bog 地址: http://blog.ednchina.com/yupc/
硬件描述:以下程序在AVR单片机上能可靠运行,运行频率为8M.
程序描述:以下程序只对DS18B20单个读写,多个读写的函数我没有写,这个不是难点,难点在于读写时续,本程序已成功运行了好几个月,
时间:2008.9.25
****************************************************************************/
#include <avr/io.h>
#include "DS18B20.h"
#include <avr/interrupt.h>
#define SetDo1() DDRD&=~_BV(PD6)
#define ClrDo1() DDRD|=_BV(PD6)
#define TestDo1() PIND&_BV(PD6)
#define SetDo2() DDRB&=~_BV(PB0)
#define ClrDo2() DDRB|=_BV(PB0)
#define TestDo2() PINB&_BV(PB0)
#define SKIPROM 0xCC
#define CONVERT 0x44
#define READSCRA 0xBE
/********************************************************************
作者:俞萍初
函数名: void tickDelay(unsigned int data)
描述:当AVR运行在8M的频率下时,当参数data为0,延时2us,分辨率为0.875us
计算公式为: 延时=2+data*0.875;
当AVR运行在4M时, 延时=(2+data*0.875)*2;
********************************************************************/
#define TD_A ((7 -2)/0.875) //延时7us
#define TD_B ((64)/0.875) //延时64us
#define TD_C ((60)/0.875) //延时60us
#define TD_D ((10-2)/0.875) ////延时10us
#define TD_E ((9-2)/0.875) //延时9us
#define TD_F ((55)/0.875) //延时55us
#define TD_G 0 ////延时0us
#define TD_H ((480)/0.875) //延时480us
#define TD_I ((70)/0.875) //延时70us
#define TD_J ((410)/0.875) //延时410us
void tickDelay(unsigned int data)
{
while(data--);
}
unsigned char OWTouchReset1(void)
{
unsigned char result;
tickDelay(TD_G);
ClrDo1();
tickDelay(TD_H);
SetDo1();
tickDelay(TD_I);
result=TestDo1();
tickDelay(TD_J);
return result;
}
void OWWriteBit1(unsigned char bit)
{
if(bit)
{
ClrDo1();
tickDelay(TD_A);
SetDo1();
tickDelay(TD_B);
}
else
{
ClrDo1();
tickDelay(TD_C);
SetDo1();
tickDelay(TD_D);
}
}
unsigned char OWReadBit1(void)
{
unsigned char result;
ClrDo1();
tickDelay(TD_A);
SetDo1();
tickDelay(TD_E);
result = TestDo1();
tickDelay(TD_F);
return result;
}
void OWWriteByte1(unsigned char data)
{
unsigned char loop;
for(loop=0;loop<8;loop++)
{
OWWriteBit1(data & 0x01);
data >>=1;
}
}
unsigned char OWReadByte1(void)
{
unsigned char loop,result;
result=0;
for(loop=0;loop<8;loop++)
{
result>>=1;
if(OWReadBit1())
result |= 0x80;
}
return result;
}
unsigned char OWTouchByte1(unsigned char data)
{
unsigned char loop,result;
result=0;
for(loop=0;loop<8;loop++)
{
result >>=1;
if(data & 0x01)
{
if(OWReadBit1())
result |= 0x80;
}
else
{
OWWriteBit1(0);
}
data >>= 1;
}
return result;
}
///////////////////////////////////////////////////////////////
unsigned char OWTouchReset2(void)
{
unsigned char result;
tickDelay(TD_G);
ClrDo2();
tickDelay(TD_H);
SetDo2();
tickDelay(TD_I);
result=TestDo2();
tickDelay(TD_J);
return result;
}
void OWWriteBit2(unsigned char bit)
{
if(bit)
{
ClrDo2();
tickDelay(TD_A);
SetDo2();
tickDelay(TD_B);
}
else
{
ClrDo2();
tickDelay(TD_C);
SetDo2();
tickDelay(TD_D);
}
}
unsigned char OWReadBit2(void)
{
unsigned char result;
ClrDo2();
tickDelay(TD_A);
SetDo2();
tickDelay(TD_E);
result = TestDo2();
tickDelay(TD_F);
return result;
}
void OWWriteByte2(unsigned char data)
{
unsigned char loop;
for(loop=0;loop<8;loop++)
{
OWWriteBit2(data & 0x01);
data >>=1;
}
}
unsigned char OWReadByte2(void)
{
unsigned char loop,result;
result=0;
for(loop=0;loop<8;loop++)
{
result>>=1;
if(OWReadBit2())
result |= 0x80;
}
return result;
}
unsigned char OWTouchByte2(unsigned char data)
{
unsigned char loop,result;
result=0;
for(loop=0;loop<8;loop++)
{
result >>=1;
if(data & 0x01)
{
if(OWReadBit2())
result |= 0x80;
}
else
{
OWWriteBit2(0);
}
data >>= 1;
}
return result;
}
////////////////////////////////////////////////////////////////
void OWBlock1(unsigned char *data,unsigned char data_len)
{
unsigned char loop;
for(loop=0;loop<data_len;loop++)
{
data[loop] = OWTouchByte1(data[loop]);
}
}
void OWBlock2(unsigned char *data,unsigned char data_len)
{
unsigned char loop;
for(loop=0;loop<data_len;loop++)
{
data[loop] = OWTouchByte2(data[loop]);
}
}
unsigned char Ds18b20_Start(unsigned char chflg)
{
cli();
if(chflg==1)
{
if(OWTouchReset1())
return 0;
OWWriteByte1(SKIPROM);
OWWriteByte1(CONVERT);
}
else
{
if(OWTouchReset2())
return 0;
OWWriteByte2(SKIPROM);
OWWriteByte2(CONVERT);
}
sei();
return 1;
}
unsigned char Ds18b20_Read(unsigned char chflg,int *pdata)
{
unsigned char *pchar;
pchar = (unsigned char *)pdata;
cli();
if(chflg==1)
{
if(OWTouchReset1())
return 0;
OWWriteByte1(SKIPROM);
OWWriteByte1(READSCRA);
*pchar=OWReadByte1();
*(pchar+1)=OWReadByte1();
}
else
{
if(OWTouchReset2())
return 0;
OWWriteByte2(SKIPROM);
OWWriteByte2(READSCRA);
*pchar=OWReadByte2();
*(pchar+1)=OWReadByte2();
}
sei();
return 1;
}
文章评论(0条评论)
登录后参与讨论