Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- *
- * COPYRIGHT(c) 2016 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f0xx_hal.h"
- #include "usb_device.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* USER CODE BEGIN Defines */
- #define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
- #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
- #define VDD_CALIB ((uint16_t) (330))
- #define VDD_APPLI ((uint16_t) (300))
- /* USER CODE END Defines */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc;
- ADC_HandleTypeDef hadc_temp;
- DMA_HandleTypeDef hdma_adc;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* 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);
- static int32_t getTemp(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(&hadc);
- HAL_ADC_Start(&hadc_temp);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- volatile uint16_t rawValue;
- volatile uint32_t adc_val;
- volatile float temp;
- HAL_GPIO_TogglePin(GPIOB, LED_Pin);
- HAL_Delay(100);
- /* USER CODE BEGIN 3 */
- HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
- adc_val = HAL_ADC_GetValue(&hadc);
- temp = getTemp();
- }
- /* USER CODE END 3 */
- }
- /** Getting system temperature
- */
- static int32_t getTemp(void) {
- volatile int32_t temperature; /* will contain the temperature in degree Celsius */
- volatile int32_t cal30, cal110;
- HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);
- temperature = HAL_ADC_GetValue(&hadc_temp);
- cal30 = (int32_t)*TEMP30_CAL_ADDR;
- cal110 = (int32_t)*TEMP110_CAL_ADDR;
- //temperature = ((temperature * VDD_APPLI / VDD_CALIB) - (int32_t) *TEMP30_CAL_ADDR );
- //temperature = temperature * (int32_t)(110 - 30);
- //temperature = temperature / (int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);
- //return temperature + 30
- temperature = (80/(int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR))*(temperature-(int32_t)*TEMP30_CAL_ADDR) + 30;
- return temperature;
- }
- /** 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_ASYNC_DIV1;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = DISABLE; //ADC_SCAN_DIRECTION_FORWARD;
- hadc.Init.EOCSelection = ADC_EOC_SINGLE_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);
- /**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_1CYCLE_5;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /* Enable ADC peripheral */
- __HAL_RCC_ADC1_CLK_ENABLE();
- /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number
- of conversion) */
- hadc_temp.Instance = ADC1;
- hadc_temp.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
- hadc_temp.Init.Resolution = ADC_RESOLUTION_12B;
- hadc_temp.Init.ScanConvMode = DISABLE;
- hadc_temp.Init.ContinuousConvMode = ENABLE;
- hadc_temp.Init.DiscontinuousConvMode = DISABLE;
- hadc_temp.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc_temp.Init.DMAContinuousRequests = DISABLE;
- hadc_temp.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- HAL_ADC_Init(&hadc_temp);
- /* Configure for the selected ADC regular channel its corresponding rank in the sequence\
- r
- and its sample time. */
- sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
- HAL_ADC_ConfigChannel(&hadc_temp, &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_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*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 pins : LED_Pin 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 pins : LED_Pin C1_Pin C2_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(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(GPIOB, LED_Pin|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
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement