Understanding SPI communications for diffrent ADC type in c++ implementation

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
spent many hours to understand how to change ADC MCP3008 SPI communications into the ADC 0832CCN. I need to change this function:

//this works for ADC MCP 3008
double Mcp3008Spi::read( uint8_t channel , bool differential )
{
if( channel > 7 )
{
std::cerr << "Incorrect channel number (allowed 0-7)." << std::endl;

return 0.0 / 0.0; // NaN
}

if( this->handle == -1 )
return 0.0 / 0.0; // NaN

uint8_t buffer[ 3 ];

// http://www.hertaville.com/files/uploads/2013/07/gg4.png
buffer[ 0 ] = 1;
buffer[ 1 ] = ( differential ? 0 : ( 1 << 7 ) ) | ( channel << 4 );
buffer[ 2 ] = 0;

if( this->transfer( buffer, 3 ) )
{
uint16_t hi = ( buffer[ 1 ] << 8 ) & 0x0300;
uint16_t lo = ( buffer[ 2 ] << 0 ) & 0x00FF;

uint16_t value = hi | lo;

return 100.0 * ( (double)value / 1023.0 );
}

return 0.0 / 0.0; // NaN
}

bool Mcp3008Spi::transfer( uint8_t *data, uint16_t length )
{
if( this->handle == -1 )
return false;

struct ::spi_ioc_transfer pack;

pack.tx_buf = (unsigned long)data; // transmit from "data"
pack.tx_nbits = 8;
pack.rx_buf = (unsigned long)data; // receive into "data"
pack.rx_nbits = 8;
pack.len = length;
pack.delay_usecs = 0 ;
pack.speed_hz = this->speed;
pack.bits_per_word = this->bpw ;
pack.cs_change = 0;

if( ::ioctl( this->handle, SPI_IOC_MESSAGE( 1 ), &pack ) < 0 )
{
std::cerr << "SPI data transmition error." << std::endl;

return false;
}

return true;
}

with transmition datasheet looking like that
https://postimg.org/image/cbgplvrjt/
and in ADC 0832CCN the transmition datasheet looking like that
https://postimg.org/image/yr4qowws9/
https://postimg.org/image/klyxn3nqx/
Unfortunatlly the procesor address space and bit opearions is black magic for me. If anyone can explain me how to understand that or show me how to read the neccesary transmition ?
0 Likes
0 Replies