Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f0xx_hal.h"
- #include "usb_device.h"
- /* USER CODE BEGIN Includes */
- /* Temperature sensor calibration value address */
- #define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
- #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
- #define VREFINT_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7BA))
- #define VDD_CALIB ((uint16_t) (330))
- #define VDD_APPLI ((uint16_t) (300))
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc;
- DMA_HandleTypeDef hdma_adc;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- volatile uint8_t i = 0;
- volatile uint16_t adcVal[4];
- volatile float vdd;
- volatile uint32_t vrefCal;
- volatile float temp;
- volatile float dBm;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_ADC_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC_Init();
- MX_USB_DEVICE_Init();
- /* USER CODE BEGIN 2 */
- HAL_ADC_Start_IT(&hadc);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_Delay(100);
- HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
- updateVdd();
- updateTemp();
- }
- /* USER CODE END 3 */
- }
- void updateVdd(void) {
- vrefCal = *VREFINT_CAL_ADDR;
- vdd = 3.3 * (float)(*VREFINT_CAL_ADDR) / adcVal[2];
- }
- void updateTemp(void) {
- volatile float temperature; /* will contain the temperature in degree Celsius */
- temperature = (((float) adcVal[1] * vdd / 3300) - (float) *TEMP30_CAL_ADDR );
- temperature = temperature * (float)(110 - 30);
- temperature = temperature / (float)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);
- temp = temperature + 30;
- }
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
- if (__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOC)) {
- adcVal[i] = HAL_ADC_GetValue(hadc);
- i++;
- }
- if (__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOS)) {
- i = 0;
- }
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSI48;
- RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
- RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
- RCC_OscInitStruct.HSI14CalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
- PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
- HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* ADC init function */
- void MX_ADC_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
- hadc.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;
- hadc.Init.LowPowerAutoWait = DISABLE;
- hadc.Init.LowPowerAutoPowerOff = DISABLE;
- hadc.Init.ContinuousConvMode = ENABLE;
- hadc.Init.DiscontinuousConvMode = DISABLE;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc.Init.DMAContinuousRequests = DISABLE;
- hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- HAL_ADC_Init(&hadc);
- //ADC_TempSensorVrefintCmd(ENABLE);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_3;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_VREFINT;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_VBAT;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- }
- /**
- * Enable DMA controller clock
- */
- void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /*Configure GPIO pin : BOOT_Pin */
- GPIO_InitStruct.Pin = BOOT_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(BOOT_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pins : W1_Pin W2_Pin */
- GPIO_InitStruct.Pin = W1_Pin|W2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : W3_Pin W4_Pin */
- GPIO_InitStruct.Pin = W3_Pin|W4_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : LED_Pin */
- GPIO_InitStruct.Pin = LED_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pins : C1_Pin C2_Pin */
- GPIO_InitStruct.Pin = C1_Pin|C2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, W3_Pin|W4_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, C1_Pin|C2_Pin, GPIO_PIN_RESET);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement