分享
 
 
 

DS18B20的C程序(IAR,AVR)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

分三个文件,

ds18b20.c

/*

****************************************************************************************************

* SSAC_A1

* Main Controller Board

* File name : DS18B20.c

* Version : 1.0

* Use for : Detect temperature

* Summary : The C code for DS18B20

* Author : Shensong (E_mail:webmaster@shensong.com / QQ:2939603 / Mob:13016461980)

* Date : 2005-05-22

****************************************************************************************************

*/

#include "DS18B20.h"

#include "DS18B20_ports.h"

////////// Bit operation macro

#define Set_Bit(Address,Bit) ((Address) |= (1 << Bit))

#define Clear_Bit(Address,Bit) ((Address) &= ~(1 << Bit))

#define Test_Bit(Address,Bit) ((Address) & (1 << Bit))

//--------------------------------------------------------------------------------------------------

////////// Make a Master reset to DS18B20, and check the result

// Input:

// None

// Return:

// 0: There is no DS18B20 or master reset operation false

// 1: There is and has be 1-Wire reset

static unsigned char DS18B20_Master_Reset (void)

{

unsigned char Value;

Value = 0;

DS18B20_Tx_Obtain (); // Set to low for 720us to reset slave device

Soft_Delay_us (480); // 720us

DS18B20_Tx_Release (); // Release the 1-Wire Bus

Soft_Delay_us (70); // 68us. Check the reset result, slave will set the 1-Wire Bus to low

Value = DS18B20_Rx ();

Soft_Delay_us (410); // 480us. Wait for cycle end

if (Value) return 0;

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Write bit to DS18B20

// Input:

// Value: bit value will be writed

// Return:

// None

static void DS18B20_Write_Bit (unsigned char Value)

{

if (Value == 0) // Master Write "0" SLOT

{

DS18B20_Tx_Obtain ();

Soft_Delay_us (65); // 70us

DS18B20_Tx_Release ();

Soft_Delay_us (10); // No this line

}

else // Master Write "1" SLOT

{

DS18B20_Tx_Obtain ();

Soft_Delay_us (10); // 7us

DS18B20_Tx_Release ();

Soft_Delay_us (65); // 63us

}

//Soft_Delay_us (10); // Wait for 1-wire restore

}

//--------------------------------------------------------------------------------------------------

////////// Read bit from DS18B20

// Input:

// None

// Return:

// The bit value that has be read out

static unsigned char DS18B20_Read_Bit (void)

{

unsigned char Value;

DS18B20_Tx_Obtain (); // Start a read time slot

Soft_Delay_us (5);

DS18B20_Tx_Release (); // Release and sample 1-Wire Bus

Soft_Delay_us (5); // 13us

Value = DS18B20_Rx (); // Wait for read cycle end

Soft_Delay_us (55); // 52us

//Soft_Delay_us (10); // Wait for 1-wire restore

if (! Value) return 0;

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Write byte to DS18B20

// Input:

// The byte will be writed

// Return:

// None

static void DS18B20_Write_Byte (unsigned char Value)

{

unsigned char i;

for (i = 0; i < 8; i++)

{

DS18B20_Write_Bit ( Test_Bit (Value, i) );

}

}

//--------------------------------------------------------------------------------------------------

////////// Read byte from DS18B20

// Input:

//

// Return:

// The byte that has be read out

static unsigned char DS18B20_Read_Byte (void)

{

unsigned char i;

unsigned char Value;

Value = 0;

for (i = 0; i < 8; i++)

{

if ( DS18B20_Read_Bit () ) Set_Bit (Value, i);

}

return Value;

}

//--------------------------------------------------------------------------------------------------

////////// Multi Write byte to DS18B20

// Input:

// * Value: The source data

// Number: number of the data

// Return:

// None

static void DS18B20_Multi_Write_Byte (unsigned char * Value, unsigned char Number)

{

unsigned char i;

if (Number == 0) return;

for (i = 0; i < Number; i++)

{

DS18B20_Write_Byte ( * (Value + i) );

}

}

//--------------------------------------------------------------------------------------------------

////////// Multi Read byte from DS18B20

// Input:

// * Value: The source data

// Number: number of the data

// Return:

// None

static void DS18B20_Multi_Read_Byte (unsigned char * Value, unsigned char Number)

{

unsigned char i;

if (Number == 0) return;

for (i = 0; i < Number; i++)

{

* (Value + i) = DS18B20_Read_Byte ();

}

}

//--------------------------------------------------------------------------------------------------

////////// 8-bit CRC check, X8+X5+X4+1, to check the data get from DS18B20

// Input:

// Crc_Org: Oringal crc value

// * Value: the data need do a crc check

// Length: The length of data

// Return:

// Return = crc result

static unsigned char DS18B20_Crc_Check (unsigned char Crc_Org, unsigned char * Value, unsigned char Length)

{

unsigned char i;

while (Length--)

{

for (i = 0x01; i != 0; i <<= 1)

{

if ( (Crc_Org & 0x01) != 0) // LSB of crcorg

{

Crc_Org >>= 1;

Crc_Org ^= 0x8C;

}

else

{

Crc_Org >>= 1;

}

if ( ((* Value) & i) != 0)

{

Crc_Org ^= 0x8C;

}

}

Value ++;

}

return Crc_Org;

}

//--------------------------------------------------------------------------------------------------

////////// Laser ROM check

// Input:

// * UID: Laser rom data to check

// Return:

// 0: Opt. false

// 1: Opt. OK

static unsigned char DS18B20_UID_Check (unsigned char * UID)

{

if ( * (UID + 0) != 0x28 ) return 0; // DS18B20's family code is 0x28. To avoid always 0 error

if ( DS18B20_Crc_Check (0x00, UID, 7) != * (UID + 7) ) return 0; // CRC check error;

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Scratchpad check

// Input:

// * SCT: scratchpad

// Return:

// 0: Opt. false

// 1: Opt. OK

static unsigned char DS18B20_SCT_Check (unsigned char * SCT)

{

if ( ((* (SCT + 4)) & 0x9F) != 0x1F) return 0; // To avoid always 0 error

if ( (DS18B20_Crc_Check (0x00, SCT, 8)) != (* (SCT + 8)) ) return 0; // Crc check error

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Temperature format transfer

// Input:

//

// * sct: The data need to transfer. 8 bytes

// * result: 3 bytes, is insignificant byte, integer byte, low bytes

// Return:

// None

static void DS18B20_Temperature_Format_Transfer (unsigned char * sct, unsigned char * result)

{

unsigned char res_t; // Resolution

unsigned int low_t; // Dept. Dot. use

res_t = * (sct + 4) & 0x60; // Get resolution from scratchpad data

if ( Test_Bit (* (sct + 1), 7) )

{

* (result + 0) = 0xFF; // Insignificant, 0x00 is +, 0xFF is -

}

else

{

* (result + 0) = 0x00;

}

switch (res_t)

{

case 0x00: // for 9-bit resolution

* (result + 1) = * (sct + 0) >> 1; // Dept. Int.

* (result + 2) = (* (sct + 0) & 0x01) * 50; // Dept. Dot.

break;

case 0x20: // for 10-bit resolution

* (result + 1) = ( (* (sct + 0) >> 2) | (((* (sct + 1)) << 6) & 0x7F) ); // Dept. Int.

* (result + 2) = (* (sct + 0) & 0x03) * 25; // Dept. Dot.

break;

case 0x40: // for 11-bit resolution

* (result + 1) = ( (* (sct + 0) >> 3) | (((* (sct + 1)) << 5) & 0x7F) ); // Dept. Int.

low_t = (unsigned int)(* (sct + 0) & 0x07) * 125; // Dept. Dot.

low_t /= 10;

* (result + 2) = (unsigned char)low_t;

break;

case 0x60: // for 12-bit resolution

* (result + 1) = ( (* (sct + 0) >> 4) | (((* (sct + 1)) << 4) & 0x7F) ); // Dept. Int.

low_t = (unsigned int)(* (sct + 0) & 0x0F) * 625; // Dept. Dot.

low_t /= 100;

* (result + 2) = (unsigned char)low_t;

break;

} // end switch (res_t)

}

//--------------------------------------------------------------------------------------------------

////////// Read the value of temperature from DS18B20. Match passed

// Input:

//

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

unsigned char DS18B20_Single_Read_Temperature (unsigned char * Value)

{

unsigned char cpu_sr;

unsigned char sct[9]; // For temp storage scratchpad data

Enter_Critical ();

// Start convert cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0xCC); // Skip ROM command

DS18B20_Write_Byte (0x44); // Convert temperature command

Exit_Critical ();

Soft_Delay_ms (751); // Wait for convert complete

Enter_Critical ();

// Cycle end

// Start read SCT cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0xCC); // Skip ROM command

DS18B20_Write_Byte (0xBE); // Read SCT

DS18B20_Multi_Read_Byte (sct, 9); // Read scratchpad data, 8 Bytes + 1 Byte CRC

// Cycle end

if (DS18B20_SCT_Check (sct) != 1) // Check scratchpad with CRC and CONFIG register

{

Exit_Critical ();

return 0;

}

DS18B20_Temperature_Format_Transfer (sct, Value);

Exit_Critical ();

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Read the Laser ROM from from DS18B20 // Notice: Only for single device on 1-Wire Bus

// Input:

// * Value: the point for UID data storage. LSB first, MSB last

// Return:

// 0: Opt. false

// 1: Opt. OK

__monitor unsigned char DS18B20_Single_Read_ROM (unsigned char * Value)

{

unsigned char i;

unsigned char rom_temp[8];

if (DS18B20_Master_Reset () != 1) // Master reset and check, error to return 0

{

return 0;

}

DS18B20_Write_Byte (0x33); // 0x33 is Read ROM command

DS18B20_Multi_Read_Byte (rom_temp, 8);

if ( DS18B20_UID_Check (rom_temp) != 1)

{

return 0;

}

for (i = 0; i < 8; i++)

{

* (Value + i) = rom_temp[i]; // CRC OK, Write back result

}

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Only convert temperature without delay. Single

// Input:

// None

// Return:

// 0: Opt. false

// 1: Opt. OK

unsigned char DS18B20_Single_Only_Convert_Temperature (void)

{

unsigned char cpu_sr;

Enter_Critical ();

// Start convert cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0xCC); // Skip ROM command

DS18B20_Write_Byte (0x44); // Convert temperature command

Exit_Critical ();

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// Only read SCT data from DS18B20. Single

// Input:

//

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

unsigned char DS18B20_Single_Only_Read_Temperature (unsigned char * Value)

{

unsigned char cpu_sr;

unsigned char sct[9]; // For temp storage scratchpad data

Enter_Critical ();

// Start read SCT cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0xCC); // Skip ROM command

DS18B20_Write_Byte (0xBE); // Read SCT

DS18B20_Multi_Read_Byte (sct, 9); // Read scratchpad data, 8 Bytes + 1 Byte CRC

// Cycle end

if (DS18B20_SCT_Check (sct) != 1) // Check scratchpad with CRC and CONFIG register

{

Exit_Critical ();

return 0;

}

DS18B20_Temperature_Format_Transfer (sct, Value);

Exit_Critical ();

return 1;

}

//--------------------------------------------------------------------------------------------------

////////// 以指定的ROM去读温度

// Input:

//

// * UID: ROM (8 bytes)

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

unsigned char DS18B20_UID_Read_Temperature (unsigned char * Value, unsigned char * UID)

{

unsigned char cpu_sr;

unsigned char sct[9]; // For temp storage scratchpad data

Enter_Critical ();

if (DS18B20_UID_Check (UID) != 1)

{

Exit_Critical ();

return 0;

}

// Start convert cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0x55); // Match ROM command

DS18B20_Multi_Write_Byte (UID, 8);

DS18B20_Write_Byte (0x44); // Convert temperature command

Exit_Critical ();

Soft_Delay_ms (751); // Wait for convert complete

Enter_Critical ();

// Cycle end

// Start read SCT cycle

if (DS18B20_Master_Reset () != 1) // Master reset

{

Exit_Critical ();

return 0;

}

DS18B20_Write_Byte (0x55); // Skip ROM command

DS18B20_Multi_Write_Byte (UID, 8);

DS18B20_Write_Byte (0xBE); // Read SCT

DS18B20_Multi_Read_Byte (sct, 9); // Read scratchpad data, 8 Bytes + 1 Byte CRC

// Cycle end

if (DS18B20_SCT_Check (sct) != 1) // Check scratchpad with CRC and CONFIG register

{

Exit_Critical ();

return 0;

}

DS18B20_Temperature_Format_Transfer (sct, Value);

Exit_Critical ();

return 1;

}

//--------------------------------------------------------------------------------------------------

ds18b20.h

/*

****************************************************************************************************

* SSAC_A1

* Main Controller Board

* File name : ds18b20.h

* Version : 1.0

* Use for : To detect temperature

* Summary : The include file

* Author : Shensong (E_mail:webmaster@shensong.com / QQ:2939603 / Mob:13016461980)

* Date : 2005-05-21

****************************************************************************************************

*/

#ifndef __DS18B20_H

#define __DS18B20_H

////////// Read the Laser ROM from from DS18b20 // Notice: Only for single device on 1-Wire Bus

// Input:

// * Value: the point for UID data storage

// Return:

// 0: Opt. false

// 1: Opt. OK

__monitor extern unsigned char DS18B20_Single_Read_ROM (unsigned char * Value);

//--------------------------------------------------------------------------------------------------

////////// Read the value of temperature from DS18b20

// Input:

// // * UID: which device with the UID will be read out

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

extern unsigned char DS18B20_Single_Read_Temperature (unsigned char * Value);

//--------------------------------------------------------------------------------------------------

////////// Only convert temperature without delay. Single

// Input:

// None

// Return:

// 0: Opt. false

// 1: Opt. OK

extern unsigned char DS18B20_Single_Only_Convert_Temperature (void);

//--------------------------------------------------------------------------------------------------

////////// Only read SCT data from DS18B20. Single

// Input:

//

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

extern unsigned char DS18B20_Single_Only_Read_Temperature (unsigned char * Value);

//--------------------------------------------------------------------------------------------------

////////// 以指定的ROM去读温度

// Input:

//

// * UID: ROM (8 bytes)

// * Value: will storage temperature.

// * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0

// * (Value + 1): Int. of temperature value

// * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)

// Return:

// 0: Opt. false

// 1: Opt. OK

extern unsigned char DS18B20_UID_Read_Temperature (unsigned char * Value, unsigned char * UID);

#endif /* __DS18B20_H */

ds18b20_ports.h

/*

****************************************************************************************************

* SSAC_A1

* Main Controller Board

* File name : ds18b20_ports.h

* Version : 1.0

* Use for : To detect temperature

* Summary : The include file for ports

* Author : Shensong (E_mail:webmaster@shensong.com / QQ:2939603 / Mob:13016461980)

* Date : 2005-05-25

****************************************************************************************************

*/

#ifndef __DS18B20_PORTS_H

#define __DS18B20_PORTS_H

#define MCU_XTAL 11.0592 // The unit is MHz, for main MCU

#define MCU_TYPE ATmega128

#define COMPLIER_TYPE IAR

#if (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR)

#include <iom128.h> /* Only for "SREG" */

#include <ina90.h> /* Only for "__disable_interrupt() */

#endif /* (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR) */

/* The realization: program critical area ----------------------------------------------------*/

#if (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR)

#define Enter_Critical() cpu_sr = SREG; __disable_interrupt();

#define Exit_Critical() if (cpu_sr & 0x80) __enable_interrupt();

#endif /* (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR) */

/**************************************************************************************************/

/* The realization: Software delay -------------------------------------------------------------*/

#if (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR)

#define Soft_Delay_us(x) __delay_cycles (x * MCU_XTAL)

#define Soft_Delay_ms(x) __delay_cycles (x * MCU_XTAL * 1000)

#define Soft_Delay_s(x) __delay_cycles (x * MCU_XTAL * 1000000)

#endif /* (MCU_TYPE == ATmega128) && (COMPLIER_TYPE == IAR) */

/**************************************************************************************************/

#define DS18B20_Tx_Release() DDRB_DDB7 = 0

#define DS18B20_Tx_Obtain() DDRB_DDB7 = 1

#define DS18B20_Rx() PINE_PINE7

#endif /* __DS18B20_PORTS_H */

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有