Advertisement
nontawat1996

Lab6-2

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