数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4850|回复: 38

[综合] 微型高压充气泵,带气压传感器

[复制链接]
发表于 2021-11-4 20:37:42 | 显示全部楼层 |阅读模式
可惜不会玩,CS1237 ADC采集,拜托eagler8大神,找了个esp8266 arduino的库,修改了一下,还是运行不起来
求大神解惑,运行了几次,虽然后面有数据,但是应该还是错误的


  1. 20:20:17.572 -> -------------
  2. 20:20:17.572 -> ADC
  3. 20:20:17.572 ->
  4. 20:20:17.572 -> Constructor:        0
  5. 20:20:17.572 -> Bit Delay :150 ns        50 ns.
  6. 20:20:17.572 -> Error: begin: Constructor was not successfull:        0
  7. 20:20:17.572 -> begin: Init error
  8. 20:20:18.571 -> 0        0
复制代码

  1. 20:21:19.550 -> -------------
  2. 20:21:19.550 -> ADC
  3. 20:21:19.550 ->
  4. 20:21:19.550 -> Constructor:        0
  5. 20:21:19.550 -> Bit Delay :150 ns        50 ns.
  6. 20:21:19.598 -> begin: Waking up time (us):        15005
  7. 20:21:19.598 -> write cnf (us)        69
  8. 20:21:19.598 -> reread cnf (us)        63
  9. 20:21:19.598 -> begin: Error: compare.
  10. 20:21:19.598 -> cnfWr..cnfRd..ADC: C        0        0
  11. 20:21:19.598 -> begin: Init error
  12. 20:21:20.597 -> 0        0
复制代码



  1. 20:23:32.563 -> -------------
  2. 20:23:32.563 -> ADC
  3. 20:23:32.563 ->
  4. 20:23:32.563 -> Constructor:        0
  5. 20:23:32.563 -> Bit Delay :150 ns        50 ns.
  6. 20:23:32.892 -> begin: Waking up time (us):        313978
  7. 20:23:32.987 -> write cnf (us)        69
  8. 20:23:32.987 -> reread cnf (us)        103952
  9. 20:23:32.987 -> begin: Error: compare.
  10. 20:23:32.987 -> cnfWr..cnfRd..ADC: C        0        77962
  11. 20:23:32.987 -> begin: Init error
  12. 20:23:33.979 -> 77962        489826
  13. 20:23:34.980 -> 77962        489826
  14. 20:23:35.981 -> 77962        489826
  15. 20:23:36.977 -> 77962        489826
复制代码




库和程序的链接:https://github.com/SiBangkotan/CS1237-ADC-cpp-library/blob/master/CS1237.ino

CS1237-ADC-cpp-library
ChipSea ADC CS1237 的库

专为 ESP8266 编写,在汇编中包含一些时序关键部分。

是在 Arduino IDE 环境中生成的。

ADC 本身声称具有 24 位深度。这种精度水平必须通过非常好的 PCB 设计、电源和通用 EMI 保护来实现。否则,即使是最小的噪声也会蔓延到测量中。所以我对这个 IC 的测试对于我未能避免的这么多干扰是无效的。尽管如此,通过 1x PGA 模拟增益和 10 sps 的设置实现了 18 位的精度。通过配置位对输入放大器进行内部短路测试,达到低于 4 位的噪声水平。也许更好的电源会产生更好的结果。

最大采样率为 1280 sps。能够捕捉 50Hz 电源的 11 次谐波。但是这个 CS1237 主要是为体重数字秤设计的。

提高 IC 前端的可编程模拟增益设置会显着提高数字化噪底。

Rafael Coelho 在https://github.com/rafaellcoellho/cs1237-datasheet准备的翻译数据表







  1. /*********************************************************** CS1237 ADC library

  2.      MIT License

  3.   Copyright (c) 2020    GitHubName:SiBangkotan

  4.   Permission is hereby granted, free of charge, to any person obtaining a copy
  5.   of this software and associated documentation files (the "Software"), to deal
  6.   in the Software without restriction, including without limitation the rights
  7.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8.   copies of the Software, and to permit persons to whom the Software is
  9.   furnished to do so, subject to the following conditions:

  10.   The above copyright notice and this permission notice shall be included in all
  11.   copies or substantial portions of the Software.

  12.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18.   SOFTWARE.




  19. ******************************************************************************/
  20. #ifndef CS1237_LIB_H
  21. #define CS1237_LIB_H

  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif


  25. /*

  26.   CS1237lib Status Register

  27.   Bit 7 bit26 of last ADC response
  28.   Bit 6 bit25 of last ADC response
  29.   Bit 5 initialize sucessfull
  30.   Bit 4 new data is ready at ADCread

  31.   Bit 3 sleeping
  32.   Bit 2 reserved
  33.   Bit 1 config write cycle is pending
  34.   Bit 0 config read cycle is pending
  35. */
  36. //bit position
  37. #define SRB_BIT26_UPDATE2     7
  38. #define SRB_BIT25_UPDATE1     6
  39. #define SRB_INIT_SUCCESS    5
  40. #define SRB_NEW_DATA_READY    4
  41. #define SRB_ADC_SLEEPING    3
  42. //#define SRB_RESERVED        2
  43. #define SRB_WRITE_CNF_PENDING   1
  44. #define SRB_READ_CNF_PENDING  0

  45. //mask
  46. #define SRM_BIT26_UPDATE2     0x80
  47. #define SRM_BIT25_UPDATE1     0x40
  48. #define SRM_INIT_SUCCESS      0x20
  49. #define SRM_NEW_DATA_READY    0x10
  50. #define SRM_ADC_SLEEPING      0x08
  51. //#define SRM_RESERVED        0x04
  52. #define SRM_WRITE_CNF_PENDING  0x02
  53. #define SRM_READ_CNF_PENDING   0x01


  54. /*

  55.   CS1237 ADC Config Register shadow

  56.   default value after reset 0Ch:
  57.   REFO_OFF  0 , REF Output active
  58.   SPEED_SEL 0 , Sampling rate 10 Hz
  59.   PGA_SEL   3 , Analog input Gain 128 x
  60.   CH_SEL    0 , Channel A


  61.   Bit 7 reserved, must be 0
  62.   Bit 6 REFO_OFF
  63.   Bit 5-4 SPEED_SEL   30h 1280 Hz      :sample rate
  64.                       20h  640 Hz
  65.                       10h   40 Hz
  66.                       00h   10 Hz
  67.   Bit 3-2 PGA_SEL     0Ch  128 x       :analog gain
  68.                       08h   64 x
  69.                       04h    2 x
  70.                       00h    1 x
  71.   Bit 1-0 CH_SEL      03h Internal short :channel select
  72.                       02h Temperature
  73.                       01h Chip retention
  74.                       00h Channel A (normal #define SPEED_SEL_1280        1280
  75.   opr)

  76. */
  77. //Config Register Bit (CRB) position
  78. #define CRB_REFO_OFF           6
  79. #define CRB_SPEED_SEL          4
  80. #define CRB_PGA_SEL            2
  81. #define CRB_CH_SEL             0

  82. //Config Register Mask (CRM) pattren
  83. #define CRM_REFO_OFF          0x40
  84. #define CRM_SPEED_SEL         0x30
  85. #define CRM_PGA_SEL           0x0C
  86. #define CRM_CH_SEL            0x03

  87. //Config Register parameter
  88. #define SPEED_SEL_1280        0x30
  89. #define SPEED_SEL_640         0x20
  90. #define SPEED_SEL_40          0x10
  91. #define SPEED_SEL_10          0x00

  92. #define PGA_SEL_128           0x0C
  93. #define PGA_SEL_64            0x08
  94. #define PGA_SEL_2             0x04
  95. #define PGA_SEL_1             0x00

  96. #define CH_SEL_CHANNEL_A        0
  97. //#define CH_SEL_Chip_retention          1
  98. #define CH_SEL_TEMPERATURE      2
  99. #define CH_SEL_INTERNAL_SHORT   3

  100. #define REFO_OFF_FALSE        false
  101. #define REFO_OFF_TRUE         true

  102. /*

  103.    I/O addr


  104. */

  105. const uint32_t cstGPOS = 0x60000304 ;
  106. const uint32_t cstGPOC = 0x60000308 ;
  107. const uint32_t cstGPES = 0x60000310 ;
  108. const uint32_t cstGPEC = 0x60000314 ;
  109. const uint32_t cstGPI  = 0x60000318 ;


  110. class CS1237 {

  111.   public:
  112.     CS1237(int _datapinno, int _clkpinno);
  113.     bool    begin(int _gain , int _samplerate , int _channel = CH_SEL_CHANNEL_A, bool _refo_off = 0);
  114.     uint8_t statusReg;          // this instance status register
  115.     uint8_t statusReg2;          // this instance status register
  116.     uint8_t configReg;          // last read hardware config register shadow
  117.     bool    ADCpool();          // test if new data available, read it to ADCdata
  118.     int     ADCdata;            // last ADC data memory register
  119.     void    sleep(bool _powerSaveMode);
  120.     //uint32_t samples[256];    // interrupt data buffer


  121.   private:
  122.     uint32_t twi_sda;             // this instance data pin mask
  123.     uint32_t twi_scl;             // this instance clk pin mask
  124.     char     twi_sda_pin;         // this instance data pin
  125.     char     twi_scl_pin;         // this instance clk pin
  126.     uint8_t  configRegW;
  127.     bool     ADCread();           // word level serdes
  128.     bool     one_bit(bool bit = 1); // bit level serdes
  129.     //void     InterruptServiceRoutinex();
  130.     //uint32_t sampleWritePointer;

  131. };

  132. #ifdef __cplusplus
  133. }
  134. #endif

  135. #endif // CS1237_LIB_H
  136. //********************************************************************
  137. //  .cpp




  138. //#include <CS1237.h>
  139. //#include "CS1237.h"



  140. /**********************************************************************

  141.      Constructor. Hardware related such as pin number, test pin functionality

  142. *********************************************************************/

  143. CS1237::CS1237(int _datapinno, int _clkpinno)
  144. {
  145.   //save pin no and create handy bit mask
  146.   twi_sda = (1 << _datapinno);
  147.   twi_scl = (1 << _clkpinno) ;
  148.   twi_sda_pin = _datapinno;   // this instance data pin
  149.   twi_scl_pin = _clkpinno ;   // this instance clk pin

  150.   //test and setup pins
  151.   statusReg |= SRM_INIT_SUCCESS;
  152.   //statusReg2=0;
  153.   unsigned long tmpReg;

  154.   //test clk stuck low
  155.   pinMode(twi_scl_pin, INPUT_PULLUP);
  156.   delay(3);
  157.   if (digitalRead(twi_scl_pin) == LOW)
  158.   {
  159.     statusReg &= ~SRM_INIT_SUCCESS;
  160.   }
  161.   
  162.   //test clk stuck high
  163.   digitalWrite(twi_scl_pin, HIGH);
  164.   pinMode(twi_scl_pin, OUTPUT);
  165.   digitalWrite(twi_scl_pin, LOW);
  166.   tmpReg = digitalRead(twi_scl_pin);
  167.   digitalWrite(twi_scl_pin, HIGH);
  168.   if (tmpReg)
  169.   {
  170.     statusReg &= ~SRM_INIT_SUCCESS;
  171.   }
  172.   //exit as output HIGH in attemp to persuade ADC to sleep

  173.   //test sda stuck high
  174.   pinMode(twi_sda_pin, OUTPUT_OPEN_DRAIN);
  175.   digitalWrite(twi_sda_pin, LOW);
  176.   delay(1);
  177.   if (digitalRead(twi_sda_pin) == HIGH)
  178.   {
  179.     statusReg &= ~SRM_INIT_SUCCESS;
  180.   }

  181.   //test sda stuck low
  182.   pinMode(twi_sda_pin, INPUT_PULLUP);
  183.   digitalWrite(twi_sda_pin, HIGH);
  184.   delay(3);
  185.   if (digitalRead(twi_sda_pin) == LOW)
  186.   {
  187.     statusReg &= ~SRM_INIT_SUCCESS;
  188.   }

  189.   //exit with: GPIO output block set to low open drain
  190.   //           but overall "output block" then disabled.
  191.   //           GPIO is set as input with pull up.
  192.   //           to output a LOW just need to reenable "output block"
  193.   pinMode(twi_sda_pin, OUTPUT_OPEN_DRAIN);
  194.   digitalWrite(twi_sda_pin, LOW);
  195.   pinMode(twi_sda_pin, INPUT_PULLUP);
  196. }
  197. /*CS1237::~CS1237()
  198.   {
  199.   //debugprint("Warning: CS1237 deconstruct not supported");
  200.   //errorblink(NO_DECONSTRUCT);//the CS1237 object is hard wired at PCB, not software alterable
  201.   }*/


  202. /*****************************************************************

  203.     "begin()" function, handle user settable parameter.

  204. *****************************************************************/



  205. bool CS1237::begin(int _gain , int _samplerate , int _channel , bool _refo_off )
  206. {
  207.   //check constructor
  208.   if (!(statusReg & SRM_INIT_SUCCESS))
  209.   {
  210.     //Debug print
  211.     Serial.print("Error: begin: Constructor was not successfull:\t");
  212.     Serial.println(statusReg,HEX);
  213.     return 0;
  214.   }

  215.   //Reset ADC via sleep
  216.   sleep(1);                                        //put ADC on a nap
  217.   delay(10);
  218.   sleep(0);                                        //wake it up, now the ADC is in a known clk state
  219.   uint32_t dRdy = 0;
  220.   uint32_t startTime = micros();
  221.   uint32_t nowTime = startTime;
  222.   do {
  223.     dRdy = (digitalRead(twi_sda_pin));             //should go low
  224.     nowTime = micros();
  225.   } while ((nowTime - startTime < 8000) && dRdy);  //in 3,125 or 6,25ms++ (datasheet page 9)
  226.   if (dRdy)                                        //if still high, have to use yield
  227.   {
  228.     do
  229.     {
  230.       yield();
  231.       dRdy = (digitalRead(twi_sda_pin));
  232.       nowTime = micros();
  233.     } while ((nowTime - startTime < 320000) && dRdy); //it may go up to 70 or 300ms++ (datasheet p9)
  234.   }
  235.   if (dRdy) {
  236.     statusReg &= ~SRM_INIT_SUCCESS;                //error condition
  237.     //Debug print
  238.     Serial.println("begin: Error: Time out waking up from sleep.");
  239.     return 0;
  240.   } else {
  241.     Serial.print("begin: Waking up time (us):\t");
  242.     Serial.println(nowTime - startTime);

  243.   }


  244.   //
  245.   //Configure
  246.   //
  247.   configRegW = 0x00;

  248.   //PGA_SEL
  249.   if      (_gain >= 100) configRegW |= (PGA_SEL_128); //Gain=128
  250.   else if (_gain >= 10) configRegW |= (PGA_SEL_64); //Gain= 64
  251.   else if (_gain >=  2) configRegW |= (PGA_SEL_2); //Gain=  2
  252.   //else                             PGA_SEL_1       Gain=  1


  253.   //SPEED_SEL
  254.   if      (_samplerate >= 1000) configRegW |= (SPEED_SEL_1280); //Sampling 1280Hz, 753 us
  255.   else if (_samplerate >= 200) configRegW |= (SPEED_SEL_640 ); //Sampling 640Hz, 1562 us
  256.   else if (_samplerate >=  20) configRegW |= (SPEED_SEL_40  ); //Sampling 40Hz, 25ms
  257.   //else                                   SPEED_SEL_10     //Sampling 10Hz, 100ms


  258.   //CH_SEL
  259.   configRegW |= ((_channel & 3));    //CH_SEL two bit channel selector

  260.   if (_refo_off) configRegW |= (CRM_REFO_OFF); //REFO_OFF


  261.   //
  262.   //Send out config
  263.   //
  264.   statusReg |= (SRM_WRITE_CNF_PENDING | SRM_READ_CNF_PENDING); //Initiate config write cycle

  265.   startTime = micros();
  266.   while (!ADCpool() && (micros() - startTime < 120000))
  267.   {
  268.     yield(); //write config
  269.   }
  270.   uint32_t wrstartTime = micros() - startTime;

  271.   startTime = micros();
  272.   while (!ADCpool() && (micros() - startTime < 360000)); //reread, allow settling more than 300ms
  273.   {
  274.     yield();
  275.   }
  276.   startTime = micros() - startTime;
  277.   Serial.print("write cnf (us)\t");
  278.   Serial.println(wrstartTime);
  279.   Serial.print("reread cnf (us)\t");
  280.   Serial.println(startTime);

  281.   if ((statusReg & SRM_WRITE_CNF_PENDING) | (statusReg & SRM_READ_CNF_PENDING) | (configRegW != configReg))
  282.   {
  283.     Serial.print("begin: Error: ");
  284.     if ((statusReg & SRM_WRITE_CNF_PENDING)) Serial.print("write ");
  285.     if ((statusReg & SRM_READ_CNF_PENDING)) Serial.print("reread ");
  286.     if ((configRegW != configReg)) Serial.print("compare");
  287.     Serial.print(".\ncnfWr..cnfRd..ADC: ");
  288.     Serial.print(configRegW, HEX);
  289.     Serial.print("\t");
  290.     Serial.print(configReg, HEX);
  291.     Serial.print("\t");
  292.     Serial.println(ADCdata, HEX);
  293.     statusReg &= ~SRM_INIT_SUCCESS;
  294.     sleep(1); //try to put ADC into sleeping
  295.     return 0;
  296.   }
  297.   else
  298.   {
  299.     Serial.println("ADC Initiated");
  300.     statusReg |= SRM_INIT_SUCCESS;
  301.     return 1;
  302.   }

  303. }


  304. /********************************************************************************************
  305.     Serdes


  306.      Bit handler

  307. *******************************************************************************************/



  308. bool ICACHE_RAM_ATTR CS1237::one_bit(bool _bit) //contains time sensitive inline code, put in RAM
  309. {
  310. #define NOMINAL_SPEED_kbps 1100
  311. #define delayTwiUp (F_CPU/NOMINAL_SPEED_kbps/1000/2-22)/4
  312. #define delayTwiDn (F_CPU/NOMINAL_SPEED_kbps/1000/2-32)/4

  313.   //SCL_HIGH();
  314.   GPOS = twi_scl;

  315.   //set SDA
  316.   if (_bit) {
  317.     GPEC = (twi_sda);
  318.   } else {
  319.     GPES = (twi_sda);
  320.   }

  321.   uint32_t delayTwi = delayTwiUp;
  322.   asm volatile (
  323.     "100:;"
  324.     " addi.n %[a2],%[a2],-1;"
  325.     " bgei   %[a2],1,100b;"
  326.     : [a2]"=r"(delayTwi) : "[a2]"(delayTwi) );


  327.   //SCL_LOW();
  328.   GPOC = twi_scl;


  329.   delayTwi = delayTwiDn;
  330.   asm volatile (
  331.     "200:;"
  332.     " addi.n %[a2],%[a2],-1;"
  333.     " bgei   %[a2],1,200b;"
  334.     : [a2]"=r"(delayTwi) : "[a2]"(delayTwi) );

  335.   //get data from ADC
  336.   _bit = (GPI & twi_sda);
  337.   if (_bit) _bit = 1;
  338.   return _bit;
  339. }


  340. /********************************************************************

  341.      word handler

  342. ********************************************************************/



  343. bool ICACHE_RAM_ATTR CS1237::ADCread()    //ToDo : test callable from ISR
  344. {

  345.   ADCdata = 0;
  346.   int32_t tmp;
  347.   for (tmp = 0; tmp < 24; tmp++)  //clk 1-24
  348.   {
  349.     ADCdata = (ADCdata << 1) | one_bit(1);
  350.   }
  351. #define SX_TMP 0x800000
  352.   ADCdata = (ADCdata ^ SX_TMP) - SX_TMP;   //sign extend
  353.   statusReg |= SRM_NEW_DATA_READY;             //new data ready

  354.   statusReg &= ~(SRM_BIT25_UPDATE1 | SRM_BIT26_UPDATE2);
  355.   statusReg |= (one_bit(1) << SRB_BIT25_UPDATE1);  //clk 25
  356.   statusReg |= (one_bit(1) << SRB_BIT26_UPDATE2);  //clk 26

  357.   one_bit(1);            //clk 27

  358.   if (statusReg & SRM_WRITE_CNF_PENDING)
  359.   {
  360.     //11 110 0101 1 cccc cccc 1
  361.     //111 1111 11
  362.     //876 5432 1098 7654 3210
  363.     //111 1001 011c cccc ccc1 = 7 9 6 0 1      B00F9601
  364.     uint32_t writeout = (0x79601  |  ((configRegW & 0xFF) << 1));
  365.     for (uint32_t bitpos = 28 ; bitpos <= 46 ; bitpos++)
  366.     {
  367.       uint32_t tempReg = (one_bit((writeout >> 18) & 1)) ;
  368.       writeout = (writeout << 1) | tempReg;

  369.     }
  370.     statusReg2=writeout;
  371.     //statusReg = writeout;
  372.     statusReg &= ~SRM_WRITE_CNF_PENDING;
  373.   }

  374.   else  //if and only if no config write cycle
  375.   {
  376.     if (statusReg & SRM_READ_CNF_PENDING)
  377.     {
  378.       //11 101 0110 1 rrrr rrrr 1
  379.       //
  380.       //111 1111 11
  381.       //876 5432 1098 7654 3210
  382.       //                                                      1098 7654 3210 9876 5432 1098 7654 3210
  383.       //111 0101 1011 1111 1111 = 7 5 b f f          DFFF5A01 1101 1111 1111 1111 0101 1010 0000 0001
  384.       //
  385.       //223 3333 3333 3444 4444
  386.       //890 1234 5678 9012 3456
  387.       //
  388.       //0123 4567 8901 2345 678
  389.       //
  390.       uint32_t writeout = 0x75bff;
  391.       for (uint32_t bitpos = 28 ; bitpos <= 46 ; bitpos++)
  392.       {
  393.         uint32_t tempReg = (one_bit((writeout >> 18) & 1)) ;
  394.         writeout = (writeout << 1) | tempReg;
  395.       }
  396.       configReg = (writeout >> 1) & 0xFF;
  397.       //statusReg3 = writeout;
  398.       statusReg &= ~SRM_READ_CNF_PENDING;
  399.     }
  400.   }
  401.   return 1;
  402. }   //takes about 25us normally or 3%CPU at 1280 sampling rate, up to 50us if config is accessed

  403. /************************************************************************************
  404.      SerDes   serial deserialize

  405.      Interrupt chaining              THIS SECTION IS STILL UNDER DEVELOPMET

  406. * **********************************************************************************

  407.   void CS1237::InterruptServiceRoutinex()
  408.   {
  409.   //reenable all other interrupts //TODO check if interrupt nesting is allowed.
  410.   ADCread();
  411.   if (_zeroCrossing)                   //zc if not zero then find zeroCrossing
  412.   {
  413.     if (sampleWritePointer == 0)        //zc is this the first sampling attempt
  414.     {
  415.       if(ADCdata == threshold)      // found a zeroCrossing at first pass; lucky
  416.       {
  417.         _zeroCrossing=0;
  418.       }
  419.     }
  420.     else                            //zc not first sampling
  421.     {
  422.       if (samples[0]>threshold)    // previous signal was above threshold
  423.       {
  424.         if (ADCdata<=threshold)  // found a zeroCrossing
  425.         {
  426.           _zeroCrossing=0;
  427.         }
  428.         else                     // still above threshold
  429.         {
  430.           sampleWritePointer=0;
  431.         }
  432.       }
  433.       else             // previous signal was bellow threshold
  434.       {
  435.         if (ADCdata>=threshold)  // found a zeroCrossing
  436.         {
  437.           _zeroCrossing=0;
  438.         }
  439.         else           // still below threshold
  440.         {
  441.           sampleWritePointer=0;
  442.         }
  443.       }
  444.     }
  445.     _zeroCrossing--;                           //zc decrement until 0
  446.   }

  447.   samples[sampleWritePointer++] = ADCdata;

  448.   if (sampleWritePointer >= numberOfSamples)
  449.   {
  450.     detachPinInterrupt(twi_sda_pin);
  451.     statusReg |= NEW_DATA_READY;      //new data ready bit
  452.     _zeroCrossing=zeroCrossing;

  453.   }
  454.   }
  455. */


  456. /************************************************************************
  457.      SerDes

  458.       Access to ADC via pooling

  459. ************************************************************************/
  460. bool CS1237::ADCpool()
  461. {
  462.   statusReg &= ~SRM_NEW_DATA_READY;   //clear stale new data ready bit
  463.   if (statusReg & SRM_INIT_SUCCESS)
  464.   {
  465.     if (statusReg & SRM_ADC_SLEEPING)
  466.     {
  467.       sleep(0); // wake it up
  468.     }
  469.     //SCL_LOW(twi_scl);               //just make sure CLK is low
  470.     if (GPI & twi_sda)  return 0;
  471.     if (ADCread())
  472.     {
  473.       statusReg |= SRM_NEW_DATA_READY;             //new data ready
  474.       return 1;
  475.     }
  476.   }
  477.   return 0;
  478. }
  479. /*******************************************************************************
  480.     SerDes

  481.       Access to ADC via interrupt       THIS SECTION IS STILL UNDER DEVELOPMENT

  482. * ******************************************************************************
  483.   bool CS1237::get(uint32_t _numberOfSamples, uint32_t _maxNumberOf_zeroCrossing_search = 0)
  484.   {
  485.   statusReg &= ~NEW_DATA_READY;                 //clear new data ready bit
  486.   if (statusReg & SRM_INIT_SUCCESS)         //dont proceed if there is error
  487.   {
  488.     if (statusReg & SRM_ADC_SLEEPING)
  489.     {
  490.       sleep(0);                   // wake it up
  491.     }

  492.     numberOfSamples =_NumberOfSamples;
  493.     if (numberOfSamples > MAX_NUMBER_OF_SAMPLES)
  494.     {
  495.       numberOfSamples = MAX_NUMBER_OF_SAMPLES;  //fool proofing
  496.     }

  497.     zeroCrossing = _maxNumberOf_zeroCrossing_search;
  498.     if (zeroCrossing > MAX_NUMBER_OF_ZERO_CROSSING)
  499.     {
  500.       zeroCrossing = MAX_NUMBER_OF_ZERO_CROSSING; //fool proofing
  501.     }

  502.     sampleWritePointer = 0;

  503.     //ADC will generate a falling edge (without clk) whenever new data is ready
  504.     attachPinInterrupt(twi_sda,CS1237ISR,FALLING_EDGE);

  505.     return 1
  506.   }
  507.   return 0;
  508.   }
  509. */


  510. /*********************************************
  511.     CS1237 util

  512.       Sleep
  513. *********************************************/
  514. void CS1237::sleep(bool _sleep)
  515. {
  516.   if (_sleep)
  517.   {
  518.     digitalWrite(twi_scl_pin, HIGH);
  519.     //    SCL_HIGH(twi_scl);  //TO DO set scl pin mode SLEEP_PULL_UP
  520.     uint32_t startTime = micros();
  521.     while (micros() - startTime < 120) //at least 100us
  522.     {
  523.       yield();
  524.     }
  525.     statusReg |= SRM_ADC_SLEEPING;
  526.   }
  527.   else //wake it up
  528.   {
  529.     digitalWrite(twi_scl_pin, LOW);
  530.     uint32_t startTime = micros();
  531.     while (micros() - startTime < 15)   //at least 10 us
  532.     {
  533.       asm(" nop.n;"); //do nothing
  534.     }
  535.     statusReg &= ~SRM_ADC_SLEEPING;
  536.   }
  537. }




  538. // .cpp lib

  539. //*************************************************************************************
  540. // .ino example

  541. //D0 = GPIO16;
  542. //D1 = GPIO5;
  543. //D2 = GPIO4;
  544. //D3 = GPIO0;can not download when connected to low
  545. //D4 = GPIO2;  LED on esp8266
  546. //D5 = GPIO14;
  547. //D6 = GPIO12;
  548. //D7 = GPIO13;
  549. //D8 = GPIO15;  can not start when high input
  550. //D9 = GPIO3; UART RX
  551. //D10 = GPIO1; UART TX
  552. //LED_BUILTIN = GPIO16 (auxiliary constant for the board LED, not a board pin);


  553. //#include "CS1237.h"

  554. //constructor
  555. CS1237 currentADC(12, 13); //sda [url=home.php?mod=space&uid=3057940]@[/url] GPIO12 ; sck @ GPIO13

  556. uint32_t printtimer;


  557. void setup()
  558. {
  559.   delay(3000);
  560.   Serial.begin(115200);
  561.   Serial.print("\n-------------\nADC\n\nConstructor:\t");
  562.   Serial.println(currentADC.statusReg2, BIN);
  563.   Serial.print("Bit Delay :");
  564.   Serial.print(delayTwiUp * 50);
  565.   Serial.print(" ns\t");
  566.   Serial.print(delayTwiDn * 50);
  567.   Serial.println(" ns.");
  568.   if (!currentADC.begin(128, 10, CH_SEL_CHANNEL_A, REFO_OFF_FALSE)) //1280 sample/sec, analog gain 1
  569.   {
  570.     //ADC error unable to initialize properly
  571.     Serial.println("begin: Init error");
  572.   }
  573.   printtimer = millis();
  574. }

  575. void loop()
  576. {
  577.   int32_t cur = 0;           //current
  578.   int64_t off = 0;           //dc offset

  579.   if (currentADC.ADCpool())
  580.   {
  581.     cur = currentADC.ADCdata;
  582.     off = off + cur - (off / 256); // calculate offset on the fly. Better to use constant

  583.     //Serial.println(current,HEX);
  584.   }
  585.   if ((millis() - printtimer) > 1000) //once every second
  586.   {
  587.     int32_t printCur = currentADC.ADCdata;
  588.     Serial.print(printCur, HEX);
  589.     Serial.print("\t");
  590.     printCur = printCur - (off / 256);
  591.     Serial.println(printCur);
  592.     printtimer += 1000;
  593.   }

  594. }
复制代码


本帖子中包含更多资源

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

x
发表于 2021-11-5 06:13:13 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-11-5 15:08:28 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-18 21:44:24 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-18 22:41:19 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 0 反对 1

使用道具 举报

发表于 2021-12-18 22:46:16 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-19 09:24:28 来自手机浏览器 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-23 14:04:22 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 15:39:02 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-23 15:42:28 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-23 16:13:11 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-23 17:20:57 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 18:52:50 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 18:56:13 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 18:59:34 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-23 19:45:50 来自手机浏览器 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 21:23:10 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-24 10:02:26 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-24 10:04:52 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2021-12-24 11:27:00 来自手机浏览器 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|数码之家-技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-7-21 17:16 , Processed in 0.140401 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

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