[资料分享] ads1118驱动分享--EXP430G2

[复制链接]
 楼主| xyz549040622 发表于 2019-12-21 10:48 | 显示全部楼层 |阅读模式
本帖最后由 xyz549040622 于 2019-12-21 23:18 编辑
  1. /*
  2. * main.c
  3. *
  4. * MSP-EXP430G2-LaunchPad (v1.5+) User Experience Application for
  5. *      Communication to ADS1118
  6. *
  7. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  8. *
  9. *
  10. *  Redistribution and use in source and binary forms, with or without
  11. *  modification, are permitted provided that the following conditions
  12. *  are met:
  13. *
  14. *    Redistributions of source code must retain the above copyright
  15. *    notice, this list of conditions and the following disclaimer.
  16. *
  17. *    Redistributions in binary form must reproduce the above copyright
  18. *    notice, this list of conditions and the following disclaimer in the
  19. *    documentation and/or other materials provided with the
  20. *    distribution.
  21. *
  22. *    Neither the name of Texas Instruments Incorporated nor the names of
  23. *    its contributors may be used to endorse or promote products derived
  24. *    from this software without specific prior written permission.
  25. *
  26. *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. */


  39. /******************************************************************************
  40. * MSP-EXP430G2-LaunchPad User Experience Application for Communication to ADS1118
  41. *
  42. * 1. Program uses GRACE configuration for Device Configuration;
  43. *                 - SMCLK, ACLK, DCO at 1MHz
  44. *                 - USCIA0 as UART, 9600, 8 bits Data, No parity, 1 Stop (Hardware Mode)
  45. *                 - USCIB0 as 3 line SPI, Master Mode, 50kHz SCLK
  46. *                 - P2.0 used as CS for ADS1118 ADC device
  47. *
  48. * 2. Application Mode
  49. *           - Initial start configures device on power up
  50. *           - Configures connected ADS1118 device for differential measurement
  51. *                   pairs of AIN0-AIN1 and AIN2-AIN3
  52. *           - Data is transmitted in continuous loop
  53. *           - Runs ADS1118 in single shot mode reading first pair, changing mux and then
  54. *                   second pair storing data in a data array
  55. *           - After data is read, data is sent out UART for display as ASCII hex representation
  56. *
  57. * Changes:
  58. * 1.0  Initial Release Version
  59. *
  60. * Texas Instruments, Inc.
  61. ******************************************************************************/



  62. /*
  63. * ======== Standard MSP430 includes ========
  64. */
  65. #include <msp430g2553.h>

  66. /*
  67. * ======== Grace related includes ========
  68. */
  69. #include <ti/mcu/msp430/csl/CSL.h>


  70. /*
  71. *  ======== Function Calls ========
  72. */

  73. void uart_txc(char c);
  74. void uart_txstr(char *c);
  75. /* "hex2asc" Converts a 32-bit integer n into an ASCII string.

  76. digs is the maximum number of digits to display.  Conversion is controlled
  77. by mode, as follows:

  78. - mode = 0: Leading zeroes are not printed.  The string may be
  79.   less than digs digits long.
  80. - mode = 1: Spaces are printed in place of leading zeroes.  The
  81.   string will be digs digits long.
  82. - mode = 2: Leading zeroes are printed.  The string will be digs
  83.   digits long.

  84. If the number is zero, at least one zero is printed in all modes.

  85. This routine works by converting n to an 8-byte BCD number and calling
  86. hex2asc.  No division by 10 is performed.
  87. */

  88. int hex2asc(void *n, int digs, int mode, char *s);

  89. void ADS_Config(void);
  90. void ADS_Read(int data[]);
  91. void Send_Result(int *data);
  92. void Port_Config(void);
  93. signed int WriteSPI(unsigned int config, int mode);
  94. void delay(void);

  95. #define h2a(d) ((d>9)?(d+'A'-10):(d+'0'))
  96. #define LITTLEENDIAN 1

  97. /*
  98. *  ======== main ========
  99. */
  100. int main(int argc, char *argv[])
  101. {
  102.     CSL_init();                     // Activate Grace-generated configuration
  103.    
  104.     // >>>>> Fill-in user code here <<<<<

  105.     // Initialize TC data array

  106.     signed int data[6];

  107.     // Port configuration
  108.     Port_Config();

  109.     // Set ADS1118 configuration
  110.     ADS_Config();
  111.     while (1)
  112.     {
  113.             // Read the data from both input pairs
  114.             ADS_Read(data);


  115.             // Transmit the data out the UART
  116.             Send_Result(data);
  117.     }


  118.     return (0);
  119. }

  120. void Port_Config(void)
  121. {
  122.         // Set P1.0, P1.3, P1.4, P2.1, P2.2, P2.4, P2.5, P2.6 and P2.7 low

  123.         P1OUT = 0x00;
  124.         P2OUT = 0x01 ;
  125. }


  126. /*
  127. * Initial configuration routine.  A header file could be created, but the configuration is really rather simple.
  128. * In this case a 16-bit value representing the register contents is set to variable temp
  129. */
  130. void ADS_Config(void)
  131. {
  132.         int i;

  133.         unsigned int temp;

  134.         // Set the configuration to AIN0/AIN1, FS=+/-1.024, SS, DR=128sps, PULLUP on DOUT
  135.         temp = 0x78A;


  136.         // Set CS low and write configuration
  137.         P2OUT &= ~BIT0;


  138.         // Write the configuration
  139.         WriteSPI(temp,0);

  140.         // Set CS high to end transaction
  141.         P2OUT |= BIT0;
  142. }



  143. void ADS_Read(int data[])
  144. {
  145.         unsigned int j, temp;

  146.         // Set the configuration to AIN0/AIN1, FS=+/-1.024, SS, DR=128sps, PULLUP on DOUT
  147.         temp = 0x78A;

  148.         // Set CS low and write configuration
  149.         P2OUT &= ~BIT0;

  150.         // First the data is captured by writing to each device to take start a conversion for A0-A1
  151.         WriteSPI(temp,1);

  152.         // Set CS high to end transaction
  153.         P2OUT |= BIT0;

  154.                 /*
  155.                  * Now we pause slightly before reading the data, or it is possible to either poll the DOUT/DRDY or enable an interrupt
  156.                  * where the DOUT/DRDY transition from high to low triggers a read.  In this case it is kept quite simple with a delay
  157.                  */

  158.         delay();                // May be needed depending on method

  159.         // When we read the data we restart the conversion with new mux channel A2-A3
  160.         temp = 0x378A;

  161.         // Set CS low and write configuration
  162.         P2OUT &= ~BIT0;

  163.         // Read the earlier conversion result and set to the new configuration
  164.         data[0] = WriteSPI(temp,1);

  165.         delay();                // May be needed depending on method

  166.         // Read second channel data
  167.         data[1]=WriteSPI(temp,0);

  168.         // Set CS high to end transaction
  169.         P2OUT |= BIT0;


  170. }


  171. signed int WriteSPI(unsigned int config, int mode)
  172. {
  173.         signed int msb;
  174.         unsigned int temp;
  175.         char dummy;

  176.         temp = config;

  177.         if (mode==1) temp = config | 0x8000;        // if mode is set to 1, this command should initiate a conversion

  178. /*
  179. * The process of communication chosen is to always send the configuration and read it back
  180. * this results in a four byte transaction.  The configuration is 16-bit (or 2 bytes) and is transmitted twice.
  181. *
  182. */
  183.         while(!(UC0IFG&UCB0TXIFG));                // Make sure buffer is clear

  184.         /*
  185.          *  First time configuration is written
  186.          */

  187.         UCB0TXBUF = (temp >> 8 );                // Write MSB of Config
  188.         while(!(UC0IFG&UCB0RXIFG));
  189.         msb=UCB0RXBUF;                                        // Read MSB of Result

  190.         while(!(UC0IFG&UCB0TXIFG));

  191.         UCB0TXBUF= (temp & 0xff);                // Write LSB of Config
  192.         while(!(UC0IFG&UCB0RXIFG));
  193.         msb = (msb << 8) | UCB0RXBUF ;        //Read LSB of Result

  194.         /*
  195.          * Second time configuration is written, although data could be sent as NOP in either transmission, just simplified in this case
  196.          */

  197.         while(!(UC0IFG&UCB0TXIFG));
  198.         UCB0TXBUF = (temp >> 8 );                // Write MSB of Config

  199.         while(!(UC0IFG&UCB0RXIFG));
  200.         dummy=UCB0RXBUF;                                // Read MSB of Config

  201.         /*
  202.          * One advantage of reading the config data is that DOUT/DRDY is forced high which makes it possible to either poll the state or set an interrupt
  203.          */
  204.         while(!(UC0IFG&UCB0TXIFG));
  205.         UCB0TXBUF= (temp & 0xff);                // Write LSB of Config

  206.         while(!(UC0IFG&UCB0RXIFG));
  207.         dummy=UCB0RXBUF;                                //Read LSB of Config


  208.         return msb;
  209. }




  210. /*
  211. * Following code relates to formatting the data for transmission on UART
  212. */

  213. void Send_Result(int *data)
  214. {
  215.         unsigned int i;
  216.         int intval = 0;
  217.         char char_array[5];

  218.         // Poke out data
  219.         uart_txstr("TEMPS:");
  220.         uart_txc('\r');
  221.         uart_txc('\n');
  222.         for (i=0; i<2; i++)
  223.         {
  224.                 intval = data[i];
  225.                 hex2asc(&intval, 4, 2, char_array);
  226.                 uart_txstr(char_array);
  227.                 uart_txc('\r');
  228.                 uart_txc('\n');
  229.         }
  230. }





  231. int hex2asc(void *npos, int digs, int mode, char *s)
  232. {
  233.         int i,zero;
  234.         char dig;
  235.         char *spos=s;
  236.         char *n=(char *)npos;

  237.         zero=1;
  238. #if LITTLEENDIAN
  239.         n+=(digs-1)>>1;
  240. #else
  241.         n+=(16-digs)>>1;
  242. #endif
  243.         for (i=digs-1;i>=0;--i) {
  244.                 if (i&1) {
  245.                         dig=(*(char *)n>>4)&15;
  246.                 } else {
  247.                         dig=*(char *)n&15;
  248. #if LITTLEENDIAN
  249.                         --n;
  250. #else
  251.                         ++n;
  252. #endif
  253.                 }
  254.                 if (zero&&dig)
  255.                         zero=0;
  256.                 if (zero) {
  257.                         switch(mode) {
  258.                         case 1:
  259.                                 *spos++=' ';
  260.                                 break;
  261.                         case 2:
  262.                                 *spos++='0';
  263.                                 break;
  264.                         default:
  265.                                 break;
  266.                         }
  267.                 } else
  268.                         *spos++=h2a(dig);
  269.         }
  270.         if (zero&&mode==1)
  271.                 *(spos-1)='0';
  272.         else if (zero&&mode==0)
  273.                 *spos++='0';
  274.         *spos=0;
  275.         return spos-s;
  276. }
  277. void uart_txc(char c)
  278. {
  279.         while (!((UC0IFG&UCA0TXIFG)));
  280.         UCA0TXBUF=c;

  281. }

  282. void uart_txstr(char *c)
  283. {
  284.         while (*c) uart_txc(*(c++));
  285. }

  286. void delay(void)
  287. {
  288.         unsigned int k;

  289.         for (k = 8000; k = 0; k--) __no_operation();

  290. }


 楼主| xyz549040622 发表于 2019-12-21 10:48 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
internally 发表于 2019-12-23 19:44 | 显示全部楼层
感谢楼主分享!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群: 嵌入式系统arm初学者 224636155←← +→→点击-->小 i 精品课全集,21ic公开课~~←←→→点击-->小 i 精品课全集,给你全方位的技能策划~~←←

2841

主题

19330

帖子

110

粉丝
快速回复 在线客服 返回列表 返回顶部