全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1501
推到 Plurk!
推到 Facebook!

有關DS1821 sensor code problems

缺席
specially527
一般會員


發表:2
回覆:2
積分:0
註冊:2012-10-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-10-02 21:06:26 IP:220.136.xxx.xxx 訂閱
 小弟最近在寫DS1821用於MSP430F449上的程式
對於下列程式碼的認知不是很清楚
懇請擅長此領域的行家幫忙解釋
非常感謝
#include
#include
#define DS1821_FCMD_READ_TEMPERATURE 0xAA
#define DS1821_FCMD_START_CONVERT_T 0xEE
#define DS1821_FCMD_STOP_CONVERT_T 0x22
#define DS1821_FCMD_WRITE_TH 0x01
#define DS1821_FCMD_WRITE_TL 0x02
#define DS1821_FCMD_READ_TH 0xA1
#define DS1821_FCMD_READ_TL 0xA2
#define DS1821_FCMD_WRITE_STATUS 0x0C
#define DS1821_FCMD_READ_STATUS 0xAC
#define DS1821_FCMD_READ_COUNTER 0xA0
#define DS1821_FCMD_LOAD_COUNTER 0x41
#define DS1821_CFG_DONE BIT7
#define DS1821_CFG_NVB BIT5
#define DS1821_CFG_THF BIT4
#define DS1821_CFG_TLF BIT3
#define DS1821_CFG_TR BIT2
#define DS1821_CFG_POL BIT1
#define DS1821_CFG_1SHOT BIT0
#define DS1821 BIT7
bool ds1821_reset(void);
void ds1821_write_bit(uint8_t value);
uint8_t ds1821_read_bit(void);
void ds1821_write(uint8_t value);
uint8_t ds1821_read(void);
bool ds1821_reset(void)
{
bool ds1821_present = false;
P1OUT &= ~DS1821;
P1DIR |= DS1821;
__delay_cycles(500);
P1DIR &= ~DS1821;
__delay_cycles(80);
ds1821_present = ((P1IN & DS1821) == 0);
__delay_cycles(420);
return ds1821_present;
}
void ds1821_write_bit(uint8_t value)
{
P1OUT &= ~DS1821;
P1DIR |= DS1821;
if(value & 0x01)
{
__delay_cycles(8);
P1DIR &= ~DS1821;
__delay_cycles(57);
}
else
{
__delay_cycles(65);
P1DIR &= ~DS1821;
}
__delay_cycles(5);
}
uint8_t ds1821_read_bit(void)
{
uint8_t value;
P1OUT &= ~DS1821;
P1DIR |= DS1821;
__delay_cycles(3);
P1DIR &= ~DS1821;
__delay_cycles(5);
value = ((P1IN & DS1821 == 0) ? 0 : 1);
__delay_cycles(62);
return value;
}
void ds1821_write(uint8_t value)
{
uint8_t mask;
for(mask = 0x01; mask; mask <<= 1)
{
ds1821_write_bit((mask & value) ? 1 : 0);
}
}
uint8_t ds1821_read(void)
{
uint8_t mask;
uint8_t value = 0;
for(mask = 0x01; mask; mask <<= 1)
{
if(ds1821_read_bit() == 1)
{
value |= mask;
}
}
return value;
}
int main(void)
{
volatile bool ds1821_present = false;
volatile uint8_t config_register = 0;
volatile uint8_t temperature = 0;
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;
ds1821_present = ds1821_reset();
if(ds1821_present)
{
ds1821_write(DS1821_FCMD_START_CONVERT_T);
do
{
ds1821_write(DS1821_FCMD_READ_STATUS);
config_register = ds1821_read();
} while((config_register & DS1821_CFG_DONE) == 0);
ds1821_write(DS1821_FCMD_READ_TEMPERATURE);
temperature = ds1821_read();
}
__nop();
__bis_status_register(LPM4_bits);
}
if8051
中階會員


發表:2
回覆:53
積分:61
註冊:2012-08-24

發送簡訊給我
#2 引用回覆 回覆 發表時間:2012-10-02 21:43:34 IP:122.121.xxx.xxx 訂閱
請首先下載 DALLAS DS1821 18 頁 datasheet ,研究 1-wire bus 
initialization timing & read / write time slot timing diagram
Programmable Digital Thermostat and Thermometer
Operates over a -55oC to 125oC temperature range
8 bit 1oC resolution

可建議使用解析度達 12 bit 0.0625oC 準確度 - 0.5oC 的 DS18B20
可接成多點系統,我使用經驗是並接 10 個 DS18B20 DQ 接到一點
MCU 輸入點。

------
ATMEL AVR 單晶片設計開發 教學

工業電子 電機自動控制 設計 維修
aki
版主


發表:30
回覆:696
積分:755
註冊:2004-01-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2012-10-03 12:36:25 IP:203.69.xxx.xxx 訂閱


post 你的 E-mail 信箱,我有 DS1820 的範例給你參考!



===================引 用 specially527 文 章===================
小弟最近在寫DS1821用於MSP430F449上的程式
對於下列程式碼的認知不是很清楚
懇請擅長此領域的行家幫忙解釋
非常感謝
#include
#include
#define DS1821_FCMD_READ_TEMPERATURE 0xAA
#define DS1821_FCMD_START_CONVERT_T 0xEE
#define DS1821_FCMD_STOP_CONVERT_T 0x22
#define DS1821_FCMD_WRITE_TH 0x01
#define DS1821_FCMD_WRITE_TL 0x02
#define DS1821_FCMD_READ_TH 0xA1
#define DS1821_FCMD_READ_TL 0xA2
#define DS1821_FCMD_WRITE_STATUS 0x0C
#define DS1821_FCMD_READ_STATUS 0xAC
#define DS1821_FCMD_READ_COUNTER 0xA0
#define DS1821_FCMD_LOAD_COUNTER 0x41
#define DS1821_CFG_DONE BIT7
#define DS1821_CFG_NVB BIT5
#define DS1821_CFG_THF BIT4
#define DS1821_CFG_TLF BIT3
#define DS1821_CFG_TR BIT2
#define DS1821_CFG_POL BIT1
#define DS1821_CFG_1SHOT BIT0
#define DS1821 BIT7
bool ds1821_reset(void);
void ds1821_write_bit(uint8_t value);
uint8_t ds1821_read_bit(void);
void ds1821_write(uint8_t value);
uint8_t ds1821_read(void);
bool ds1821_reset(void)
{
bool ds1821_present = false;
P1OUT &= ~DS1821;
P1DIR |= DS1821;
__delay_cycles(500);
P1DIR &= ~DS1821;
__delay_cycles(80);
ds1821_present = ((P1IN & DS1821) == 0);
__delay_cycles(420);
return ds1821_present;
}
void ds1821_write_bit(uint8_t value)
{
P1OUT &= ~DS1821;
P1DIR |= DS1821;
if(value & 0x01)
{
__delay_cycles(8);
P1DIR &= ~DS1821;
__delay_cycles(57);
}
else
{
__delay_cycles(65);
P1DIR &= ~DS1821;
}
__delay_cycles(5);
}
uint8_t ds1821_read_bit(void)
{
uint8_t value;
P1OUT &= ~DS1821;
P1DIR |= DS1821;
__delay_cycles(3);
P1DIR &= ~DS1821;
__delay_cycles(5);
value = ((P1IN & DS1821 == 0) ? 0 : 1);
__delay_cycles(62);
return value;
}
void ds1821_write(uint8_t value)
{
uint8_t mask;
for(mask = 0x01; mask; mask <<= 1)
{
ds1821_write_bit((mask & value) ? 1 : 0);
}
}
uint8_t ds1821_read(void)
{
uint8_t mask;
uint8_t value = 0;
for(mask = 0x01; mask; mask <<= 1)
{
if(ds1821_read_bit() == 1)
{
value |= mask;
}
}
return value;
}
int main(void)
{
volatile bool ds1821_present = false;
volatile uint8_t config_register = 0;
volatile uint8_t temperature = 0;
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;
ds1821_present = ds1821_reset();
if(ds1821_present)
{
ds1821_write(DS1821_FCMD_START_CONVERT_T);
do
{
ds1821_write(DS1821_FCMD_READ_STATUS);
config_register = ds1821_read();
} while((config_register & DS1821_CFG_DONE) == 0);
ds1821_write(DS1821_FCMD_READ_TEMPERATURE);
temperature = ds1821_read();
}
__nop();
__bis_status_register(LPM4_bits);
}
specially527
一般會員


發表:2
回覆:2
積分:0
註冊:2012-10-02

發送簡訊給我
#4 引用回覆 回覆 發表時間:2012-10-03 22:40:16 IP:140.135.xxx.xxx 訂閱
 specially527@hotmail.com
謝謝版主
麻煩你了
aki
版主


發表:30
回覆:696
積分:755
註冊:2004-01-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2012-10-04 06:08:50 IP:111.243.xxx.xxx 訂閱

please confirm your e-mail account. and good luck!

===================引 用 specially527 文 章===================
specially527@hotmail.com
謝謝版主
麻煩你了
specially527
一般會員


發表:2
回覆:2
積分:0
註冊:2012-10-02

發送簡訊給我
#6 引用回覆 回覆 發表時間:2012-10-05 17:13:09 IP:220.136.xxx.xxx 訂閱
got it !! thanks~
系統時間:2024-11-23 17:42:38
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!