2013-10-26

MCP3208 ADC function refactoring notes

*** MCP3208 Test - 2013oct26hk1441 ***

*** Tx Rx buffer contents before ADC *** 
Index    0  1   2
TxBuf  06 80 00 
RxBuf  00 00 00 

*** Tx Rx buffer contents after ADC *** 
Index    0  1   2
TxBuf  06 80 00 
RxBuf  ff ea 7f 

***ADC Results Summary *** 
ADC Channel = 2
SPI Channel = 0
ADC result hex = a7f
ADC result decimal = 2687
ADC result milliVolt = 1633

*** End of Test. *** 

// ***********************************************************************
// Main Function
// ***********************************************************************
int main()
{

testMcp3208();

}

// ***********************************************************************
// test050.h 2013oct24hkt1022
// ***********************************************************************
#include "led050.h"
#include "key050.h"
#include "spi050.h"
#include "adc050.h"

void testMcp3208() // 2013oct26
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Test MCP3028 ***
mcp3208v03(SPI_CHANNEL_0); // SPI0
}

// ***********************************************************************
// adc050.h 2013oct26hk1207
// ***********************************************************************

// ***********************************************************************
// Mcp3208 using SPI0
// ***********************************************************************

void mcp3208v03(int spiChannelNumber) // 2013oct26hkt1208
{
// *** Setup SPI transfer channels and buffers ***************************
if (spiChannelNumber == 0)
{
// Setup SPI0 with clock pin = P06
setupSpi050(SCK0_PIO0_6);

// Setup SSEL0 pin = P02
setupSpiSsel();
}
else //(spiChannel == 1)
{

}

    // 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;

    // *** Select ADC 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 transfer buffers ***************************************

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

RxBuf[0] = 0x00; // Don't care
RxBuf[1] = 0x00; // Don't care
RxBuf[2] = 0x00; // Don't care

uint8_t TxBuf0[BUFFER_SIZE]; // for printing only
uint8_t RxBuf0[BUFFER_SIZE]; // for printing only
memcpy(TxBuf0, TxBuf, BUFFER_SIZE); // remember values for later printing
memcpy(RxBuf0, RxBuf, BUFFER_SIZE); // remember values for later printing

// *** Do ADC ************************************************************

    SpiWriteRead050(xferConfig, spiChannelNumber);

    // *** Calculate ADC results ***
    uint32_t adcResultDecimal;
    uint32_t adcResultMilliVolt;
    adcResultDecimal = ((RxBuf[1] & 0x0f) * 256) + RxBuf[2];
    adcResultMilliVolt =  (2490 * adcResultDecimal) / 4096;

    // *** Print results *****************************************************

    printf("*** MCP3208 Test - 2013oct26hk1441 ***\n\n");

    char TxTitle[10];
char RxTitle[10];
strcpy(TxTitle, "TxBuf ");
strcpy(RxTitle, "RxBuf ");

printf("\n*** Tx Rx buffer contents before ADC *** \n\n");
printf("Index    0  1   2\n\n");
printf("%s %02x %02x %02x \n", TxTitle, TxBuf0[0], TxBuf0[1], TxBuf0[2]);
printf("%s %02x %02x %02x \n", RxTitle, RxBuf0[0], RxBuf0[1], RxBuf0[2]);

printf("\n*** Tx Rx buffer contents after ADC *** \n\n");
printf("Index    0  1   2\n\n");
printf("%s %02x %02x %02x \n", TxTitle, TxBuf[0], TxBuf[1], TxBuf[2]);
printf("%s %02x %02x %02x \n", RxTitle, RxBuf[0], RxBuf[1], RxBuf[2]);

printf("\n***ADC Results Summary *** \n\n");
printf("ADC Channel = %d\n", channelNumber);
printf("SPI Channel = %d\n", spiChannelNumber);
        printf("ADC result hex = %x\n", adcResultDecimal);
        printf("ADC result decimal = %4d\n", adcResultDecimal);
        printf("ADC result milliVolt = %4d\n", adcResultMilliVolt);
printf("\n*** End of Test. *** \n\n");

    // *** Loop to display waveform by scope ***
    while (1)
    {
        SpiWriteRead050(xferConfig, spiChannelNumber);
    delayMilliSecond(1);
    }
}

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

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

// *** SPI0 Functions ***

#define SPI_CHANNEL_0 0
#define SPI_CHANNEL_1 1

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