2017-08-21 2 views
1

Hallo Ich versuche die Zeit von RTC-DS1338 mit meinem Linux Single Board Computer zu lesen, aber es gibt ein Problem mit meinem Code. Ich lese schlechte Ausgaben damit.DS1338 i2c Ausgabe lesen

Ich versuchte zu lesen mit i2cget -y 0 0x68 0 es funktioniert. aber mein Code nicht.

Kann mir jemand helfen?

#include <stdio.h> 
#include <fcntl.h> 
#include <linux/i2c-dev.h> 
#include <errno.h> 

#define I2C_ADDR 0x68 

int main (void) 
{ 

char value; 
int fd; 
unsigned char pData[10]; 
char i; 

    if ((fd = open("/dev/i2c-0", O_RDWR)) < 0) 
    { 
      printf("Error: Couldn't open device! %d\n", fd); 
      return 1; 
    } 

    if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) 
    { 
      printf("Error: Couldn't find device on address!\n"); 
      return 1; 
    } 

    while (1) 
    { 

     if (read(fd, &pData, 4) != 4) 
     { 
      perror("Read conversion"); 
     } 
     else 
     { 
      for(i=0;i<4;i++) printf(" %02x ",(pData[i] & 0xFF));  
      printf("\n"); 
     } 

     sleep(2); 

    } 

    return 0; 
} 

Bad Ausgang:

00 b3 49 47 
4e 27 09 21 
24 81 29 00 
1a 20 02 10 
16 1e 1a 46 
1a 00 96 18 
45 82 03 e0 
24 40 88 1c 

Gute Daten:

00 05 08 00 
01 05 08 00 
02 05 08 00 
03 05 08 00 
04 05 08 00 
05 05 08 00 
+0

Was bedeutet die guten Daten aus? – donjuedo

+0

Ich habe die gute Datenausgabe hinzugefügt. Sekunden sollten sinnvoller werden – elanktronik

Antwort

0

Ich habe mein Problem mit dem Code unten lösen

if ((fd = open("/dev/i2c-0", O_RDWR)) < 0) 
{ 
     printf("Error: Couldn't open device! %d\n", fd); 
     return 1; 
} 

if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) 
{ 
     printf("Error: Couldn't find device on address!\n"); 
     return 1; 
} 

if (write(fd, wData, 1) != 1) 
{ 
    perror("Write to register"); 
} 

if (read(fd, rData, 64) != 64) 
{ 
    perror("Read conversion"); 
} 
else 
{ 
    memcpy(Dat,rData,7); 
} 

close(fd); 


return 0; 
+0

Wenn Sie möchten, dass dies eine nützliche Antwort ist, wäre es hilfreich, zumindest zu erklären, was Sie anders gemacht haben und mehr Code zu zeigen (die vorherige Version hatte nicht 'wData' und' rData '). Ich nehme an, es ist der Aufruf 'write()', der die Dinge wirklich zum Laufen brachte? – jszakmeister