Advertisement
Guest User

Cap Sensor 2

a guest
Jul 5th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 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 readybit = (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(0x0F); //This will vary depending on your setup. See page 17 of datasheet
  31. Wire.endTransmission();
  32. delay(4);
  33. Wire.beginTransmission(0x48);
  34.  
  35. Wire.write(0x0A); //address pointer for the configuration register
  36. Wire.write(0xA1); //b'00011001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
  37. Wire.endTransmission();
  38. Serial.println("Hello!"); //test to make sure serial connection is working
  39. }
  40.  
  41.  
  42. void readycheck()
  43. {
  44. Wire.beginTransmission(0x48); //talking to chip
  45. Wire.write(byte(0x00)); //status register address
  46. Wire.endTransmission();
  47. Wire.requestFrom(0x48,1); //request status register data
  48. delay(100);
  49. if(readybit = 0) //if the ADC has data for us
  50. {
  51. loop(); //goto the capacitance read program
  52. }
  53. else
  54. { readycheck(); //start this check over
  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. char capacitance = Wire.read();
  68. Serial.print(capacitance, DEC); //prints the capacitance data in decimal through serial port
  69. Serial.print(",");
  70. }
  71. Serial.println();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement