/* * * by Lewis Loflin www.bristolwatch.com lewis@bvu.net * http://www.bristolwatch.com/rpi/pcf8591Rpi.htm * PCF8591Test2.c * Using wiringPi by Gordon Henderson * This must be installed first. * * Here connect the DAC output to AN0 and a potentiometer to AN1 * Read pot on AN1, write value to DAC, Read DAC with AN0 * DAC output value only good to ~200 * * There is no warrenty of any kind use at your own risk. * */ #include #include #include #include const char PCF8591 = 0x48; // base address int fd, adcVal; int main() { if (wiringPiSetup () == -1) exit (1); fd = wiringPiI2CSetup(PCF8591); while (1) { // read ADC1 wiringPiI2CReadReg8(fd, PCF8591 + 1) ; // dummy read adcVal = wiringPiI2CReadReg8(fd, PCF8591 + 1) ; printf("Pot value = %d ", adcVal); // write to DAC reg, 0x04 wiringPiI2CWriteReg8 (fd, PCF8591 + 4, adcVal) ; // read back DAC value with AN0 wiringPiI2CReadReg8(fd, PCF8591) ; // dummy read adcVal = wiringPiI2CReadReg8(fd, PCF8591); printf("DAC value = %d \n\n" , adcVal); delay(500); } return 0; }