Advertisement
nontawat1996

Lab6-1

Mar 2nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.10 KB | None | 0 0
  1. /*
  2. void TIM1_UP_IRQHandler(void)
  3. {
  4.   /* USER CODE BEGIN TIM1_UP_IRQn 0 */
  5.  
  6.   /* USER CODE END TIM1_UP_IRQn 0 */
  7.   HAL_TIM_IRQHandler(&htim1);
  8.   /* USER CODE BEGIN TIM1_UP_IRQn 1 */
  9.   count++;
  10.   /* USER CODE END TIM1_UP_IRQn 1 */
  11. }
  12. */
  13.  
  14. /**
  15.   ******************************************************************************
  16.   * File Name          : main.c
  17.   * Description        : Main program body
  18.   ******************************************************************************
  19.   *
  20.   * COPYRIGHT(c) 2016 STMicroelectronics
  21.   *
  22.   * Redistribution and use in source and binary forms, with or without modification,
  23.   * are permitted provided that the following conditions are met:
  24.   *   1. Redistributions of source code must retain the above copyright notice,
  25.   *      this list of conditions and the following disclaimer.
  26.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  27.   *      this list of conditions and the following disclaimer in the documentation
  28.   *      and/or other materials provided with the distribution.
  29.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  30.   *      may be used to endorse or promote products derived from this software
  31.   *      without specific prior written permission.
  32.   *
  33.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  34.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  36.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  37.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  38.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  41.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  42.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43.   *
  44.   ******************************************************************************
  45.   */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32f1xx_hal.h"
  48.  
  49. /* USER CODE BEGIN Includes */
  50. #include "string.h"
  51. /* USER CODE END Includes */
  52.  
  53. /* Private variables ---------------------------------------------------------*/
  54. TIM_HandleTypeDef htim1;
  55. TIM_HandleTypeDef htim2;
  56.  
  57. UART_HandleTypeDef huart2;
  58.  
  59. /* USER CODE BEGIN PV */
  60. /* Private variables ---------------------------------------------------------*/
  61. uint32_t count;
  62. /* USER CODE END PV */
  63.  
  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. static void MX_GPIO_Init(void);
  67. static void MX_TIM1_Init(void);
  68. static void MX_TIM2_Init(void);
  69. static void MX_USART2_UART_Init(void);
  70.  
  71. /* USER CODE BEGIN PFP */
  72. /* Private function prototypes -----------------------------------------------*/
  73. void displayNumber(void);
  74. /* USER CODE END PFP */
  75.  
  76. /* USER CODE BEGIN 0 */
  77.  
  78. /* USER CODE END 0 */
  79.  
  80. int main(void)
  81. {
  82.  
  83.   /* USER CODE BEGIN 1 */
  84.  
  85.   /* USER CODE END 1 */
  86.  
  87.   /* MCU Configuration----------------------------------------------------------*/
  88.  
  89.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  90.   HAL_Init();
  91.  
  92.   /* Configure the system clock */
  93.   SystemClock_Config();
  94.  
  95.   /* Initialize all configured peripherals */
  96.   MX_GPIO_Init();
  97.   MX_TIM1_Init();
  98.   MX_TIM2_Init();
  99.   MX_USART2_UART_Init();
  100.  
  101.   /* USER CODE BEGIN 2 */
  102.   HAL_TIM_Base_Start_IT(&htim1);
  103.   /* USER CODE END 2 */
  104.  
  105.   /* Infinite loop */
  106.   /* USER CODE BEGIN WHILE */
  107.   while (1)
  108.   {
  109.   /* USER CODE END WHILE */
  110.     displayNumber();
  111.     HAL_Delay(400);
  112.   /* USER CODE BEGIN 3 */
  113.  
  114.   }
  115.   /* USER CODE END 3 */
  116.  
  117. }
  118.  
  119. /** System Clock Configuration
  120. */
  121. void SystemClock_Config(void)
  122. {
  123.  
  124.   RCC_OscInitTypeDef RCC_OscInitStruct;
  125.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  126.  
  127.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  128.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  129.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;
  130.   RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
  131.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  132.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  133.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  134.   RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
  135.   RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
  136.   RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
  137.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  138.  
  139.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  140.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  141.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  142.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  143.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  144.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  145.  
  146.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  147.  
  148.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  149.  
  150.   __HAL_RCC_PLLI2S_ENABLE();
  151.  
  152.   /* SysTick_IRQn interrupt configuration */
  153.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  154. }
  155.  
  156. /* TIM1 init function */
  157. void MX_TIM1_Init(void)
  158. {
  159.  
  160.   TIM_ClockConfigTypeDef sClockSourceConfig;
  161.   TIM_MasterConfigTypeDef sMasterConfig;
  162.  
  163.   htim1.Instance = TIM1;
  164.   htim1.Init.Prescaler = 72-1;
  165.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  166.   htim1.Init.Period = 1000-1;
  167.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  168.   htim1.Init.RepetitionCounter = 0;
  169.   HAL_TIM_Base_Init(&htim1);
  170.  
  171.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  172.   HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
  173.  
  174.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  175.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  176.   HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
  177.  
  178. }
  179.  
  180. /* TIM2 init function */
  181. void MX_TIM2_Init(void)
  182. {
  183.  
  184.   TIM_ClockConfigTypeDef sClockSourceConfig;
  185.   TIM_MasterConfigTypeDef sMasterConfig;
  186.  
  187.   htim2.Instance = TIM2;
  188.   htim2.Init.Prescaler = 72-1;
  189.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  190.   htim2.Init.Period = 1000-1;
  191.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  192.   HAL_TIM_Base_Init(&htim2);
  193.  
  194.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  195.   HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  196.  
  197.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  198.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  199.   HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  200.  
  201. }
  202.  
  203. /* USART2 init function */
  204. void MX_USART2_UART_Init(void)
  205. {
  206.  
  207.   huart2.Instance = USART2;
  208.   huart2.Init.BaudRate = 115200;
  209.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  210.   huart2.Init.StopBits = UART_STOPBITS_1;
  211.   huart2.Init.Parity = UART_PARITY_NONE;
  212.   huart2.Init.Mode = UART_MODE_TX_RX;
  213.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  214.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  215.   HAL_UART_Init(&huart2);
  216.  
  217. }
  218.  
  219. /** Configure pins as
  220.         * Analog
  221.         * Input
  222.         * Output
  223.         * EVENT_OUT
  224.         * EXTI
  225. */
  226. void MX_GPIO_Init(void)
  227. {
  228.  
  229.   /* GPIO Ports Clock Enable */
  230.   __HAL_RCC_GPIOD_CLK_ENABLE();
  231.  
  232. }
  233.  
  234. /* USER CODE BEGIN 4 */
  235. void displayNumber() {
  236.   char str[20];
  237.   sprintf(str, "%d", count);
  238.   HAL_UART_Transmit(&huart2, (uint8_t*) str, strlen(str),1000);
  239.   HAL_UART_Transmit(&huart2, (uint8_t*) "\r\n", 2,1000);
  240. }
  241. /* USER CODE END 4 */
  242.  
  243. #ifdef USE_FULL_ASSERT
  244.  
  245. /**
  246.    * @brief Reports the name of the source file and the source line number
  247.    * where the assert_param error has occurred.
  248.    * @param file: pointer to the source file name
  249.    * @param line: assert_param error line source number
  250.    * @retval None
  251.    */
  252. void assert_failed(uint8_t* file, uint32_t line)
  253. {
  254.   /* USER CODE BEGIN 6 */
  255.   /* User can add his own implementation to report the file name and line number,
  256.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  257.   /* USER CODE END 6 */
  258.  
  259. }
  260.  
  261. #endif
  262.  
  263. /**
  264.   * @}
  265.   */
  266.  
  267. /**
  268.   * @}
  269. */
  270.  
  271. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement