AD7793.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************//**
  2. * @file AD7793.c
  3. * @brief Implementation of AD7793 Driver.
  4. * @author Bancisor MIhai
  5. ********************************************************************************
  6. * Copyright 2012(c) Analog Devices, Inc.
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. * - Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * - Neither the name of Analog Devices, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. * - The use of this software may or may not infringe the patent rights
  22. * of one or more patent holders. This license does not release you
  23. * from the requirement that you obtain separate licenses from these
  24. * patent holders to use this software.
  25. * - Use of the software either in source or binary form, must be run
  26. * on or directly connected to an Analog Devices Inc. component.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
  29. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ********************************************************************************
  40. * SVN Revision: 499
  41. *******************************************************************************/
  42. /******************************************************************************/
  43. /* Include Files */
  44. /******************************************************************************/
  45. #include "AD7793.h" // AD7793 definitions.
  46. #include "Communication.h" // Communication definitions.
  47. /***************************************************************************//**
  48. * @brief Initializes the AD7793 and checks if the device is present.
  49. *
  50. * @return status - Result of the initialization procedure.
  51. * Example: 1 - if initialization was successful (ID is 0x0B).
  52. * 0 - if initialization was unsuccessful.
  53. *******************************************************************************/
  54. unsigned char AD7793_Init(void)
  55. {
  56. unsigned char status = 0x1;
  57. SPI_Init(0, 1000000, 1, 1);
  58. if((AD7793_GetRegisterValue(AD7793_REG_ID, 1, 1) & 0x0F) != AD7793_ID)
  59. {
  60. status = 0x0;
  61. }
  62. return(status);
  63. }
  64. /***************************************************************************//**
  65. * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
  66. *
  67. * @return None.
  68. *******************************************************************************/
  69. void AD7793_Reset(void)
  70. {
  71. unsigned char dataToSend[5] = {0x03, 0xff, 0xff, 0xff, 0xff};
  72. ADI_PART_CS_LOW;
  73. SPI_Write(dataToSend,4);
  74. ADI_PART_CS_HIGH;
  75. }
  76. /***************************************************************************//**
  77. * @brief Reads the value of the selected register
  78. *
  79. * @param regAddress - The address of the register to read.
  80. * @param size - The size of the register to read.
  81. *
  82. * @return data - The value of the selected register register.
  83. *******************************************************************************/
  84. unsigned long AD7793_GetRegisterValue(unsigned char regAddress,
  85. unsigned char size,
  86. unsigned char modifyCS)
  87. {
  88. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  89. unsigned long receivedData = 0x00;
  90. unsigned char i = 0x00;
  91. data[0] = 0x01 * modifyCS;
  92. data[1] = AD7793_COMM_READ | AD7793_COMM_ADDR(regAddress);
  93. SPI_Read(data,(1 + size));
  94. for(i = 1;i < size + 1;i ++)
  95. {
  96. receivedData = (receivedData << 8) + data[i];
  97. }
  98. return (receivedData);
  99. }
  100. /***************************************************************************//**
  101. * @brief Writes the value to the register
  102. *
  103. * @param - regAddress - The address of the register to write to.
  104. * @param - regValue - The value to write to the register.
  105. * @param - size - The size of the register to write.
  106. *
  107. * @return None.
  108. *******************************************************************************/
  109. void AD7793_SetRegisterValue(unsigned char regAddress,
  110. unsigned long regValue,
  111. unsigned char size,
  112. unsigned char modifyCS)
  113. {
  114. unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
  115. unsigned char* dataPointer = (unsigned char*)&regValue;
  116. unsigned char bytesNr = size + 1;
  117. data[0] = 0x01 * modifyCS;
  118. data[1] = AD7793_COMM_WRITE | AD7793_COMM_ADDR(regAddress);
  119. while(bytesNr > 1)
  120. {
  121. data[bytesNr] = *dataPointer;
  122. dataPointer ++;
  123. bytesNr --;
  124. }
  125. SPI_Write(data,(1 + size));
  126. }
  127. /***************************************************************************//**
  128. * @brief Waits for RDY pin to go low.
  129. *
  130. * @return None.
  131. *******************************************************************************/
  132. void AD7793_WaitRdyGoLow(void)
  133. {
  134. while( AD7793_RDY_STATE )
  135. {
  136. ;
  137. }
  138. }
  139. /***************************************************************************//**
  140. * @brief Sets the operating mode of AD7793.
  141. *
  142. * @param mode - Mode of operation.
  143. *
  144. * @return None.
  145. *******************************************************************************/
  146. void AD7793_SetMode(unsigned long mode)
  147. {
  148. unsigned long command;
  149. command = AD7793_GetRegisterValue(AD7793_REG_MODE,
  150. 2,
  151. 1); // CS is modified by SPI read/write functions.
  152. command &= ~AD7793_MODE_SEL(0xFF);
  153. command |= AD7793_MODE_SEL(mode);
  154. AD7793_SetRegisterValue(
  155. AD7793_REG_MODE,
  156. command,
  157. 2,
  158. 1); // CS is modified by SPI read/write functions.
  159. }
  160. /***************************************************************************//**
  161. * @brief Selects the channel of AD7793.
  162. *
  163. * @param channel - ADC channel selection.
  164. *
  165. * @return None.
  166. *******************************************************************************/
  167. void AD7793_SetChannel(unsigned long channel)
  168. {
  169. unsigned long command;
  170. command = AD7793_GetRegisterValue(AD7793_REG_CONF,
  171. 2,
  172. 1); // CS is modified by SPI read/write functions.
  173. command &= ~AD7793_CONF_CHAN(0xFF);
  174. command |= AD7793_CONF_CHAN(channel);
  175. AD7793_SetRegisterValue(
  176. AD7793_REG_CONF,
  177. command,
  178. 2,
  179. 1); // CS is modified by SPI read/write functions.
  180. }
  181. /***************************************************************************//**
  182. * @brief Sets the gain of the In-Amp.
  183. *
  184. * @param gain - Gain.
  185. *
  186. * @return None.
  187. *******************************************************************************/
  188. void AD7793_SetGain(unsigned long gain)
  189. {
  190. unsigned long command;
  191. command = AD7793_GetRegisterValue(AD7793_REG_CONF,
  192. 2,
  193. 1); // CS is modified by SPI read/write functions.
  194. command &= ~AD7793_CONF_GAIN(0xFF);
  195. command |= AD7793_CONF_GAIN(gain);
  196. AD7793_SetRegisterValue(
  197. AD7793_REG_CONF,
  198. command,
  199. 2,
  200. 1); // CS is modified by SPI read/write functions.
  201. }
  202. /***************************************************************************//**
  203. * @brief Sets the reference source for the ADC.
  204. *
  205. * @param type - Type of the reference.
  206. * Example: AD7793_REFSEL_EXT - External Reference Selected
  207. * AD7793_REFSEL_INT - Internal Reference Selected.
  208. *
  209. * @return None.
  210. *******************************************************************************/
  211. void AD7793_SetIntReference(unsigned char type)
  212. {
  213. unsigned long command = 0;
  214. command = AD7793_GetRegisterValue(AD7793_REG_CONF,
  215. 2,
  216. 1); // CS is modified by SPI read/write functions.
  217. command &= ~AD7793_CONF_REFSEL(AD7793_REFSEL_INT);
  218. command |= AD7793_CONF_REFSEL(type);
  219. AD7793_SetRegisterValue(AD7793_REG_CONF,
  220. command,
  221. 2,
  222. 1); // CS is modified by SPI read/write functions.
  223. }
  224. /***************************************************************************//**
  225. * @brief Performs the given calibration to the specified channel.
  226. *
  227. * @param mode - Calibration type.
  228. * @param channel - Channel to be calibrated.
  229. *
  230. * @return none.
  231. *******************************************************************************/
  232. void AD7793_Calibrate(unsigned char mode, unsigned char channel)
  233. {
  234. unsigned short oldRegValue = 0x0;
  235. unsigned short newRegValue = 0x0;
  236. AD7793_SetChannel(channel);
  237. oldRegValue &= AD7793_GetRegisterValue(AD7793_REG_MODE, 2, 1); // CS is modified by SPI read/write functions.
  238. oldRegValue &= ~AD7793_MODE_SEL(0x7);
  239. newRegValue = oldRegValue | AD7793_MODE_SEL(mode);
  240. ADI_PART_CS_LOW;
  241. AD7793_SetRegisterValue(AD7793_REG_MODE, newRegValue, 2, 0); // CS is not modified by SPI read/write functions.
  242. AD7793_WaitRdyGoLow();
  243. ADI_PART_CS_HIGH;
  244. }
  245. /***************************************************************************//**
  246. * @brief Returns the result of a single conversion.
  247. *
  248. * @return regData - Result of a single analog-to-digital conversion.
  249. *******************************************************************************/
  250. unsigned long AD7793_SingleConversion(void)
  251. {
  252. unsigned long command = 0x0;
  253. unsigned long regData = 0x0;
  254. command = AD7793_MODE_SEL(AD7793_MODE_SINGLE);
  255. ADI_PART_CS_LOW;
  256. AD7793_SetRegisterValue(AD7793_REG_MODE,
  257. command,
  258. 2,
  259. 0);// CS is not modified by SPI read/write functions.
  260. AD7793_WaitRdyGoLow();
  261. regData = AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  262. ADI_PART_CS_HIGH;
  263. return(regData);
  264. }
  265. /***************************************************************************//**
  266. * @brief Returns the average of several conversion results.
  267. *
  268. * @return samplesAverage - The average of the conversion results.
  269. *******************************************************************************/
  270. unsigned long AD7793_ContinuousReadAvg(unsigned char sampleNumber)
  271. {
  272. unsigned long samplesAverage = 0x0;
  273. unsigned long command = 0x0;
  274. unsigned char count = 0x0;
  275. command = AD7793_MODE_SEL(AD7793_MODE_CONT);
  276. ADI_PART_CS_LOW;
  277. AD7793_SetRegisterValue(AD7793_REG_MODE,
  278. command,
  279. 2,
  280. 0);// CS is not modified by SPI read/write functions.
  281. for(count = 0;count < sampleNumber;count ++)
  282. {
  283. AD7793_WaitRdyGoLow();
  284. samplesAverage += AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions.
  285. }
  286. ADI_PART_CS_HIGH;
  287. samplesAverage = samplesAverage / sampleNumber;
  288. return(samplesAverage);
  289. }