msp430单片机的ds18b20测温度c程序
时间:11-27
来源:互联网
点击:
#include
#define DS18B20_DIR P2DIR
#define DS18B20_IN P2IN
#define DS18B20_OUT P2OUT
#define DS18B20_DQ BIT3 //定义DS18B20的接口
#define DS18B20_DQ_2 BIT4 //定义另一个DS18B20的接口
// DS18B20 ROM命令宏定义
#define ReadRom 0x33
#define MatchRom 0x55
#define SearchRom 0xf0
#define AlertSearch 0xec
#define CopyScratchpad 0x48
#define SkipRom 0xcc
#define ReadPower 0xb4
// DS18B20 功能命令宏定义
#define ConvertTemperature 0x44
#define ReadScratchpad 0xbe
#define WriteScratchpad 0x4e
#define RecallE 0xb8
#define SMCLK 2000 //(KHz)用于系统延时
// 温度的十进制编码(查表用)
unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
// 变量定义
unsigned char GetScratchpad[9];
unsigned char GetScratchpad_2[9];
float ResultTemperatureH; //温度的整数部分
float ResultTemperatureLH; //温度的小数部分(高位)
float ResultTemperatureLL; //温度的小数部分(低位)
float ResultTemperatureH_2; //温度的整数部分
float ResultTemperatureLH_2; //温度的小数部分(高位)
float ResultTemperatureLL_2; //温度的小数部分(低位)
float ResultTemperature; //温度的真实值
float ResultTemperature_2; //温度的真实值
float Temperaturefromtwo; //温度的真实值差
float OilPercentage; //油相含率
void DS18B20_WriteBit(unsigned char oww_dat);
void DS18B20_WriteBit_2(unsigned char oww_dat);
void DS18B20_WriteByte(unsigned char oww_dat);
void DS18B20_WriteByte_2(unsigned char oww_dat);
void DS18B20_ReadTemp(void);
void DS18B20_ReadTemp_2(void);
unsigned char DS18B20_Init(void);
unsigned char DS18B20_Init_2(void);
unsigned char DS18B20_ReadBit(void);
unsigned char DS18B20_ReadBit_2(void);
unsigned char DS18B20_ReadByte(void);
unsigned char DS18B20_ReadByte_2(void);
void Delay10us(void);
void DelayX10us(unsigned char x10us);
void main(void) {
volatile unsigned int i,j;
WDTCTL = WDTPW + WDTHOLD; //停止看门狗
BCSCTL1 &= ~XT2OFF; //XT2 = HF XTAL
do {
IFG1 &= ~OFIFG; //Clear OSCFault flag
for (i=0xFF;i>0;i--); //Time for flag to set
}while((IFG1&OFIFG)); //OSCFault flag still set?
BCSCTL2 |= (SELM1 + SELS); //MCLK = SMCLK = XT2
DS18B20_Init(); //初始化DS18B20
DS18B20_Init_2(); //初始化DS18B20.2
//Z7289_Init();
while(1) {
DS18B20_ReadTemp();
i=ResultTemperatureH/10;
j=ResultTemperatureH-(i*10);
//Z7289_Show(7,1,i);
//Z7289_Show(6,1,j);
//Z7289_Show(5,1,0x0C);
i=ResultTemperatureLH/10;
j=ResultTemperatureLH-(i*10);
//Z7289_Show(4,1,i);
//Z7289_Show(3,1,j);
i=ResultTemperatureLL/10;
j=ResultTemperatureLL-(i*10);
//Z7289_Show(2,1,i);
//Z7289_Show(1,1,j);
ResultTemperature=ResultTemperatureH+ResultTemperatureLH/100+ResultTemperatureLL/10000;
ResultTemperature_2=ResultTemperatureH_2+ResultTemperatureLH_2/100+ResultTemperatureLL_2/10000;
Temperaturefromtwo=ResultTemperature_2-ResultTemperature;
OilPercentage=1.6939-0.9260/Temperaturefromtwo;
}
}
// 功能函数定义
unsigned char DS18B20_Init(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= ~DS18B20_DQ; // DS18B20_DQ=0;
DelayX10us(48); // Bus master pulling low 480us minimum;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
DelayX10us(6); // Resister pull up 15-60us;
DS18B20_DIR &= ~DS18B20_DQ; // ow input
result = DS18B20_IN & DS18B20_DQ;
DS18B20_DIR |= DS18B20_DQ; // ow output
DelayX10us(48); // End of timeslot total 480us;
return(result); // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}//Intialization the 1-wire devices;
unsigned char DS18B20_Init_2(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ_2; // ow output
DS18B20_OUT &= ~DS18B20_DQ_2; // DS18B20_DQ=0;
DelayX10us(48); // Bus master pulling low 480us minimum;
DS18B20_OUT |= DS18B20_DQ_2; // DS18B20_DQ=1;
DelayX10us(6); // Resister pull up 15-60us;
DS18B20_DIR &= ~DS18B20_DQ_2; // ow input
result = DS18B20_IN & DS18B20_DQ_2;
DS18B20_DIR |= DS18B20_DQ_2; // ow output
DelayX10us(48); // End of timeslot total 480us;
return(result); // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}//Intialization the 1-wire devices;
unsigned char DS18B20_ReadBit(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= ~DS18B20_DQ; // DS18B20_DQ=0;
_NOP(); // Start of timeslot;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
_NOP();_NOP();_NOP();_NOP();
// Wait from the start;
DS18B20_DIR &= ~DS18B20_DQ; // ow input
result = DS18B20_IN & DS18B20_DQ;
DS18B20_DIR |= DS18B20_DQ; // ow output
return(result); // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;
unsigned char DS18B20_ReadBit_2(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ_2; // ow output
DS18B20_OUT &= ~DS18B20_DQ_2; // DS18B20_DQ=0;
_NOP(); // Start of timeslot;
DS18B20_OUT |= DS18B20_DQ_2; // DS18B20_DQ=1;
_NOP();_NOP();_NOP();_NOP();
// Wait from the start;
DS18B20_DIR &= ~DS18B20_DQ_2; // ow input
result = DS18B20_IN & DS18B20_DQ_2;
DS18B20_DIR |= DS18B20_DQ_2; // ow output
return(result); // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;
void DS18B20_WriteBit(unsigned char oww_dat){
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= ~DS18B20_DQ; // DS18B20_DQ=0;
if (1 == oww_dat)
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
DelayX10us(10); // Remain the state for 100us;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
}//Write a bit to the 1-wire bus;
void DS18B20_WriteBit_2(unsigned char oww_dat){
DS18B20_DIR |= DS18B20_DQ_2; // ow output
DS18B20_OUT &= ~DS18B20_DQ_2; // DS18B20_DQ=0;
if (1 == oww_dat)
DS18B20_OUT |= DS18B20_DQ_2; // DS18B20_DQ=1;
DelayX10us(10); // Remain the state for 100us;
DS18B20_OUT |= DS18B20_DQ_2; // DS18B20_DQ=1;
}//Write a bit to the 1-wire bus;
unsigned char DS18B20_ReadByte(void){
unsigned char i;
unsigned char result=0;
for(i = 0; i 8; i++){
if(DS18B20_ReadBit())
result |= 0x01 < i;
DelayX10us(12); // ??
}
return(result); // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
unsigned char DS18B20_ReadByte_2(void){
unsigned char i;
unsigned char result=0;
for(i = 0; i 8; i++){
if(DS18B20_ReadBit_2())
result |= 0x01 < i;
DelayX10us(12); // ??
}
return(result); // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
void DS18B20_WriteByte(unsigned char oww_dat){
unsigned char i,temp;
for(i = 0; i 8; i++){
temp = oww_dat >> i;
temp &= 0x01;
DS18B20_WriteBit(temp);
}
DelayX10us(7); // ??
}//Write a byte to the 1-wire bus;
void DS18B20_WriteByte_2(unsigned char oww_dat){
unsigned char i,temp;
for(i = 0; i 8; i++){
temp = oww_dat >> i;
temp &= 0x01;
DS18B20_WriteBit_2(temp);
}
DelayX10us(7); // ??
}//Write a byte to the 1-wire bus;
void DS18B20_ReadTemp(void)
{ unsigned char tempH,tempL,tempH_2,tempL_2;
DS18B20_Init();
DS18B20_Init_2();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte_2(SkipRom);
_NOP();
//There is just one DS1820 on the bus;
DS18B20_WriteByte(ConvertTemperature);
DelayX10us(10);
DS18B20_WriteByte_2(ConvertTemperature);
DelayX10us(10);
//Start to convert temperature;
DS18B20_Init();
DS18B20_Init_2();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte_2(SkipRom);
_NOP();
DS18B20_WriteByte(ReadScratchpad);
DS18B20_WriteByte_2(ReadScratchpad);
GetScratchpad[0]=DS18B20_ReadByte();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=DS18B20_ReadByte();
//Master samples the MSB temperature from the scratchpad;
GetScratchpad[2]=DS18B20_ReadByte();
//Master samples the Th register or userbyte1 from the scratchpad;
GetScratchpad[3]=DS18B20_ReadByte();
//Master samples the Tl register or userbyte0 from the scratchpad;
GetScratchpad[4]=DS18B20_ReadByte();
//Master samples the configuration register from the scratchpad;
GetScratchpad[5]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[6]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[7]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[8]=DS18B20_ReadByte();
//Master samples the CRC from the scratchpad;
tempH=(GetScratchpad[1] < 4) | (GetScratchpad[0] >> 4);
tempL=(GetScratchpad[0] & 0x0f);
DS18B20_Init();
GetScratchpad_2[0]=DS18B20_ReadByte_2();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad_2[1]=DS18B20_ReadByte_2();
//Master samples the MSB temperature from the scratchpad;
GetScratchpad_2[2]=DS18B20_ReadByte_2();
//Master samples the Th register or userbyte1 from the scratchpad;
GetScratchpad_2[3]=DS18B20_ReadByte_2();
//Master samples the Tl register or userbyte0 from the scratchpad;
GetScratchpad_2[4]=DS18B20_ReadByte_2();
//Master samples the configuration register from the scratchpad;
GetScratchpad_2[5]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[6]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[7]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[8]=DS18B20_ReadByte_2();
//Master samples the CRC from the scratchpad;
tempH_2=(GetScratchpad_2[1] < 4) | (GetScratchpad_2[0] >> 4);
tempL_2=(GetScratchpad_2[0] & 0x0f);
DS18B20_Init_2();
//Issue a reset to terminate left parts;
//if(tempH & 0x80)
//{ tempH=~tempH;
// tempL=~tempL+1;
// ResultSignal=1;
// //Negative temperature;
//}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];
ResultTemperatureH_2=tempH_2;
ResultTemperatureLL_2=decimalL[tempL_2];
ResultTemperatureLH_2=decimalH[tempL_2];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad;
void Delay10us(void){
unsigned char i;
for (i=0; i<(SMCLK/500-3); i++);
}
// Time is accurately !!
void DelayX10us(unsigned char x10us){
unsigned int i;
for (i=0; i
Delay10us
}
#define DS18B20_DIR
#define DS18B20_IN
#define DS18B20_OUT
#define DS18B20_DQ
#define DS18B20_DQ_2
// DS18B20 ROM命令宏定义
#define ReadRom
#define MatchRom
#define SearchRom
#define AlertSearch
#define CopyScratchpad
#define SkipRom
#define ReadPower
// DS18B20 功能命令宏定义
#define ConvertTemperature
#define ReadScratchpad
#define WriteScratchpad
#define RecallE
#define SMCLK
// 温度的十进制编码(查表用)
unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
// 变量定义
unsigned char GetScratchpad[9];
unsigned char GetScratchpad_2[9];
float ResultTemperatureH;
float ResultTemperatureLH;
float ResultTemperatureLL;
float ResultTemperatureH_2;
float ResultTemperatureLH_2;
float ResultTemperatureLL_2;
float ResultTemperature;
float ResultTemperature_2;
float Temperaturefromtwo;
float OilPercentage;
void DS18B20_WriteBit(unsigned char oww_dat);
void DS18B20_WriteBit_2(unsigned char oww_dat);
void DS18B20_WriteByte(unsigned char oww_dat);
void DS18B20_WriteByte_2(unsigned char oww_dat);
void DS18B20_ReadTemp(void);
void DS18B20_ReadTemp_2(void);
unsigned char DS18B20_Init(void);
unsigned char DS18B20_Init_2(void);
unsigned char DS18B20_ReadBit(void);
unsigned char DS18B20_ReadBit_2(void);
unsigned char DS18B20_ReadByte(void);
unsigned char DS18B20_ReadByte_2(void);
void Delay10us(void);
void DelayX10us(unsigned char x10us);
void main(void) {
}
// 功能函数定义
unsigned char DS18B20_Init(void){
}//Intialization the 1-wire devices;
unsigned char DS18B20_Init_2(void){
}//Intialization the 1-wire devices;
unsigned char DS18B20_ReadBit(void){
}//Read a bit on the 1-wire bus;
unsigned char DS18B20_ReadBit_2(void){
}//Read a bit on the 1-wire bus;
void DS18B20_WriteBit(unsigned char oww_dat){
}//Write a bit to the 1-wire bus;
void DS18B20_WriteBit_2(unsigned char oww_dat){
}//Write a bit to the 1-wire bus;
unsigned char DS18B20_ReadByte(void){
}//Read a byte from the 1-wire bus;
unsigned char DS18B20_ReadByte_2(void){
}//Read a byte from the 1-wire bus;
void DS18B20_WriteByte(unsigned char oww_dat){
}//Write a byte to the 1-wire bus;
void DS18B20_WriteByte_2(unsigned char oww_dat){
}//Write a byte to the 1-wire bus;
void DS18B20_ReadTemp(void)
{
}//Read the byte0 and byte1 from scratchpad;
void Delay10us(void){
}
//
void DelayX10us(unsigned char x10us){
}
msp430单片机ds18b2 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)