*** Tx Rx buffer contents before ADC ***
Index 0 1 2
TxBuf 06 40 00
RxBuf 00 00 00
*** Tx Rx buffer contents after ADC ***
Index 0 1 2
TxBuf 06 40 00
RxBuf ff ea 7c
***ADC Results Summary ***
ADC Channel = 1
SPI Channel = 0
ADC result hex = a7c
ADC result decimal = 2684
ADC result milliVolt = 1631
*** End of Test. ***
testMcp3208();
void testMcp3208() // 2013oct26
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led
// *** Test MCP3028 ***
mcp3208v03(SPI_CHANNEL_0, ADC_CHANNEL_1); // SPI0
}
// ***********************************************************************
// adc050.h 2013oct26hk1645
// ***********************************************************************
// ***********************************************************************
// Mcp3208 using SPI0/1
// ***********************************************************************
#define ADC_CHANNEL_0 0
#define ADC_CHANNEL_1 1
#define ADC_CHANNEL_2 2
#define ADC_CHANNEL_3 3
#define ADC_CHANNEL_4 4
#define ADC_CHANNEL_5 5
#define ADC_CHANNEL_6 6
#define ADC_CHANNEL_7 7
void mcp3208v03(int spiChannelNumber, uint8_t adcChannelNumber)
{
// *** Setup SPI transfer channels and buffers ***************************
setupSpi051(spiChannelNumber);
// Setup transfer buffers TxBuf, RxBuf
#define BUFFER_SIZE 3
uint8_t TxBuf[BUFFER_SIZE];
uint8_t RxBuf[BUFFER_SIZE];
// Setup xferConfig
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 **************************
#define SINGLE_END_MODE 0x06; // bit 1 = 1
#define DIFFERENTIAL_END_MODE 0x04; // bit 1 = 0
uint8_t adcInputMode;
adcInputMode = SINGLE_END_MODE;
// *** Initialize transfer buffers ***************************************
TxBuf[0] = adcInputMode | (adcChannelNumber >> 2);
TxBuf[1] = adcChannelNumber << 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 *****************************************************
char TxTitle[10];
char RxTitle[10];
strcpy(TxTitle, "TxBuf ");
strcpy(RxTitle, "RxBuf ");
printf("*** MCP3208 Test - 2013oct26hk1646 ***\n\n");
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", adcChannelNumber);
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 2013oct26hk1611
// ***********************************************************************
#include "gpio050.h"
#include "led050.h"
#include "lpc11xx_ssp.h"
#include "semihosting.h"
#include "stdio.h"
#include "config050.h"
// *** SPI Functions ***
#define SPI_CHANNEL_0 0
#define SPI_CHANNEL_1 1
void setupSpi051(int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
// 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_PIO0_6, DISABLE); // Select P06, disable
// 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);
// Setup Ssel0
setupGpioPinOutputLow050(PortPinArraySsel0);
}
else // (if spiChannelNumber == 1)
{
// Enable SSP1 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP1, ENABLE);
// Reset SSP1 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, DISABLE);
SYSCON_SetSPI1ClockDiv(10);
// No need to assign GPIO pins for SPI1, just disable
SSP_SSP1PinsInit(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_SSP1, &SSP_ConfigStruct);
// Enable SSP1 peripheral
SSP_Cmd(LPC_SSP1, ENABLE);
// Setup Ssel1
setupGpioPinOutputLow050(PortPinArraySsel1);
}
}
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 SpiWriteRead050(SSP_DATA_SETUP_Type xferConfig, int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
setGpioDataPinLow01(PortPinArraySsel0);
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel0);
}
else // (spiChannelNumber == 1)
{
setGpioDataPinLow01(PortPinArraySsel1);
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel1);
}
}
.END
No comments:
Post a Comment