Guest User

Untitled

a guest
Nov 12th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <syslog.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <stddef.h>
  10. #include <sys/ioctl.h>
  11. #include <linux/i2c-dev.h>
  12.  
  13. int ds2482init()
  14. {
  15.     int port;
  16.     int file;
  17.     int test;
  18.  
  19.     // Open the device
  20.     file = open("/dev/i2c-0", O_RDWR);
  21.     if (file == -1)
  22.     {
  23.         printf("Error (/sys/bus/i2c/devices/i2c-0): %m\n" );
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     // Define I2C Address ( sudo i2cdetect -y 0 )  
  28.     #define DS2482_ADDR 0x18
  29.  
  30.     // Set I2C Adress for the rest of code
  31.     if ( ioctl ( file, I2C_SLAVE, DS2482_ADDR ) == -1 )
  32.     {
  33.         printf("Failed to set slave address: %m\n" );
  34.         exit(EXIT_FAILURE);
  35.     }
  36.  
  37.     // DS2482 device reset (0xF0)
  38.     if ( i2c_smbus_write_byte(file, 0xF0 ) == -1 )
  39.     {
  40.         printf("DS2482 device reset(0xF0):%m\n" );
  41.         exit(EXIT_FAILURE);
  42.     }
  43.         printf("DS2482 device reset(0xF0):%m\n" );
  44.  
  45.     // 1Wire line reset (0xB4)
  46.     if ( i2c_smbus_write_byte(file, 0xB4 ) == -1 )
  47.     {
  48.         printf("1Wire line reset(0xB4):%m\n" );
  49.         exit(EXIT_FAILURE);
  50.     }
  51.         printf("1Wire line reset(0xB4):%m\n" );
  52.  
  53.        
  54.         // Set read pointer Statusregister -> 0xF0
  55.        
  56.         if ( i2c_smbus_write_byte_data( file, 0xE1, 0xF0 ) == -1 )
  57.         {
  58.             printf ( "Set read pointer to Statusregister(0xF0): %m\n" );
  59.         }
  60.             else
  61.         {
  62.             printf ( "Set read pointer to Statusregister(0xF0): %m\n" );
  63.         }
  64.  
  65.         if ( test = i2c_smbus_read_byte_data( file, 0x02 ) == -1 )
  66.         {
  67.             printf("Read DS2482 'PPD' Status (%m): %x\n", test );
  68.         }
  69.             else
  70.         {
  71.             printf("Read DS2482 'PPD' Status (%m): %x\n", test );
  72.         }
  73. }
  74.  
  75. int main()
  76. {
  77.     system("clear");
  78.     ds2482init();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment