Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // g++ -o i2c i2c.cc -lwiringPi -Wall -std=c++14
- #include <wiringPiI2C.h>
- #include <wiringPi.h>
- #include <ctime>
- #include <iostream>
- using namespace std;
- #define FRAM 0x50 //0x51 52 53 54 55 56 57 .... 64 bytes
- #define RTC 0x68 //Max 0x7FF .. CLOCK 0x00 to 0x07 RAM 0x08 to 0x3F
- #define SECOND 0x00
- #define MINUTE 0x01
- #define HEURE 0x02
- #define DAY 0x03
- #define DATE 0x04
- #define MONTH 0x05
- #define YEAR 0x06
- void setDate(){
- time_t now=time(0);
- tm *ltm=localtime(&now);
- cout<<" Year " <<1900+ltm->tm_year<<endl;
- cout <<" Month "<<1+ltm->tm_mon<<endl;
- cout <<" Day "<<ltm->tm_mday<<endl;
- cout <<" Time "<<1+ltm->tm_hour<<endl;
- cout <<" Min "<<1+ltm->tm_min<<endl;
- cout <<" sec "<<1+ltm->tm_sec<<endl;
- }
- int main(){
- int fd(-1),reg(0),data(0);
- cout <<"Test I2C WiringPi "<<endl;
- //open file descriptor for i2C DEVICE
- fd=wiringPiI2CSetup(FRAM);
- if(fd<0){cout <<"Error fd !"<<endl;return 0;}
- else{cout<<"OK fd= "<<fd<<endl<<endl;}
- //Write test data FM24CL16
- for(data=2;data<0x255;data+=2,reg++){wiringPiI2CWriteReg8(fd,reg,data);}//methode 1
- //for(data=2;data<0xFF;data+=2){wiringPiI2CWrite(fd,data);}//methode 2
- //Read registers FM24Cl16
- for (reg=0;reg<50;reg++){cout <<reg<<" = "<<wiringPiI2CReadReg8(fd,reg)<<endl;}
- //Test RTC
- fd=wiringPiI2CSetup(RTC);
- if(fd<0){cout<<"Error RTC"<<endl;return 0;}
- else{cout<<"OK fd= "<<fd<<endl<<endl;}
- //Write RAM RTC 0x08 to 0x3F
- char text[]={"Antonio Villanueva Segura"};
- char *pto=text;
- reg =0x08 ;//Debut memoire RTC
- while (*pto!=0){
- cout <<"Write reg "<<reg<<" = "<<*pto<<endl;
- wiringPiI2CWriteReg8(fd,reg,*pto);
- pto++;
- reg++;
- };
- wiringPiI2CWriteReg8(fd,reg,0);//Fin texte
- reg=0x08;//Debut RAM
- //Read RAM RTC
- while ((wiringPiI2CReadReg8(fd,reg))!=0){
- cout<<"Read reg "<<reg<<" = "
- <<(char) wiringPiI2CReadReg8(fd,reg)<<endl;
- reg++;
- }
- //Date heure
- if ((wiringPiI2CReadReg8(fd,SECOND) & 0x80) !=0){
- cout <<"SET\n"; wiringPiI2CWriteReg8(fd,SECOND,0x00);
- }//SET ON RTC
- cout <<"DATE RTC "<<endl;
- setDate();
- while (TRUE)
- {
- cout <<std::hex<< ((wiringPiI2CReadReg8(fd,SECOND)) & 0xFF)<<" seg "
- <<std::hex<< ((wiringPiI2CRead(fd)) & 0xFF)<<" min "
- <<std::hex<< ((wiringPiI2CRead(fd)) & 0xFF)<<" heures "
- <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x07)<<" jour "
- <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x0F)<<" date "
- <<std::dec<<(int) ((wiringPiI2CRead(fd)) & 0x0F)<<" mois "
- <<std::dec<<(int)((wiringPiI2CRead(fd)) & 0x0F)<<" Year "
- <<endl;
- delay (2000);
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement