Advertisement
AntonioVillanueva

RaspberryPi WiringPi DS1307 FM24CL16 Tests

Mar 1st, 2017
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // g++ -o i2c i2c.cc -lwiringPi -Wall -std=c++14
  2. #include <wiringPiI2C.h>
  3. #include <wiringPi.h>
  4. #include <ctime>
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. #define FRAM 0x50 //0x51 52 53 54 55 56 57  .... 64 bytes
  10. #define RTC 0x68 //Max 0x7FF .. CLOCK 0x00 to 0x07  RAM 0x08 to 0x3F
  11. #define SECOND 0x00
  12. #define MINUTE 0x01
  13. #define HEURE 0x02
  14. #define DAY 0x03
  15. #define DATE 0x04
  16. #define MONTH 0x05
  17. #define YEAR 0x06
  18.  
  19.  
  20. void setDate(){
  21.     time_t now=time(0);
  22.     tm *ltm=localtime(&now);
  23.  
  24.     cout<<" Year " <<1900+ltm->tm_year<<endl;
  25.     cout <<" Month "<<1+ltm->tm_mon<<endl;
  26.     cout <<" Day "<<ltm->tm_mday<<endl;
  27.     cout <<" Time "<<1+ltm->tm_hour<<endl;
  28.     cout <<" Min "<<1+ltm->tm_min<<endl;
  29.     cout <<" sec "<<1+ltm->tm_sec<<endl;
  30. }
  31.  
  32. int main(){
  33.     int fd(-1),reg(0),data(0);
  34.     cout <<"Test I2C WiringPi "<<endl;
  35.  
  36. //open file descriptor for i2C DEVICE
  37.     fd=wiringPiI2CSetup(FRAM);
  38.     if(fd<0){cout <<"Error fd !"<<endl;return 0;}
  39.     else{cout<<"OK fd= "<<fd<<endl<<endl;}
  40.  
  41. //Write test data FM24CL16
  42.     for(data=2;data<0x255;data+=2,reg++){wiringPiI2CWriteReg8(fd,reg,data);}//methode 1
  43.     //for(data=2;data<0xFF;data+=2){wiringPiI2CWrite(fd,data);}//methode 2
  44.  
  45. //Read registers FM24Cl16
  46.     for (reg=0;reg<50;reg++){cout <<reg<<" = "<<wiringPiI2CReadReg8(fd,reg)<<endl;}
  47.  
  48. //Test RTC
  49.     fd=wiringPiI2CSetup(RTC);
  50.     if(fd<0){cout<<"Error RTC"<<endl;return 0;}
  51.     else{cout<<"OK fd= "<<fd<<endl<<endl;}
  52.  
  53. //Write RAM RTC 0x08 to 0x3F
  54.     char text[]={"Antonio Villanueva Segura"};
  55.     char *pto=text;
  56.     reg =0x08 ;//Debut memoire RTC
  57.     while (*pto!=0){
  58.         cout <<"Write reg "<<reg<<" = "<<*pto<<endl;
  59.         wiringPiI2CWriteReg8(fd,reg,*pto);
  60.         pto++;
  61.         reg++;
  62.     };
  63.  
  64.     wiringPiI2CWriteReg8(fd,reg,0);//Fin texte
  65.     reg=0x08;//Debut RAM
  66.  
  67. //Read RAM RTC
  68.     while ((wiringPiI2CReadReg8(fd,reg))!=0){
  69.         cout<<"Read reg "<<reg<<" = "
  70.         <<(char) wiringPiI2CReadReg8(fd,reg)<<endl;
  71.         reg++;
  72.     }
  73.  
  74. //Date heure
  75.     if ((wiringPiI2CReadReg8(fd,SECOND) & 0x80) !=0){
  76.         cout <<"SET\n"; wiringPiI2CWriteReg8(fd,SECOND,0x00);
  77.         }//SET ON RTC
  78.    
  79.     cout <<"DATE RTC "<<endl;
  80.     setDate();
  81.     while (TRUE)
  82.     {
  83.         cout <<std::hex<< ((wiringPiI2CReadReg8(fd,SECOND)) & 0xFF)<<" seg "
  84.         <<std::hex<< ((wiringPiI2CRead(fd)) & 0xFF)<<" min "
  85.         <<std::hex<< ((wiringPiI2CRead(fd)) & 0xFF)<<" heures "
  86.         <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x07)<<" jour "
  87.         <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x0F)<<" date "
  88.         <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x0F)<<" mois "
  89.         <<std::dec<<(int)((wiringPiI2CRead(fd)) & 0x0F)<<" Year "
  90.         <<endl;
  91.  
  92.         delay (2000);
  93.     }
  94.     return 0 ;
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement