AD7793.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "AD7793.h"
  2. #include "Communication.h"
  3. /***************************************************************************//**
  4. * @brief Initializes the MS5192T and checks if the device is present.
  5. *
  6. * @return status - Result of the initialization procedure.
  7. * Example: 1 - if initialization was successful (ID is 0x0B).
  8. * 0 - if initialization was unsuccessful.
  9. *******************************************************************************/
  10. unsigned char MS5192T_Init(void)
  11. {
  12. unsigned char status = 0x1;
  13. if((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
  14. {
  15. status = 0x0;
  16. }
  17. return(status);
  18. }
  19. /***************************************************************************//**
  20. * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
  21. *
  22. * @return None.
  23. *******************************************************************************/
  24. void MS5192T_Reset(void)
  25. {
  26. unsigned char dataToSend[5] = {0xff, 0xff, 0xff, 0xff};
  27. MS5192T_CS_LOW;
  28. SPI_Write(dataToSend,4);
  29. MS5192T_CS_HIGH;
  30. }
  31. /***************************************************************************//**
  32. * @brief Reads the value of the selected register
  33. *
  34. * @param regAddress - The address of the register to read.
  35. * @param size - The size of the register to read.
  36. *
  37. * @return data - The value of the selected register register.
  38. *******************************************************************************/
  39. unsigned long MS5192T_GetRegisterValue(unsigned char regAddress,
  40. unsigned char size,
  41. unsigned char modifyCS)
  42. {
  43. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  44. unsigned long receivedData = 0x00;
  45. unsigned char i = 0x00;
  46. data[0] = 0x01 * modifyCS;
  47. data[1] = MS5192T_COMM_READ | MS5192T_COMM_ADDR(regAddress);
  48. SPI_Read(data,(1 + size));
  49. for(i = 1;i < size + 1;i ++)
  50. {
  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. /***************************************************************************//**
  160. * @brief Sets the reference source for the ADC.
  161. *
  162. * @param type - Type of the reference.
  163. * Example: AD7793_REFSEL_EXT - External Reference Selected
  164. * AD7793_REFSEL_INT - Internal Reference Selected.
  165. *
  166. * @return None.
  167. *******************************************************************************/
  168. void MS5192T_SetIntReference(unsigned char type)
  169. {
  170. unsigned long command = 0;
  171. command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
  172. 2,
  173. 1); // CS is modified by SPI read/write functions.
  174. command &= ~MS5192T_CONF_REFSEL(MS5192T_REFSEL_INT);
  175. command |= MS5192T_CONF_REFSEL(type);
  176. MS5192T_SetRegisterValue(MS5192T_REG_CONF,
  177. command,
  178. 2,
  179. 1); // CS is modified by SPI read/write functions.
  180. }
  181. /***************************************************************************//**
  182. * @brief Performs the given calibration to the specified channel.
  183. *
  184. * @param mode - Calibration type.
  185. * @param channel - Channel to be calibrated.
  186. *
  187. * @return none.
  188. *******************************************************************************/
  189. void MS5192T_Calibrate(unsigned char mode, unsigned char channel)
  190. {
  191. unsigned short oldRegValue = 0x0;
  192. unsigned short newRegValue = 0x0;
  193. MS5192T_SetChannel(channel);
  194. oldRegValue &= MS5192T_GetRegisterValue(MS5192T_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
  195. oldRegValue &= ~MS5192T_MODE_SEL(0x7);
  196. newRegValue = oldRegValue | MS5192T_MODE_SEL(mode);
  197. MS5192T_CS_LOW;
  198. MS5192T_SetRegisterValue(MS5192T_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
  199. MS5192T_WaitRdyGoLow();
  200. MS5192T_CS_HIGH;
  201. }
  202. /***************************************************************************//**
  203. * @brief Returns the result of a single conversion.
  204. *
  205. * @return regData - Result of a single analog-to-digital conversion.
  206. *******************************************************************************/
  207. unsigned long MS5192T_SingleConversion(void)
  208. {
  209. unsigned long command = 0x0;
  210. unsigned long regData = 0x0;
  211. command = MS5192T_MODE_SEL(MS5192T_MODE_SINGLE);
  212. MS5192T_CS_LOW;
  213. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  214. command,
  215. 2,
  216. 0);// CS is not modified by SPI read/write functions.
  217. MS5192T_WaitRdyGoLow();
  218. regData = MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  219. MS5192T_CS_HIGH;
  220. return(regData);
  221. }
  222. /***************************************************************************//**
  223. * @brief Returns the average of several conversion results.
  224. *
  225. * @return samplesAverage - The average of the conversion results.
  226. *******************************************************************************/
  227. unsigned long MS5192T_ContinuousReadAvg(unsigned char sampleNumber)
  228. {
  229. unsigned long samplesAverage = 0x0;
  230. unsigned long command = 0x0;
  231. unsigned char count = 0x0;
  232. command = MS5192T_MODE_SEL(MS5192T_MODE_CONT);
  233. MS5192T_CS_LOW;
  234. MS5192T_SetRegisterValue(MS5192T_REG_MODE,
  235. command,
  236. 2,
  237. 0);// CS is not modified by SPI read/write functions.
  238. for(count = 0;count < sampleNumber;count ++)
  239. {
  240. MS5192T_WaitRdyGoLow();
  241. samplesAverage += MS5192T_GetRegisterValue(MS5192T_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  242. }
  243. MS5192T_CS_HIGH;
  244. samplesAverage = samplesAverage / sampleNumber;
  245. return(samplesAverage);
  246. }