// Title : ADDA-Umsetzer mit PCF8591 // Micro : Arduino 2009 w/ ATmega328 // // ----------------------------------------------------------------------------------------- // Verbindungen I2C-Analog - Arduino // SCL SDA GND +5V // I2C-Analog ST1-SCL ST1-SDA ST1-GND ST1-5V // Arduino A5 A4 GND 5 #include #define PCF8591 (0x9E >> 1) // Deviceadresse = 7 #define PCF8591_DAC_ENABLE 0x40 #define PCF8591_ADC_CH0 0x40 #define PCF8591_ADC_CH1 0x41 #define PCF8591_ADC_CH2 0x42 #define PCF8591_ADC_CH3 0x43 #define PURPOSE "Test of PCF8591" const byte LED = 13; byte adc_value, dac_value=0; void putDAC(byte dac_value) { Wire.beginTransmission(PCF8591); Wire.send(PCF8591_DAC_ENABLE); Wire.send(dac_value); Wire.endTransmission(); } byte getADC(byte config) { Wire.beginTransmission(PCF8591); Wire.send(config); Wire.endTransmission(); Wire.requestFrom((int) PCF8591,2); while (Wire.available()) { adc_value = Wire.receive(); adc_value = Wire.receive(); } return adc_value; } void setup() { pinMode(LED, OUTPUT); Serial.begin(19200); Wire.begin(); Serial.println(PURPOSE); Serial.println("DAC\tADC\tADC-DAC"); } void loop() { putDAC(dac_value); // DAC Wert setzen digitalWrite(LED, 1); // LED ein delay(10); adc_value = getADC(PCF8591_ADC_CH0); // ADC Wert von Kanal0 auslesen digitalWrite(LED, 0); // LED aus Serial.print(dac_value, HEX); // DAC Wert ausgeben Serial.print("\t"); Serial.print(adc_value, HEX); // ADC Wert ausgeben Serial.print("\t"); Serial.println(dac_value - adc_value); // Abweichung berechnen und ausgeben dac_value++; delay(200); }