Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <signal.h>
- #include <syslog.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <stddef.h>
- #include <sys/ioctl.h>
- #include <linux/i2c-dev.h>
- int ds2482init()
- {
- int port;
- int file;
- int test;
- // Open the device
- file = open("/dev/i2c-0", O_RDWR);
- if (file == -1)
- {
- printf("Error (/sys/bus/i2c/devices/i2c-0): %m\n" );
- exit(EXIT_FAILURE);
- }
- // Define I2C Address ( sudo i2cdetect -y 0 )
- #define DS2482_ADDR 0x18
- // Set I2C Adress for the rest of code
- if ( ioctl ( file, I2C_SLAVE, DS2482_ADDR ) == -1 )
- {
- printf("Failed to set slave address: %m\n" );
- exit(EXIT_FAILURE);
- }
- // DS2482 device reset (0xF0)
- if ( i2c_smbus_write_byte(file, 0xF0 ) == -1 )
- {
- printf("DS2482 device reset(0xF0):%m\n" );
- exit(EXIT_FAILURE);
- }
- printf("DS2482 device reset(0xF0):%m\n" );
- // 1Wire line reset (0xB4)
- if ( i2c_smbus_write_byte(file, 0xB4 ) == -1 )
- {
- printf("1Wire line reset(0xB4):%m\n" );
- exit(EXIT_FAILURE);
- }
- printf("1Wire line reset(0xB4):%m\n" );
- // Set read pointer Statusregister -> 0xF0
- if ( i2c_smbus_write_byte_data( file, 0xE1, 0xF0 ) == -1 )
- {
- printf ( "Set read pointer to Statusregister(0xF0): %m\n" );
- }
- else
- {
- printf ( "Set read pointer to Statusregister(0xF0): %m\n" );
- }
- if ( test = i2c_smbus_read_byte_data( file, 0x02 ) == -1 )
- {
- printf("Read DS2482 'PPD' Status (%m): %x\n", test );
- }
- else
- {
- printf("Read DS2482 'PPD' Status (%m): %x\n", test );
- }
- }
- int main()
- {
- system("clear");
- ds2482init();
- }
Advertisement
Add Comment
Please, Sign In to add comment