STM8L controls the temperature sensor DS18B20

Publisher:数字奇迹Latest update time:2022-04-23 Source: eefocusKeywords:STM8L Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#define DS18B20_GPIO_PORT (GPIOC)

#define DS18B20_GPIO_PIN (GPIO_Pin_2)



#define DS18B20_PIN_SET_OUT() GPIO_Init(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN, GPIO_Mode_Out_PP_High_Fast)

#define DS18B20_PIN_SET_IN() GPIO_Init(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN, GPIO_Mode_In_PU_No_IT)

#define DS18B20_WR1() GPIO_SetBits(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)

#define DS18B20_WR0() GPIO_ResetBits(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)

#define R_DS18B20() GPIO_ReadInputDataBit(DS18B20_GPIO_PORT, (GPIO_Pin_TypeDef)DS18B20_GPIO_PIN)

/* Private macro -------------------------------------------------------------*/

#define READ_ROM 0x33 //Read serial number

#define SKIP_ROM 0xCC //Skip ROM

#define MATCH_ROM 0x55 //Match ROM

#define CONVERT_TEM 0x44 //Conversion temperature

#define READ_RAM ​​0xBE //Read temporary register

#define WRITE_RAM 0x4E //Write register

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

void _delay_us(uint16_t nCount)

{

    nCount *= 3; 

    while(--nCount);

}

/**

  * @brief _delay_ms

  * @param nCount

  * @retval None

  */

void _delay_ms(uint16_t nCount)

{

    while(nCount--)

    {

        _delay_us(1000);

    }

}

/* Private functions ---------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/

void Alarm_for_No_DS18B20(void)

{

    //If DS18B20 is not found on the single bus, an alarm will be triggered. This action will be handled according to the specific application.

}

//---------------------------------------------------------------------

unsigned char DS18B20_Start(void) //Reset ds18b20 chip

{

    unsigned char i,succ=0xff;

    DS18B20_PIN_SET_OUT(); //Set to output port

    DS18B20_WR0(); //The bus generates a falling edge, initialization starts

    for(i=0; i<30; i++)_delay_us(25); //The bus keeps low level between 480 - 960 microseconds; i<20; (25us)

    DS18B20_WR1(); //Pull the bus high

    DS18B20_PIN_SET_IN(); //Set to input, the host releases the bus and prepares to receive the response pulse of DS18B20

    i=0;

    while(R_DS18B20()) //Wait for DS18B20 to send a response pulse

    {

        _delay_us(5); //5

        if(++i>12) //DS18B20 waits for 15-60us after detecting the rising edge of the bus

        {

            succ=0x00; //If the waiting time is greater than about 60us, report reset failure

            break;

        }

    }

    i=0;

    while(!R_DS18B20()) //DS18B20 sends out existence pulse, lasting 60-240us

    {

        _delay_us(5); //5

        if(++i>48) //If the waiting time is greater than about 240us, report reset failure

        {

            succ=0x00;

            break;

        }

    }

    _delay_us(20);

    return succ;

}

//---------------------------------------------------------------------

void DS18B20_SendU8(unsigned char d8) // write one byte to DS18B20

{

    unsigned char i;

    DS18B20_PIN_SET_OUT(); //Set to output port

    for(i=0; i<8; i++)

    {

        DS18B20_WR0(); //Pull the bus low to start the "write time slice"

        _delay_us(2); // greater than 1 microsecond

        if(d8&0x01)DS18B20_WR1();

        _delay_us (32); //Delay at least 60 microseconds to make the write effective

        _delay_us (30);

        DS18B20_WR1(); //Pull the bus high, release the bus, and prepare to start the next "write time slice"

        d8>>=1;

        _delay_us (1);

    }

    DS18B20_PIN_SET_IN(); //Host releases the bus

}

//---------------------------------------------------------------------

unsigned char DS18B20_ReadU8(void) // read 1 byte from DS18B20

{

    unsigned char i,d8;

    for(i=0; i<8; i++)

    {

        d8>>=1;

        DS18B20_PIN_SET_OUT(); //Set as output port

        DS18B20_WR0(); //Pull the bus low to start reading the "time slice"

        _delay_us(2); // greater than 1 microsecond

        DS18B20_WR1(); //Host releases the bus, and the read is valid within (2~15)us

        DS18B20_PIN_SET_IN(); //Set the pin as input port, ready to read

        _delay_us(2); //Delay 2 us before reading



        if(R_DS18B20())d8|=0x80; //Read bus data within about 15 microseconds from the time the bus is pulled low

        _delay_us(32); //Reading completed after 60us

        _delay_us(30);

        DS18B20_WR1(); //Pull the bus high, the host releases the bus, and prepares to start the next "write time slice"

    }

    DS18B20_PIN_SET_IN(); //Host releases the bus

    return(d8);

}

//------------------------------------------------------------------------------------

void Init_DS18B20(void) //Initialize DS18B20

{

    unsigned char i;

    i=DS18B20_Start(); //Reset

    if(!i) //no DS18B20 is found on the single bus, then an alarm is raised

    {

        Alarm_for_No_DS18B20();

        return;

    }

    DS18B20_SendU8 (SKIP_ROM); //Skip ROM matching

    DS18B20_SendU8 (WRITE_RAM); //Set write mode

    DS18B20_SendU8 (0x64); //Set the upper temperature limit to 100 degrees Celsius

    DS18B20_SendU8 (0x8a); //Set the temperature to -10 degrees Celsius

    DS18B20_SendU8 (0x7f); //12bit (default)

}



//--------------------------------------------------------------------------------------



unsigned int Read_DS18B20(void) //Read and calculate the temperature to be output

{

    unsigned char i;

    unsigned char tl;

    unsigned int th;

    i=DS18B20_Start(); //Reset

    if(!i) //no DS18B20 is found on the single bus, then an alarm is raised

    {

        Alarm_for_No_DS18B20();

        return 0;

    }

    _delay_ms(1);

    DS18B20_SendU8(SKIP_ROM); //Send the command to skip the serial number detection

    DS18B20_SendU8(CONVERT_TEM); // Command Ds18b20 to start converting temperature

    i=0;

    _delay_ms(1);

    while(!R_DS18B20()) //When temperature conversion is in progress, the host will receive 0 when reading the bus, and 1 when the conversion is completed

    {

        _delay_ms(3);

        if(++i>250) break; //The maximum conversion time is 750ms

    }

    DS18B20_Start(); //Initialization

    _delay_ms(1);

    DS18B20_SendU8(SKIP_ROM); //Send the command to skip the serial number detection

    DS18B20_SendU8(READ_RAM); //Send command to read temperature data

    tl=DS18B20_ReadU8(); //Read the lower 8 bits of temperature data first

    th=DS18B20_ReadU8()<<8; //Read the high 8-bit temperature data again

    return (th|tl)*10>>4; //The temperature is magnified 10 times,*0.0625=1/16=>>4

}


Keywords:STM8L Reference address:STM8L controls the temperature sensor DS18B20

Previous article:How does STVD/COSMIC assign variables to specified addresses?
Next article:Summary of STM8 usage problems 2----STVD (COSMIC) defines variables to specify their types and bit definitions

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号