Advertisement
Guest User

Untitled

a guest
Jul 6th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 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
  31. ); //This will vary depending on your setup. See page 17 of datasheet
  32. Wire.endTransmission();
  33. delay(4);
  34. Wire.beginTransmission(0x48);
  35. Wire.write(0x0A); //address pointer for the configuration register
  36. Wire.write(0x39); //b'00111001' for continuous conversion, arbitrary VTF setting, and mid-range capacitive conversion time
  37. Wire.endTransmission();
  38. Wire.beginTransmission(0x48);
  39. Wire.write(0x0B); //CAP DAC A Register address (Positive pin data)
  40. Wire.write(0x80); //b'10000000' for enable Cap DAC A
  41. Wire.endTransmission();
  42. Serial.println("Hello!"); //test to make sure serial connection is working
  43. }
  44.  
  45.  
  46. void readycheck()
  47. {
  48. Wire.beginTransmission(0x48); //talking to chip
  49. Wire.write(byte(0x00)); //status register address
  50. Wire.endTransmission();
  51. Wire.requestFrom(0x48,1); //request status register data
  52. delay(100);
  53. while(capreadybit == 0) //if the ADC has data for us then this bit will be low
  54. {
  55. loop(); //goto the capacitance read program
  56. }
  57. }
  58.  
  59. void loop() //read capacitance
  60.  
  61. {
  62. Wire.beginTransmission(0x48); //arduino asks for data from ad7747
  63. Wire.write(0x01); //set address point to capacitive DAC register 1
  64. Wire.endTransmission(); //pointer is set so now we can read the data
  65. Wire.requestFrom(0x48,3); //reads data from cap DAC registers 1-3
  66. while(Wire.available())
  67. {
  68. char capacitance = Wire.read();
  69. Serial.print(capacitance, DEC); //prints the capacitance data in decimal through serial port
  70. Serial.print(",");
  71. }
  72. Serial.println();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement