Advertisement
nontawat1996

Interface-Lab08-2

Mar 22nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.15 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 "stm32f1xx_hal.h"
  35.  
  36. /* USER CODE BEGIN Includes */
  37. #include <string.h>
  38. #include "et_stm32f_arm_kit_lcd.h"
  39. /* USER CODE END Includes */
  40.  
  41. /* Private variables ---------------------------------------------------------*/
  42. SPI_HandleTypeDef hspi3;
  43.  
  44. TIM_HandleTypeDef htim1;
  45.  
  46. /* USER CODE BEGIN PV */
  47. /* Private variables ---------------------------------------------------------*/
  48. int ss=0,mm=0;
  49. int ms=0;
  50. /* USER CODE END PV */
  51.  
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. static void MX_GPIO_Init(void);
  55. static void MX_SPI3_Init(void);
  56. static void MX_TIM1_Init(void);
  57.  
  58. /* USER CODE BEGIN PFP */
  59. /* Private function prototypes -----------------------------------------------*/
  60.  
  61. /* USER CODE END PFP */
  62.  
  63. /* USER CODE BEGIN 0 */
  64.  
  65. /* USER CODE END 0 */
  66.  
  67. int main(void)
  68. {
  69.  
  70.   /* USER CODE BEGIN 1 */
  71.  
  72.   /* USER CODE END 1 */
  73.  
  74.   /* MCU Configuration----------------------------------------------------------*/
  75.  
  76.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  77.   HAL_Init();
  78.  
  79.   /* Configure the system clock */
  80.   SystemClock_Config();
  81.  
  82.   /* Initialize all configured peripherals */
  83.   MX_GPIO_Init();
  84.   MX_SPI3_Init();
  85.   MX_TIM1_Init();
  86.  
  87.   /* USER CODE BEGIN 2 */
  88.  
  89.   /* USER CODE END 2 */
  90.  
  91.   /* Infinite loop */
  92.   /* USER CODE BEGIN WHILE */
  93.   LCD_Setup();
  94.   /*
  95.   LCD_Clear(Red);
  96.   LCD_SetBackColor(Blue);
  97.   LCD_SetTextColor(Red);
  98.   LCD_DisplayStringLine(Line0, "Nontawat Numor");
  99.   LCD_DisplayStringLine(Line1, "57010669");
  100.   LCD_DisplayStringLine(Line2, "Thitirat Choksawat");
  101.   LCD_DisplayStringLine(Line3, "57010354xxxxxxxxxxx0");
  102.  
  103.   LCD_SetBackColor(Red);
  104.   LCD_SetTextColor(Yellow);
  105.   LCD_DisplayStringLine(Line5, "5xxxxxxxxxxxxxxxxxx0");
  106.   LCD_DisplayStringLine(Line6, "6xxxxxxxxxxxxxxxxxx0");
  107.   LCD_DisplayStringLine(Line7, "7xxxxxxxxxxxxxxxxxx0");
  108.   LCD_DisplayStringLine(Line8, "8xxxxxxxxxxxxxxxxxx0");
  109.   LCD_DisplayStringLine(Line9, "9xxxxxxxxxxxxxxxxxx0");
  110.   */
  111.   LCD_Clear(Red);
  112.  
  113.   LCD_SetBackColor(Blue);
  114.   LCD_SetTextColor(Red);
  115.   LCD_DisplayStringLine(Line0, "Nontawat Numor");
  116.   LCD_DisplayStringLine(Line1, "57010669");
  117.   LCD_DisplayStringLine(Line2, "Thithirat Choksawat");
  118.   LCD_DisplayStringLine(Line3, "57010354");
  119.  
  120.   LCD_SetBackColor(Yellow);
  121.   LCD_SetTextColor(Blue);
  122.  
  123.   HAL_TIM_Base_Start_IT (&htim1);
  124.   while (1)
  125.   {
  126.   /* USER CODE END WHILE */
  127.   /* USER CODE BEGIN 3 */
  128.   char Strmm[5],Strss[5];
  129.   sprintf(Strmm,"%d",mm);
  130.   sprintf(Strss,"%d",ss);
  131.   char output[20]="\0";
  132.   strcat(output,"       ");
  133.   strcat(output,Strmm);
  134.   strcat(output,":");
  135.   strcat(output,Strss);
  136.   LCD_DisplayStringLine(Line6, "");
  137.   LCD_DisplayStringLine(Line6, output);
  138.   }
  139.   /* USER CODE END 3 */
  140.  
  141. }
  142.  
  143. /** System Clock Configuration
  144. */
  145. void SystemClock_Config(void)
  146. {
  147.  
  148.   RCC_OscInitTypeDef RCC_OscInitStruct;
  149.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  150.  
  151.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  152.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  153.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;
  154.   RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
  155.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  156.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  157.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  158.   RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
  159.   RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
  160.   RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
  161.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  162.  
  163.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  164.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  165.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  166.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  167.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  168.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  169.  
  170.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  171.  
  172.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  173.  
  174.   __HAL_RCC_PLLI2S_ENABLE();
  175.  
  176.   /* SysTick_IRQn interrupt configuration */
  177.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  178. }
  179.  
  180. /* SPI3 init function */
  181. void MX_SPI3_Init(void)
  182. {
  183.  
  184.   hspi3.Instance = SPI3;
  185.   hspi3.Init.Mode = SPI_MODE_MASTER;
  186.   hspi3.Init.Direction = SPI_DIRECTION_2LINES;
  187.   hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
  188.   hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
  189.   hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
  190.   hspi3.Init.NSS = SPI_NSS_SOFT;
  191.   hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  192.   hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  193.   hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
  194.   hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  195.   hspi3.Init.CRCPolynomial = 10;
  196.   HAL_SPI_Init(&hspi3);
  197.  
  198. }
  199.  
  200. /* TIM1 init function */
  201. void MX_TIM1_Init(void)
  202. {
  203.  
  204.   TIM_ClockConfigTypeDef sClockSourceConfig;
  205.   TIM_MasterConfigTypeDef sMasterConfig;
  206.  
  207.   htim1.Instance = TIM1;
  208.   htim1.Init.Prescaler = 72-1;
  209.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  210.   htim1.Init.Period = 1000-1;
  211.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  212.   htim1.Init.RepetitionCounter = 0;
  213.   HAL_TIM_Base_Init(&htim1);
  214.  
  215.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  216.   HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
  217.  
  218.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  219.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  220.   HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
  221.  
  222. }
  223.  
  224. /** Configure pins as
  225.         * Analog
  226.         * Input
  227.         * Output
  228.         * EVENT_OUT
  229.         * EXTI
  230. */
  231. void MX_GPIO_Init(void)
  232. {
  233.  
  234.   GPIO_InitTypeDef GPIO_InitStruct;
  235.  
  236.   /* GPIO Ports Clock Enable */
  237.   __HAL_RCC_GPIOE_CLK_ENABLE();
  238.   __HAL_RCC_GPIOC_CLK_ENABLE();
  239.   __HAL_RCC_GPIOD_CLK_ENABLE();
  240.  
  241.   /*Configure GPIO pins : PE3 PE4 */
  242.   GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
  243.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  244.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  245.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  246.  
  247.   /*Configure GPIO pins : PE5 PE6 PE7 */
  248.   GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  249.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  250.   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  251.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  252.  
  253.   /*Configure GPIO pin : PC8 */
  254.   GPIO_InitStruct.Pin = GPIO_PIN_8;
  255.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  256.   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  257.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  258.  
  259.   /*Configure GPIO pin : PD7 */
  260.   GPIO_InitStruct.Pin = GPIO_PIN_7;
  261.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  262.   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  263.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  264.  
  265. }
  266.  
  267. /* USER CODE BEGIN 4 */
  268.  
  269. /* USER CODE END 4 */
  270.  
  271. #ifdef USE_FULL_ASSERT
  272.  
  273. /**
  274.    * @brief Reports the name of the source file and the source line number
  275.    * where the assert_param error has occurred.
  276.    * @param file: pointer to the source file name
  277.    * @param line: assert_param error line source number
  278.    * @retval None
  279.    */
  280. void assert_failed(uint8_t* file, uint32_t line)
  281. {
  282.   /* USER CODE BEGIN 6 */
  283.   /* User can add his own implementation to report the file name and line number,
  284.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  285.   /* USER CODE END 6 */
  286.  
  287. }
  288.  
  289. #endif
  290.  
  291. /**
  292.   * @}
  293.   */
  294.  
  295. /**
  296.   * @}
  297. */
  298.  
  299. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  300.  
  301. ---------------------------------------------------------------------------------------------------------------------------
  302. /**
  303.   ******************************************************************************
  304.   * @file    stm32f1xx_it.c
  305.   * @brief   Interrupt Service Routines.
  306.   ******************************************************************************
  307.   *
  308.   * COPYRIGHT(c) 2016 STMicroelectronics
  309.   *
  310.   * Redistribution and use in source and binary forms, with or without modification,
  311.   * are permitted provided that the following conditions are met:
  312.   *   1. Redistributions of source code must retain the above copyright notice,
  313.   *      this list of conditions and the following disclaimer.
  314.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  315.   *      this list of conditions and the following disclaimer in the documentation
  316.   *      and/or other materials provided with the distribution.
  317.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  318.   *      may be used to endorse or promote products derived from this software
  319.   *      without specific prior written permission.
  320.   *
  321.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  322.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  323.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  324.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  325.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  326.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  327.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  328.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  329.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  330.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  331.   *
  332.   ******************************************************************************
  333.   */
  334. /* Includes ------------------------------------------------------------------*/
  335. #include "stm32f1xx_hal.h"
  336. #include "stm32f1xx.h"
  337. #include "stm32f1xx_it.h"
  338.  
  339. /* USER CODE BEGIN 0 */
  340. extern int ss,mm;
  341. extern int ms;
  342. /* USER CODE END 0 */
  343.  
  344. /* External variables --------------------------------------------------------*/
  345. extern TIM_HandleTypeDef htim1;
  346.  
  347. /******************************************************************************/
  348. /*            Cortex-M3 Processor Interruption and Exception Handlers         */
  349. /******************************************************************************/
  350.  
  351. /**
  352. * @brief This function handles System tick timer.
  353. */
  354. void SysTick_Handler(void)
  355. {
  356.   /* USER CODE BEGIN SysTick_IRQn 0 */
  357.  
  358.   /* USER CODE END SysTick_IRQn 0 */
  359.   HAL_IncTick();
  360.   HAL_SYSTICK_IRQHandler();
  361.   /* USER CODE BEGIN SysTick_IRQn 1 */
  362.  
  363.   /* USER CODE END SysTick_IRQn 1 */
  364. }
  365.  
  366. /******************************************************************************/
  367. /* STM32F1xx Peripheral Interrupt Handlers                                    */
  368. /* Add here the Interrupt Handlers for the used peripherals.                  */
  369. /* For the available peripheral interrupt handler names,                      */
  370. /* please refer to the startup file (startup_stm32f1xx.s).                    */
  371. /******************************************************************************/
  372.  
  373. /**
  374. * @brief This function handles TIM1 update interrupt.
  375. */
  376. void TIM1_UP_IRQHandler(void)
  377. {
  378.   /* USER CODE BEGIN TIM1_UP_IRQn 0 */
  379.   ms++;
  380.   /* USER CODE END TIM1_UP_IRQn 0 */
  381.   HAL_TIM_IRQHandler(&htim1);
  382.   /* USER CODE BEGIN TIM1_UP_IRQn 1 */
  383.   if(ms == 1000) {
  384.     ss++;
  385.     ms = 0;
  386.   }
  387.   if(ss == 60) {
  388.     mm++;
  389.     ss = 0;
  390.   }
  391.  
  392.   /* USER CODE END TIM1_UP_IRQn 1 */
  393. }
  394.  
  395. /* USER CODE BEGIN 1 */
  396.  
  397. /* USER CODE END 1 */
  398. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement