- /******************************************************************************
- * @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;
- }
|