AD7190_LoRa
Dependencies: mbed
Revision 0:0e20215b178e, committed 2020-08-06
- Comitter:
- peng103617
- Date:
- Thu Aug 06 06:52:19 2020 +0000
- Commit message:
- AD7190_LoRa
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/AD7190.cpp Thu Aug 06 06:52:19 2020 +0000
@@ -0,0 +1,319 @@
+/***************************************************************************//**
+ * @file AD7190.c
+ * @brief Implementation of AD7190 Driver.
+ * @author DNechita (Dan.Nechita@analog.com)
+********************************************************************************
+ * Copyright 2012(c) Analog Devices, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Analog Devices, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * - The use of this software may or may not infringe the patent rights
+ * of one or more patent holders. This license does not release you
+ * from the requirement that you obtain separate licenses from these
+ * patent holders to use this software.
+ * - Use of the software either in source or binary form, must be run
+ * on or directly connected to an Analog Devices Inc. component.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+********************************************************************************
+ * SVN Revision: 903
+*******************************************************************************/
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+#include "AD7190.h" // AD7190 definitions.
+#include "TIME_AD.h" // TIME definitions.
+
+/***************************************************************************//**
+ * @brief Writes data into a register.
+ *
+ * @param registerAddress - Address of the register.
+ * @param registerValue - Data value to write.
+ * @param bytesNumber - Number of bytes to be written.
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_SetRegisterValue(unsigned char registerAddress,
+ unsigned long registerValue,
+ unsigned char bytesNumber)
+{
+ unsigned char writeCommand[5] = {0, 0, 0, 0, 0};
+ unsigned char* dataPointer = (unsigned char*)®isterValue;
+ unsigned char bytesNr = bytesNumber;
+
+ writeCommand[0] = AD7190_COMM_WRITE |
+ AD7190_COMM_ADDR(registerAddress);
+ while(bytesNr > 0)
+ {
+ writeCommand[bytesNr] = *dataPointer;
+ dataPointer ++;
+ bytesNr --;
+ }
+ SPI_Write(writeCommand, bytesNumber + 1);
+}
+
+/***************************************************************************//**
+ * @brief Reads the value of a register.
+ *
+ * @param registerAddress - Address of the register.
+ * @param bytesNumber - Number of bytes that will be read.
+ *
+ * @return buffer - Value of the register.
+*******************************************************************************/
+unsigned long AD7190_GetRegisterValue(unsigned char registerAddress,
+ unsigned char bytesNumber)
+{
+ cs.write(0);
+ unsigned char registerWord[5] = {0, 0, 0, 0, 0};
+ unsigned long buffer = 0x0;
+ unsigned char i = 0;
+
+ registerWord[0] = AD7190_COMM_READ |
+ AD7190_COMM_ADDR(registerAddress);
+ SPI_Read(registerWord, bytesNumber + 1);
+ for(i = 1; i < bytesNumber + 1; i++)
+ {
+ buffer = (buffer << 8) + registerWord[i];
+ }
+ //cs.write(1);
+ return buffer;
+}
+
+/***************************************************************************//**
+ * @brief Checks if the AD7190 part is present.
+ *
+ * @return status - Indicates if the part is present or not. 1 for ok , 0 for error
+*******************************************************************************/
+unsigned char AD7190_Init(unsigned int speed)
+{
+ unsigned char status = 1;
+ unsigned char regVal = 0;
+
+
+ SPI_Init(0, (unsigned long) 1000*speed, 1, 0);
+
+ AD7190_Reset();
+ /* Allow at least 500 us before accessing any of the on-chip registers. */
+ TIME_DelayMs(1);
+ regVal = AD7190_GetRegisterValue(AD7190_REG_ID, 1);
+ if( (regVal & AD7190_ID_MASK) != ID_AD7190)
+ {
+ status = 0;
+ }
+ return status ;
+}
+
+/***************************************************************************//**
+ * @brief Resets the device.
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_Reset(void)
+{
+ unsigned char registerWord[7];
+
+ registerWord[0] = 0x01;
+ registerWord[1] = 0xFF;
+ registerWord[2] = 0xFF;
+ registerWord[3] = 0xFF;
+ registerWord[4] = 0xFF;
+ registerWord[5] = 0xFF;
+ registerWord[6] = 0xFF;
+ cs.write(1);
+ SPI_Write(registerWord, 7);
+ cs.write(0);
+ AD7190_WaitRdyGoLow();
+}
+
+/***************************************************************************//**
+ * @brief Set device to idle or power-down.
+ *
+ * @param pwrMode - Selects idle mode or power-down mode.
+ * Example: 0 - power-down
+ * 1 - idle
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_SetPower(unsigned char pwrMode)
+{
+ unsigned long oldPwrMode = 0x0;
+ unsigned long newPwrMode = 0x0;
+
+ oldPwrMode = AD7190_GetRegisterValue(AD7190_REG_MODE, 3);
+ oldPwrMode &= ~(AD7190_MODE_SEL(0x7));
+ newPwrMode = oldPwrMode |
+ AD7190_MODE_SEL((pwrMode * (AD7190_MODE_IDLE)) |
+ (!pwrMode * (AD7190_MODE_PWRDN)));
+ AD7190_SetRegisterValue(AD7190_REG_MODE, newPwrMode, 3);
+}
+
+/***************************************************************************//**
+ * @brief Waits for RDY pin to go low.
+ * @comment timeOutCnt related to CPU clock
+ * @return none.
+*******************************************************************************/
+void AD7190_WaitRdyGoLow(void)
+{
+ unsigned long timeOutCnt = 0x4FFFF;
+
+ while(ad_rdy.read()==1 && timeOutCnt--);
+ //wait_us(500);
+}
+
+/***************************************************************************//**
+ * @brief Selects the channel to be enabled.
+ *
+ * @param channel - Selects a channel.
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_ChannelSelect(unsigned short channel)
+{
+ unsigned long oldRegValue = 0x0;
+ unsigned long newRegValue = 0x0;
+
+ oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF, 3);
+ oldRegValue &= ~(AD7190_CONF_CHAN(0xFF));
+ newRegValue = oldRegValue | AD7190_CONF_CHAN(1 << channel);
+ AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3);
+}
+
+/***************************************************************************//**
+ * @brief Performs the given calibration to the specified channel.
+ *
+ * @param mode - Calibration type.
+ * @param channel - Channel to be calibrated.
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_Calibrate(unsigned char mode, unsigned char channel)
+{
+ unsigned long oldRegValue = 0x0;
+ unsigned long newRegValue = 0x0;
+
+ AD7190_ChannelSelect(channel);
+ oldRegValue = AD7190_GetRegisterValue(AD7190_REG_MODE, 3);
+ oldRegValue &= ~AD7190_MODE_SEL(0x7);
+ newRegValue = oldRegValue | AD7190_MODE_SEL(mode);
+ cs.write(0); //ADI_PART_CS_LOW;
+ AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3); // CS is not modified.
+ AD7190_WaitRdyGoLow();
+ cs.write(1); //ADI_PART_CS_HIGH;
+}
+
+/***************************************************************************//**
+ * @brief Selects the polarity of the conversion and the ADC input range.
+ *
+ * @param polarity - Polarity select bit.
+ Example: 0 - bipolar operation is selected.
+ 1 - unipolar operation is selected.
+* @param range - Gain select bits. These bits are written by the user to select
+ the ADC input range.
+ *
+ * @return none.
+*******************************************************************************/
+void AD7190_RangeSetup(unsigned char polarity, unsigned char range)
+{
+ unsigned long oldRegValue = 0x0;
+ unsigned long newRegValue = 0x0;
+
+ oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF,3);
+ oldRegValue &= ~(AD7190_CONF_UNIPOLAR |
+ AD7190_CONF_GAIN(0x7));
+ newRegValue = oldRegValue |
+ (polarity * AD7190_CONF_UNIPOLAR) |
+ AD7190_CONF_GAIN(range);
+ AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3);
+}
+
+/***************************************************************************//**
+ * @brief Returns the result of a single conversion.
+ *
+ * @return regData - Result of a single analog-to-digital conversion.
+*******************************************************************************/
+unsigned long AD7190_SingleConversion(void)
+{
+ unsigned long command = 0x0;
+ unsigned long regData = 0x0;
+
+ command = AD7190_MODE_SEL(AD7190_MODE_SINGLE) |
+ AD7190_MODE_CLKSRC(AD7190_CLK_INT) |
+ AD7190_MODE_RATE(0x060);
+ cs.write(0); //ADI_PART_CS_LOW;
+ AD7190_SetRegisterValue(AD7190_REG_MODE, command, 3); // CS is not modified.
+ AD7190_WaitRdyGoLow();
+ //wait_us(500);
+ regData = AD7190_GetRegisterValue(AD7190_REG_DATA, 3);
+ cs.write(1); //ADI_PART_CS_HIGH;
+
+ return regData;
+}
+
+/***************************************************************************//**
+ * @brief Returns the average of several conversion results.
+ *
+ * @return samplesAverage - The average of the conversion results.
+*******************************************************************************/
+unsigned long AD7190_ContinuousReadAvg(unsigned char sampleNumber)
+{
+ unsigned long samplesAverage = 0x0;
+ unsigned char count = 0x0;
+ unsigned long command = 0x0;
+
+ command = AD7190_MODE_SEL(AD7190_MODE_CONT) |
+ AD7190_MODE_CLKSRC(AD7190_CLK_INT) |
+ AD7190_MODE_RATE(0x060);
+ cs.write(0); //ADI_PART_CS_LOW;
+ AD7190_SetRegisterValue(AD7190_REG_MODE, command, 3); // CS is not modified.
+ for(count = 0;count < sampleNumber;count ++)
+ {
+ AD7190_WaitRdyGoLow();
+ samplesAverage += AD7190_GetRegisterValue(AD7190_REG_DATA, 3); // CS is not modified.
+ }
+ cs.write(1); //ADI_PART_CS_HIGH;
+ samplesAverage = samplesAverage / sampleNumber;
+
+ return samplesAverage ;
+}
+
+/***************************************************************************//**
+ * @brief Read data from temperature sensor and converts it to Celsius degrees.
+ *
+ * @return temperature - Celsius degrees.
+*******************************************************************************/
+unsigned long AD7190_TemperatureRead(void)
+{
+ unsigned char temperature = 0x0;
+ unsigned long dataReg = 0x0;
+ AD7190_RangeSetup(0, AD7190_CONF_GAIN_1);
+ AD7190_ChannelSelect(AD7190_CH_TEMP_SENSOR);
+ dataReg = AD7190_SingleConversion();
+ dataReg -= 0x800000;
+ dataReg /= 2815; // Kelvin Temperature
+ dataReg -= 273; //Celsius Temperature
+ temperature = (unsigned long) dataReg;
+
+ return temperature;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AD7190.h Thu Aug 06 06:52:19 2020 +0000 @@ -0,0 +1,284 @@ + +/***************************************************************************//** + * @file AD7190.h + * @brief Header file of AD7190 Driver. + * @author DNechita (Dan.Nechita@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * +******************************************************************************** + * SVN Revision: 903 +*******************************************************************************/ + +#ifndef __AD7190_H__ +#define __AD7190_H__ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + + + +#include "Communication.h" +#include "mbed.h" + + +extern DigitalOut cs; +extern DigitalIn ad_rdy; +/******************************************************************************/ +/******************************** AD7190 **************************************/ +/******************************************************************************/ + + +/* AD7190 Register Map */ +#define AD7190_REG_COMM 0 // Communications Register (WO, 8-bit) +#define AD7190_REG_STAT 0 // Status Register (RO, 8-bit) +#define AD7190_REG_MODE 1 // Mode Register (RW, 24-bit +#define AD7190_REG_CONF 2 // Configuration Register (RW, 24-bit) +#define AD7190_REG_DATA 3 // Data Register (RO, 24/32-bit) +#define AD7190_REG_ID 4 // ID Register (RO, 8-bit) +#define AD7190_REG_GPOCON 5 // GPOCON Register (RW, 8-bit) +#define AD7190_REG_OFFSET 6 // Offset Register (RW, 24-bit +#define AD7190_REG_FULLSCALE 7 // Full-Scale Register (RW, 24-bit) + +/* Communications Register Bit Designations (AD7190_REG_COMM) */ +#define AD7190_COMM_WEN (1 << 7) // Write Enable. +#define AD7190_COMM_WRITE (0 << 6) // Write Operation. +#define AD7190_COMM_READ (1 << 6) // Read Operation. +#define AD7190_COMM_ADDR(x) (((x) & 0x7) << 3) // Register Address. +#define AD7190_COMM_CREAD (1 << 2) // Continuous Read of Data Register. + +/* Status Register Bit Designations (AD7190_REG_STAT) */ +#define AD7190_STAT_RDY (1 << 7) // Ready. +#define AD7190_STAT_ERR (1 << 6) // ADC error bit. +#define AD7190_STAT_NOREF (1 << 5) // Error no external reference. +#define AD7190_STAT_PARITY (1 << 4) // Parity check of the data register. +#define AD7190_STAT_CH2 (1 << 2) // Channel 2. +#define AD7190_STAT_CH1 (1 << 1) // Channel 1. +#define AD7190_STAT_CH0 (1 << 0) // Channel 0. + +/* Mode Register Bit Designations (AD7190_REG_MODE) */ +#define AD7190_MODE_SEL(x) (((x) & 0x7) << 21) // Operation Mode Select. +#define AD7190_MODE_DAT_STA (1 << 20) // Status Register transmission. +#define AD7190_MODE_CLKSRC(x) (((x) & 0x3) << 18) // Clock Source Select. +#define AD7190_MODE_SINC3 (1 << 15) // SINC3 Filter Select. +#define AD7190_MODE_ENPAR (1 << 13) // Parity Enable. +#define AD7190_MODE_SCYCLE (1 << 11) // Single cycle conversion. +#define AD7190_MODE_REJ60 (1 << 10) // 50/60Hz notch filter. +#define AD7190_MODE_RATE(x) ((x) & 0x3FF) // Filter Update Rate Select. + +/* Mode Register: AD7190_MODE_SEL(x) options */ +#define AD7190_MODE_CONT 0 // Continuous Conversion Mode. +#define AD7190_MODE_SINGLE 1 // Single Conversion Mode. +#define AD7190_MODE_IDLE 2 // Idle Mode. +#define AD7190_MODE_PWRDN 3 // Power-Down Mode. +#define AD7190_MODE_CAL_INT_ZERO 4 // Internal Zero-Scale Calibration. +#define AD7190_MODE_CAL_INT_FULL 5 // Internal Full-Scale Calibration. +#define AD7190_MODE_CAL_SYS_ZERO 6 // System Zero-Scale Calibration. +#define AD7190_MODE_CAL_SYS_FULL 7 // System Full-Scale Calibration. + +/* Mode Register: AD7190_MODE_CLKSRC(x) options */ +#define AD7190_CLK_EXT_MCLK1_2 0 // External crystal. The external crystal + // is connected from MCLK1 to MCLK2. +#define AD7190_CLK_EXT_MCLK2 1 // External Clock applied to MCLK2 +#define AD7190_CLK_INT 2 // Internal 4.92 MHz clock. + // Pin MCLK2 is tristated. +#define AD7190_CLK_INT_CO 3 // Internal 4.92 MHz clock. The internal + // clock is available on MCLK2. + +/* Configuration Register Bit Designations (AD7190_REG_CONF) */ +#define AD7190_CONF_CHOP (1 << 23) // CHOP enable. +#define AD7190_CONF_REFSEL (1 << 20) // REFIN1/REFIN2 Reference Select. +#define AD7190_CONF_CHAN(x) (((x) & 0xFF) << 8) // Channel select. +#define AD7190_CONF_BURN (1 << 7) // Burnout current enable. +#define AD7190_CONF_REFDET (1 << 6) // Reference detect enable. +#define AD7190_CONF_BUF (1 << 4) // Buffered Mode Enable. +#define AD7190_CONF_UNIPOLAR (1 << 3) // Unipolar/Bipolar Enable. +#define AD7190_CONF_GAIN(x) ((x) & 0x7) // Gain Select. + +/* Configuration Register: AD7190_CONF_CHAN(x) options */ +#define AD7190_CH_AIN1P_AIN2M 0 // AIN1(+) - AIN2(-) +#define AD7190_CH_AIN3P_AIN4M 1 // AIN3(+) - AIN4(-) +#define AD7190_CH_TEMP_SENSOR 2 // Temperature sensor +#define AD7190_CH_AIN2P_AIN2M 3 // AIN2(+) - AIN2(-) +#define AD7190_CH_AIN1P_AINCOM 4 // AIN1(+) - AINCOM +#define AD7190_CH_AIN2P_AINCOM 5 // AIN2(+) - AINCOM +#define AD7190_CH_AIN3P_AINCOM 6 // AIN3(+) - AINCOM +#define AD7190_CH_AIN4P_AINCOM 7 // AIN4(+) - AINCOM + +/* Configuration Register: AD7190_CONF_GAIN(x) options */ +// ADC Input Range (5 V Reference) +#define AD7190_CONF_GAIN_1 0 // Gain 1 +-5 V +#define AD7190_CONF_GAIN_8 3 // Gain 8 +-625 mV +#define AD7190_CONF_GAIN_16 4 // Gain 16 +-312.5 mV +#define AD7190_CONF_GAIN_32 5 // Gain 32 +-156.2 mV +#define AD7190_CONF_GAIN_64 6 // Gain 64 +-78.125 mV +#define AD7190_CONF_GAIN_128 7 // Gain 128 +-39.06 mV + +/* ID Register Bit Designations (AD7190_REG_ID) */ +#define ID_AD7190 0x4 +#define AD7190_ID_MASK 0x0F + +/* GPOCON Register Bit Designations (AD7190_REG_GPOCON) */ +#define AD7190_GPOCON_BPDSW (1 << 6) // Bridge power-down switch enable +#define AD7190_GPOCON_GP32EN (1 << 5) // Digital Output P3 and P2 enable +#define AD7190_GPOCON_GP10EN (1 << 4) // Digital Output P1 and P0 enable +#define AD7190_GPOCON_P3DAT (1 << 3) // P3 state +#define AD7190_GPOCON_P2DAT (1 << 2) // P2 state +#define AD7190_GPOCON_P1DAT (1 << 1) // P1 state +#define AD7190_GPOCON_P0DAT (1 << 0) // P0 state + + + +/******************************************************************************/ +/*********************** Functions Declarations *******************************/ +/******************************************************************************/ +/* Read-buffer in order to use mbed-api */ + + +/***************************************************************************//** + * @brief Writes data into a register. + * + * @param registerAddress - Address of the register. + * @param registerValue - Data value to write. + * @param bytesNumber - Number of bytes to be written. + * + * @return none. +*******************************************************************************/ +void AD7190_SetRegisterValue(unsigned char registerAddress, + unsigned long registerValue, + unsigned char bytesNumber); + +/***************************************************************************//** + * @brief Reads the value of a register. + * + * @param registerAddress - Address of the register. + * @param bytesNumber - Number of bytes that will be read. + * + * @return buffer - Value of the register. +*******************************************************************************/ +unsigned long AD7190_GetRegisterValue(unsigned char registerAddress, + unsigned char bytesNumber); + +/***************************************************************************//** + * @brief Checks if the AD7190 part is present. + * + * @return status - Indicates if the part is present or not. 1 for ok , 0 for error +*******************************************************************************/ +unsigned char AD7190_Init(unsigned int speed); + + +/***************************************************************************//** + * @brief Resets the device. + * + * @return none. +*******************************************************************************/ +void AD7190_Reset(void); + +/***************************************************************************//** + * @brief Set device to idle or power-down. + * + * @param pwrMode - Selects idle mode or power-down mode. + * Example: 0 - power-down + * 1 - idle + * + * @return none. +*******************************************************************************/ +void AD7190_SetPower(unsigned char pwrMode); + + +/***************************************************************************//** + * @brief Waits for RDY pin to go low. + * @comment timeOutCnt related to CPU clock + * @return none. +*******************************************************************************/ +void AD7190_WaitRdyGoLow(void); + +/***************************************************************************//** + * @brief Selects the channel to be enabled. + * + * @param channel - Selects a channel. + * + * @return none. +*******************************************************************************/ +void AD7190_ChannelSelect(unsigned short channel); + + +/***************************************************************************//** + * @brief Performs the given calibration to the specified channel. + * + * @param mode - Calibration type. + * @param channel - Channel to be calibrated. + * + * @return none. +*******************************************************************************/ +void AD7190_Calibrate(unsigned char mode, unsigned char channel); + +/***************************************************************************//** + * @brief Selects the polarity of the conversion and the ADC input range. + * + * @param polarity - Polarity select bit. + Example: 0 - bipolar operation is selected. + 1 - unipolar operation is selected. + * @param range - Gain select bits. These bits are written by the user to select + the ADC input range. + * + * @return none. +*******************************************************************************/ +void AD7190_RangeSetup(unsigned char polarity, unsigned char range); + +/***************************************************************************//** + * @brief Returns the result of a single conversion. + * + * @return regData - Result of a single analog-to-digital conversion. +*******************************************************************************/ +unsigned long AD7190_SingleConversion(void); + +/***************************************************************************//** + * @brief Returns the average of several conversion results. + * + * @return samplesAverage - The average of the conversion results. +*******************************************************************************/ +unsigned long AD7190_ContinuousReadAvg(unsigned char sampleNumber); + +/***************************************************************************//** + * @brief Read data from temperature sensor and converts it to Celsius degrees. + * + * @return temperature - Celsius degrees. +*******************************************************************************/ +unsigned long AD7190_TemperatureRead(void); + + +extern Serial pc; +#endif /* __AD7190_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Communication.cpp Thu Aug 06 06:52:19 2020 +0000
@@ -0,0 +1,175 @@
+/***************************************************************************//**
+ * @file Communication.c
+ * @brief Implementation of Communication Driver.
+ * @author DBogdan (dragos.bogdan@analog.com)
+********************************************************************************
+ * Copyright 2012-2015(c) Analog Devices, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Analog Devices, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * - The use of this software may or may not infringe the patent rights
+ * of one or more patent holders. This license does not release you
+ * from the requirement that you obtain separate licenses from these
+ * patent holders to use this software.
+ * - Use of the software either in source or binary form, must be run
+ * on or directly connected to an Analog Devices Inc. component.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+*******************************************************************************/
+
+/******************************************************************************/
+/* Include Files */
+/******************************************************************************/
+#include "Communication.h"
+#include "stdlib.h"
+#include "mbed.h"
+/***************************************************************************//**
+ * @brief Initializes the SPI communication peripheral.
+ *
+ * @param lsbFirst - Transfer format (0 or 1).
+ * Example: 0x0 - MSB first.
+ * 0x1 - LSB first.
+ * @param clockFreq - SPI clock frequency (Hz).
+ * Example: 1000 - SPI clock frequency is 1 kHz.
+ * @param clockPol - SPI clock polarity (0 or 1).
+ * Example: 0x0 - Idle state for clock is a low level; active
+ * state is a high level;
+ * 0x1 - Idle state for clock is a high level; active
+ * state is a low level.
+ * @param clockEdg - SPI clock edge (0 or 1).
+ * Example: 0x0 - Serial output data changes on transition
+ * from idle clock state to active clock state;
+ * 0x1 - Serial output data changes on transition
+ * from active clock state to idle clock state.
+ *
+ * @return status - Result of the initialization procedure.
+ * Example: 1 - if initialization was successful;
+ * 0 - if initialization was unsuccessful.
+*******************************************************************************/
+#define PRINT_SPI_INFO
+
+
+unsigned char read_buff[AD7190_READ_BUFFER_SIZE];
+
+
+unsigned char SPI_Init(unsigned char lsbFirst,
+ unsigned long clockFreq,
+ unsigned char clockPol,
+ unsigned char clockEdg)
+{
+ /* Add your code here. */
+ spi.format(8,3);
+ spi.frequency(clockFreq);
+ memset(read_buff, 0, AD7190_READ_BUFFER_SIZE);
+ return 1;
+}
+
+/***************************************************************************//**
+ * @brief Reads data from SPI.
+ *
+ * @param data - Data represents the write buffer as an input parameter and the
+ * read buffer as an output parameter.
+ * @param bytesNumber - Number of bytes to read.
+ *
+ * @return Number of read bytes.
+*******************************************************************************/
+unsigned char SPI_Read(unsigned char* data,
+ unsigned char bytesNumber)
+{
+ /* Add your code here. */
+ int r_cnt = 0;
+
+ if(bytesNumber > AD7190_READ_BUFFER_SIZE) return 0;
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("[SPI-OTR] 0x%02x\r\n", data[0]);
+ #endif
+
+ data[r_cnt] = spi.write(data[0]);
+ bytesNumber --;
+ r_cnt++;
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("[SPI-IN ] ");
+ #endif
+ while(bytesNumber > 0){
+ data[r_cnt] = spi.write(0xFF);
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("0x%02x\t", data[r_cnt]);
+ #endif
+
+ r_cnt++;
+ bytesNumber --;
+ }
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("\r\n");
+ #endif
+
+ return r_cnt;
+}
+
+/***************************************************************************//**
+ * @brief Writes data to SPI.
+ *
+ * @param data - Data represents the write buffer.
+ * @param bytesNumber - Number of bytes to write.
+ *
+ * @return Number of written bytes.
+*******************************************************************************/
+unsigned char SPI_Write(unsigned char* data,
+ unsigned char bytesNumber)
+{
+ /* Add your code here. */
+ memset(read_buff, 0, AD7190_READ_BUFFER_SIZE);
+ int w_cnt = 0;
+
+ if(bytesNumber > AD7190_READ_BUFFER_SIZE) return 0;
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("[SPI-OUT] ");
+ #endif
+
+ while(bytesNumber > 0){
+ read_buff[w_cnt] = spi.write(data[w_cnt]);
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("0x%02x\t", data[w_cnt]);
+ #endif
+
+ w_cnt++;
+ bytesNumber --;
+ }
+
+ #ifdef PRINT_SPI_INFO
+ // pc.printf("\r\n[SPI-RTN] ");
+ for(int i=0; i<w_cnt; i++){
+ // pc.printf("0x%02x\t", read_buff[w_cnt]);
+ }
+ // pc.printf("\r\n");
+ #endif
+
+ return w_cnt;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Communication.h Thu Aug 06 06:52:19 2020 +0000 @@ -0,0 +1,77 @@ +/***************************************************************************//** + * @file Communication.h + * @brief Header file of Communication Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2012-2015(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * +*******************************************************************************/ +#ifndef _COMMUNICATION_H_ +#define _COMMUNICATION_H_ + +/******************************************************************************/ +/* Include Files */ +/******************************************************************************/ +#include "mbed.h" +/******************************************************************************/ +/* Functions Prototypes */ +/******************************************************************************/ + +#define AD7190_READ_BUFFER_SIZE 16 + +extern SPI spi; // mosi, miso, sclk +extern DigitalOut cs; +extern Serial pc; + +/*! Initializes the SPI communication peripheral. */ +unsigned char SPI_Init(unsigned char lsbFirst, + unsigned long clockFreq, + unsigned char clockPol, + unsigned char clockEdg); + +/*! Initializes the SPI communication peripheral. */ +unsigned char SPI_Init(unsigned char lsbFirst, + unsigned long clockFreq, + unsigned char clockPol, + unsigned char clockEdg); + +/*! Reads data from SPI. */ +unsigned char SPI_Read(unsigned char* data, + unsigned char bytesNumber); + +/*! Writes data to SPI. */ +unsigned char SPI_Write(unsigned char* data, + unsigned char bytesNumber); + +#endif /* _COMMUNICATION_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TIME_AD.cpp Thu Aug 06 06:52:19 2020 +0000
@@ -0,0 +1,110 @@
+/***************************************************************************//**
+ * @file TIME_AD.c
+ * @brief Implementation of TIME Driver.
+ * @author Dan Nechita
+********************************************************************************
+ * Copyright 2012(c) Analog Devices, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Analog Devices, Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * - The use of this software may or may not infringe the patent rights
+ * of one or more patent holders. This license does not release you
+ * from the requirement that you obtain separate licenses from these
+ * patent holders to use this software.
+ * - Use of the software either in source or binary form, must be run
+ * on or directly connected to an Analog Devices Inc. component.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+*******************************************************************************/
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+#include "TIME_AD.h"
+#include "mbed.h"
+/******************************************************************************/
+/************************ Variables Declarations ******************************/
+/******************************************************************************/
+
+
+/******************************************************************************/
+/************************ Functions Definitions *******************************/
+/******************************************************************************/
+
+/***************************************************************************//**
+ * @brief Initializes the timer used in this driver.
+ *
+ * @return status - Result of the initialization procedure.
+ * Example: 1 - if initialization was successful;
+ * 0 - if initialization was unsuccessful.
+*******************************************************************************/
+unsigned char TIME_Init(void)
+{
+ // Add your code here.
+ return 1;
+}
+
+/***************************************************************************//**
+ * @brief The timer begins to count in steps of microseconds(us) until the user
+ * calls a stop measurement function.
+ *
+ * @return None.
+*******************************************************************************/
+void TIME_StartMeasure(void)
+{
+ // Add your code here.
+}
+
+/***************************************************************************//**
+ * @brief Stops the measurement process when the functions is called.
+ *
+ * @return Time(in microseconds) elapsed since the measurement began.
+*******************************************************************************/
+unsigned long TIME_StopMeasure(void)
+{
+ // Add your code here.
+ return 0;
+}
+
+/***************************************************************************//**
+ * @brief Creates a delay of microseconds.
+ *
+ * @return None.
+*******************************************************************************/
+void TIME_DelayUs(unsigned short usUnits)
+{
+ // Add your code here.
+ wait_us(usUnits);
+}
+
+/***************************************************************************//**
+ * @brief Creates a delay of milliseconds.
+ *
+ * @return None.
+*******************************************************************************/
+void TIME_DelayMs(unsigned short msUnits)
+{
+ // Add your code here.
+ wait_ms(msUnits);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TIME_AD.h Thu Aug 06 06:52:19 2020 +0000 @@ -0,0 +1,62 @@ +/***************************************************************************//** + * @file TIME_AD.h + * @brief Header file of TIME Driver. + * @author DNechita (Dan.Nechita@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * +*******************************************************************************/ +#ifndef __TIME_AD_H__ +#define __TIME_AD_H__ +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/*! Initializes the timer used in this driver. */ +unsigned char TIME_Init(void); + +/*! The timer begins to count in steps of microseconds(us) until the user calls a +stop measurement function. */ +void TIME_StartMeasure(void); + +/*! Stops the measurement process when the functions is called. */ +unsigned long TIME_StopMeasure(void); + +/*! Creates a delay of microseconds. */ +void TIME_DelayUs(unsigned short usUnits); + +/*! Creates a delay of milliseconds. */ +void TIME_DelayMs(unsigned short msUnits); + +#endif // __TIME_AD_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Aug 06 06:52:19 2020 +0000
@@ -0,0 +1,81 @@
+/* -- MBED - AD7190
+
+Porting by Rododo Science. .... 2019/9/13
+
+-- */
+
+
+#include "mbed.h"
+#include "AD7190.h"
+SPI spi(D11, D12, D13); // mosi, miso, sclk
+DigitalOut cs(D10); // Different CS operations
+DigitalIn ad_rdy(D9); // Use of a _RDY Pin
+
+Serial uart(PC_4, PC_5);//TX4,RX4
+int idx=0;
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+void setup()
+{
+ cs.write(1);
+ wait(1);
+ pc.printf("Example Start\n");
+ cs.write(0);
+ while(AD7190_Init(800)==0){
+ pc.printf("Can't allocate AD7190\n");
+ wait(5);
+ }
+
+ /* Calibrates channel AIN3(+) - AIN4(-). */
+ AD7190_Calibrate(AD7190_MODE_CAL_INT_ZERO, AD7190_CH_AIN3P_AIN4M);
+
+}
+
+void loop()
+{
+
+ unsigned long val = AD7190_TemperatureRead();
+ //pc.printf("Temperature = \t =%lu \r\n", val);
+
+ AD7190_ChannelSelect(AD7190_CH_AIN1P_AIN2M);
+ /* Selects unipolar operation and ADC's input range to +-Vref/1. plesase refer AD7190_CONF_GAIN(x) */
+ AD7190_RangeSetup(0, AD7190_CONF_GAIN_64);
+
+ //val = AD7190_SingleConversion();
+ //pc.printf("ADC = \t%lu \r\n", val);
+
+ val = AD7190_ContinuousReadAvg(8);
+ //pc.printf("ADC_avg = \t%lu \r\n", val);
+
+ double voltageAn34 = ( val * 0.00000011921 - 1 ) * 4.096 /64 ; //AIN=(輸出碼/2^23-1)*(Vref/gain)
+
+
+ pc.printf("%f \r\n", voltageAn34);
+
+ uart.printf("s%f# \r\n", voltageAn34);
+
+ idx=idx+1;
+ wait(0.01);
+ //pc.printf("TX send s %f #\r\n",voltageAn34);
+ //wait(1);
+
+
+}
+
+
+//---DON'T CHANGE BELOW----
+// MBED compensate
+// main() runs in its own thread in the OS
+int main() {
+ //---Enable Debug---
+ pc.baud(9600);
+ uart.baud(9600);
+ //--Mark out to disable debug
+ setup();
+ while (true) {
+ loop();
+
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Aug 06 06:52:19 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file