?? ds18b20.c
字號(hào):
#include <msp430x14x.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define DQ1 P3OUT |= BIT0
#define DQ0 P3OUT &= ~BIT0
/////////////////////////////////////////////
void DelayNus(unsigned int n);
unsigned char Init_18B20(void);
void Write_18B20(unsigned char wdata);
unsigned char Read_18B20(void);
void Skip(void);
void Convert(void);
void Read_SP(void);
unsigned int ReadTemp(void);
unsigned int Do1Convert(void);
/*******************************************
函數(shù)名稱:DelayNus
功 能:實(shí)現(xiàn)N個(gè)微秒的延時(shí)
參 數(shù):n--延時(shí)長(zhǎng)度
返回值 :無(wú)
說(shuō)明 :定時(shí)器A的計(jì)數(shù)時(shí)鐘是1MHz,CPU主頻8MHz
所以通過(guò)定時(shí)器延時(shí)能夠得到極為精確的
us級(jí)延時(shí)
********************************************/
void DelayNus(uint n)
{
CCR0 = n;
TACTL |= MC_1; //增計(jì)數(shù)到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止計(jì)數(shù)
TACTL &= ~BIT0; //清除中斷標(biāo)志
}
/*******************************************
函數(shù)名稱:Init_18B20
功 能:對(duì)DS18B20進(jìn)行復(fù)位操作
參 數(shù):無(wú)
返回值 :初始化狀態(tài)標(biāo)志:1--失敗,0--成功
********************************************/
uchar Init_18B20(void)
{
uchar Error;
_DINT();
DQ0;
DelayNus(500);
DQ1;
DelayNus(55);
P3DIR &=~ BIT0;
_NOP();
if(P3IN & BIT0)
{
Error = 1; //初始化失敗
P3DIR |= BIT0;
}
else
{
Error = 0; //初始化成功
P3DIR |= BIT0;
DQ1;
}
_EINT();
DelayNus(400);
return Error;
}
/*******************************************
函數(shù)名稱:Write_18B20
功 能:向DS18B20寫(xiě)入一個(gè)字節(jié)的數(shù)據(jù)
參 數(shù):wdata--寫(xiě)入的數(shù)據(jù)
返回值 :無(wú)
********************************************/
void Write_18B20(uchar wdata)
{
uchar i;
_DINT();
for(i = 0; i < 8;i++)
{
DQ0;
DelayNus(6); //延時(shí)6us
if(wdata & 0X01) DQ1;
else DQ0;
wdata >>= 1;
DelayNus(50); //延時(shí)50us
DQ1;
DelayNus(10); //延時(shí)10us
}
_EINT();
}
/*******************************************
函數(shù)名稱:Read_18B20
功 能:從DS18B20讀取一個(gè)字節(jié)的數(shù)據(jù)
參 數(shù):無(wú)
返回值 :讀出的一個(gè)字節(jié)數(shù)據(jù)
********************************************/
uchar Read_18B20(void)
{
uchar i;
uchar temp = 0;
_DINT();
for(i = 0;i < 8;i++)
{
temp >>= 1;
DQ0;
DelayNus(6); //延時(shí)6us
DQ1;
DelayNus(8); //延時(shí)9us
P3DIR &= ~BIT0;
_NOP();
if(P3IN & BIT0) temp |= 0x80;
DelayNus(45); //延時(shí)45us
P3DIR |= BIT0;
DQ1;
DelayNus(10); //延時(shí)10us
}
_EINT();
return temp;
}
/*******************************************
函數(shù)名稱:Skip
功 能:發(fā)送跳過(guò)讀取產(chǎn)品ID號(hào)命令
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Skip(void)
{
Write_18B20(0xcc);
}
/*******************************************
函數(shù)名稱:Convert
功 能:發(fā)送溫度轉(zhuǎn)換命令
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Convert(void)
{
Write_18B20(0x44);
}
/*******************************************
函數(shù)名稱:Read_SP
功 能:發(fā)送讀ScratchPad命令
參 數(shù):無(wú)
返回值 :無(wú)
********************************************/
void Read_SP(void)
{
Write_18B20(0xbe);
}
/*******************************************
函數(shù)名稱:ReadTemp
功 能:從DS18B20的ScratchPad讀取溫度轉(zhuǎn)換結(jié)果
參 數(shù):無(wú)
返回值 :讀取的溫度數(shù)值
********************************************/
uint ReadTemp(void)
{
uchar temp_low;
uint temp;
temp_low = Read_18B20(); //讀低位
temp = Read_18B20(); //讀高位
temp = (temp<<8) | temp_low;
return temp;
}
/*******************************************
函數(shù)名稱:ReadTemp
功 能:控制DS18B20完成一次溫度轉(zhuǎn)換
參 數(shù):無(wú)
返回值 :測(cè)量的溫度數(shù)值
********************************************/
uint Do1Convert(void)
{
uchar i;
do
{
i = Init_18B20();
}
while(i);
Skip();
Convert();
for(i = 20;i > 0;i--)
DelayNus(60000); //延時(shí)800ms以上
do
{
i = Init_18B20();
}
while(i);
Skip();
Read_SP();
return ReadTemp();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -