I'm trying to understand I2C bus for controlling a PCF8591 D/A Converter with the wiringPi C library on my Raspberry Pi B+.
I wrote a test code (in c++) which work fine, but I don't know if I'm doing it the right way. The program reads and display the 4 analog input values each 0.5 seconds, and set the output accordingly to the average value of the 4 inputs pins.
#include <iostream>
#include <wiringPi.h>
#include <wiringPiI2C.h>
using namespace std;
int readInput(int fd, int reg)
{
wiringPiI2CReadReg8(fd, reg);
return wiringPiI2CReadReg8(fd, reg);
}
int main()
{
wiringPiSetupGpio();
int dacModule = wiringPiI2CSetup(0x48);
if (dacModule < 0)
{
cout << "I2C Setup Error" << endl;
return 0;
}
int i;
int A[4] = {0, 0, 0, 0};
int A_Reg[4] = {0x40, 0x41, 0x42, 0x43};
while (1)
{
for (i=0;i<4;i++) A[i] = readInput(dacModule, A_Reg[i]);
wiringPiI2CWriteReg8(dacModule, 0x40, (A[0]+A[1]+A[2]+A[3])/4);
cout << endl;
for (i=0;i<4;i++) cout << i << " : " << A[i] << endl;
delay(500);
}
return 0;
}
My questions are :
- I noticed I need to call the wiringPiI2CReadReg8 function twice in order to read the correct input value, why ? (If I don't do so, the function return the previous input value).
- I read there is a special module of the wiringPi library specially made for the PCF8591 (This one), is there any performance differences between the two approach (special lib vs generic I2C lib) ?
Thanks a lot for your help / feedbacks.
Aucun commentaire:
Enregistrer un commentaire