打印
[DemoCode下载]

DS18B20的操作

[复制链接]
596|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jiekou001|  楼主 | 2024-2-6 18:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     DS18B20.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* $Date: 19/06/17 3:56p $
* [url=home.php?mod=space&uid=247401]@brief[/url] DS18B20 Driver
*
*****************************************************************************/
#include "NuMicro.h"
/*---------------------------------------------------------------------------*/
/* Macro definition                                                          */
/*---------------------------------------------------------------------------*/
#define  DQ                      PB2            //DQ Pin D0

#define  CMD_SKIP_ROM            0xCC
#define  CMD_CONVERT_T           0x44
#define  CMD_READ_SCRATCHPAD     0xBE
/*---------------------------------------------------------------------------*/
/* Functions                                                                 */
/*---------------------------------------------------------------------------*/
static void DS18B20_Reset(void);
static void DS18B20_WriteByte(uint8_t u8dat);
static uint8_t DS18B20_ReadByte(void);



/**
  * @brief  Read temperature from DS18B20
  * @param  None
  * [url=home.php?mod=space&uid=266161]@return[/url]  i16temp: temperature
  */
int16_t DS18B20_ReadTemperature(void)
{
    uint8_t  u8tempH, u8tempL;
    int16_t  i16temp;

    DS18B20_Reset();
    DS18B20_WriteByte(CMD_SKIP_ROM);
    DS18B20_WriteByte(CMD_CONVERT_T);

    while (!DQ);                             //Waiting for conversion to complete

    DS18B20_Reset();
    DS18B20_WriteByte(CMD_SKIP_ROM);
    DS18B20_WriteByte(CMD_READ_SCRATCHPAD);
    u8tempL = DS18B20_ReadByte();            //Read temperature low byte
    u8tempH = DS18B20_ReadByte();            //Read temperature high byte
    i16temp = (u8tempH << 8) | u8tempL;

    return (i16temp);
}



/**
  * @brief  Reset the DS18B20 and check if the device is present
  * @param  None
  * @return None
  */
static void DS18B20_Reset(void)
{
    uint8_t i;
    i = 1;

    while (i)
    {
        DQ = 0;                              //Send a low level reset signal
        CLK_SysTickDelay(480);               //Delay at least 480us
        DQ = 1;                              //Release data line
        CLK_SysTickDelay(60);                //Waiting for 60us
        i = DQ;                              //Detect the presence of pulses
        CLK_SysTickDelay(420);               //Waiting for the device to release the data line
    }
}


/**
  * @brief  Read 1 byte data from DS18B20
  * @param  None
  * @return u8dat: Read data
  */
static uint8_t DS18B20_ReadByte(void)
{
    uint8_t i;
    uint8_t u8dat = 0;

    for (i = 0; i < 8; i++)
    {
        u8dat >>= 1;
        DQ = 0;                              //Start time slice
        CLK_SysTickDelay(1);                 //Delay waiting
        DQ = 1;                              //Ready to receive
        CLK_SysTickDelay(1);                 //Reception delay

        if (DQ) u8dat |= 0x80;               //Read data

        CLK_SysTickDelay(60);                //Waiting for the end of time
    }

    return u8dat;
}


/**
  * @brief  Write 1 byte of data to the DS18B20
  * @param  u8dat            Write data
  * @return None
  */
static void DS18B20_WriteByte(uint8_t u8dat)
{
    uint8_t i;

    for (i = 0; i < 8; i++)
    {
        DQ = 0;                              //Start time slice
        CLK_SysTickDelay(1);                 //Delay waiting

        if (u8dat & 0x01) DQ = 1;            //Send data

        CLK_SysTickDelay(60);                //Waiting for the end of time
        DQ = 1;                              //Restore data line
        CLK_SysTickDelay(1);                 //Recovery delay
        u8dat >>= 1;
    }
}


使用特权

评论回复
沙发
jiekou001|  楼主 | 2024-2-6 18:09 | 只看该作者
主要就是用IO操作。

使用特权

评论回复
板凳
jiekou001|  楼主 | 2024-2-6 18:09 | 只看该作者
调用方式
/**************************************************************************//**
* @file     main.c
* @version  V3.00
* @brief    DS18B20 Driver Sample Code
*
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include "stdio.h"
#include "NuMicro.h"


#define PLL_CLOCK       192000000

int16_t DS18B20_ReadTemperature(void);

void SYS_Init(void)
{

    /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
    PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

    /* Enable HXT clock (external XTAL 12MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Wait for HXT clock ready */
    CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);

    /* Set PCLK0/PCLK1 to HCLK/2 */
    CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);

    CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_LXT);       
       
       
    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Select UART module clock source as HXT and UART module clock divider as 1 */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
               
                SystemCoreClockUpdate();

}

void UART0_Init()
{
    /* Configure UART0 and set UART0 baud rate */
    UART_Open(UART0, 115200);
}

int32_t main(void)
{

                float t;

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

    printf("+-------------------------------------------------+\n");
    printf("|    PH.0(Output) and PG.15(Input) Sample Code     |\n");
    printf("+-------------------------------------------------+\n\n");




    /* Configure PH.0 as Output mode and PG.15 as Input mode then close it */
    GPIO_SetMode(PH, BIT0, GPIO_MODE_OUTPUT);
    GPIO_SetMode(PG, BIT15, GPIO_MODE_INPUT);
    GPIO_SetMode(PB, BIT2, GPIO_MODE_QUASI);               
       
    while(1)
                {
                        PH0=1;
                        CLK_SysTickDelay(1000000);
                        PH0=0;
                        CLK_SysTickDelay(1000000);
                        t = DS18B20_ReadTemperature() * 0.0625;
        printf("\n%f\n", t);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);                       
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);
                        CLK_SysTickDelay(1000000);                       
                       
                       
                }

}

使用特权

评论回复
地板
598330983| | 2024-2-12 21:44 | 只看该作者
如果用C++ 就可以很容易实现多个IO挂载多个DS18B20

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

120

主题

1253

帖子

2

粉丝