Forum: Mikrocontroller und Digitale Elektronik Programm fuer Kommunikation mit ext. ADC via SPI


von Thomas H. (Gast)


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.

von MarkusS (Gast)


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

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.