Archives mensuelles : août 2015

image_pdfimage_print

Example of use with MCP3204 on Linux

It uses :

  • A MCP3204 SPI ADC
  • A USB to SPI converter from Embedded adventures
  • MCP2210 library

 

mcp3208_bb


#include "mcp2210.h"

using namespace std;

void TestMCP3204(hid_device* handle) {
ChipSettingsDef chipDef;

//set GPIO pins to be CS
chipDef = GetChipSettings(handle);

for (int i = 0; i < 9; i++) {
chipDef.GP[i].PinDesignation = GP_PIN_DESIGNATION_CS;
chipDef.GP[i].GPIODirection = GPIO_DIRECTION_OUTPUT;
chipDef.GP[i].GPIOOutput = 1;
}
int r = SetChipSettings(handle, chipDef);

//configure SPI
SPITransferSettingsDef def;
def = GetSPITransferSettings(handle);

//chip select is GP1
def.ActiveChipSelectValue = 0xfffd;
def.IdleChipSelectValue = 0xffff;
def.BitRate = 50000l;
def.BytesPerSPITransfer =3;

r = SetSPITransferSettings(handle, def);

if (r != 0) {
printf(« Errror setting SPI parameters.\n »);
return;
}

byte spiCmdBuffer[4];

//query ch0
spiCmdBuffer[0] = 0x06; //00000110B (start, single ended, ch2)
spiCmdBuffer[1] = 0x00; //(ch1 ch0)

SPIDataTransferStatusDef def1 = SPISendReceive(handle, spiCmdBuffer, 3);

unsigned int val = ((def1.DataReceived[1] & 0xF) <<8) | def1.DataReceived[2];

printf(« %d,%4.3f\n », val,val/4096.0*2.5*2);
}

int main(int argc, char** argv) {
hid_device *handle;

/**
* initializing the MCP2210 device.
*/
handle = InitMCP2210();

if (handle == 0) {
printf(« ERROR opening device. Try using sudo.\n »);
exit(-1);
}

TestMCP3204(handle);

/**
* release the handle
*/
ReleaseMCP2210(handle);

return 0;
}