2013-10-26

MCP3208 ADC SPI0 function refactoring notes

/*
*** MCP3208 Test - 2013oct26hk1201 ***

*** Before ADC values of TxBuf and RxBuf ***
Index    0  1   2
TxBuf  06 80 00
RxBuf  00 00 00

*** After ADC (Ch 2 = 1.64V, ARef = 2.49V) ***
Index    0  1   2
TxBuf  06 80 00
RxBuf  ff ea 7f
ADC result hex = a7f
ADC result decimal = 2687
ADC result milliVolt = 1633

*** End of Test. ***
*/


// ***********************************************************************
// spi050.h 2013oct26hk1207
// ***********************************************************************

#include "gpio050.h"
#include "led050.h"
#include "lpc11xx_ssp.h"
#include "semihosting.h"
#include "stdio.h"

void mcp3208v02() // 2013oct26hkt1208
{
    // *** Print title ***
printf("*** MCP3208 Test - 2013oct26hk1201 ***\n\n");

// Setup SPI0 with clock pin = P06
setupSpi050(SCK0_PIO0_6);

// Setup SSEL0 pin = P02
setupSpiSsel();

    // Setup transfer buffers TxBuf, RxBuf
#define BUFFER_SIZE 3
uint8_t TxBuf[BUFFER_SIZE];
uint8_t RxBuf[BUFFER_SIZE];

    // Setup xferConfig = TxBuf, RxBuf
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = (void*)TxBuf;
    xferConfig.rx_data = (void*)RxBuf;
    xferConfig.length = BUFFER_SIZE;

    // Initialize MCP3208 Tx and Rx buffers

    // *** Select input mode and channel number ***

    uint8_t inputMode;
    uint8_t channelNumber;

    #define SINGLE_END_MODE 0x06; // bit 1 = 1
    #define DIFFERENTIAL_END_MODE 0x04; // bit 1 = 0

    inputMode = SINGLE_END_MODE;
channelNumber = 2;

    // *** Initialize TxBuf and RxBuf ***

    TxBuf[0] = inputMode | (channelNumber >> 2);
    TxBuf[1] = channelNumber << 0x6;
    TxBuf[2] = 0x00; // Don't care

RxBuf[0] = 0x00;
RxBuf[1] = 0x00;
RxBuf[2] = 0x00;

// *** Print TxBuf and RxBuf

char TxTitle[10];
char RxTitle[10];

strcpy(TxTitle, "TxBuf ");
strcpy(RxTitle, "RxBuf ");

// *** Before ADC ***
printf("\n*** Before ADC values of TxBuf and RxBuf *** \n\n");
printf("Index    0  1   2\n\n");
printXferBuffers(TxBuf, RxBuf, TxTitle, RxTitle);

// *** ADC once and print results ***

setSselLow();
    SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
    setSselHigh();

// *** After ADC (Ch 2 = 1.64V) ***
printf("\n*** After ADC (Ch 2 = 1.64V, ARef = 2.49V) *** \n\n");

printf("Index    0  1   2\n\n");
printXferBuffers(TxBuf, RxBuf, TxTitle, RxTitle);

    // *** Calculate ADC results ***

    uint32_t adcResultDecimal;
    adcResultDecimal = ((RxBuf[1] & 0x0f) * 256) + RxBuf[2];

    // *** Print results ***
    printf("ADC result hex = %x\n", adcResultDecimal);

    printf("ADC result decimal = %4d\n", adcResultDecimal);

    uint32_t adcResultMilliVolt;
    adcResultMilliVolt =  (2490 * adcResultDecimal) / 4096;

    printf("ADC result milliVolt = %4d\n", adcResultMilliVolt);

// *** Print End of Test ***
printf("\n*** End of Test. *** \n\n");

    // *** Loop to display waveform by scope ***
    uint8_t i;
    for (i = 0; i < 10000000; i++)
    {
    delayMilliSecond(1);
    setSselLow();
        SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
        setSselHigh();
    }
}

void printXferBuffers(uint8_t TxBuf[BUFFER_SIZE], uint8_t RxBuf[BUFFER_SIZE], char TxTitle[10], char RxTitle[10])
{
uint8_t Tx0 = TxBuf[0];
uint8_t Tx1 = TxBuf[1];
uint8_t Tx2 = TxBuf[2];

uint8_t Rx0 = RxBuf[0];
uint8_t Rx1 = RxBuf[1];
uint8_t Rx2 = RxBuf[2];

printf("%s %02x %02x %02x \n", TxTitle, Tx0, Tx1, Tx2);
printf("%s %02x %02x %02x \n", RxTitle, Rx0, Rx1, Rx2);
}

/*
*** MCP3208 Test - 2013oct26hk1201 ***

*** Before ADC values of TxBuf and RxBuf ***
Index    0  1   2
TxBuf  06 80 00
RxBuf  00 00 00

*** After ADC (Ch 2 = 1.64V, ARef = 2.49V) ***
Index    0  1   2
TxBuf  06 80 00
RxBuf  ff ea 7f
ADC result hex = a7f
ADC result decimal = 2687
ADC result milliVolt = 1633

*** End of Test. ***
*/

// *** SPI0 Functions ***

void setupSpi050(SCK0_Position_Typedef sck0)
{
// Enable SSP0 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP0, ENABLE);

// Reset SSP0 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);

// Assign GPIO pins for SPI
// SSP_SSP0PinsInit(sck0, ENABLE);
SSP_SSP0PinsInit(sck0, DISABLE); // Disable SSEL

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP0, &SSP_ConfigStruct);

// Enable SSP peripheral
SSP_Cmd(LPC_SSP0, ENABLE);
}

void setupXferConfig(SSP_DATA_SETUP_Type *xferConfigPointer, uint8_t digitControlRegisterBuffer[], \
            uint8_t dummyReceiveBuffer[], uint8_t bufferSize)
{
    xferConfigPointer->tx_data = digitControlRegisterBuffer;
    xferConfigPointer->rx_data = dummyReceiveBuffer;
    xferConfigPointer->length = bufferSize;
 }

void setupSpiSsel()
{
setupGpioPinOutputLow050(PortPinArraySsel); // setup GPIO pin as output
}

void setSselLow()
{
setGpioDataPinLow01(PortPinArraySsel); // SSEL low
}

void setSselHigh()
{
setGpioDataPinHigh01(PortPinArraySsel); // SSEL High
}

.END

No comments:

Post a Comment