找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1636|回复: 0
打印 上一主题 下一主题
收起左侧

AD7193驱动源程序

[复制链接]
跳转到指定楼层
#
  1. /******************************************************************************/
  2. #include "AD7193.h"                // AD7193 definitions.

  3. /******************************************************************************/
  4. /************************ Variables Definitions *******************************/
  5. /******************************************************************************/
  6. unsigned char currentPolarity = 0;
  7. unsigned char currentGain     = 1;

  8. /******************************************************************************/
  9. /************************ Functions Definitions *******************************/
  10. /******************************************************************************/

  11. /***************************************************************************//**
  12. * @brief Checks if the AD7139 part is present.
  13. *
  14. * @return status - Indicates if the part is present or not.
  15. *******************************************************************************/
  16. unsigned char AD7193_Init(void)
  17. {
  18.     unsigned char status = 1;
  19.     unsigned char regVal = 0;
  20.    
  21.     status = SPI_Init(0, 1000000, 1, 0);
  22.     regVal = AD7193_GetRegisterValue(AD7193_REG_ID, 1, 1);
  23.     if((regVal & AD7193_ID_MASK) != ID_AD7193)
  24.     {
  25.         status = 0;
  26.     }
  27.     return status;
  28. }

  29. /***************************************************************************//**
  30. * @brief Writes data into a register.
  31. *
  32. * @param registerAddress - Address of the register.
  33. * @param registerValue - Data value to write.
  34. * @param bytesNumber - Number of bytes to be written.
  35. * @param modifyCS - Allows Chip Select to be modified.
  36. *
  37. * @return none.
  38. *******************************************************************************/
  39. void AD7193_SetRegisterValue(unsigned char registerAddress,
  40.                              unsigned long registerValue,
  41.                              unsigned char bytesNumber,
  42.                              unsigned char modifyCS)
  43. {
  44.     unsigned char writeCommand[5] = {0, 0, 0, 0, 0};
  45.     unsigned char* dataPointer    = (unsigned char*)®isterValue;
  46.     unsigned char bytesNr         = bytesNumber;
  47.    
  48.     writeCommand[0] = AD7193_COMM_WRITE |
  49.                       AD7193_COMM_ADDR(registerAddress);
  50.     while(bytesNr > 0)
  51.     {
  52.         writeCommand[bytesNr] = *dataPointer;
  53.         dataPointer ++;
  54.         bytesNr --;
  55.     }
  56.     SPI_Write(AD7193_SLAVE_ID * modifyCS, writeCommand, bytesNumber + 1);
  57. }

  58. /***************************************************************************//**
  59. * @brief Reads the value of a register.
  60. *
  61. * @param registerAddress - Address of the register.
  62. * @param bytesNumber - Number of bytes that will be read.
  63. * @param modifyCS    - Allows Chip Select to be modified.
  64. *
  65. * @return buffer - Value of the register.
  66. *******************************************************************************/
  67. unsigned long AD7193_GetRegisterValue(unsigned char registerAddress,
  68.                                       unsigned char bytesNumber,
  69.                                       unsigned char modifyCS)
  70. {
  71.     unsigned char registerWord[5] = {0, 0, 0, 0, 0};
  72.     unsigned long buffer          = 0x0;
  73.     unsigned char i               = 0;
  74.    
  75.     registerWord[0] = AD7193_COMM_READ |
  76.                       AD7193_COMM_ADDR(registerAddress);
  77.     SPI_Read(AD7193_SLAVE_ID * modifyCS, registerWord, bytesNumber + 1);
  78.     for(i = 1; i < bytesNumber + 1; i++)
  79.     {
  80.         buffer = (buffer << 8) + registerWord[i];
  81.     }
  82.    
  83.     return buffer;
  84. }

  85. /***************************************************************************//**
  86. * @brief Resets the device.
  87. *
  88. * @return none.
  89. *******************************************************************************/
  90. void AD7193_Reset(void)
  91. {
  92.     unsigned char registerWord[6] = {0, 0, 0, 0, 0, 0};
  93.    
  94.     registerWord[0] = 0xFF;
  95.     registerWord[1] = 0xFF;
  96.     registerWord[2] = 0xFF;
  97.     registerWord[3] = 0xFF;
  98.     registerWord[4] = 0xFF;
  99.     registerWord[5] = 0xFF;
  100.     SPI_Write(AD7193_SLAVE_ID, registerWord, 6);
  101. }

  102. /***************************************************************************//**
  103. * @brief Set device to idle or power-down.
  104. *
  105. * @param pwrMode - Selects idle mode or power-down mode.
  106. *                  Example: 0 - power-down
  107. *                           1 - idle
  108. *
  109. * @return none.
  110. *******************************************************************************/
  111. void AD7193_SetPower(unsigned char pwrMode)
  112. {
  113.      unsigned long oldPwrMode = 0x0;
  114.      unsigned long newPwrMode = 0x0;

  115.      oldPwrMode = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
  116.      oldPwrMode &= ~(AD7193_MODE_SEL(0x7));
  117.      newPwrMode = oldPwrMode |
  118.                   AD7193_MODE_SEL((pwrMode * (AD7193_MODE_IDLE)) |
  119.                                   (!pwrMode * (AD7193_MODE_PWRDN)));
  120.      AD7193_SetRegisterValue(AD7193_REG_MODE, newPwrMode, 3, 1);  
  121. }

  122. /***************************************************************************//**
  123. * @brief Waits for RDY pin to go low.
  124. *
  125. * @return none.
  126. *******************************************************************************/
  127. void AD7193_WaitRdyGoLow(void)
  128. {
  129.     while(AD7193_RDY_STATE)
  130.     {
  131.         ;
  132.     }
  133. }

  134. /***************************************************************************//**
  135. * @brief Selects the channel to be enabled.
  136. *
  137. * @param channel - Selects a channel.
  138. *                  Example: AD7193_CH_0 - AIN1(+) - AIN2(-);  (Pseudo = 0)
  139. *                           AD7193_CH_1 - AIN3(+) - AIN4(-);  (Pseudo = 0)
  140. *                           AD7193_TEMP - Temperature sensor
  141. *                           AD7193_SHORT - AIN2(+) - AIN2(-); (Pseudo = 0)
  142. *  
  143. * @return none.
  144. *******************************************************************************/
  145. void AD7193_ChannelSelect(unsigned short channel)
  146. {
  147.     unsigned long oldRegValue = 0x0;
  148.     unsigned long newRegValue = 0x0;   
  149.      
  150.     oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1);
  151.     oldRegValue &= ~(AD7193_CONF_CHAN(0x3FF));
  152.     newRegValue = oldRegValue | AD7193_CONF_CHAN(1 << channel);   
  153.     AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
  154. }

  155. /***************************************************************************//**
  156. * @brief Performs the given calibration to the specified channel.
  157. *
  158. * @param mode - Calibration type.
  159. * @param channel - Channel to be calibrated.
  160. *
  161. * @return none.
  162. *******************************************************************************/
  163. void AD7193_Calibrate(unsigned char mode, unsigned char channel)
  164. {
  165.     unsigned long oldRegValue = 0x0;
  166.     unsigned long newRegValue = 0x0;
  167.    
  168.     AD7193_ChannelSelect(channel);
  169.     oldRegValue = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
  170.     oldRegValue &= ~AD7193_MODE_SEL(0x7);
  171.     newRegValue = oldRegValue | AD7193_MODE_SEL(mode);
  172.     PMOD1_CS_LOW;
  173.     AD7193_SetRegisterValue(AD7193_REG_MODE, newRegValue, 3, 0); // CS is not modified.
  174.     AD7193_WaitRdyGoLow();
  175.     PMOD1_CS_HIGH;
  176. }

  177. /***************************************************************************//**
  178. * @brief Selects the polarity of the conversion and the ADC input range.
  179. *
  180. * @param polarity - Polarity select bit.
  181.                      Example: 0 - bipolar operation is selected.
  182.                               1 - unipolar operation is selected.
  183. * @param range - Gain select bits. These bits are written by the user to select
  184.                  the ADC input range.     
  185. *
  186. * @return none.
  187. *******************************************************************************/
  188. void AD7193_RangeSetup(unsigned char polarity, unsigned char range)
  189. {
  190.     unsigned long oldRegValue = 0x0;
  191.     unsigned long newRegValue = 0x0;
  192.    
  193.     oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF,3, 1);
  194.     oldRegValue &= ~(AD7193_CONF_UNIPOLAR |
  195.                      AD7193_CONF_GAIN(0x7));
  196.     newRegValue = oldRegValue |
  197.                   (polarity * AD7193_CONF_UNIPOLAR) |
  198.                   AD7193_CONF_GAIN(range);
  199.     AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
  200.     /* Store the last settings regarding polarity and gain. */
  201.     currentPolarity = polarity;
  202.     currentGain = 1 << range;
  203. }

  204. /***************************************************************************//**
  205. * @brief Returns the result of a single conversion.
  206. *
  207. * @return regData - Result of a single analog-to-digital conversion.
  208. *******************************************************************************/
  209. unsigned long AD7193_SingleConversion(void)
  210. {
  211.     unsigned long command = 0x0;
  212.     unsigned long regData = 0x0;

  213.     command = AD7193_MODE_SEL(AD7193_MODE_SINGLE) |
  214.               AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
  215.               AD7193_MODE_RATE(0x060);   
  216.     PMOD1_CS_LOW;
  217.     AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
  218.     AD7193_WaitRdyGoLow();
  219.     regData = AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0);
  220.     PMOD1_CS_HIGH;
  221.    
  222.     return regData;
  223. }

  224. /***************************************************************************//**
  225. * @brief Returns the average of several conversion results.
  226. *
  227. * @return samplesAverage - The average of the conversion results.
  228. *******************************************************************************/
  229. unsigned long AD7193_ContinuousReadAvg(unsigned char sampleNumber)
  230. {
  231.     unsigned long samplesAverage = 0;
  232.     unsigned long command        = 0;
  233.     unsigned char count          = 0;
  234.         
  235.     command = AD7193_MODE_SEL(AD7193_MODE_CONT) |
  236.               AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
  237.               AD7193_MODE_RATE(0x060);
  238.     PMOD1_CS_LOW;
  239.     AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
  240.     for(count = 0; count < sampleNumber; count++)
  241.     {
  242.         AD7193_WaitRdyGoLow();
  243.         samplesAverage += AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0); // CS is not modified.
  244.     }
  245.     PMOD1_CS_HIGH;
  246.     samplesAverage = samplesAverage / sampleNumber;
  247.    
  248.     return samplesAverage;
  249. }

  250. /***************************************************************************//**
  251. * @brief Read data from temperature sensor and converts it to Celsius degrees.
  252. *
  253. * @return temperature - Celsius degrees.
  254. *******************************************************************************/
  255. unsigned long AD7193_TemperatureRead(void)
  256. {
  257.     unsigned long dataReg     = 0;
  258.     unsigned long temperature = 0;   
  259. ……………………

  260. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码



原理图: 无
仿真: 无
上图4个代码文件: ad7193_generic.zip (10.93 KB, 下载次数: 26)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表