123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "ms5192t.h"
- #include "at32f403a_407.h"
- #include "adc_transport.h"
- /***************************************************************************//**
- * @brief Initializes the MS5192T and checks if the device is present.
- *
- * @return status - Result of the initialization procedure.
- * Example: 1 - if initialization was successful (ID is 0x0B).
- * 0 - if initialization was unsuccessful.
- *******************************************************************************/
- unsigned char MS5192T_Init(void)
- {
- unsigned char status = 0x1;
-
- if((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
- {
- status = 0x0;
- }
-
- return(status);
- }
- /***************************************************************************//**
- * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_Reset(void)
- {
- unsigned char dataToSend[5] = {0xff, 0xff, 0xff, 0xff};
-
- MS5192T_CS_LOW;
- SPI_Write(dataToSend,4);
- MS5192T_CS_HIGH;
- }
- /***************************************************************************//**
- * @brief Reads the value of the selected register
- *
- * @param regAddress - The address of the register to read.
- * @param size - The size of the register to read.
- *
- * @return data - The value of the selected register register.
- *******************************************************************************/
- unsigned long MS5192T_GetRegisterValue(unsigned char regAddress,
- unsigned char size,
- unsigned char modifyCS)
- {
- unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
- unsigned long receivedData = 0x00;
- unsigned char i = 0x00;
-
- data[0] = 0x01 * modifyCS;
- data[1] = MS5192T_COMM_READ | MS5192T_COMM_ADDR(regAddress);
- SPI_Read(data,(1 + size));
- for(i = 1;i < size + 1;i ++)
- {
- receivedData = (receivedData << 8) + data[i];
- }
-
- return (receivedData);
- }
- /***************************************************************************//**
- * @brief Writes the value to the register
- *
- * @param - regAddress - The address of the register to write to.
- * @param - regValue - The value to write to the register.
- * @param - size - The size of the register to write.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_SetRegisterValue(unsigned char regAddress,
- unsigned long regValue,
- unsigned char size,
- unsigned char modifyCS)
- {
- unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
- unsigned char* dataPointer = (unsigned char*)®Value;
- unsigned char bytesNr = size + 1;
-
- data[0] = 0x01 * modifyCS;
- data[1] = MS5192T_COMM_WRITE | MS5192T_COMM_ADDR(regAddress);
- while(bytesNr > 1)
- {
- data[bytesNr] = *dataPointer;
- dataPointer ++;
- bytesNr --;
- }
- SPI_Write(data,(1 + size));
- }
- /***************************************************************************//**
- * @brief Waits for RDY pin to go low.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_WaitRdyGoLow(void)
- {
- /*
- while( MS5192T_RDY_STATE )
- {
- ;
- }
- */
- }
- /***************************************************************************//**
- * @brief Sets the operating mode of MS5192T.
- *
- * @param mode - Mode of operation.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_SetMode(unsigned long mode)
- {
- unsigned long command;
-
- command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
- 2,
- 1); // CS is modified by SPI read/write functions.
- command &= ~MS5192T_MODE_SEL(0xFF);
- command |= MS5192T_MODE_SEL(mode);
- MS5192T_SetRegisterValue(
- MS5192T_REG_MODE,
- command,
- 2,
- 1); // CS is modified by SPI read/write functions.
- }
- /***************************************************************************//**
- * @brief Selects the channel of MS5192T.
- *
- * @param channel - ADC channel selection.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_SetChannel(unsigned long channel)
- {
- unsigned long command;
-
- command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
- 2,
- 1); // CS is modified by SPI read/write functions.
- command &= ~MS5192T_CONF_CHAN(0xFF);
- command |= MS5192T_CONF_CHAN(channel);
- MS5192T_SetRegisterValue(
- MS5192T_REG_CONF,
- command,
- 2,
- 1); // CS is modified by SPI read/write functions.
- }
- /***************************************************************************//**
- * @brief Sets the gain of the In-Amp.
- *
- * @param gain - Gain.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_SetGain(unsigned long gain)
- {
- unsigned long command;
-
- command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
- 2,
- 1); // CS is modified by SPI read/write functions.
- command &= ~MS5192T_CONF_GAIN(0xFF);
- command |= MS5192T_CONF_GAIN(gain);
- MS5192T_SetRegisterValue(
- MS5192T_REG_CONF,
- command,
- 2,
- 1); // CS is modified by SPI read/write functions.
- }
- /***************************************************************************//**
- * @brief Sets the reference source for the ADC.
- *
- * @param type - Type of the reference.
- * Example: AD7793_REFSEL_EXT - External Reference Selected
- * AD7793_REFSEL_INT - Internal Reference Selected.
- *
- * @return None.
- *******************************************************************************/
- void MS5192T_SetIntReference(unsigned char type)
- {
- unsigned long command = 0;
-
- command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
- 2,
- 1); // CS is modified by SPI read/write functions.
- command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
- command |= MS5192T_CONF_REFSEL(type);
- MS5192T_SetRegisterValue(MS5192T_REG_CONF,
- command,
- 2,
- 1); // CS is modified by SPI read/write functions.
- }
- /***************************************************************************//**
- * @brief Performs the given calibration to the specified channel.
- *
- * @param mode - Calibration type.
- * @param channel - Channel to be calibrated.
- *
- * @return none.
- *******************************************************************************/
- void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
- {
- unsigned short oldRegValue = 0x0;
- unsigned short newRegValue = 0x0;
-
- MS5192T_SetChannel(channel);
- oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
- oldRegValue &= ~MS5192T_MODE_SEL(0x7);
- newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
- MS5192T_CS_LOW;
- MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
- MS5192T_WaitRdyGoLow();
- MS5192T_CS_HIGH;
-
- }
- /***************************************************************************//**
- * @brief Returns the result of a single conversion.
- *
- * @return regData - Result of a single analog-to-digital conversion.
- *******************************************************************************/
- unsigned long MS5192T_SingleConversion(void)
- {
- unsigned long command = 0x0;
- unsigned long regData = 0x0;
-
- command = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
- MS5192T_CS_LOW;
- MS5192T_SetRegisterValue(MS5192T_REG_MODE,
- command,
- 2,
- 0);// CS is not modified by SPI read/write functions.
- MS5192T_WaitRdyGoLow();
- regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
- MS5192T_CS_HIGH;
- return(regData);
- }
- /***************************************************************************//**
- * @brief Returns the average of several conversion results.
- *
- * @return samplesAverage - The average of the conversion results.
- *******************************************************************************/
- unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
- {
- unsigned long samplesAverage = 0x0;
- unsigned long command = 0x0;
- unsigned char count = 0x0;
-
- command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
- MS5192T_CS_LOW;
- MS5192T_SetRegisterValue(MS5192T_REG_MODE,
- command,
- 2,
- 0);// CS is not modified by SPI read/write functions.
- for(count = 0;count < sampleNumber;count ++)
- {
- MS5192T_WaitRdyGoLow();
- samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
- }
- MS5192T_CS_HIGH;
- samplesAverage = samplesAverage / sampleNumber;
-
- return(samplesAverage);
- }
|