2018-06-18 10:47 PM
Hi All,
I am developing driver for ADC104S021 for STM32F103
I have tried to read AD converted value from ADC via SPI, the value seemed not correct one.
timing diagram as follows (from datasheet).
my SPI configuration and read function are as follows
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
HAL_StatusTypeDef SPI_readADC(uint8_t *pBuf, uint8_t channel)
{
uint8_t i;
uint8_t controlReg[2];
HAL_StatusTypeDef status;
controlReg[0] = (channel ? 0x08 : 0x00); //
controlReg[1] = 0;
// enable nCS : 1 0
for(i=0; i<180; i++) {
HAL_GPIO_WritePin(MCU_A1_GPIO_Port, MCU_A1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(MCU_A0_GPIO_Port, MCU_A0_Pin, GPIO_PIN_RESET);
}
HAL_GPIO_WritePin(MCU_nSCS_OUT2_GPIO_Port, MCU_nSCS_OUT2_Pin, GPIO_PIN_RESET);
// send the address of indexed register
status = HAL_SPI_TransmitReceive(&hspi1, controlReg, pBuf, 2, 100);
// disable nCS
HAL_GPIO_WritePin(MCU_nSCS_OUT2_GPIO_Port, MCU_nSCS_OUT2_Pin, GPIO_PIN_SET);
return status;
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
MCU_A1_Pin and MCU_A0_Pin are for generating nCS signal, and they are OK.
controlReg variable is for selecting input channel.
I have called HAL_SPI_TransmitReceive() API, since writing control register is overlapped with reading ADC data.
I tried to read value with following code.
uint8_t adcBuf[2] = {0, 0}, channel=0;
channel = 1;
spi_status = SPI_readADC(adcBuf, (uint8_t)channel);
printf(''ADC chan %d value 0x%x 0x%x, status=%d\r\n'', channel, adcBuf[0], adcBuf[1], spi_status);�?�?�?�?�?
printed value is
====
ADC chan 1 value 0x2 0xf9, status=0
====
captured data from oscilloscope as follows.
As I read value from capture, it seems to be 0x2, 0x50, but printed log showed it as 0x2, 0xF9.
this is just example, all the values I've got from log are not matched with measured value from scope.
Trying test with multiple ADC values, first byte seems OK, but second byte is totally wrong.
Would you please let me know, what is wrong with my code ?
please help me to debug this, and let me know if you need more info. (FYI, datasheet is attached)
Thanks in advance.
Best Regards,
Hae Ryong
#stm32f103Solved! Go to Solution.
2018-06-24 09:57 PM
Hi All,
Sorry for confusing.
It was caused by capacitor connected with DOUT pin, data is correctly received after removing capacitor.
2018-06-24 09:57 PM
Hi All,
Sorry for confusing.
It was caused by capacitor connected with DOUT pin, data is correctly received after removing capacitor.