ATMEGA 128 - SPI Kommunikation aus einem Timerinterrupt

OP #471148
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;
}
Gast #471149
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.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren