void delay_18B20(unsigned int i)
{
while(i--);
}
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay_18B20(80); //稍做延时
DQ = 0; //单片机将DQ拉低
delay_18B20(800); //精确延时 大于 480us
DQ = 1; //拉高总线
delay_18B20(140);
x=DQ; delay_18B20(200);
}
unsigned char ReadOneChar(void)
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay_18B20(40); //40
}
return(dat);
}
void WriteOneChar(uchar dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(50); //50
DQ = 1;
dat>>=1;
}
}
unsigned int ReadTemp(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int temp_value=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
delay_18B20(1000);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
delay_18B20(1000);
a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
temp_value = b<<8;
temp_value |= a;
return temp_value;
}
用c很简单 ,
/
FILE NAME: DS18B20c
CHIP TYPE: ATMEGA16
CLOCK FREQUENCY: 8MHZ
IDE: VSMStudio
COMPILER: AVR-GCC
TIME: September 2010
/
#include <avr/ioh>
#include <util/delayh>
#define uchar unsigned char
#define uint unsigned int
#define BUS PORTC
// Low level port/pin definitions
#define sbit(x,PORT) (PORT) |= (1<<x)
#define cbit(x,PORT) (PORT) &= ~(1<<x)
#define pin(x,PIN) (PIN) & (1<<x)
// Pins definition
#define s_digit1 sbit(5,PORTC)
#define c_digit1 cbit(5,PORTC)
#define s_digit2 sbit(4,PORTC)
#define c_digit2 cbit(4,PORTC)
#define out PORTC
#define DQ_IN DDRA&=~(1<<7)
#define DQ_OUT DDRA|=(1<<7)
#define S_DQ sbit(7,PORTA)
#define C_DQ cbit(7,PORTA)
#define DQ pin(7,PINA)
// Function Prototypes
void init_ds18b20(void);
uchar readbyte(void);
void writecommand(uchar);
uchar readtemp(void);
uchar a, b, tt;
// Main program
int main(void)
{ uchar i=0, temp;
// Initialize Stack Pointer
SPL=0x54;
SPH=0x04;
// Configure port pins
DDRC = 0xff;
DDRA = 0xff;
while(1)
{ temp = readtemp();
for(i=0; i<10; i++) // 10 measures
{ // output the units
out = (temp/10) & 0x0f;
s_digit1;
c_digit2;
_delay_ms(5);
// output the tens
out = (temp%10) & 0x0f;
c_digit1;
s_digit2;
_delay_ms(5);
}
}
}
// Start transaction with 1-wire line
void init_ds18b20(void)
{ DQ_OUT;
C_DQ ;
_delay_us(600);
S_DQ;
_delay_us(50);
DQ_IN;
while(DQ);
_delay_us(240);
DQ_OUT;
S_DQ;
_delay_us(300);
}
// Read a byte from the sensor
uchar readbyte(void)
{ uchar i = 0,data = 0;
DQ_OUT;
for (i=0; i<8; i++)
{ C_DQ ;
data >>= 1;
_delay_us(3);
S_DQ;
DQ_IN;
_delay_us(12);
if(DQ)
data |= 0x80;
DQ_OUT;
S_DQ;
_delay_us(45);
_delay_us(5);
}
return(data);
}
// Write a command to the sensor
void writecommand(uchar data)
{ uchar i;
for(i=0; i<8; i++)
{ C_DQ;
_delay_us(15);
if(data & 0x01)
S_DQ;
else
C_DQ;
_delay_us(45);
data >>= 1;
S_DQ;
_delay_us(2);
}
}
// Read value from the sensor
uchar readtemp(void)
{ uint t;
init_ds18b20();
// Convert
writecommand(0xCC);
writecommand(0x44);
init_ds18b20();
// Read Scratch memory area
writecommand(0xCC);
writecommand(0xBE);
a = readbyte();
b = readbyte();
t = b;
t <<= 8;
t = t|a;
tt = t00625;
return(tt);
}
比如
while(t--)
{
_nop_();
}
t在delayus(10)中已经赋值,即t=10,_nop_()是一个空 *** 作NOP(汇编语言中),即一个机器周期,主频12Mhz中就是1um
你的问题是,温度校准过程中能否省去降温过程。
一般而言,必须完整进行增序和减序校准的传感器,都是工作过程中具有机械变形位移、材料应变的传感器。这是为了检测传感器的重复性和滞后特性。
温度传感器是依靠材料内部分子状态的改变工作,在工作中都没有变形、应变的现象发生(老式的双金属片结构除外),所以,你的温度传感器校准完全可以省去降温过程,这不会对你的校准精度有任何影响。
查阅各种温度传感器校准规程,里面都没有要求必须“升温,降温”过程。
以上就是关于c51单片机温度传感器c语言程序全部的内容,包括:c51单片机温度传感器c语言程序、基于数字温度传感器的数字温度计的汇编语言程序。、ds18b20温度传感器程序 求解释!!! 延时时间怎么计算的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)