新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > msp430单片机的ds18b20测温度c程序

msp430单片机的ds18b20测温度c程序

作者: 时间:2016-11-27 来源:网络 收藏


// 功能函数定义

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; i8; 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; i8; 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; i8; 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; i8; i++){
temp = oww_dat >> i;
temp &= 0x01;
DS18B20_WriteBit_2(temp);
}
DelayX10us(7);// ??
}//Write a byte to the 1-wire bus;


关键词: msp430单片机ds18b2

评论


技术专区

关闭