yefj
Advanced Member level 5
Hello,I want to connect in single ended external ADC source which is PA1 and PA0 in the extension header as shown bellow.
i have connected 3.3V to PA1 and GND to PA0 from the expansion as shown bellow.
The basic ADC github example was modified with sending the data using SPI as shown in the code bellow.
I noticed that there is no ADC location setting in the code bellow, maybe because it has only one ADC location.
So the reference voltage is 5 and we sample 3.3V. We have Sample variable which is uint32 which i broke into uint8 peaces to transfer over SPI.
The SPI in the photos bellow sent the binary data as shown in the last photo 00000000000000000000011010111111(twos complement) which is 1727 in decimal. V_LSB=5/(2^12)=0.0012V so 1727*0.0012=2.1 which is far from being 3.3. Also there is a formula shown bellow which by it i also get millivolts = (sample * 2500) / 4096=1054
Where did i go wrong? why i dont get 3.3V i sampled?
Thanks.
i have connected 3.3V to PA1 and GND to PA0 from the expansion as shown bellow.
The basic ADC github example was modified with sending the data using SPI as shown in the code bellow.
I noticed that there is no ADC location setting in the code bellow, maybe because it has only one ADC location.
So the reference voltage is 5 and we sample 3.3V. We have Sample variable which is uint32 which i broke into uint8 peaces to transfer over SPI.
The SPI in the photos bellow sent the binary data as shown in the last photo 00000000000000000000011010111111(twos complement) which is 1727 in decimal. V_LSB=5/(2^12)=0.0012V so 1727*0.0012=2.1 which is far from being 3.3. Also there is a formula shown bellow which by it i also get millivolts = (sample * 2500) / 4096=1054
Where did i go wrong? why i dont get 3.3V i sampled?
Thanks.
Code:
#include "em_gpio.h"
#include <stdio.h>
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_adc.h"
#include "em_usart.h"
#define adcFreq 16000000
volatile uint32_t sample;
volatile uint32_t millivolts;
uint32_t Tx_PP= 0x02F003E7;
uint8_t arr[4];
void initUSART1 (void)
{
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(cmuClock_USART1, true);
// Configure GPIO mode
GPIO_PinModeSet(gpioPortC, 8, gpioModePushPull, 0); // US1_CLK is push pull
GPIO_PinModeSet(gpioPortC, 9, gpioModePushPull, 1); // US1_CS is push pull
GPIO_PinModeSet(gpioPortC, 6, gpioModePushPull, 1); // US1_TX (MOSI) is push pull
GPIO_PinModeSet(gpioPortC, 7, gpioModeInput, 1); // US1_RX (MISO) is input
GPIO_PinModeSet(gpioPortF, 7, gpioModeInput, 1); // delay gpio
// Start with default config, then modify as necessary
USART_InitSync_TypeDef config = USART_INITSYNC_DEFAULT;
config.master = true; // master mode
config.baudrate = 1000000; // CLK freq is 1 MHz
config.autoCsEnable = true; // CS pin controlled by hardware, not firmware
config.clockMode = usartClockMode0; // clock idle low, sample on rising/first edge
config.autoCsEnable = true; // CS pin controlled by firmware
config.msbf = true; // send MSB first
config.enable = usartDisable; // Make sure to keep USART disabled until it's all set up
USART_InitSync(USART1, &config);
// Set USART pin locations
USART1->ROUTELOC0 = (USART_ROUTELOC0_CLKLOC_LOC11) | // US1_CLK on location 11 = PC8 per datasheet section 6.4 = EXP Header pin 8
(USART_ROUTELOC0_CSLOC_LOC11) | // US1_CS on location 11 = PC9 per datasheet section 6.4 = EXP Header pin 10
(USART_ROUTELOC0_TXLOC_LOC11) | // US1_TX (MOSI) on location 11 = PC6 per datasheet section 6.4 = EXP Header pin 4
(USART_ROUTELOC0_RXLOC_LOC11); // US1_RX (MISO) on location 11 = PC7 per datasheet section 6.4 = EXP Header pin 6
// Enable USART pins
USART1->ROUTEPEN = USART_ROUTEPEN_CLKPEN | USART_ROUTEPEN_CSPEN | USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;
// Enable USART1
USART_Enable(USART1, usartEnable);
// Enable oscillator to GPIO and USART0 modules
}//end usart init
//////////////////////end SPI
/**************************************************************************//**
* @brief Initialize ADC function
*****************************************************************************/
void initADC (void)
{
// Enable ADC0 clock
CMU_ClockEnable(cmuClock_ADC0, true);
// Declare init structs
ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
ADC_InitSingle_TypeDef initSingle = ADC_INITSINGLE_DEFAULT;
// Modify init structs and initialize
init.prescale = ADC_PrescaleCalc(adcFreq, 0); // Init to max ADC clock for Series 1
init.timebase = ADC_TimebaseCalc(0);
initSingle.diff = false; // single ended
initSingle.reference = adcRef5V; // internal 5V reference
initSingle.resolution = adcRes12Bit; // 12-bit resolution
initSingle.acqTime = adcAcqTime4; // set acquisition time to meet minimum requirements
// Select ADC input. See README for corresponding EXP header pin.
initSingle.posSel = adcPosSelAPORT4XCH11;
ADC_Init(ADC0, &init);
ADC_InitSingle(ADC0, &initSingle);
// Enable ADC Single Conversion Complete interrupt
ADC_IntEnable(ADC0, ADC_IEN_SINGLE);
// Enable ADC interrupts
NVIC_ClearPendingIRQ(ADC0_IRQn);
NVIC_EnableIRQ(ADC0_IRQn);
}
/**************************************************************************//**
* @brief ADC Handler
*****************************************************************************/
void ADC0_IRQHandler(void)
{
// Get ADC result
sample = ADC_DataSingleGet(ADC0);
arr[0]=(sample >> 24) & 0xFF;
arr[1]=(sample >> 16) & 0xFF;
arr[2]=(sample >> 8) & 0xFF;
arr[3]=(sample) & 0xFF;
USART_SpiTransfer(USART1,arr[0]);
USART_SpiTransfer(USART1,arr[1]);
USART_SpiTransfer(USART1,arr[2]);
USART_SpiTransfer(USART1,arr[3]);
// Calculate input voltage in mV
// millivolts = (sample * 2500) / 4096;
// Start next ADC conversion
ADC_Start(ADC0, adcStartSingle);
}
/**************************************************************************//**
* @brief Main function
*****************************************************************************/
int main(void)
{
CHIP_Init();
initADC();
initUSART1();
// Start first conversion
ADC_Start(ADC0, adcStartSingle);
// Infinite loop
while(1);
}