Programm fuer Kommunikation mit ext. ADC via SPI

Gast #12323
Lesenswert?
• ▲
▼
Hallo,

ich moechte mich etwas ins SPI einarbeiten. Als Anwendung moechte ich 
mit einem ext. ADC kommunizieren. Hat jemand schon einmal so einen Code 
fuer soetwas gemacht, damit ich mir einmal ansehen kann, wie das 
Auslesen funktioniert.

Bisher habe ich nur etwas ueber Software-SPI-Loesungen gefunden.

Danke,

Thomas H.
Gast #12324
Lesenswert?
• ▲
▼
Hallo,

ich habe mal ein Programm geschrieben bei dem ich einen exteren 
DA-Converter angesteuert habe.
Dazu habe ich einen ATS90S8535 als Controller benuntzt der externe 
DA-Converter war von MAXIM MAX5158.
Als Compiler habe ich den AVRGCC benutzt.

Die Routinen für das lesen der Daten vom SPI-Interface habe ich nicht, 
das sollte jedoch nicht so schwierig sein.



/*********************************************************************** 
**************/
/* 
*/
/* AVR SPI-Interface 
*/
/* 
*/
/*********************************************************************** 
**************/

// Initial SPI - Interface
void InitSPIInterface(void)
{

  /* SPI Control Register SPCR, Enable SPI, MSB-first, SPI-Master, 
SPI-Clock f/16*/
    outp((1<<SPE) |(1<<MSTR) | (1<<SPR0), SPCR);

}

// Write a Databyte the the SPI-Out Port, wait first to be the the port 
is free
bool WriteDataToSPI( char cData)
{

  if( IsSPIfree( ) == FALSE)      // check if SPI-Interface is free
    return FALSE;          // then return with error

  outp( cData, SPDR);          // if interface is free then wirt the 
data to
                    // the interface
  return TRUE;
}


// Check SPI-Interface, if free the return TRUE
bool IsSPIfree( void)
{

  char cc;
  cc = TIME_OUT_DA_CONVERTER;
  while( cc--)
  {
                    // read the statusregister of the SPI-Interface
    if ( bit_is_set (SPSR, SPIF))  // if data is shifted out
      break;            // then leave the loop
  }
  if (cc == 0)            // if the SPI-Interface is not free
    return FALSE;

  return TRUE;
}


/*********************************************************************** 
**************/
/* 
*/
/* DA - Converter MAX5158 
*/
/* Subroutines 
*/
/* 
*/
/*********************************************************************** 
**************/

// Write Data to the DA-Converter MAX 5158
bool WriteValueToMAXDAConverter( DAChannel Channel, int iValue)
{

int  iDataWord = 0;
bool bRet = TRUE;


                    // Set Data Value
  iDataWord |= ((iValue & AD_DATA_MASK) << AD_DATA_OFFSET);

                    // Set the Address Value
  iDataWord |= ((Channel & AD_ADDRESS_MASK) << AD_ADDRESS_OFFSET);

                    // Set the Configuration Value
  iDataWord |= (AD_CONTROAL_VALUE << AD_CONTROAL_OFFSET);

  ENABLE_DA_INTERFACE;        // Enable Chip-Select line

  if ( (bRet = WriteDataToSPI( (char) ( (iDataWord >> 8) & 0x0ff))) == 
TRUE)  // Send MSB-Data first
  {

    bRet = WriteDataToSPI( (char) (iDataWord & 0x0ff));      // Send 
LSB-Data next

  }
  IsSPIfree( );            // waits if SPI-Interface clocks all data out

  DISABLE_DA_INTERFACE;        // if all Data clocks out then disable 
the
                    // Chip-Select line
  return bRet;
}

Gruß

MarkusS

Antwort schreiben

Hinweis: der ursprüngliche Beitrag ist mehr als 6 Monate alt. Bitte hier nur auf die ursprüngliche Frage antworten, für neue Fragen einen neuen Beitrag erstellen.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.