ms5192t.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /*
  90. while( MS5192T_RDY_STATE )
  91. {
  92. ;
  93. }
  94. */
  95. }
  96. /***************************************************************************//**
  97. * @brief Sets the operating mode of MS5192T.
  98. *
  99. * @param mode - Mode of operation.
  100. *
  101. * @return None.
  102. *******************************************************************************/
  103. void MS5192T_SetMode(unsigned long mode)
  104. {
  105. unsigned long command;
  106. command = MS5192T_GetRegisterValue(MS5192T_REG_MODE,
  107. 2,
  108. 1); // CS is modified by SPI read/write functions.
  109. command &= ~MS5192T_MODE_SEL(0xFF);
  110. command |= MS5192T_MODE_SEL(mode);
  111. MS5192T_SetRegisterValue(
  112. MS5192T_REG_MODE,
  113. command,
  114. 2,
  115. 1); // CS is modified by SPI read/write functions.
  116. }
  117. /***************************************************************************//**
  118. * @brief Selects the channel of MS5192T.
  119. *
  120. * @param channel - ADC channel selection.
  121. *
  122. * @return None.
  123. *******************************************************************************/
  124. void MS5192T_SetChannel(unsigned long channel)
  125. {
  126. unsigned long command;
  127. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  128. 2,
  129. 1); // CS is modified by SPI read/write functions.
  130. command &= ~MS5192T_CONF_CHAN(0xFF);
  131. command |= MS5192T_CONF_CHAN(channel);
  132. MS5192T_SetRegisterValue(
  133. MS5192T_REG_CONF,
  134. command,
  135. 2,
  136. 1); // CS is modified by SPI read/write functions.
  137. }
  138. /***************************************************************************//**
  139. * @brief Sets the gain of the In-Amp.
  140. *
  141. * @param gain - Gain.
  142. *
  143. * @return None.
  144. *******************************************************************************/
  145. void MS5192T_SetGain(unsigned long gain)
  146. {
  147. unsigned long command;
  148. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  149. 2,
  150. 1); // CS is modified by SPI read/write functions.
  151. command &= ~MS5192T_CONF_GAIN(0xFF);
  152. command |= MS5192T_CONF_GAIN(gain);
  153. MS5192T_SetRegisterValue(
  154. MS5192T_REG_CONF,
  155. command,
  156. 2,
  157. 1); // CS is modified by SPI read/write functions.
  158. }
  159. void MS5192T_SetPolar(unsigned long polar)
  160. {
  161. unsigned long command;
  162. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  163. 2,
  164. 1); // CS is modified by SPI read/write functions.
  165. command &= ~(1 << 12);
  166. command |= polar;
  167. MS5192T_SetRegisterValue(
  168. MS5192T_REG_CONF,
  169. command,
  170. 2,
  171. 1); // CS is modified by SPI read/write functions.
  172. }
  173. /***************************************************************************//**
  174. * @brief Sets the reference source for the ADC.
  175. *
  176. * @param type - Type of the reference.
  177. * Example: AD7793_REFSEL_EXT - External Reference Selected
  178. * AD7793_REFSEL_INT - Internal Reference Selected.
  179. *
  180. * @return None.
  181. *******************************************************************************/
  182. void MS5192T_SetIntReference(unsigned char type)
  183. {
  184. unsigned long command = 0;
  185. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  186. 2,
  187. 1); // CS is modified by SPI read/write functions.
  188. command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
  189. command |= MS5192T_CONF_REFSEL(type);
  190. MS5192T_SetRegisterValue(MS5192T_REG_CONF,
  191. command,
  192. 2,
  193. 1); // CS is modified by SPI read/write functions.
  194. }
  195. /***************************************************************************//**
  196. * @brief Performs the given calibration to the specified channel.
  197. *
  198. * @param mode - Calibration type.
  199. * @param channel - Channel to be calibrated.
  200. *
  201. * @return none.
  202. *******************************************************************************/
  203. void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
  204. {
  205. unsigned short oldRegValue = 0x0;
  206. unsigned short newRegValue = 0x0;
  207. MS5192T_SetChannel(channel);
  208. oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
  209. oldRegValue &= ~MS5192T_MODE_SEL(0x7);
  210. newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
  211. MS5192T_CS_LOW;
  212. MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
  213. MS5192T_WaitRdyGoLow();
  214. MS5192T_CS_HIGH;
  215. }
  216. /***************************************************************************//**
  217. * @brief Returns the result of a single conversion.
  218. *
  219. * @return regData - Result of a single analog-to-digital conversion.
  220. *******************************************************************************/
  221. unsigned long MS5192T_SingleConversion(void)
  222. {
  223. unsigned long command = 0x0;
  224. unsigned long regData = 0x0;
  225. command = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
  226. MS5192T_CS_LOW;
  227. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  228. command,
  229. 2,
  230. 0);// CS is not modified by SPI read/write functions.
  231. MS5192T_WaitRdyGoLow();
  232. regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  233. MS5192T_CS_HIGH;
  234. return(regData);
  235. }
  236. /***************************************************************************//**
  237. * @brief Returns the average of several conversion results.
  238. *
  239. * @return samplesAverage - The average of the conversion results.
  240. *******************************************************************************/
  241. unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
  242. {
  243. unsigned long samplesAverage = 0x0;
  244. unsigned long command = 0x0;
  245. unsigned char count = 0x0;
  246. command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
  247. MS5192T_CS_LOW;
  248. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  249. command,
  250. 2,
  251. 0);// CS is not modified by SPI read/write functions.
  252. for(count = 0;count < sampleNumber;count ++)
  253. {
  254. MS5192T_WaitRdyGoLow();
  255. samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  256. }
  257. MS5192T_CS_HIGH;
  258. samplesAverage = samplesAverage / sampleNumber;
  259. return(samplesAverage);
  260. }