M16+DS18B2驅(qū)動(dòng)程序
//頭文件名稱:DS18B2驅(qū)動(dòng)程序
//晶振: 8.000000
//參考資料 :OURAVR.NET, 偉納中文手冊(cè)
#ifndef _DS18B20_H_
#define _DS18B20_H_
#include
#include "delay.h"
#define WIRE_DDR_OUT DDRC |= ( 1 << 3 ) //使用PD.2口
#define WIRE_DDR_IN DDRC &=~ ( 1 << 3 ) //
#define WIRE_PORT_SET PORTC |= ( 1 << 3 ) //
#define WIRE_PORT_CLR PORTC &=~ ( 1 << 3 ) //
#define check_ack PINC & ( 1 << 3 ) //pind & 0b0000 0010
#define TRUE 1
#define FALSE 0
#define uchar unsigned char
#define uint unsigned int
//函數(shù)聲明
uchar init_1820( void );
void write_1820( uchar command );
uchar read_1820( void );
float get_temp( void );
#endif
#define _DS18B20_C_
#include "DS18B20.H"
//定義變量
float temperature = 0.0; //定義溫度
/*
DS18B20的初始化
(1) 先將數(shù)據(jù)線置高電平"1"。
(2) 延時(shí)(該時(shí)間要求的不是很嚴(yán)格,但是盡可能的短一點(diǎn))
(3) 數(shù)據(jù)線拉到低電平"0"。
(4) 延時(shí)750微秒(該時(shí)間的時(shí)間范圍可以從480到960微秒)。
(5) 數(shù)據(jù)線拉到高電平"1"。上拉, 等待器件回應(yīng)
(6) 延時(shí)等待(如果初始化成功則在15到60毫秒時(shí)間之內(nèi)產(chǎn)生一個(gè)由DS18B20所返回的低電平"0"。據(jù)該狀態(tài)可以來(lái)確定它的存在,但是應(yīng)注意不能無(wú)限的進(jìn)行等待,不然會(huì)使程序進(jìn)入死循環(huán),所以要進(jìn)行超時(shí)控制)。
(7) 若CPU讀到了數(shù)據(jù)線上的低電平"0"后,還要做延時(shí),其延時(shí)的時(shí)間從發(fā)出的高電平算起(第(5)步的時(shí)間算起)最少要480微秒。
(8) 將數(shù)據(jù)線再次拉高到高電平"1"后結(jié)束。
*/
uchar init_1820( void )
{
uchar ACK;
WIRE_DDR_OUT;
WIRE_PORT_SET; //
delay_1us(); //稍做延時(shí)
WIRE_PORT_CLR; //發(fā)送復(fù)位脈沖(最短為480us的低電平信號(hào))
delay_nus(480); //480us以上
WIRE_DDR_IN;
WIRE_PORT_SET; //釋放總線并進(jìn)入接收狀態(tài), 上拉
delay_nus(50);//變?yōu)榈碗娖胶?,延時(shí)15到60us, 用以確定電平, DS18B20對(duì)1總線進(jìn)行輸入采樣, 如果返回為0, 剛存在器件
if(check_ack)
return TRUE;
else
return FALSE;
}
//寫時(shí)間隙
//主機(jī)總線t o時(shí)刻從高拉至低電平時(shí)就產(chǎn)生寫時(shí)間隙,
//從to時(shí)刻開始15us之內(nèi)應(yīng)將所需寫的位送到總線上DSl820在t后15-60us間對(duì)總線采樣,
//若低電平寫入的位是0,若高電平寫入的位是1,連續(xù)寫2位間的間隙應(yīng)大于1us
/*
DS18B20的寫操作
(1) 數(shù)據(jù)線先置低電平"0"。
(2) 按從低位到高位的順序發(fā)送字節(jié)(一次只發(fā)送一位)。
(3) 延時(shí)確定的時(shí)間為15微秒。
(5) 將數(shù)據(jù)線拉到高電平。
(6) 重復(fù)上(1)到(6)的操作直到所有的字節(jié)全部發(fā)送完為止。
(7) 最后將數(shù)據(jù)線拉高。
*/
void write_1820( uchar command )
{
uchar i;
for( i = 8; i > 0; i--)
{
WIRE_DDR_OUT;
WIRE_PORT_CLR; //置為低電平 , 寫時(shí)間隙開始
if( ( command & 0x01 ) == 0x01 ) //寫數(shù)據(jù),從低位開始
WIRE_PORT_SET; //寫入1
else
WIRE_PORT_CLR; //寫入0
delay_nus(15); //15~60us
WIRE_PORT_SET;
command >>= 1; //向右移1位,即高位向底位移動(dòng)
}
WIRE_PORT_SET;
}
/*
DS18B20的讀操作
(3)將數(shù)據(jù)線拉低"0"。
(5)將數(shù)據(jù)線拉高"1"? 上拉, 等待讀入
(7)讀數(shù)據(jù)線的狀態(tài)得到1個(gè)狀態(tài)位,并進(jìn)行數(shù)據(jù)處理。
(8)延時(shí)480以上微秒
*/
//讀時(shí)間隙
uchar read_1820( void )
{
uchar temp = 0, i;
for( i = 0; i < 8; i++ )
{
WIRE_DDR_OUT;
WIRE_PORT_CLR; //拉低 , 讀時(shí)間隙開始
WIRE_DDR_IN; //上拉, 等待讀入
WIRE_PORT_SET; //
delay_1us();
if( check_ack )
{
temp |= 1 << i;
}
delay_nus(480); //480us以上
}
return temp;
}
float get_temp( void )
{
uchar temh;
uchar teml;
init_1820(); //復(fù)位18b20
write_1820(0xCC); // 發(fā)出轉(zhuǎn)換命令
write_1820(0x44);
delay_nus(10);
init_1820();
write_1820(0xCC); //發(fā)出讀命令
write_1820(0xBE);
teml=read_1820(); //讀數(shù)據(jù)
temh=read_1820();
temperature = ( ( temh << 8 ) + teml ) * 0.0625; //計(jì)算具體溫度
return temperature;
}