Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.62 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2016 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f0xx_hal.h"
  35. #include "usb_device.h"
  36.  
  37. /* USER CODE BEGIN Includes */
  38.  
  39. /* USER CODE END Includes */
  40.  
  41. /* USER CODE BEGIN Defines */
  42. #define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
  43. #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
  44. #define VDD_CALIB ((uint16_t) (330))
  45. #define VDD_APPLI ((uint16_t) (300))
  46. /* USER CODE END Defines */
  47.  
  48. /* Private variables ---------------------------------------------------------*/
  49. ADC_HandleTypeDef hadc;
  50. ADC_HandleTypeDef hadc_temp;
  51. DMA_HandleTypeDef hdma_adc;
  52.  
  53. /* USER CODE BEGIN PV */
  54. /* Private variables ---------------------------------------------------------*/
  55.  
  56.  
  57. /* USER CODE END PV */
  58.  
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. static void MX_GPIO_Init(void);
  62. static void MX_DMA_Init(void);
  63. static void MX_ADC_Init(void);
  64. static int32_t getTemp(void);
  65.  
  66. /* USER CODE BEGIN PFP */
  67. /* Private function prototypes -----------------------------------------------*/
  68.  
  69. /* USER CODE END PFP */
  70.  
  71. /* USER CODE BEGIN 0 */
  72.  
  73. /* USER CODE END 0 */
  74.  
  75. int main(void)
  76. {
  77.  
  78.   /* USER CODE BEGIN 1 */
  79.  
  80.  
  81.   /* USER CODE END 1 */
  82.  
  83.   /* MCU Configuration----------------------------------------------------------*/
  84.  
  85.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  86.   HAL_Init();
  87.  
  88.   /* Configure the system clock */
  89.   SystemClock_Config();
  90.  
  91.   /* Initialize all configured peripherals */
  92.   MX_GPIO_Init();
  93.   MX_DMA_Init();
  94.   MX_ADC_Init();
  95.   MX_USB_DEVICE_Init();
  96.  
  97.   /* USER CODE BEGIN 2 */
  98.  
  99.   HAL_ADC_Start(&hadc);
  100.   HAL_ADC_Start(&hadc_temp);
  101.  
  102.   /* USER CODE END 2 */
  103.  
  104.   /* Infinite loop */
  105.   /* USER CODE BEGIN WHILE */
  106.   while (1)
  107.   {
  108.   /* USER CODE END WHILE */
  109.       volatile uint16_t rawValue;
  110.       volatile uint32_t adc_val;
  111.       volatile float temp;
  112.       HAL_GPIO_TogglePin(GPIOB, LED_Pin);
  113.       HAL_Delay(100);
  114.   /* USER CODE BEGIN 3 */
  115.  
  116.  
  117.  
  118.       HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
  119.       adc_val = HAL_ADC_GetValue(&hadc);
  120.       temp = getTemp();
  121.   }
  122.   /* USER CODE END 3 */
  123.  
  124. }
  125.  
  126. /** Getting system temperature
  127.  */
  128. static int32_t getTemp(void) {
  129.     volatile int32_t temperature; /* will contain the temperature in degree Celsius */
  130.     volatile int32_t cal30, cal110;
  131.     HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);
  132.  
  133.     temperature = HAL_ADC_GetValue(&hadc_temp);
  134.  
  135.     cal30 = (int32_t)*TEMP30_CAL_ADDR;
  136.     cal110 = (int32_t)*TEMP110_CAL_ADDR;
  137.     //temperature = ((temperature * VDD_APPLI / VDD_CALIB) - (int32_t) *TEMP30_CAL_ADDR );
  138.     //temperature = temperature * (int32_t)(110 - 30);
  139.     //temperature = temperature / (int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);
  140.     //return temperature + 30
  141.  
  142.     temperature = (80/(int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR))*(temperature-(int32_t)*TEMP30_CAL_ADDR) + 30;
  143.     return  temperature;
  144. }
  145.  
  146. /** System Clock Configuration
  147. */
  148. void SystemClock_Config(void)
  149. {
  150.  
  151.   RCC_OscInitTypeDef RCC_OscInitStruct;
  152.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  153.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  154.  
  155.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSI48;
  156.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  157.   RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  158.   RCC_OscInitStruct.HSI14CalibrationValue = 16;
  159.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  160.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  161.  
  162.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  163.                               |RCC_CLOCKTYPE_PCLK1;
  164.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  165.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  166.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  167.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  168.  
  169.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  170.   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  171.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  172.  
  173.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  174.  
  175.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  176.  
  177.   /* SysTick_IRQn interrupt configuration */
  178.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  179. }
  180.  
  181. /* ADC init function */
  182. void MX_ADC_Init(void)
  183. {
  184.  
  185.   ADC_ChannelConfTypeDef sConfig;
  186.  
  187.     /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  188.     */
  189.   hadc.Instance = ADC1;
  190.   hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  191.   hadc.Init.Resolution = ADC_RESOLUTION_12B;
  192.   hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  193.   hadc.Init.ScanConvMode = DISABLE; //ADC_SCAN_DIRECTION_FORWARD;
  194.   hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  195.   hadc.Init.LowPowerAutoWait = DISABLE;
  196.   hadc.Init.LowPowerAutoPowerOff = DISABLE;
  197.   hadc.Init.ContinuousConvMode = ENABLE;
  198.   hadc.Init.DiscontinuousConvMode = DISABLE;
  199.   hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  200.   hadc.Init.DMAContinuousRequests = DISABLE;
  201.   hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  202.   HAL_ADC_Init(&hadc);
  203.  
  204.     /**Configure for the selected ADC regular channel to be converted.
  205.     */
  206.   sConfig.Channel = ADC_CHANNEL_3;
  207.   sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  208.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  209.   HAL_ADC_ConfigChannel(&hadc, &sConfig);
  210.  
  211.   /* Enable ADC peripheral */
  212.   __HAL_RCC_ADC1_CLK_ENABLE();
  213.  
  214.   /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number
  215.   of conversion) */
  216.   hadc_temp.Instance = ADC1;
  217.   hadc_temp.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  218.   hadc_temp.Init.Resolution = ADC_RESOLUTION_12B;
  219.   hadc_temp.Init.ScanConvMode = DISABLE;
  220.   hadc_temp.Init.ContinuousConvMode = ENABLE;
  221.   hadc_temp.Init.DiscontinuousConvMode = DISABLE;
  222.   hadc_temp.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  223.   hadc_temp.Init.DMAContinuousRequests = DISABLE;
  224.   hadc_temp.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  225.   HAL_ADC_Init(&hadc_temp);
  226.  
  227.   /* Configure for the selected ADC regular channel its corresponding rank in the sequence\
  228.   r
  229.   and its sample time. */
  230.   sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  231.   sConfig.Rank = 1;
  232.   sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  233.   HAL_ADC_ConfigChannel(&hadc_temp, &sConfig);
  234.  
  235. }
  236.  
  237. /**
  238.   * Enable DMA controller clock
  239.   */
  240. void MX_DMA_Init(void)
  241. {
  242.   /* DMA controller clock enable */
  243.   __HAL_RCC_DMA1_CLK_ENABLE();
  244.  
  245.   /* DMA interrupt init */
  246.   /* DMA1_Channel1_IRQn interrupt configuration */
  247.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  248.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  249.  
  250. }
  251.  
  252. /** Configure pins as
  253.         * Analog
  254.         * Input
  255.         * Output
  256.         * EVENT_OUT
  257.         * EXTI
  258. */
  259. void MX_GPIO_Init(void)
  260. {
  261.  
  262.   GPIO_InitTypeDef GPIO_InitStruct;
  263.  
  264.   /* GPIO Ports Clock Enable */
  265.   __HAL_RCC_GPIOF_CLK_ENABLE();
  266.   __HAL_RCC_GPIOA_CLK_ENABLE();
  267.   __HAL_RCC_GPIOB_CLK_ENABLE();
  268.  
  269.   /*Configure GPIO pins : W1_Pin W2_Pin */
  270.   GPIO_InitStruct.Pin = W1_Pin|W2_Pin;
  271.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  272.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  273.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  274.  
  275.   /*Configure GPIO pins : W3_Pin W4_Pin */
  276.   GPIO_InitStruct.Pin = W3_Pin|W4_Pin;
  277.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  278.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  279.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  280.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  281.  
  282.   /*Configure GPIO pins : LED_Pin C1_Pin C2_Pin */
  283.   GPIO_InitStruct.Pin = C1_Pin|C2_Pin;
  284.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  285.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  286.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  287.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  288.  
  289.   /*Configure GPIO pins : LED_Pin C1_Pin C2_Pin */
  290.    GPIO_InitStruct.Pin = LED_Pin;
  291.    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  292.    GPIO_InitStruct.Pull = GPIO_PULLUP;
  293.    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  294.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  295.  
  296.  
  297.   /*Configure GPIO pin Output Level */
  298.   HAL_GPIO_WritePin(GPIOA, W3_Pin|W4_Pin, GPIO_PIN_RESET);
  299.  
  300.   /*Configure GPIO pin Output Level */
  301.   HAL_GPIO_WritePin(GPIOB, LED_Pin|C1_Pin|C2_Pin, GPIO_PIN_RESET);
  302.  
  303. }
  304.  
  305. /* USER CODE BEGIN 4 */
  306.  
  307. /* USER CODE END 4 */
  308.  
  309. #ifdef USE_FULL_ASSERT
  310.  
  311. /**
  312.    * @brief Reports the name of the source file and the source line number
  313.    * where the assert_param error has occurred.
  314.    * @param file: pointer to the source file name
  315.    * @param line: assert_param error line source number
  316.    * @retval None
  317.    */
  318. void assert_failed(uint8_t* file, uint32_t line)
  319. {
  320.   /* USER CODE BEGIN 6 */
  321.   /* User can add his own implementation to report the file name and line number,
  322.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  323.   /* USER CODE END 6 */
  324.  
  325. }
  326.  
  327. #endif
  328.  
  329. /**
  330.   * @}
  331.   */
  332.  
  333. /**
  334.   * @}
  335. */
  336.  
  337. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement