Alpaslan_Ersoz
Junior Member level 2
- Joined
- Aug 9, 2018
- Messages
- 20
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 220
uint8_t i;
uint16_t Sample;
float volt=0;
uint8_t ADC_Buf[2];
float testdata_in[16];
uint8_t spidata[2];
void ADC_Conversion_of_Voltage_Transients()
{
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]);
volt = (float)(Sample * (5.0 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
}
Please tell us what "volt levels" you expect. In numbers. Best if you can give a range.I miss something because the 'volt' shows different values than input signal amplitudes.
I expect that the values of 'volt' change between 0 and 0.52V. But in Keil debug mode, the 'volt' values in the 'watch window' is different than this values.Please tell us what "volt levels" you expect. In numbers. Best if you can give a range.
void ADC_Conversion_of_Voltage_Transients()
{
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,2,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;
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_8;
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__);
}
}
I don't see how you did it...thus I have to guess:I try to implement NSS enable with Hardware output. But I didn't observe any signal at ADC CS. IF you refer any source, I will definitely implement it.
--> Did you change to following line from SOFT(-ware controlled)to HARD(-ware controlled)? (As suggested in my first post)
--> Did you connect a pullup to the NSS line? (As suggested in my first post)
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_1EDGE;
hspi4.Init.NSS = SPI_NSS_HARD_OUTPUT;
hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
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__);
}
}
void ADC_Conversion_of_Voltage_Transients()
{
spidata[0]=0x00; // Set everything up before you start the exchange
spidata[1]=0x00;
HAL_SPI_TransmitReceive(&hspi4,spidata,ADC_Buf,1,100); // See below
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;
volt = (float)(Sample * (3.3 / 4096.0)); //
testdata_in[i++]=volt;
i %= 16;
You need to externally solder a pull up resistor.Yes I enabled NSS with Hardware output in CubeMX and pulled-up CS pin.
The receiving values are OK?Now the SPI works ....
It does not depend on pull up resistor, it just depends on setup in your software.full duplex master mode so is it also true?
Just my curiosity, could you briefly describe this part of your message?But in detail it depends in:
* expected speed
* drive strength of the output pin
* signal trace length, connected devices (= capacitance)
Although I can't follow your idea, I can say it is completely wrong.So, based on the graphics, the conversion time (tconv) is 1.2us. The sampling rate is (13.5Mbits/s)/16=843.75ksample/s. So these calculations are true?
Simply every piece of trace is a capacitance, every input is a capacitance...now you want a to charge/discharge this capacitance.Just my curiosity, could you briefly describe this part of your message?
Sorry, I really don't understand what you tell in post#30.I don't know how long should I wait to see
How can I change it?You can simply read the sampling frequency in the scope picture: 1.7311kHz.
If I set the SPI baudrate as around 1Mbits/s, the clock signal shape looks rectangular.The SCK looks far away from being a square wave
read my previous postsHow can I change it?
that's the wrong way roundIf I set the SPI baudrate as around 1Mbits/s, the clock signal shape looks rectangular.
read my previous posts
--> /CS = NSS needs to toggle!
while(1)
{
if(Timer_Flag)
{
ADC_Conversion_of_Voltage_Transients();
Timer_Flag=0;
}
}
void ADC_Conversion_of_Voltage_Transients()
{
spidata[0]=0x00; // Set everything up before you start the exchange
spidata[1]=0x00;
HAL_SPI_TransmitReceive(&hspi4,ADC_Buf,ADC_Buf,1,100); // See below
Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;
volt = (float)(Sample * (3.3 / 4095.0)); //
testdata_in[i++]=volt;
i %= 16;
}
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(TIM_CRTL_GPIO_Port, TIM_CRTL_Pin);
Timer_Flag=1;
/* USER CODE END TIM4_IRQn 1 */
}
It depends on how often you toggle /CS .... and read the conversion data.
But hopefully you don't try to use te run time of the main loop for sampling timing control.
--> You need at least an Interrupt with fixed, known timing. Or let the hardware (timer) directly start the conversion (without tge need fir an ISR)
If you are running low on processing power, then I recommend to use SPI in DMA mode.
If you want a 10kHz sampling rate, then you need a falling edge every 100us.
.. but for sure after the falling /CS edge you need to go on according interface specification:
At least 16 SCK cycles to perform the data transfer.
And after this you need to set /CS high.
I think you are getting a number of concepts mixed up here.So based on that I would use SPI DMA mode instead of timer interrupt for setting sampling frequency?
/* TIM4 init function */
static void MX_TIM4_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim4.Instance = TIM4;
htim4.Init.Prescaler = 108-1;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 10-1;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
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?