Advertisement
Guest User

Untitled

a guest
Nov 8th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Distributed with a free-will license.
  2. // Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
  3. // AD7999
  4. // This code is designed to work with the AD7999_I2CADC I2C Mini Module available from ControlEverything.com.
  5. // https://www.controleverything.com/content/Analog-Digital-Converters?sku=AD7999_I2CADC#tabs-0-product_tabset-2
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <linux/i2c-dev.h>
  10. #include <sys/ioctl.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13.  
  14. void main()
  15. {
  16. // Create I2C bus
  17. int file;
  18. char *bus = "/dev/i2c-1";
  19. if ((file = open(bus, O_RDWR)) < 0)
  20. {
  21. printf("Failed to open the bus. \n");
  22. exit(1);
  23. }
  24. // Get I2C device, AD7999 I2C address is 0x29(41)
  25. ioctl(file, I2C_SLAVE, 0x29);
  26.  
  27.  
  28. // Send configuration command(0x10)
  29. // Channel-1, filter enabled
  30. char config[1] = {0x10};
  31. write(file, config, 1);
  32. sleep(1);
  33.  
  34. // Read 2 bytes of data
  35. // raw_adc msb, raw_adc lsb
  36. char data[2]={0};
  37. if(read(file, data, 2) != 2)
  38. {
  39. printf("Error : Input/Output Error \n");
  40. }
  41. else
  42. {
  43. // Convert the data to 8-bits
  44. int raw_adc = ((data[0] & 0x0F) * 256 + (data[1] & 0xF0)) / 16 ;
  45.  
  46. // Output data to screen
  47. printf("Digital Value of Analog Input on Channel-1: %d \n", raw_adc);
  48. }
  49.  
  50. // Send configuration command(0x20)
  51. // Channel-2, filter enabled
  52. config[0] = {0x20};
  53. write(file, config, 1);
  54. sleep(1);
  55.  
  56. // Read 2 bytes of data
  57. // raw_adc msb, raw_adc lsb
  58. char data[2]={0};
  59. if(read(file, data, 2) != 2)
  60. {
  61. printf("Error : Input/Output Error \n");
  62. }
  63. else
  64. {
  65. // Convert the data to 8-bits
  66. int raw_adc = ((data[0] & 0x0F) * 256 + (data[1] & 0xF0)) / 16 ;
  67.  
  68. // Output data to screen
  69. printf("Digital Value of Analog Input on Channel-2: %d \n", raw_adc);
  70. }
  71.  
  72. // Send configuration command(0x40)
  73. // Channel-3, filter enabled
  74. config[0] = {0x40};
  75. write(file, config, 1);
  76. sleep(1);
  77.  
  78. // Read 2 bytes of data
  79. // raw_adc msb, raw_adc lsb
  80. memset(data, 0, sizeof(data));
  81. if(read(file, data, 2) != 2)
  82. {
  83. printf("Error : Input/Output Error \n");
  84. }
  85. else
  86. {
  87. // Convert the data to 8-bits
  88. int raw_adc = ((data[0] & 0x0F) * 256 + (data[1] & 0xF0)) / 16 ;
  89.  
  90. // Output data to screen
  91. printf("Digital Value of Analog Input on Channel-3: %d \n", raw_adc);
  92. }
  93.  
  94. // Send configuration command(0x80)
  95. // Channel-4, filter enabled
  96. config[0] = {0x80};
  97. write(file, config, 1);
  98. sleep(1);
  99.  
  100. // Read 2 bytes of data
  101. // raw_adc msb, raw_adc lsb
  102. memset(data, 0, sizeof(data));
  103. if(read(file, data, 2) != 2)
  104. {
  105. printf("Error : Input/Output Error \n");
  106. }
  107. else
  108. {
  109. // Convert the data to 8-bits
  110. int raw_adc = ((data[0] & 0x0F) * 256 + (data[1] & 0xF0)) / 16 ;
  111.  
  112. // Output data to screen
  113. printf("Digital Value of Analog Input on Channel-4: %d \n", raw_adc);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement