Advertisement
Guest User

7/10

a guest
Jul 10th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1.  
  2.  
  3. //----------------------------------------------------------------------------------------
  4. //| AD7747 Capacitive Sensor |
  5. //| Based on code by MiG found at:http://forum.arduino.cc/index.php/topic,11882.0.html| |
  6. //| |
  7. //| Author: Aidan Williamson (although I didn't do much original work) |
  8. //| Written: 7/3/2014 |
  9. //| ad7747 datasheet:http://www.analog.com/static/imported-files/data_sheets/AD7747.pdf |
  10. //| |
  11. //| |
  12. //| |
  13. //| |
  14. //-----------------------------------------------------------------------------------------
  15. #include <Wire.h> //include the library for i2c communication
  16.  
  17. int capreadybit = (bitRead(TWDR,0)); //initialize variable readybit to be two wire data register's lsb
  18.  
  19. void setup()
  20. {
  21. Wire.begin(); //sets up i2c for operation
  22. Serial.begin(9600); // we will monitor this via serial cable
  23. Wire.beginTransmission(0x48); // begins write cycle
  24. Wire.write(0x07); //address pointer for cap setup register
  25. Wire.write(0xA0); //b'10100000' found from datasheet page 16
  26. Wire.endTransmission(); //ends write cycle
  27. delay(4); // Wait for data to clock out? I'm not 100% sure why this delay is here (or why it's 4ms)
  28. Wire.beginTransmission(0x48); //begins transmission again
  29. Wire.write(0x09); //address pointer for capacitive channel excitation register
  30. Wire.write(0x0E); //recommended value from datasheet
  31. Wire.endTransmission();
  32. delay(4);
  33. Wire.beginTransmission(0x48);
  34. Wire.write(0x0A); //address pointer for the configuration register
  35. Wire.write(0x21); //b'00100001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
  36. Wire.endTransmission();
  37. Wire.beginTransmission(0x48);
  38. Wire.write(0x0B); //CAP DAC A Register address (Positive pin data)
  39. Wire.write(0x80); //b'10000000' for enable Cap DAC A
  40. Wire.endTransmission();
  41. Serial.println("Hello!"); //test to make sure serial connection is working
  42. }
  43.  
  44.  
  45. void readycheck()
  46. {
  47. Wire.beginTransmission(0x48); //talking to chip
  48. Wire.write(byte(0x00)); //status register address
  49. Wire.endTransmission();
  50. Wire.requestFrom(0x48,1); //request status register data
  51. delay(100);
  52. while(capreadybit == 0) //if the ADC has data for us then this bit will be low
  53. {
  54. loop(); //goto the capacitance read program
  55. }
  56. }
  57.  
  58. void loop() //read capacitance
  59.  
  60. {
  61. Wire.beginTransmission(0x48); //arduino asks for data from ad7747
  62. Wire.write(0x01); //set address point to capacitive DAC register 1
  63. Wire.endTransmission(); //pointer is set so now we can read the data
  64. Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
  65. while(Wire.available())
  66. {
  67. unsigned char hi;
  68. unsigned char mid;
  69. unsigned char lo; //1 byte numbers
  70. long capacitance; //will be a 3byte number
  71. float pf; //scaled value of capacitance
  72. hi=Wire.read();
  73. mid=Wire.read();
  74. lo=Wire.read();
  75. capacitance=(hi<<16)+(mid<<8)+lo-0x800000; //shift high left 16 bits, shift mid left 8 bits, add lo and subtract [what?]
  76. pf=(float)capacitance/(float)0x800000*8.192f; //8.192 is maximum capacitance reading
  77. Serial.print(pf, DEC); //prints the capacitance data in decimal through serial port
  78. Serial.print(",");
  79. }
  80. Serial.println();
  81. delay(1000);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement