[DemoCode下载] 使用M030G的 I2C 读取NCT7712Y 温度传感器

[复制链接]
1323|7
 楼主| xinxianshi 发表于 2024-2-25 20:10 | 显示全部楼层 |阅读模式
en-us--EC_M030G_Read_NCT7712Y_Thermal_Sensor_V1.00 (1).zip (1.75 MB, 下载次数: 0)
微控制器广泛的应用在不同的环境温度下,Nuvoton NuMicro® Cortex®–M0 M030G/M031G 系列搭配NCT7712Y 高精准度的温度传感器,以此可以随时监控当下的温度。此范例程序将说明如何使用I2C取得温度传感器的温度值。
NCT7712Y是一颗高精度的温度传感器芯片。 NCT7712Y内建一组12bit的ADC,以0.0625°C的分辨率转换监测的温度值。
2289965db2dfd8d637.png

1358665db2e074eee3.png

869465db2e1a8db2b.png

1963365db2e2788d03.png
 楼主| xinxianshi 发表于 2024-2-25 20:10 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 2 $
  5. * $Date: 20/06/11 7:12p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Measure the current temperture by thermal sensor.
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. *****************************************************************************/
  13. #include <stdio.h>
  14. #include "NuMicro.h"
  15. #include "M030G.h"

  16. /*---------------------------------------------------------------------------------------------------------*/
  17. /* Global variables                                                                                        */
  18. /*---------------------------------------------------------------------------------------------------------*/
  19. uint32_t volatile done;
  20. uint32_t volatile u32thermalData;
  21. uint32_t volatile data_refresh_flag = 0;

  22. uint32_t volatile MCU_Temp_Data = 0;
  23. uint32_t volatile NCT7712Y_Data = 0;
  24. uint32_t volatile NCT7712Y_Old_Data = 0;
  25. float volatile NCT7712Y_Data_F = 0;

  26. /* Function prototype declaration */
  27. void SYS_Init(void);
  28. extern uint32_t  NCT7712Y_Read_TSensor(void);
  29. extern void I2C0_Init(void);
  30. extern void NCT7712Y_init(uint8_t u8RegIndex);
  31. extern uint32_t g_u32flag1;

  32. void SYS_Init(void)
  33. {
  34.     /*---------------------------------------------------------------------------------------------------------*/
  35.     /* Init System Clock                                                                                       */
  36.     /*---------------------------------------------------------------------------------------------------------*/

  37.     /* Enable HIRC clock */
  38.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

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

  43.     /* Enable UART peripheral clock */
  44.     CLK_EnableModuleClock(UART0_MODULE);

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

  47.     /* Enable I2C0 peripheral clock */
  48.     CLK_EnableModuleClock(I2C0_MODULE);

  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init I/O Multi-function                                                                                 */
  51.     /*---------------------------------------------------------------------------------------------------------*/

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

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

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

  62. /*---------------------------------------------------------------------------------------------------------*/
  63. /* Main function                                                                                           */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. int main(void)
  66. {
  67.     /* Unlock protected registers */
  68.     SYS_UnlockReg();
  69.     /* Init System, IP clock and multi-function I/O. */
  70.     SYS_Init();
  71.     /* Lock protected registers */
  72.     SYS_LockReg();

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

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

  77.     /* Init NCT7712Y configuration register */
  78.     NCT7712Y_init(0x01);
  79.     MCU_Temp_Data = 0;
  80.     NCT7712Y_Data = 0;

  81.     while (1)
  82.     {
  83.         /* Read NCT7712Y data */
  84.         NCT7712Y_Data = NCT7712Y_Read_TSensor();

  85.         if ((g_u32flag1 == 1) && (NCT7712Y_Old_Data != NCT7712Y_Data))
  86.         {
  87.             g_u32flag1 = 0;
  88.             NCT7712Y_Old_Data = NCT7712Y_Data;
  89.             NCT7712Y_Data = NCT7712Y_Data >> 4U;    // bit0 ~ bit3 are reserved

  90.             /* Determine if the temperature is positive or negative */
  91.             if (NCT7712Y_Data & 0x800)
  92.             {
  93.                 /* Get NCT7712Y_Data 2'S complement */
  94.                 NCT7712Y_Data = ~NCT7712Y_Data & 0x0000FFF;
  95.                 NCT7712Y_Data +=  1;
  96.                 /* Get negative Celsius temperature value */
  97.                 NCT7712Y_Data_F = (float)(NCT7712Y_Data * -0.0625);
  98.             }
  99.             else
  100.             {
  101.                 /* Get positive Celsius temperature value */
  102.                 NCT7712Y_Data_F = (float)(NCT7712Y_Data * 0.0625);
  103.             }

  104.             printf("Current Temperature = " "%.4f\n", NCT7712Y_Data_F);
  105.         }
  106.     }
  107. }
 楼主| xinxianshi 发表于 2024-2-25 20:10 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     NCT7712Y.c
  3. * @version  V1.00
  4. * $Revision: 2 $
  5. * $Date: 22/09/22 11:43a $
  6. * @brief    Measure the current temperture by thermal sensor.
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. *******************************************************************************/
  13. #include <stdio.h>
  14. #include "M030G.h"

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

  21. /*---------------------------------------------------------------------------------------*/
  22. /* Global variables                                                                      */
  23. /*---------------------------------------------------------------------------------------*/
  24. uint32_t g_u32flag1;
  25. uint32_t NCT7712Y_Read_TSensor(void);

  26. static uint32_t NCT7712Y_get_data(uint8_t u8RegIndex)
  27. {
  28.     uint32_t u32NCT7712YData;
  29.     uint8_t u8i2cBuf[2];

  30.     u8i2cBuf[0] = 0x00;
  31.     u8i2cBuf[1] = 0x00;
  32.     I2C_ReadMultiBytesOneReg(I2C0, NCT7712Y_SLAVE_ADDR, u8RegIndex, u8i2cBuf, NCT7712Y_REG_WIDTH);
  33.     u32NCT7712YData = u8i2cBuf[0];
  34.     u32NCT7712YData <<= 8;
  35.     u32NCT7712YData &= 0xff00;
  36.     u32NCT7712YData |= u8i2cBuf[1];
  37.     return u32NCT7712YData;
  38. }

  39. void NCT7712Y_init(uint8_t u8RegIndex)
  40. {
  41.     uint8_t u8i2cBuf[2];

  42.     u8i2cBuf[0] = 0x60;// High Byte : Converter Resolution :  0.0625 ;
  43.     u8i2cBuf[1] = 0x80;// Low Byte  : Conversion Rate : 4Hz conversion rate;
  44.     /*-----------------------------------------------------------------------------------------------*/
  45.     /* bit3~0 :   Reserved                                                                           */
  46.     /* bit4   :   Extended Mode :                                                                    */
  47.     /*                  0: normal mode: 12 bit, -128~127.9375;                                       */
  48.     /*                  1: extended mode: 13 bit;                                                    */
  49.     /*                  (temperature register, high- & low-limit registers)                          */
  50.     /* bit5   :   Alert status (read only)                                                           */
  51.     /*                  Comparator mode (real time) status;                                          */
  52.     /* bit7~6 :   Conversion Rate :                                                                  */
  53.     /*                  00 : 0.25Hz conversion rate;                                                 */
  54.     /*                  01 : 1Hz conversion rate;                                                    */
  55.     /*                  10 : 4Hz conversion rate;                                                    */
  56.     /*                  11 : 8Hz conversion rate;                                                    */
  57.     /* bit8   :   Shutdown :                                                                         */
  58.     /*                  1 indicates deep shun-down is enable;                                        */
  59.     /* bit9   :   Mode Alert :                                                                       */
  60.     /*                  ALERT output mode:                                                           */
  61.     /*                  1=interrupt mode;                                                            */
  62.     /*                  0=compare interrupt mode;                                                    */
  63.     /* bit10  :   Polarity :                                                                         */
  64.     /*                  The polarity bit lets the user adjust the polarity of the ALERT pin output.  */
  65.     /*                  If the POL bit is set to 0 (default), the ALERT pin becomes active low.      */
  66.     /*                  When POL bit is set to 1, the ALERT pin becomes active high                  */
  67.     /*                  and the state of the ALERT pin is inverted.                                  */
  68.     /*                  0: low active                                                                */
  69.     /*                  1: high active                                                               */
  70.     /* bit12~11:  Fault Queue :                                                                      */
  71.     /* bit14~13:  Converter Resolution :                                                             */
  72.     /*                  00: decimal point set 1 bit  (0.5'C)                                         */
  73.     /*                  01: decimal point set 2 bits (0.25'C)                                        */
  74.     /*                  10: decimal point set 3 bits (0.125'C)                                       */
  75.     /*                  11: decimal point set 4 bits (0.0625'C)                                      */
  76.     /* bit15   :  One Shot :                                                                         */
  77.     /*                  Write 1 ADC will monitor one time                                            */
  78.     /*                  If read'1' indicates ADC is busy converting, '0'indicates ADC is idle status.*/
  79.     /*-----------------------------------------------------------------------------------------------*/

  80.     I2C_WriteMultiBytesOneReg(I2C0, NCT7712Y_SLAVE_ADDR, u8RegIndex, u8i2cBuf, NCT7712Y_REG_WIDTH);
  81. }

  82. uint32_t NCT7712Y_Read_TSensor(void)
  83. {
  84.     uint32_t u32NCT7712YData;

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

  87.     if ((u32NCT7712YData & NCT7712Y_DATA_READY) == 0)
  88.     {
  89.         g_u32flag1 = 1;

  90.         /* get NCT7712Y data */
  91.         u32NCT7712YData = NCT7712Y_get_data(NCT7712Y_REG_TEMP);
  92.         return u32NCT7712YData;
  93.     }

  94.     return 0;
  95. }

jiekou001 发表于 2024-2-26 17:17 | 显示全部楼层
挺不错,很好用的样子,贵不贵
捉虫天师 发表于 2024-2-27 22:09 | 显示全部楼层
这种方便挂在I2C总线上,实现阵列式测温。
捉虫天师 发表于 2024-2-27 22:09 | 显示全部楼层
节省IO,用法很好。
天灵灵地灵灵 发表于 2024-2-28 15:41 | 显示全部楼层
I2C的传感器还是挺多的。
夜晚有三年 发表于 2025-9-11 14:06 | 显示全部楼层
包含 I2C 库,初始化 I2C,发送 NCT7712Y 地址 (0x48) 和寄存器,读取两字节数据,转换为温度值。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1019

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部