This code is for interfacing with a DS18B20 temperature sensor...

May 12, 2024 at 03:20 PM

#include "ds18b20.h" #include "smg.h" void DS18B20_IO_OUT(void) { GPIO_InitTypeDef GPIO_Initure; DS18B20_DATA_CLK(); //¿ªÆôGPIOAʱÖÓ GPIO_Initure.Pin = DS18B20_DATA_PIN; //PA0 GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; //ÍÆÍìÊä³ö GPIO_Initure.Pull = GPIO_NOPULL; //ÉÏÀ­ GPIO_Initure.Speed = GPIO_SPEED_FREQ_LOW;//¸ßËÙ HAL_GPIO_Init(DS18B20_DATA_PORT, &GPIO_Initure); //³õʼ»¯ } void DS18B20_IO_IN(void) { GPIO_InitTypeDef GPIO_Initure; DS18B20_DATA_CLK(); //¿ªÆôGPIOAʱÖÓ GPIO_Initure.Pin = DS18B20_DATA_PIN; //PA0 GPIO_Initure.Mode = GPIO_MODE_INPUT; //ÍÆÍìÊä³ö GPIO_Initure.Pull = GPIO_NOPULL; //ÉÏÀ­ GPIO_Initure.Speed = GPIO_SPEED_FREQ_LOW;//¸ßËÙ HAL_GPIO_Init(DS18B20_DATA_PORT, &GPIO_Initure); //³õʼ»¯ } //¸´Î»DS18B20 void DS18B20_Rst(void) { DS18B20_IO_OUT(); //SET PA0 OUTPUT DS18B20_DQ_OUT(0); //À­µÍDQ SEG_delay_us(750); //À­µÍ750us DS18B20_DQ_OUT(1); //DQ=1 SEG_delay_us(15); //15US } //µÈ´ýDS18B20µÄ»ØÓ¦ //·µ»Ø1:δ¼ì²âµ½DS18B20µÄ´æÔÚ //·µ»Ø0:´æÔÚ uint8_t DS18B20_Check(void) { uint8_t retry=0; DS18B20_IO_IN();//SET PA0 INPUT while (DS18B20_DQ_IN&&retry<200) { retry++; SEG_delay_us(1); }; if(retry>=200)return 1; else retry=0; while (!DS18B20_DQ_IN&&retry<240) { retry++; SEG_delay_us(1); }; if(retry>=240)return 1; return 0; } //´ÓDS18B20¶ÁÈ¡Ò»¸öλ //·µ»ØÖµ£º1/0 uint8_t DS18B20_Read_Bit(void) // read one bit { uint8_t data; DS18B20_IO_OUT();//SET PA0 OUTPUT DS18B20_DQ_OUT(0); SEG_delay_us(2); DS18B20_DQ_OUT(1); DS18B20_IO_IN();//SET PA0 INPUT SEG_delay_us(12); if(DS18B20_DQ_IN)data=1; else data=0; SEG_delay_us(50); return data; } //´ÓDS18B20¶ÁÈ¡Ò»¸ö×Ö½Ú //·µ»ØÖµ£º¶Áµ½µÄÊý¾Ý uint8_t DS18B20_Read_Byte(void) // read one byte { uint8_t i,j,dat; dat=0; for (i=1;i<=8;i++) { j=DS18B20_Read_Bit(); dat=(j<<7)|(dat>>1); } return dat; } //дһ¸ö×Ö½Úµ½DS18B20 //dat£ºÒªÐ´ÈëµÄ×Ö½Ú void DS18B20_Write_Byte(uint8_t dat) { uint8_t j; uint8_t testb; DS18B20_IO_OUT();//SET PA0 OUTPUT; for (j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if (testb) { DS18B20_DQ_OUT(0);// Write 1 SEG_delay_us(2); DS18B20_DQ_OUT(1); SEG_delay_us(60); } else { DS18B20_DQ_OUT(0);// Write 0 SEG_delay_us(60); DS18B20_DQ_OUT(1); SEG_delay_us(2); } } } //¿ªÊ¼Î¶Èת»» void DS18B20_Start(void)// ds1820 start convert { DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0x44);// convert } //³õʼ»¯DS18B20µÄIO¿Ú DQ ͬʱ¼ì²âDSµÄ´æÔÚ //·µ»Ø1:²»´æÔÚ //·µ»Ø0:´æÔÚ uint8_t DS18B20_Init(void) { GPIO_InitTypeDef GPIO_Initure; DS18B20_DATA_CLK(); //¿ªÆôGPIOAʱÖÓ GPIO_Initure.Pin = DS18B20_DATA_PIN; //PA0 GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; //ÍÆÍìÊä³ö GPIO_Initure.Pull = GPIO_NOPULL; //ÉÏÀ­ GPIO_Initure.Speed = GPIO_SPEED_FREQ_LOW;//¸ßËÙ HAL_GPIO_Init(DS18B20_DATA_PORT, &GPIO_Initure); //³õʼ»¯ DS18B20_Rst(); return DS18B20_Check(); } //´Óds18b20µÃµ½Î¶ÈÖµ //¾«¶È£º0.1C //·µ»ØÖµ£ºÎ¶ÈÖµ £¨-550~1250£© short DS18B20_Get_Temp(void) { uint8_t temp; uint8_t TL,TH; short tem; DS18B20_Start (); // ds1820 start convert DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH>7) { TH=~TH; TL=~TL; temp=0;//ζÈΪ¸º } else temp=1;//ζÈΪÕý tem=TH; //»ñµÃ¸ß°Ëλ tem<<=8; tem+=TL;//»ñµÃµ×°Ëλ tem=(float)tem*0.625;//ת»» if(temp) return tem; //·µ»ØζÈÖµ else return -tem; }

This code is for interfacing with a DS18B20 temperature sensor using a microcontroller. It includes functions to initialize the sensor, reset it, read temperature data from the sensor, and start temperature conversion.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node