Advertisement
Guest User

cap sensor 7/11

a guest
Jul 11th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 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);
  24. Wire.write(0xBF);
  25. Wire.write(0x00);
  26. Wire.endTransmission();
  27. delay(4);
  28. Wire.beginTransmission(0x48); // begins write cycle
  29. Wire.write(0x07); //address pointer for cap setup register
  30. Wire.write(0xA0); //b'10100000' found from datasheet page 16
  31. Wire.endTransmission(); //ends write cycle
  32. delay(4); // Wait for data to clock out? I'm not 100% sure why this delay is here (or why it's 4ms)
  33. Wire.beginTransmission(0x48); //begins transmission again
  34. Wire.write(0x09); //address pointer for capacitive channel excitation register
  35. Wire.write(0x0E); //recommended value from datasheet
  36. Wire.endTransmission();
  37. delay(4);
  38. Wire.beginTransmission(0x48);
  39. Wire.write(0x0A); //address pointer for the configuration register
  40. Wire.write(0x21); //b'00100001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
  41. Wire.endTransmission();
  42. Wire.beginTransmission(0x48);
  43. Wire.write(0x0B); //CAP DAC A Register address (Positive pin data)
  44. Wire.write(0x80); //b'10111111' for enable Cap DAC A
  45. Wire.endTransmission();
  46. Serial.println("Hello!"); //test to make sure serial connection is working
  47. }
  48.  
  49.  
  50. void loop()
  51. {
  52. Wire.beginTransmission(0x48); //talking to chip
  53. Wire.write(byte(0x00)); //status register address
  54. Wire.endTransmission();
  55. Wire.requestFrom(0x48,1); //request status register data
  56. int readycap;
  57. readycap=Wire.read();
  58. if((readycap&0x1)==0){ // ready?
  59. Wire.beginTransmission(0x48); //arduino asks for data from ad7747
  60. Wire.write(0x01); //set address point to capacitive DAC register 1
  61.  
  62. Wire.endTransmission(); //pointer is set so now we can read the data
  63.  
  64. Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
  65.  
  66. while(Wire.available()){
  67. unsigned char hi,mid,lo; //1 byte numbers
  68. long capacitance; //will be a 3byte number
  69. float pf; //scaled value of capacitance
  70. hi=Wire.read();
  71. mid=Wire.read();
  72. lo=Wire.read();
  73. capacitance=(hi<<16)+(mid<<8)+lo-0x800000;
  74. pf=(float)capacitance*-1/(float)0x800000*8.192f;
  75. Serial.print(pf, DEC); //prints the capacitance data in decimal through serial port
  76.  
  77. Serial.print(",");
  78. }
  79. Serial.println();
  80. }
  81. delay(1000);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement