Forum: Mikrocontroller und Digitale Elektronik PIC I2C Probleme


von Kowalkowski (Gast)


Lesenswert?

Hallo

ich versuche mit einem PIC einen PCF8591 Baustein anzusprechen. Die
Digital-Analog-Umwandlung funktioniert auch bereits prima. Nur beim
Einlesen von analogen Werten habe ich das Problem, dass sich der PIC in
der Receive-Routine scheinbar aufhängt:

char i2c_rx(selectACK doack)
{  unsigned long timeout=0;
  char rx;
  SSPCON2bits.RCEN = 1;
  while (!PIR1bits.SSPIF)
  PIR1bits.SSPIF = 0; // clear slave ack
  rx = SSPBUF;
  if(doack==ACK) SSPCON2bits.ACKEN=1;
  return rx;
}

Die Routine zum Einlesen der Analogwerte sieht so aus:

char ADsample[4];
void readAD(char adr)
/* Read in all 4 AD channels of a PCF8591 device */
{  char n;
  // set configuration
  i2c_on();
  i2c_tx(adr);
  i2c_tx(0x44);  // internal osc on, autoincrement,
          // 4-single ended measurments
  i2c_tx(0x00);  // start with A/D 0
  i2c_off();

  // read result
  i2c_on();
  i2c_tx(adr);
  i2c_rx(ACK);  // throw the first sample away

  for(n=0; n<4; n++)
  {
    if (n!=3)
      ADsample[n]=i2c_rx(ACK);
    else
      ADsample[n]=i2c_rx(NACK);
  }
  i2c_off();
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.