void ADC_Conversion_of_Voltage_Transients()
{
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi4,ADC_Buf,2,100);
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]);
//Sample = 0x0 << 12 | ADC_Buf;
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
}
/* SPI4 init function */
static void MX_SPI4_Init(void)
{
/* SPI4 parameter configuration*/
hspi4.Instance = SPI4;
hspi4.Init.Mode = SPI_MODE_MASTER;
hspi4.Init.Direction = SPI_DIRECTION_2LINES;
hspi4.Init.DataSize = SPI_DATASIZE_16BIT;
hspi4.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi4.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi4.Init.NSS = SPI_NSS_SOFT;
hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi4.Init.CRCPolynomial = 7;
hspi4.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
No, datasheet does not say you need to drive SCLK with 20MHz .... instead it says maximum SCLK frequency is 20MHz.I should set ADC SCK as 20 MHz, but I couldn't do that with SPI. Because I can set only either 13.5 Mbits/s or 26 Mbits/s baudrate of the SPI. So how can I correctly drive ADC and read the digitized values?
Right! When I double check the datasheet, I revealed the SCLK maximum frequency. But TINA-TI simulation for ADC SPICE model, they run the ADC around 41.2ns clock period.No, darasheet does not say you need to drive SCLK with 20MHz .... instead it says maximum SCLK frequency is 20MHz.
Thus 26MHz is out of specification.
But you may safely use 13.5MHz.
This is good point. Actually the STM has 3.3V supply voltage, so it may goes to saturation for more than 3.3V voltages. But the figure, whose name is test adc input 10 7 19 (you will see figure name at the bottom right of the figure when you click of it), is the ADC input analog signal and its amplitude maximum around 2.76V. So it is in the limit both microcontroller and ADC.Is your STM input 5V compatible?
Honestly, I didn't enable NSS pin of the SPI. Instead of it I use GPIO pin as reset and set function for CS of the ADC.You use NSS_soft. This is not wrong, but why not hardware generated NSS? Mind: at least in hardware generated NSS mode the NSS signal is "open collector style" and needs external pull up resistor
Actually, in the datasheet, it says like "SDO goes to the 3-state condition", so I assume that it is mode 3 SPI, then I set as CPOL=1 and CPHA=1. But I will try your recommendation.You use "SPI_PHASE_2EDGE", I'm not sure, but I'd use "SPI_PHASE_1EDGE" ... according ADC timing diagram.
Let's try to make a short list each of the figure name and its explanations. They may be more clear.I can't verify your scope picures, because it's not clear to me which picture shows which signals..
Give one picture only with clear signal description.
I don't see any Tina simulation, nor any timing. But indeed it does not matter anyway.But TINA-TI simulation for ADC SPICE model, they run the ADC around 41.2ns clock period.
/CS controls the conversion. Falling edge samples the analog input voltage and starts the conversion.Honestly, I didn't enable NSS pin of the SPI. Instead of it I use GPIO pin as reset and set function for CS of the ADC.
"3-state" is not related to SPI mode ... not to SPI at all."SDO goes to the 3-state condition", so I assume that it is mode 3 SPI, then I set as CPOL=1 and CPHA=1. But I will try your recommendation.
uint8_t ADC_Buf[2];
uint16_t Sample;
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi4,ADC_Buf,1,100);
Sample = (((uint16_t) ADC_Buf[0]) << 8 | ADC_Buf[1]) & 0x0FFF;
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
SPI settings have nothing to do with sampling frequency.so sampling frequency should be 30kHz. After that I don't know how can I modify SPI settings based on this calculation?
What do you mean with "all the outputs"?The other thing is that how long should I wait to see all the outputs of the digital conversion?
What does this mean? Plotting a graph, or plotting the numbers?If I can plot the digitized value, it will be great.
Ok, I will try to control timer interrupt and share my results.
/* USER CODE BEGIN 0 */
uint8_t Timer_Flag;
/* USER CODE END 0 */
void TIM4_IRQHandler(void)
{
/* USER CODE BEGIN TIM4_IRQn 0 */
/* USER CODE END TIM4_IRQn 0 */
HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
HAL_GPIO_TogglePin(ADC_CS_GPIO_Port, ADC_CS_Pin);
Timer_Flag=1;
/* USER CODE END TIM4_IRQn 1 */
}
void ADC_Conversion_of_Voltage_Transients()
{
if(Timer_Flag)
{
HAL_SPI_Receive(&hspi4,ADC_Buf,16,100);
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
Timer_Flag=0;
}
}
void TIM4_IRQHandler(void)
{
/* USER CODE BEGIN TIM4_IRQn 0 */
/* USER CODE END TIM4_IRQn 0 */
HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
HAL_GPIO_TogglePin(ADC_CS_GPIO_Port, ADC_CS_Pin);
Timer_Flag=1;
//volt = (float)(testdata_in * (5.0 / 4096.0)); //
/* USER CODE END TIM4_IRQn 1 */
}
void ADC_Conversion_of_Voltage_Transients()
{
HAL_SPI_Receive(&hspi4,ADC_Buf,16,100);
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
}
if (Timer_Flag)
{
ADC_Conversion_of_Voltage_Transients();
Timer_Flag=0;
}
I see it differently: SCK is active all the time.But ADC SCK is not active only ADC CS is at low level.
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);
spidata[0]=0x00;
spidata[1]=0x00;
HAL_SPI_Transmit(&hspi4, spidata,2,100);
HAL_SPI_Receive(&hspi4,ADC_Buf,16,100);
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
spidata[0]=0x00; // Set everything up before you start the exchange
spidata[1]=0x00;
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi4, spidata,ADC_Buf,1,100); // See below
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF; // Do this after you have turned off the ADC
HAL_SPI_TransmitReceive(&hspi4, ADC_Buf,ADC_Buf,1,100);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?