打印
[DemoCode下载]

使用M030G的 I2C 读取NCT7712Y 温度传感器

[复制链接]
277|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
en-us--EC_M030G_Read_NCT7712Y_Thermal_Sensor_V1.00 (1).zip (1.75 MB)
微控制器广泛的应用在不同的环境温度下,Nuvoton NuMicro® Cortex®–M0 M030G/M031G 系列搭配NCT7712Y 高精准度的温度传感器,以此可以随时监控当下的温度。此范例程序将说明如何使用I2C取得温度传感器的温度值。
NCT7712Y是一颗高精度的温度传感器芯片。 NCT7712Y内建一组12bit的ADC,以0.0625°C的分辨率转换监测的温度值。







使用特权

评论回复
沙发
xinxianshi|  楼主 | 2024-2-25 20:10 | 只看该作者
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* $Revision: 2 $
* $Date: 20/06/11 7:12p $
* [url=home.php?mod=space&uid=247401]@brief[/url]    Measure the current temperture by thermal sensor.
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
*
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "M030G.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t volatile done;
uint32_t volatile u32thermalData;
uint32_t volatile data_refresh_flag = 0;

uint32_t volatile MCU_Temp_Data = 0;
uint32_t volatile NCT7712Y_Data = 0;
uint32_t volatile NCT7712Y_Old_Data = 0;
float volatile NCT7712Y_Data_F = 0;

/* Function prototype declaration */
void SYS_Init(void);
extern uint32_t  NCT7712Y_Read_TSensor(void);
extern void I2C0_Init(void);
extern void NCT7712Y_init(uint8_t u8RegIndex);
extern uint32_t g_u32flag1;

void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Enable HIRC clock */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

    /* Waiting for HIRC clock ready */
    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

    /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

    /* Enable UART peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Switch UART0 clock source to HIRC */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

    /* Enable I2C0 peripheral clock */
    CLK_EnableModuleClock(I2C0_MODULE);

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

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

    /* Set I2C0 multi-function pins */
    SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB4MFP_Msk | SYS_GPB_MFPL_PB5MFP_Msk)) |
                    (SYS_GPB_MFPL_PB4MFP_I2C0_SDA | SYS_GPB_MFPL_PB5MFP_I2C0_SCL);

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
    SystemCoreClockUpdate();
}

/*---------------------------------------------------------------------------------------------------------*/
/* Main function                                                                                           */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();
    /* Init System, IP clock and multi-function I/O. */
    SYS_Init();
    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    /* Init I2C0 clock to 100 kHz */
    I2C_Open(I2C0, 100000);

    /* Init NCT7712Y configuration register */
    NCT7712Y_init(0x01);
    MCU_Temp_Data = 0;
    NCT7712Y_Data = 0;

    while (1)
    {
        /* Read NCT7712Y data */
        NCT7712Y_Data = NCT7712Y_Read_TSensor();

        if ((g_u32flag1 == 1) && (NCT7712Y_Old_Data != NCT7712Y_Data))
        {
            g_u32flag1 = 0;
            NCT7712Y_Old_Data = NCT7712Y_Data;
            NCT7712Y_Data = NCT7712Y_Data >> 4U;    // bit0 ~ bit3 are reserved

            /* Determine if the temperature is positive or negative */
            if (NCT7712Y_Data & 0x800)
            {
                /* Get NCT7712Y_Data 2'S complement */
                NCT7712Y_Data = ~NCT7712Y_Data & 0x0000FFF;
                NCT7712Y_Data +=  1;
                /* Get negative Celsius temperature value */
                NCT7712Y_Data_F = (float)(NCT7712Y_Data * -0.0625);
            }
            else
            {
                /* Get positive Celsius temperature value */
                NCT7712Y_Data_F = (float)(NCT7712Y_Data * 0.0625);
            }

            printf("Current Temperature = " "%.4f\n", NCT7712Y_Data_F);
        }
    }
}

使用特权

评论回复
板凳
xinxianshi|  楼主 | 2024-2-25 20:10 | 只看该作者
/******************************************************************************
* @file     NCT7712Y.c
* @version  V1.00
* $Revision: 2 $
* $Date: 22/09/22 11:43a $
* @brief    Measure the current temperture by thermal sensor.
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
*
*******************************************************************************/
#include <stdio.h>
#include "M030G.h"

#define OPT_NCT7712Y_RAWDATA
#define NCT7712Y_SLAVE_ADDR   0x48   // NCT7712Y ADR pin is Low
#define NCT7712Y_REG_WIDTH    0x02   // register data width 2 bytes
#define NCT7712Y_DATA_READY   BIT15  // NCT7712Y DataReady flag in bit-15 of Config. regsiter
#define NCT7712Y_REG_TEMP     0x00   // temp data in regIndex 0x00
#define NCT7712Y_REG_CONFIG   0x01   // Config. register in regIndex 0x01

/*---------------------------------------------------------------------------------------*/
/* Global variables                                                                      */
/*---------------------------------------------------------------------------------------*/
uint32_t g_u32flag1;
uint32_t NCT7712Y_Read_TSensor(void);

static uint32_t NCT7712Y_get_data(uint8_t u8RegIndex)
{
    uint32_t u32NCT7712YData;
    uint8_t u8i2cBuf[2];

    u8i2cBuf[0] = 0x00;
    u8i2cBuf[1] = 0x00;
    I2C_ReadMultiBytesOneReg(I2C0, NCT7712Y_SLAVE_ADDR, u8RegIndex, u8i2cBuf, NCT7712Y_REG_WIDTH);
    u32NCT7712YData = u8i2cBuf[0];
    u32NCT7712YData <<= 8;
    u32NCT7712YData &= 0xff00;
    u32NCT7712YData |= u8i2cBuf[1];
    return u32NCT7712YData;
}

void NCT7712Y_init(uint8_t u8RegIndex)
{
    uint8_t u8i2cBuf[2];

    u8i2cBuf[0] = 0x60;// High Byte : Converter Resolution :  0.0625 ;
    u8i2cBuf[1] = 0x80;// Low Byte  : Conversion Rate : 4Hz conversion rate;
    /*-----------------------------------------------------------------------------------------------*/
    /* bit3~0 :   Reserved                                                                           */
    /* bit4   :   Extended Mode :                                                                    */
    /*                  0: normal mode: 12 bit, -128~127.9375;                                       */
    /*                  1: extended mode: 13 bit;                                                    */
    /*                  (temperature register, high- & low-limit registers)                          */
    /* bit5   :   Alert status (read only)                                                           */
    /*                  Comparator mode (real time) status;                                          */
    /* bit7~6 :   Conversion Rate :                                                                  */
    /*                  00 : 0.25Hz conversion rate;                                                 */
    /*                  01 : 1Hz conversion rate;                                                    */
    /*                  10 : 4Hz conversion rate;                                                    */
    /*                  11 : 8Hz conversion rate;                                                    */
    /* bit8   :   Shutdown :                                                                         */
    /*                  1 indicates deep shun-down is enable;                                        */
    /* bit9   :   Mode Alert :                                                                       */
    /*                  ALERT output mode:                                                           */
    /*                  1=interrupt mode;                                                            */
    /*                  0=compare interrupt mode;                                                    */
    /* bit10  :   Polarity :                                                                         */
    /*                  The polarity bit lets the user adjust the polarity of the ALERT pin output.  */
    /*                  If the POL bit is set to 0 (default), the ALERT pin becomes active low.      */
    /*                  When POL bit is set to 1, the ALERT pin becomes active high                  */
    /*                  and the state of the ALERT pin is inverted.                                  */
    /*                  0: low active                                                                */
    /*                  1: high active                                                               */
    /* bit12~11:  Fault Queue :                                                                      */
    /* bit14~13:  Converter Resolution :                                                             */
    /*                  00: decimal point set 1 bit  (0.5'C)                                         */
    /*                  01: decimal point set 2 bits (0.25'C)                                        */
    /*                  10: decimal point set 3 bits (0.125'C)                                       */
    /*                  11: decimal point set 4 bits (0.0625'C)                                      */
    /* bit15   :  One Shot :                                                                         */
    /*                  Write 1 ADC will monitor one time                                            */
    /*                  If read'1' indicates ADC is busy converting, '0'indicates ADC is idle status.*/
    /*-----------------------------------------------------------------------------------------------*/

    I2C_WriteMultiBytesOneReg(I2C0, NCT7712Y_SLAVE_ADDR, u8RegIndex, u8i2cBuf, NCT7712Y_REG_WIDTH);
}

uint32_t NCT7712Y_Read_TSensor(void)
{
    uint32_t u32NCT7712YData;

    /* wait NCT7712Y data is ready */
    u32NCT7712YData = NCT7712Y_get_data(NCT7712Y_REG_CONFIG);

    if ((u32NCT7712YData & NCT7712Y_DATA_READY) == 0)
    {
        g_u32flag1 = 1;

        /* get NCT7712Y data */
        u32NCT7712YData = NCT7712Y_get_data(NCT7712Y_REG_TEMP);
        return u32NCT7712YData;
    }

    return 0;
}

使用特权

评论回复
地板
jiekou001| | 2024-2-26 17:17 | 只看该作者
挺不错,很好用的样子,贵不贵

使用特权

评论回复
5
捉虫天师| | 2024-2-27 22:09 | 只看该作者
这种方便挂在I2C总线上,实现阵列式测温。

使用特权

评论回复
6
捉虫天师| | 2024-2-27 22:09 | 只看该作者
节省IO,用法很好。

使用特权

评论回复
7
天灵灵地灵灵| | 2024-2-28 15:41 | 只看该作者
I2C的传感器还是挺多的。

使用特权

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

本版积分规则

79

主题

820

帖子

1

粉丝