Forum: Mikrocontroller und Digitale Elektronik ATMEGA 128 - SPI Kommunikation aus einem Timerinterrupt


von Malte S. (maltes)


Lesenswert?

Erst mal ein herzliches hallo...

ich stehe vor folgendem Problem: Ein ATMEGA soll über das SPI interface 
einen externen A/D kontinuierlich abfragen. Dazu gibts eine Funktion die 
auch funktioniert wenn sie aus dem normalen Programm heraus aufgerufen 
wird. Wenn ich diese Funktion aber im der Timerinterruptroutine (Timer 
alle 20ms) aufrufe ist das SPIF Flag aber immer nicht gesetzt und wird 
auch nie gesetzt.

Warum?

Gruß
Malte




uint16_t volatile result;
void a2d_converter_external() {
  if ( (MAX110_PIN & _BV(MAX110_BUSY)) ) {      // if MAX110 has 
finalized conversion start next one and obtain result
    MAX110_PORT ^= _BV(MAX110_CS);         // Chip Select = 0
      result = spiTransferWord(0x8c90); //obtain result for channel 0 
conversion
  MAX110_PORT |= _BV(MAX110_CS);       // Chip Select = 1 (high during 
conversion, shall be kept high until Busy gets low)
  }
}

u08 spiTransferByte(u08 data)
{
  // send the given data
  spiTransferComplete = FALSE;
  outb(SPDR, data);

  // wait for transfer to complete
    while(!(inb(SPSR) & (1<<SPIF)));
    // *** reading of the SPSR and SPDR are crucial
    // *** to the clearing of the SPIF flag
    // *** in non-interrupt mode
    //inb(SPDR);
    // set flag
    spiTransferComplete = TRUE;

  // return the received data
  return inb(SPDR);
}

u16 spiTransferWord(u16 data)
{
  u16 rxData = 0;

  // send MS byte of given data
  rxData = (spiTransferByte((data>>8) & 0x00FF))<<8;
  // send LS byte of given data
  rxData |= (spiTransferByte(data & 0x00FF));

  // return the received data
  return rxData;
}

von crazy horse (Gast)


Lesenswert?

keine Ahnung, warum nicht - aber warum willst du das machen?
Setz in der Timer-ISR ein flag, welches anzeigt, dass wieder mal 20ms um 
sind.
Das Hauptprogramm schlendert an den diversen flags vorbei und führt alle 
aktiv gesetzten Teilprogramme aus.

von MalteS (Gast)


Lesenswert?

ICh wollte das machen um immer wenn der A/D Wandler fertig ist eine neue 
Wandlung anzustossen und das ERgebniss abzuholen

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.