2019-12-10 08:30 AM
Initialization
1. As prerequisite, fill in the HAL_SDADC_MspInit() : Enable SDADCx clock interface with __SDADCx_CLK_ENABLE().
Configure SDADCx clock divider with HAL_RCCEx_PeriphCLKConfig.
Enable power on SDADC with HAL_PWREx_EnableSDADC().
Enable the clocks for the SDADC GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
Configure these SDADC pins in analog mode using HAL_GPIO_Init().
If interrupt mode is used, enable and configure SDADC global interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
If DMA mode is used, configure DMA with HAL_DMA_Init and link it with SDADC handle using __HAL_LINKDMA.
2. Configure the SDADC low power mode, fast conversion mode, slow clock mode and SDADC1 reference voltage using the HAL_ADC_Init() function.
Note: Common reference voltage. is common to all SDADC instances.
3. Prepare channel configurations (input mode, common mode, gain and offset) using HAL_SDADC_PrepareChannelConfig and associate channel with one configuration using HAL_SDADC_AssociateChannelConfig.
Calibration
1. Start calibration using HAL_SDADC_StartCalibration or HAL_SDADC_CalibrationStart_IT.
2. In polling mode, use HAL_SDADC_PollForCalibEvent to detect the end of calibration.
3. In interrupt mode, HAL_SDADC_CalibrationCpltCallback will be called at the end of calibration.
Regular channel conversion
1. Select trigger for regular conversion using HAL_SDADC_SelectRegularTrigger.
2. Select regular channel and enable/disable continuous mode using HAL_SDADC_ConfigChannel.
3. Start regular conversion using HAL_SDADC_Start, HAL_SDADC_Start_IT or HAL_SDADC_Start_DMA.
4. In polling mode, use HAL_SDADC_PollForConversion to detect the end of regular conversion.
5. In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the end of regular conversion.
6. Get value of regular conversion using HAL_SDADC_GetValue.
7. In DMA mode, HAL_SDADC_ConvHalfCpltCallback and HAL_SDADC_ConvCpltCallback will be called respectively at the half transfer and at the transfer complete.
8. Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT or HAL_SDADC_Stop_DMA.
and my code is:
#include "main.h"
#include"stdio.h"
#include "stm32f3xx_hal.h"
ADC_HandleTypeDef hadc1;
SDADC_HandleTypeDef hsdadc1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SDADC1_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
volatile int16_t rawAnalogValueI = 0;
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//Code permettant d'utiliser la liaison série pour une commande printf
int __io_putchar (int ch)
{
HAL_UART_Transmit (&huart2, (uint8_t *) &ch, 1, 100);
return (ch);
}
int _write (int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar (*ptr++);
}
return len;
}
void ErrorDisplay (char * strLine)
{
printf ("%s", strLine);
while (1)
{
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
void HAL_SDADC_ConvCpltCallback (SDADC_HandleTypeDef *hsdadc)
{
if (hsdadc == &hsdadc1)
rawAnalogValueI = HAL_SDADC_GetValue(hsdadc);
printf("%d\n", rawAnalogValueI);
}
//---------------------------------------------------------------------------------------------------------pourquoi redéfinir convcpltcallback?
void ErrorDisplay (char * strLine)
{
printf ("%s", strLine);
while (1)
{
}
}
#define V_REF_CTSR03P 0.0
#define V_OFFSET_CTSR03P 0.01
#define GAIN_CTSR03P 0.235
float getIntensity (void)
{
float analogValueI;
float analogValueISensorOutput;
float gainSDAC1 = 1.0;
HAL_SDADC_Start(&hsdadc1);
HAL_SDADC_ConvCpltCallback(&hsdadc1);
HAL_SDADC_Stop(&hsdadc1);
// Convertion binaire / tension (SDADC)
analogValueISensorOutput = ((float) rawAnalogValueI + 32767) * 3.3 / (gainSDAC1 * (2.0 * 32767.0));
// Convertion tension / courant (CTSR03P)
analogValueI = (((analogValueISensorOutput - V_OFFSET_CTSR03P) * GAIN_CTSR03P)*251.18) - 623.95;
//printf ("%+05d|%f|%f\r\n", rawAnalogValueI, analogValueISensorOutput, analogValueI);
printf("%d\n", rawAnalogValueI);
printf("%f\n", analogValueISensorOutput);
printf("%f\n\n", analogValueI);
return analogValueI;
}
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDADC1_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
if (HAL_SDADC_CalibrationStart(&hsdadc1, SDADC_CALIBRATION_SEQ_1) != HAL_OK)
{
ErrorDisplay ((uint8_t*)"Fail to calibrate hsdadc1\r\n");
}
else
{
printf("La calibration a ete faite\n");
}
HAL_SDADC_CalibrationCpltCallback(&hsdadc1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
printf("yo\n");
getIntensity();
HAL_Delay(5000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Do you know what i have done wrong? If you need the ioc or the rest of the code please tell me.
Thank you for your time,