ms5192t.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "ms5192t.h"
  2. #include "at32f403a_407.h"
  3. #include "adc_transport.h"
  4. /***************************************************************************//**
  5. * @brief Initializes the MS5192T and checks if the device is present.
  6. *
  7. * @return status - Result of the initialization procedure.
  8. * Example: 1 - if initialization was successful (ID is 0x0B).
  9. * 0 - if initialization was unsuccessful.
  10. *******************************************************************************/
  11. unsigned char MS5192T_Init(void)
  12. {
  13. unsigned char status = 0x1;
  14. if((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
  15. {
  16. status = 0x0;
  17. }
  18. return(status);
  19. }
  20. /***************************************************************************//**
  21. * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
  22. *
  23. * @return None.
  24. *******************************************************************************/
  25. void MS5192T_Reset(void)
  26. {
  27. unsigned char dataToSend[5] = {0xff, 0xff, 0xff, 0xff};
  28. MS5192T_CS_LOW;
  29. SPI_Write(dataToSend,4);
  30. MS5192T_CS_HIGH;
  31. }
  32. /***************************************************************************//**
  33. * @brief Reads the value of the selected register
  34. *
  35. * @param regAddress - The address of the register to read.
  36. * @param size - The size of the register to read.
  37. *
  38. * @return data - The value of the selected register register.
  39. *******************************************************************************/
  40. unsigned long MS5192T_GetRegisterValue(unsigned char regAddress,
  41. unsigned char size,
  42. unsigned char modifyCS)
  43. {
  44. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  45. unsigned long receivedData = 0x00;
  46. unsigned char i = 0x00;
  47. data[0] = 0x01 * modifyCS;
  48. data[1] = MS5192T_COMM_READ | MS5192T_COMM_ADDR(regAddress);
  49. SPI_Read(data,(1 + size));
  50. for(i = 1;i < size + 1;i ++)
  51. {
  52. receivedData = (receivedData << 8) + data[i];
  53. }
  54. return (receivedData);
  55. }
  56. /***************************************************************************//**
  57. * @brief Writes the value to the register
  58. *
  59. * @param - regAddress - The address of the register to write to.
  60. * @param - regValue - The value to write to the register.
  61. * @param - size - The size of the register to write.
  62. *
  63. * @return None.
  64. *******************************************************************************/
  65. void MS5192T_SetRegisterValue(unsigned char regAddress,
  66. unsigned long regValue,
  67. unsigned char size,
  68. unsigned char modifyCS)
  69. {
  70. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  71. unsigned char* dataPointer = (unsigned char*)&regValue;
  72. unsigned char bytesNr = size + 1;
  73. data[0] = 0x01 * modifyCS;
  74. data[1] = MS5192T_COMM_WRITE | MS5192T_COMM_ADDR(regAddress);
  75. while(bytesNr > 1)
  76. {
  77. data[bytesNr] = *dataPointer;
  78. dataPointer ++;
  79. bytesNr --;
  80. }
  81. SPI_Write(data,(1 + size));
  82. }
  83. /***************************************************************************//**
  84. * @brief Waits for RDY pin to go low.
  85. *
  86. * @return None.
  87. *******************************************************************************/
  88. void MS5192T_WaitRdyGoLow(void)
  89. {
  90. /*
  91. while( MS5192T_RDY_STATE )
  92. {
  93. ;
  94. }
  95. */
  96. }
  97. /***************************************************************************//**
  98. * @brief Sets the operating mode of MS5192T.
  99. *
  100. * @param mode - Mode of operation.
  101. *
  102. * @return None.
  103. *******************************************************************************/
  104. void MS5192T_SetMode(unsigned long mode)
  105. {
  106. unsigned long command;
  107. command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
  108. 2,
  109. 1); // CS is modified by SPI read/write functions.
  110. command &= ~MS5192T_MODE_SEL(0xFF);
  111. command |= MS5192T_MODE_SEL(mode);
  112. MS5192T_SetRegisterValue(
  113. MS5192T_REG_MODE,
  114. command,
  115. 2,
  116. 1); // CS is modified by SPI read/write functions.
  117. }
  118. /***************************************************************************//**
  119. * @brief Selects the channel of MS5192T.
  120. *
  121. * @param channel - ADC channel selection.
  122. *
  123. * @return None.
  124. *******************************************************************************/
  125. void MS5192T_SetChannel(unsigned long channel)
  126. {
  127. unsigned long command;
  128. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  129. 2,
  130. 1); // CS is modified by SPI read/write functions.
  131. command &= ~MS5192T_CONF_CHAN(0xFF);
  132. command |= MS5192T_CONF_CHAN(channel);
  133. MS5192T_SetRegisterValue(
  134. MS5192T_REG_CONF,
  135. command,
  136. 2,
  137. 1); // CS is modified by SPI read/write functions.
  138. }
  139. /***************************************************************************//**
  140. * @brief Sets the gain of the In-Amp.
  141. *
  142. * @param gain - Gain.
  143. *
  144. * @return None.
  145. *******************************************************************************/
  146. void MS5192T_SetGain(unsigned long gain)
  147. {
  148. unsigned long command;
  149. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  150. 2,
  151. 1); // CS is modified by SPI read/write functions.
  152. command &= ~MS5192T_CONF_GAIN(0xFF);
  153. command |= MS5192T_CONF_GAIN(gain);
  154. MS5192T_SetRegisterValue(
  155. MS5192T_REG_CONF,
  156. command,
  157. 2,
  158. 1); // CS is modified by SPI read/write functions.
  159. }
  160. /***************************************************************************//**
  161. * @brief Sets the reference source for the ADC.
  162. *
  163. * @param type - Type of the reference.
  164. * Example: AD7793_REFSEL_EXT - External Reference Selected
  165. * AD7793_REFSEL_INT - Internal Reference Selected.
  166. *
  167. * @return None.
  168. *******************************************************************************/
  169. void MS5192T_SetIntReference(unsigned char type)
  170. {
  171. unsigned long command = 0;
  172. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  173. 2,
  174. 1); // CS is modified by SPI read/write functions.
  175. command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
  176. command |= MS5192T_CONF_REFSEL(type);
  177. MS5192T_SetRegisterValue(MS5192T_REG_CONF,
  178. command,
  179. 2,
  180. 1); // CS is modified by SPI read/write functions.
  181. }
  182. /***************************************************************************//**
  183. * @brief Performs the given calibration to the specified channel.
  184. *
  185. * @param mode - Calibration type.
  186. * @param channel - Channel to be calibrated.
  187. *
  188. * @return none.
  189. *******************************************************************************/
  190. void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
  191. {
  192. unsigned short oldRegValue = 0x0;
  193. unsigned short newRegValue = 0x0;
  194. MS5192T_SetChannel(channel);
  195. oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
  196. oldRegValue &= ~MS5192T_MODE_SEL(0x7);
  197. newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
  198. MS5192T_CS_LOW;
  199. MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
  200. MS5192T_WaitRdyGoLow();
  201. MS5192T_CS_HIGH;
  202. }
  203. /***************************************************************************//**
  204. * @brief Returns the result of a single conversion.
  205. *
  206. * @return regData - Result of a single analog-to-digital conversion.
  207. *******************************************************************************/
  208. unsigned long MS5192T_SingleConversion(void)
  209. {
  210. unsigned long command = 0x0;
  211. unsigned long regData = 0x0;
  212. command = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
  213. MS5192T_CS_LOW;
  214. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  215. command,
  216. 2,
  217. 0);// CS is not modified by SPI read/write functions.
  218. MS5192T_WaitRdyGoLow();
  219. regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  220. MS5192T_CS_HIGH;
  221. return(regData);
  222. }
  223. /***************************************************************************//**
  224. * @brief Returns the average of several conversion results.
  225. *
  226. * @return samplesAverage - The average of the conversion results.
  227. *******************************************************************************/
  228. unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
  229. {
  230. unsigned long samplesAverage = 0x0;
  231. unsigned long command = 0x0;
  232. unsigned char count = 0x0;
  233. command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
  234. MS5192T_CS_LOW;
  235. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  236. command,
  237. 2,
  238. 0);// CS is not modified by SPI read/write functions.
  239. for(count = 0;count < sampleNumber;count ++)
  240. {
  241. MS5192T_WaitRdyGoLow();
  242. samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  243. }
  244. MS5192T_CS_HIGH;
  245. samplesAverage = samplesAverage / sampleNumber;
  246. return(samplesAverage);
  247. }