2013-10-23

MCP3208 testing notes

So I used semhosting printf to display the ADC results in Rx.  But I don't find the ADC results in RxBuf.  

It appears that I made a wrong guess that SSP_ReadWrite() would write the results back to RxBuf.  Perhaps I should do it myself by the following statement.

    RxBuf = xferConfig.rx_data; 


*** MCP3208 Test - 2013oct23hk0940 ***


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

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

.END

// ***********************************************************************
// spi050.h 2013oct23hkt1022
// ***********************************************************************

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

// *** MCP320802 *************************************************************

void mcp320802()
{
    // *** Print title ***
printf("*** MCP3208 Test - 2013oct23hk0940 ***\n\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 = TxBuf;
    xferConfig.rx_data = 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, print TxBuf and RxBuf ***
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, print TxBuf and RxBuf ***
printf("\n*** After ADC values of TxBuf and RxBuf *** \n\n");
printf("Index    0  1   2\n\n");
printXferBuffers(TxBuf, RxBuf, TxTitle, RxTitle);

    // *** calculate ADC results ***

    uint16_t adcOutputHex;

    // adcOutputHex = ((Rx_Buf[1] & 0x0f) * (2 ** 8)) + Rx_Buf[2];
    adcOutputHex = ((RxBuf[1] & 0x0f) * 256) + RxBuf[2];


    // *** 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);
}

// *** MCP320802 *************************************************************

No comments:

Post a Comment