ms5192t.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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] = {0x03, 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. receivedData = (receivedData << 8) + data[i];
  52. }
  53. return receivedData;
  54. }
  55. /***************************************************************************//**
  56. * @brief Writes the value to the register
  57. *
  58. * @param - regAddress - The address of the register to write to.
  59. * @param - regValue - The value to write to the register.
  60. * @param - size - The size of the register to write.
  61. *
  62. * @return None.
  63. *******************************************************************************/
  64. void MS5192T_SetRegisterValue(unsigned char regAddress,
  65. unsigned long regValue,
  66. unsigned char size,
  67. unsigned char modifyCS)
  68. {
  69. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  70. unsigned char* dataPointer = (unsigned char*)&regValue;
  71. unsigned char bytesNr = size + 1;
  72. data[0] = 0x01 * modifyCS;
  73. data[1] = MS5192T_COMM_WRITE | MS5192T_COMM_ADDR(regAddress);
  74. while(bytesNr > 1)
  75. {
  76. data[bytesNr] = *dataPointer;
  77. dataPointer ++;
  78. bytesNr --;
  79. }
  80. SPI_Write(data,(1 + size));
  81. }
  82. /***************************************************************************//**
  83. * @brief Waits for RDY pin to go low.
  84. *
  85. * @return None.
  86. *******************************************************************************/
  87. void MS5192T_WaitRdyGoLow(void)
  88. {
  89. while (adc_get_rdy() == SET) {}
  90. }
  91. /***************************************************************************//**
  92. * @brief Sets the operating mode of MS5192T.
  93. *
  94. * @param mode - Mode of operation.
  95. *
  96. * @return None.
  97. *******************************************************************************/
  98. void MS5192T_SetMode(unsigned long mode)
  99. {
  100. unsigned long command;
  101. command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
  102. 2,
  103. 1); // CS is modified by SPI read/write functions.
  104. command &= ~MS5192T_MODE_SEL(0xFF);
  105. command |= MS5192T_MODE_SEL(mode);
  106. MS5192T_SetRegisterValue(
  107. MS5192T_REG_MODE,
  108. command,
  109. 2,
  110. 1); // CS is modified by SPI read/write functions.
  111. }
  112. void MS5192T_SetUpdateRate(unsigned long rate)
  113. {
  114. unsigned long command;
  115. command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
  116. 2,
  117. 1); // CS is modified by SPI read/write functions.
  118. command &= ~MS5192T_MODE_RATE(0xFF);
  119. command |= MS5192T_MODE_RATE(rate);
  120. MS5192T_SetRegisterValue(
  121. MS5192T_REG_MODE,
  122. command,
  123. 2,
  124. 1); // CS is modified by SPI read/write functions.
  125. }
  126. /***************************************************************************//**
  127. * @brief Selects the channel of MS5192T.
  128. *
  129. * @param channel - ADC channel selection.
  130. *
  131. * @return None.
  132. *******************************************************************************/
  133. void MS5192T_SetChannel(unsigned long channel)
  134. {
  135. unsigned long command;
  136. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  137. 2,
  138. 1); // CS is modified by SPI read/write functions.
  139. command &= ~MS5192T_CONF_CHAN(0xFF);
  140. command |= MS5192T_CONF_CHAN(channel);
  141. MS5192T_SetRegisterValue(
  142. MS5192T_REG_CONF,
  143. command,
  144. 2,
  145. 1); // CS is modified by SPI read/write functions.
  146. }
  147. /***************************************************************************//**
  148. * @brief Sets the gain of the In-Amp.
  149. *
  150. * @param gain - Gain.
  151. *
  152. * @return None.
  153. *******************************************************************************/
  154. void MS5192T_SetGain(unsigned long gain)
  155. {
  156. unsigned long command;
  157. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  158. 2,
  159. 1); // CS is modified by SPI read/write functions.
  160. command &= ~MS5192T_CONF_GAIN(0xFF);
  161. command |= MS5192T_CONF_GAIN(gain);
  162. MS5192T_SetRegisterValue(
  163. MS5192T_REG_CONF,
  164. command,
  165. 2,
  166. 1); // CS is modified by SPI read/write functions.
  167. }
  168. void MS5192T_SetPolar(unsigned long polar)
  169. {
  170. unsigned long command;
  171. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  172. 2,
  173. 1); // CS is modified by SPI read/write functions.
  174. command &= ~(1 << 12);
  175. command |= polar;
  176. MS5192T_SetRegisterValue(
  177. MS5192T_REG_CONF,
  178. command,
  179. 2,
  180. 1); // CS is modified by SPI read/write functions.
  181. }
  182. /***************************************************************************//**
  183. * @brief Sets the reference source for the ADC.
  184. *
  185. * @param type - Type of the reference.
  186. * Example: AD7793_REFSEL_EXT - External Reference Selected
  187. * AD7793_REFSEL_INT - Internal Reference Selected.
  188. *
  189. * @return None.
  190. *******************************************************************************/
  191. void MS5192T_SetIntReference(unsigned char type)
  192. {
  193. unsigned long command = 0;
  194. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  195. 2,
  196. 1); // CS is modified by SPI read/write functions.
  197. command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
  198. command |= MS5192T_CONF_REFSEL(type);
  199. MS5192T_SetRegisterValue(MS5192T_REG_CONF,
  200. command,
  201. 2,
  202. 1); // CS is modified by SPI read/write functions.
  203. }
  204. /***************************************************************************//**
  205. * @brief Performs the given calibration to the specified channel.
  206. *
  207. * @param mode - Calibration type.
  208. * @param channel - Channel to be calibrated.
  209. *
  210. * @return none.
  211. *******************************************************************************/
  212. void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
  213. {
  214. unsigned short oldRegValue = 0x0;
  215. unsigned short newRegValue = 0x0;
  216. MS5192T_SetChannel(channel);
  217. oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
  218. oldRegValue &= ~MS5192T_MODE_SEL(0x7);
  219. newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
  220. MS5192T_CS_LOW;
  221. MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
  222. MS5192T_WaitRdyGoLow();
  223. MS5192T_CS_HIGH;
  224. }
  225. /***************************************************************************//**
  226. * @brief Returns the result of a single conversion.
  227. *
  228. * @return regData - Result of a single analog-to-digital conversion.
  229. *******************************************************************************/
  230. unsigned long MS5192T_SingleConversion(void)
  231. {
  232. unsigned long command = 0x0;
  233. unsigned long regData = 0x0;
  234. command = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
  235. MS5192T_CS_LOW;
  236. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  237. command,
  238. 2,
  239. 0);// CS is not modified by SPI read/write functions.
  240. MS5192T_WaitRdyGoLow();
  241. regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 2, 0); // CS is not modified by SPI read/write functions.
  242. MS5192T_CS_HIGH;
  243. return regData;
  244. }
  245. /***************************************************************************//**
  246. * @brief Returns the average of several conversion results.
  247. *
  248. * @return samplesAverage - The average of the conversion results.
  249. *******************************************************************************/
  250. unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
  251. {
  252. unsigned long samplesAverage = 0x0;
  253. unsigned long command = 0x0;
  254. unsigned char count = 0x0;
  255. command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
  256. MS5192T_CS_LOW;
  257. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  258. command,
  259. 2,
  260. 0);// CS is not modified by SPI read/write functions.
  261. for(count = 0;count < sampleNumber;count ++)
  262. {
  263. MS5192T_WaitRdyGoLow();
  264. samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  265. }
  266. MS5192T_CS_HIGH;
  267. samplesAverage = samplesAverage / sampleNumber;
  268. return(samplesAverage);
  269. }