Temperatursensorteknik

Design av DS18B20 digital temperatursensor för STM32

DS18B20 temperaturavkännande tråd i rostfritt stål

DS18B20 är en digital temperatursensor som använder en enda busstiming för att kommunicera med värden. Endast 1 Kabel behövs för att slutföra temperaturdataavläsningen;

DS18B20 har ett inbyggt 64-bitars produktserienummer för enkel identifiering. Flera DS18B20-sensorer kan anslutas till 1 Tråd, och genom 64-bitars identitetsautentisering, temperaturinformationen som samlas in från olika sensorer kan läsas separat.

DS18B20 temperaturavkännande tråd i rostfritt stål

DS18B20 temperaturavkännande tråd i rostfritt stål

DS18B20 temperaturgivare sond TPE övergjutningssats

DS18B20 temperaturgivare sond TPE övergjutningssats

1 tråd DS18B20 temperaturgivare

1 tråd DS18B20 temperaturgivare

Introduktion till DS18B20
2.1 Huvudfunktioner hos DS18B20
1. Helt digital temperaturomvandling och utgång.
2. Avancerad enkelbuss datakommunikation.
3. Upp till 12-bitars upplösning, med en noggrannhet på upp till ±0,5 grader Celsius.
4. Den maximala arbetscykeln vid 12-bitars upplösning är 750 millisekunder.
5. Parasitiskt arbetsläge kan väljas.
6. Detekteringstemperaturområdet är –55°C ~+125°C (–67° F ~+257° F).
7. Inbyggt EEPROM, temperaturgräns larmfunktion.
8. 64-bit fotolitografi ROM, inbyggt produktserienummer, bekvämt för multimaskinanslutning.
9. Olika förpackningsformer, anpassa sig till olika hårdvarusystem.

DS18B20-chippaketstruktur

DS18B20-chippaketstruktur

2.2 DS18B20 stiftfunktion
GND spänning jord;
DQ enkel databuss;
VDD strömförsörjningsspänning;
NC tom stift;

DS18B20 chip RAM och EEPROM strukturdiagram

DS18B20 chip RAM och EEPROM strukturdiagram

2.3 DS18B20 arbetsprincip och tillämpning
DS18B20 temperaturdetektering och digital datautgång är helt integrerade på ett chip, så den har starkare anti-interferensförmåga. Dess ena arbetscykel kan delas upp i två delar, nämligen temperaturdetektering och databehandling.

18B20 har tre former av minnesresurser. Det är de: ROM skrivskyddat minne, används för att lagra DS18B20ID-kod; den första 8 bitar är en rads seriekod (DS18B20-koden är 19H), följande 48 bitar är chipets unika serienummer; den sista 8 bitar är CRC-koden (redundanskontroll) av ovanstående 56 bitar. Data ställs in vid produktion och kan inte ändras av användaren. DS18B20 har totalt 64 bitar av ROM.

RAM-dataregister, används för intern beräkning och dataåtkomst, data går förlorade efter strömavbrott, DS18B20 har totalt 9 byte RAM, varje byte är 8 bitar. Den första och andra byten är datavärdesinformationen efter temperaturomvandling; den tredje och fjärde byten är spegelbilden av användarens EEPROM (används vanligtvis för lagring av temperaturlarmvärden). Dess värde kommer att uppdateras när strömmen återställs. Den femte byten är spegelbilden av användarens tredje EEPROM. Den 6:e, 7th, och 8:e byte är räkningsregister, som är designade för att tillåta användare att få högre temperaturupplösning. De är också tillfälliga lagringsenheter för intern temperaturomvandling och beräkning. Den 9:e byten är CRC-koden för den första 8 bytes. EEPROM är ett icke-flyktigt minne som används för att lagra data som behöver sparas under lång tid, övre och nedre temperaturlarmvärden, och verifieringsdata. DS18B20 har totalt 3 bitar av EEPROM, och det finns spegelbilder i RAM-minnet för att underlätta användarens användning.

DS18B20 fungerar som standard i 12-bitars upplösningsläge. 12-bitars data som erhålls efter konvertering lagras i två 8-bitars RAM-minnen på DS18B20 (de två första byten). Den första 5 bitar i binär är teckenbitar. Om den uppmätta temperaturen är högre än 0, dessa 5 bitar är 0. Multiplicera bara det uppmätta värdet med 0.0625 för att få den faktiska temperaturen. Om temperaturen är lägre än 0, dessa 5 bitar är 1. Det uppmätta värdet måste inverteras, tillagd av 1, och sedan multipliceras med 0.0625 för att få den faktiska temperaturen. Eller använd bitdrift för att extrahera temperatur: decimalerna upptar den lägre 4 bitar, och de övre bitarna är heltalsbitar (negativa tal beaktas inte).

2.4 DS18B20 chip ROM instruktionstabell
1. Läs rom [33H] (det hexadecimala kommandoordet står inom hakparenteser).
Detta kommando gör att bussstyrenheten kan läsa 64-bitars ROM i DS18B20. Denna instruktion kan endast användas när det bara finns en DS18B20 på bussen. Om mer än en är ansluten, datakonflikter kommer att uppstå under kommunikation.

2. atch ROM [55H]
Denna instruktion följs av ett 64-bitars serienummer utfärdat av styrenheten. När det finns flera DS18B20 på bussen, endast chippet med samma serienummer som det som utfärdats av styrenheten kan svara, och de andra markerna väntar på nästa återställning. Denna instruktion är lämplig för enkelchips- och multi-chipanslutning.

3. Skeppsrum [CCH]
Denna instruktion gör att chipet inte svarar på ROM-koden. När det gäller en enda buss, denna instruktion kan användas för att spara tid. Om denna instruktion används när flera chips är anslutna, datakonflikter kommer att uppstå, resulterar i fel.

4. Sök ROM [F0H]
Efter att chippet har initierats, sökinstruktionen gör att 64-bitars ROM för alla enheter kan identifieras genom eliminering när flera chips är anslutna till bussen.

5. Larmsökning [VARJE]
I fallet med flera marker, larmchipssökningsinstruktionen svarar endast på chips som uppfyller larmvillkoret för temperatur högre än TH eller lägre än TL. Så länge chippet inte är avstängt, larmtillståndet kommer att bibehållas tills temperaturen mäts igen och larmtillståndet inte uppnås.

6. Skriv skrapplatta [4VA]
Detta är instruktionen för att skriva data till RAM. De två bytes med data som skrivs efteråt kommer att lagras på adressen 2 (TH av larm-RAM) och adress 3 (TL för larm-RAM). Skrivprocessen kan avslutas med en återställningssignal.

7. Läs Scratch Pad (läsa data från RAM) [BEH]
Denna instruktion kommer att läsa data från RAM, med början från adress 0 och upp till adress 9, completing the reading of the entire RAM data. The chip allows the reset signal to terminate the reading process, som är, the subsequent unnecessary bytes can be ignored to reduce the reading time.

8. Kopiera skrapplatta (copy RAM data to EEPROM) [48H]
This instruction stores the data in RAM into EEPROM so that the data will not be lost when power is off. Since the chip is busy with EEPROM storage processing, when the controller sends a read time slot, the bus outputs “0”, and when the storage work is completed, the bus will output “1”.
In parasitic working mode, a strong pull-up must be used immediately after this instruction is issued and maintained for at least 10MS to maintain chip operation.

9. Convert T (temperature conversion) [44H]
After receiving this instruction, the chip will perform a temperature conversion and store the converted temperature value in the 1st and 2nd addresses of RAM. Since the chip is busy with temperature conversion processing, when the controller sends a read time slot, the bus outputs “0”, and when the storage work is completed, the bus will output “1”. In parasitic working mode, a strong pull-up must be used immediately after this instruction is issued and maintained for at least 500MS to maintain chip operation.

10. Recall EEPROM (Copy the alarm value in EEPROM to RAM) [B8H]
This instruction copies the alarm value in EEPROM to the 3rd and 4th bytes in RAM. Since the chip is busy with copying processing, when the controller sends a read time slot, the bus outputs “0”, and when the storage work is completed, the bus outputs “1”. Dessutom, this instruction will be automatically executed when the chip is powered on and reset. På det här sättet, the two alarm byte bits in RAM will always be the mirror image of the data in EEPROM.

11. Read Power Supply (Working Mode Switch) [B4H]
After this instruction is issued, a read time gap is issued, and the chip will return its power status word. “0” is the parasitic power state and “1” is the external power state.

2.5 DS18B20 Timing Diagram
2.5.1 DS18B20 Reset and Response Relationship Diagram
A reset must be performed before each communication. The reset time, waiting time, and response time should be strictly programmed according to the timing.
DS18B20 read and write time gap: DS18B20 data reading and writing is confirmed by the time gap processing bit and command word to exchange information.

DS18B20 reset and response relationship diagram

DS18B20 reset and response relationship diagram

2.5.2 Write data 0 and data 1 to DS18B20
In the first 15uS of the write data time gap, the bus needs to be pulled low by the controller, and then it will be the chip sampling time for the bus data. The sampling time is 15~60uS. If the controller pulls the bus high during the sampling time, it means writing “1”, and if the controller pulls the bus low, it means writing “0”.
Each bit of transmission should have a low-level start bit of at least 15uS, and the subsequent data “0” eller “1” should be completed within 45uS.
The transmission time of the entire bit should be kept at 60~120uS, otherwise the normal communication cannot be guaranteed.
Notera: DS18B20 reads and writes data from the low bit.

Write data 0 and data 1 to DS18B20

Write data 0 and data 1 to DS18B20

2.5.3 Reading data 0 and data 1 from DS18B20
The sampling time of the control during the read time gap should be more accurate. During the read time gap, the host must also generate a low level of at least 1uS to indicate the start of the read time. Sedan, in 15uS after the bus is released, the DS18B20 will send the internal data bit. Just nu, if the control finds that the bus is high, it means reading “1”, and if the bus is low, it means reading data “0”. Before reading each bit, the controller adds a start signal.

Read data 0 and data 1 from DS18B20

Read data 0 and data 1 from DS18B20

Notera: The data bit must be read within 15uS of the start of the read gap to ensure correct communication.

Under kommunikation, 8 bits of “0” eller “1” are used as a byte, and the reading or writing of the byte starts from the low bit.

2.5.4 Order of reading temperature once (only a single DS18B20 on the bus)

1. Send reset signal
2. Detect response signal
3. Send 0xCC
4. Send 0x44
5. Send reset signal
6. Detect response signal
7. Write 0xcc
8. Write 0xbe
9. Slinga 8 times to read the low byte of temperature
10. Slinga 8 times to read the high byte of temperature
11. Synthesize 16-bit temperature data and process

3. Driver code

3.1 DS18B20.c
#includeds18b20.h
/*
Fungera: DS18B20 initialization
Hardware connection: PB15
*/
void DS18B20_Init(void)
{
RCC->APB2ENR|=1<<3; //PB
GPIOB->CRH&=0x0FFFFFFF;
GPIOB->CRH|=0x30000000;
GPIOB->ODR|=1<<15; //Pull-up
}

/*
Fungera: Check if the DS18B20 device exists
Return value: 1 means the device does not exist 0 means the device is normal
*/
u8 DS18B20_CheckDevice(void) //Contains reset pulse, detection pulse
{
DS18B20_OUTPUT_MODE();//Initialize to output mode
DS18B20_OUT=0; //Generate reset pulse
DelayUs(750); //Generate 750us low level
DS18B20_OUT=1; //Release bus
DelayUs(15); //Wait for DS18B20 response
om(DS18B20_CleckAck())//Detect existence pulse
{
return 1;
}
return 0;
}

/*
Fungera: Detect existence pulse of DS18B20 device
Return value: 1 indicates error 0 indicates normal
*/
u8 DS18B20_CleckAck(void)
{
u8 cnt=0;
DS18B20_INPUT_MODE();//Initialize to input mode
medan(DS18B20_IN&&cnt<200) //Wait for DS18B20 response existence pulse
{
DelayUs(1);
cnt++;
}
om(cnt>=200)return 1; //fel

cnt=0;
medan((!DS18B20_IN)&&cnt<240) //wait for DS18B20 to release the bus
{
DelayUs(1);
cnt++;
}
om(cnt>=240)return 1; //fel
return 0;
}

/*
Fungera: Write a byte
First learn how to write a bit.
*/
void DS18B20_WriteByte(u8 cmd)
{
u8 i;
DS18B20_OUTPUT_MODE(); //Initialize to output mode
för(i=0;i<8;i++)
{
DS18B20_OUT=0; //Generate write time gap (write start)
DelayUs(2);
DS18B20_OUT=cmd&0x01; //Send actual data bit
DelayUs(60); //Wait for write completion
DS18B20_OUT=1; //Release the bus and prepare for the next transmission
cmd>>=1; //Continue to send the next bit of data
}
}

/*
Fungera: Read a byte
First learn how to read a bit.
*/
u8 DS18B20_ReadByte(void)
{
u8 i,data=0;
för(i=0;i<8;i++)
{
DS18B20_OUTPUT_MODE(); //Initialize to output mode
DS18B20_OUT=0; //Generate read time gap (read start)
DelayUs(2);
DS18B20_OUT=1; //Release bus
DS18B20_INPUT_MODE(); //Initialize to input mode
DelayUs(8); //Wait for DS18B20 data output
data>>=1; //Fill high bit with 0, default is 0
om(DS18B20_IN) data|=0x80;
DelayUs(60);
DS18B20_OUT=1; //Release bus, wait for reading next bit of data
}
returnera data;
}

/*
Fungera: Read the temperature data of DS18B20 once
Return value: the temperature data read
Considered situation: There is only one DS18B20 connected to the bus
*/
u16 DS18B20_ReadTemp(void)
{
u16 temp=0;
u8 temp_H,temp_L;
DS18B20_CheckDevice(); //Send reset pulse, detect pulse
DS18B20_WriteByte(0xCC); //Skip ROM sequence detection
DS18B20_WriteByte(0x44); //Start a temperature conversion

//Wait for temperature conversion to complete
medan(DS18B20_ReadByte()!=0xFF){}

DS18B20_CheckDevice(); //Send reset pulse, detect pulse
DS18B20_WriteByte(0xCC); //Skip ROM sequence detection
DS18B20_WriteByte(0xBE); //Read temperature

temp_L=DS18B20_ReadByte(); //Read low temperature data
temp_H=DS18B20_ReadByte(); //Read high temperature data
temp=temp_L|(temp_H<<8); //Synthesized temperature
return temp;
}

3.2 DS18B20.h

#ifndef DS18B20_H
#define DS18B20_H
#includestm32f10x.h
#includesys.h
#includedelay.h
#includeds18b20.h
#includeusart.h

/*Package interface*/

//Initialize DS18B20 to input mode
#define DS18B20_INPUT_MODE() {GPIOB->CRH&=0x0FFFFFFF;GPIOB->CRH|=0x80000000;}

//Initialize DS18B20 to output mode
#define DS18B20_OUTPUT_MODE(){GPIOB->CRH&=0x0FFFFFFF;GPIOB->CRH|=0x30000000;}

//DS18B20 IO port output
#define DS18B20_OUT PBout(15)

//DS18B20 IO port input
#define DS18B20_IN PBin(15)

//Function declaration
u8 DS18B20_CleckAck(void);
u8 DS18B20_CheckDevice(void);
void DS18B20_Init(void);
u16 DS18B20_ReadTemp(void);
u8 DS18B20_ReadByte(void);
void DS18B20_WriteByte(u8 cmd);
#endif

poYBAGDYdXCAWkKMAAAAK8RNs4s030.png
3.3 Delay function

/*
Fungera: Delay in us
*/
void DelayUs(int us)
{
#ifdef _SYSTICK_IRQ_
int i,j;
för(i=0;iVAL=0; //CNT counter value
SysTick->LOAD=9*us; //9 means 1us
SysTick->CTRL|=1<<0; //Start timer
do
{
tmp=SysTick->CTRL; //Read status
}medan((!(tmp&1<<16))&&(tmp&1<<0));
SysTick->VAL=0; //CNT counter value
SysTick->CTRL&=~(1<<0); //Turn off the timer
#endif
};i++)>

3.4 main.c Call DS18B20 to read the temperature and print it to the serial port

#includestm32f10x.h

#includeds18b20.h

u8 DS18B20_ROM[8]; //Store the 64-bit ROM code of DS18B20

int main(void)
{
u16 temp;
USARTx_Init(USART1,72,115200);//Initialization of serial port 1
DS18B20_Init(); //DS18B20 initialization

/*1. Read the 64-bit ROM code of DS18B20*/
//Send reset pulse, detect existence pulse
medan(DS18B20_CheckDevice())
{
printf(“DS18B20 device does not exist!\n”);
DelayMs(500);
}
//Send the command to read the 64-bit ROM code
DS18B20_WriteByte(0x33);

//Loop read 64-bit ROM code
för(i=0;i<8;i++)
{
DS18B20_ROM[i]= DS18B20_ReadByte();
printf(“DS18B20_ROM[%d]=0x%X\n”,i,DS18B20_ROM[i]);
}

medan(1)
{
/*2. Simultaneously operate all DS18B20 on the bus to start converting temperature*/
DS18B20_CheckDevice(); //Send reset pulse, detect pulse
DS18B20_WriteByte(0xCC); //Skip ROM sequence detection
DS18B20_WriteByte(0x44); //Start a temperature conversion (let all DS18B20 on the bus convert the temperature)
DelayMs(500); //Wait for all DS18B20 temperature conversions on the line to complete

/*3. Single targeted reading of the temperature of each DS18B20*/
DS18B20_CheckDevice(); //Send reset pulse, detect pulse
DS18B20_WriteByte(0x55); //Send command to match ROM
för(i=0;i<8;i++) //Send 64-bit code
{
DS18B20_WriteByte(DS18B20_ROM[i]);
}
DS18B20_WriteByte(0xBE); //Read temperature
temp=DS18B20_ReadByte(); //Read low-order temperature data
temp|=DS18B20_ReadByte()<<8; //Read high-order temperature data
printf(“temp1=%d.%d\n”,temp>>4,temp&0xF);
printf(“temp2=%f\n”,temp*0.0625);

DelayMs(500);
}
}