AD7793.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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: 381
  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. * @param None.
  51. *
  52. * @return status - Result of the initialization procedure.
  53. * Example: 1 - if initialization was successful (ID is 0x0B).
  54. * 0 - if initialization was unsuccessful.
  55. *******************************************************************************/
  56. unsigned char AD7793_Init(void)
  57. {
  58. unsigned char status = 0x1;
  59. if((AD7793_GetRegisterValue(AD7793_REG_ID, 1) & 0x0F) != AD7793_ID)
  60. {
  61. status = 0x0;
  62. }
  63. return(status);
  64. }
  65. /***************************************************************************//**
  66. * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
  67. *
  68. * @param None.
  69. *
  70. * @return None.
  71. *******************************************************************************/
  72. void AD7793_Reset(void)
  73. {
  74. unsigned char dataToSend[5] = {0x03, 0xff, 0xff, 0xff, 0xff};
  75. AD7793_CS_LOW;
  76. SPI_Write(dataToSend,4);
  77. AD7793_CS_HIGH;
  78. }
  79. /***************************************************************************//**
  80. * @brief Reads the value of the selected register
  81. *
  82. * @param regAddress - The address of the register to read.
  83. * @param size - The size of the register to read.
  84. *
  85. * @return data - The value of the selected register register.
  86. *******************************************************************************/
  87. unsigned long AD7793_GetRegisterValue(unsigned char regAddress, unsigned char size)
  88. {
  89. unsigned char data[5] = {0x03, 0x00, 0x00, 0x00, 0x00};
  90. unsigned long receivedData = 0x00;
  91. data[1] = AD7793_COMM_READ | AD7793_COMM_ADDR(regAddress);
  92. AD7793_CS_LOW;
  93. SPI_Write(data,1);
  94. SPI_Read(data,size);
  95. AD7793_CS_HIGH;
  96. if(size == 1)
  97. {
  98. receivedData += (data[0] << 0);
  99. }
  100. if(size == 2)
  101. {
  102. receivedData += (data[0] << 8);
  103. receivedData += (data[1] << 0);
  104. }
  105. if(size == 3)
  106. {
  107. receivedData += (data[0] << 16);
  108. receivedData += (data[1] << 8);
  109. receivedData += (data[2] << 0);
  110. }
  111. return receivedData;
  112. }
  113. /***************************************************************************//**
  114. * @brief Writes the value to the register
  115. *
  116. * @param - regAddress - The address of the register to write to.
  117. * @param - regValue - The value to write to the register.
  118. * @param - size - The size of the register to write.
  119. *
  120. * @return None.
  121. *******************************************************************************/
  122. void AD7793_SetRegisterValue(unsigned char regAddress,
  123. unsigned long regValue,
  124. unsigned char size)
  125. {
  126. unsigned char data[5] = {0x03, 0x00, 0x00, 0x00, 0x00};
  127. data[1] = AD7793_COMM_WRITE | AD7793_COMM_ADDR(regAddress);
  128. if(size == 1)
  129. {
  130. data[2] = (unsigned char)regValue;
  131. }
  132. if(size == 2)
  133. {
  134. data[3] = (unsigned char)((regValue & 0x0000FF) >> 0);
  135. data[2] = (unsigned char)((regValue & 0x00FF00) >> 8);
  136. }
  137. if(size == 3)
  138. {
  139. data[4] = (unsigned char)((regValue & 0x0000FF) >> 0);
  140. data[3] = (unsigned char)((regValue & 0x00FF00) >> 8);
  141. data[2] = (unsigned char)((regValue & 0xFF0000) >> 16);
  142. }
  143. AD7793_CS_LOW;
  144. SPI_Write(data,(1 + size));
  145. AD7793_CS_HIGH;
  146. }
  147. /***************************************************************************//**
  148. * @brief Reads /RDY bit of status reg.
  149. *
  150. * @param None.
  151. *
  152. * @return rdy - 0 if RDY is 1.
  153. * - 1 if RDY is 0.
  154. *******************************************************************************/
  155. unsigned char AD7793_Ready(void)
  156. {
  157. unsigned char rdy = 0;
  158. rdy = (AD7793_GetRegisterValue( AD7793_REG_STAT,1) & 0x80);
  159. return(!rdy);
  160. }
  161. /***************************************************************************//**
  162. * @brief Sets the operating mode of AD7793.
  163. *
  164. * @param mode - Mode of operation.
  165. *
  166. * @return None.
  167. *******************************************************************************/
  168. void AD7793_SetMode(unsigned long mode)
  169. {
  170. unsigned long command;
  171. command = AD7793_GetRegisterValue(AD7793_REG_MODE,2);
  172. command &= ~AD7793_MODE_SEL(0xFF);
  173. command |= AD7793_MODE_SEL(mode);
  174. AD7793_SetRegisterValue(
  175. AD7793_REG_MODE,
  176. command,
  177. 2
  178. );
  179. }
  180. /***************************************************************************//**
  181. * @brief Selects the channel of AD7793.
  182. *
  183. * @param channel - ADC channel selection.
  184. *
  185. * @return None.
  186. *******************************************************************************/
  187. void AD7793_SetChannel(unsigned long channel)
  188. {
  189. unsigned long command;
  190. command = AD7793_GetRegisterValue(AD7793_REG_CONF,2);
  191. command &= ~AD7793_CONF_CHAN(0xFF);
  192. command |= AD7793_CONF_CHAN(channel);
  193. AD7793_SetRegisterValue(
  194. AD7793_REG_CONF,
  195. command,
  196. 2
  197. );
  198. }
  199. /***************************************************************************//**
  200. * @brief Sets the gain of the In-Amp.
  201. *
  202. * @param gain - Gain.
  203. *
  204. * @return None.
  205. *******************************************************************************/
  206. void AD7793_SetGain(unsigned long gain)
  207. {
  208. unsigned long command;
  209. command = AD7793_GetRegisterValue(AD7793_REG_CONF,2);
  210. command &= ~AD7793_CONF_GAIN(0xFF);
  211. command |= AD7793_CONF_GAIN(gain);
  212. AD7793_SetRegisterValue(
  213. AD7793_REG_CONF,
  214. command,
  215. 2
  216. );
  217. }
  218. /***************************************************************************//**
  219. * @brief Sets the reference source for the ADC.
  220. *
  221. * @param type - Type of the reference.
  222. * Example: AD7793_REFSEL_EXT - External Reference Selected
  223. * AD7793_REFSEL_INT - Internal Reference Selected.
  224. *
  225. * @return None.
  226. *******************************************************************************/
  227. void AD7793_SetReference(unsigned char type)
  228. {
  229. unsigned long command = 0;
  230. command = AD7793_GetRegisterValue(AD7793_REG_CONF,2);
  231. command &= ~AD7793_CONF_REFSEL(AD7793_REFSEL_INT);
  232. command |= AD7793_CONF_REFSEL(type);
  233. AD7793_SetRegisterValue(AD7793_REG_CONF,
  234. command,
  235. 2);
  236. }