Advertisement
eikhner

stm32 quartz

Jan 12th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.14 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2025 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "../../ECUAL/I2C_LCD/I2C_LCD.h"
  25. /* USER CODE END Includes */
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. #define MyI2C_LCD I2C_LCD_1
  30. /* USER CODE END PTD */
  31.  
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34.  
  35. /* USER CODE END PD */
  36.  
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39.  
  40. /* USER CODE END PM */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43. I2C_HandleTypeDef hi2c1;
  44.  
  45. TIM_HandleTypeDef htim2;
  46.  
  47. /* USER CODE BEGIN PV */
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_I2C1_Init(void);
  55. static void MX_TIM2_Init(void);
  56. /* USER CODE BEGIN PFP */
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62.  
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.  
  72.   /* USER CODE BEGIN 1 */
  73.  
  74.   /* USER CODE END 1 */
  75.  
  76.   /* MCU Configuration--------------------------------------------------------*/
  77.  
  78.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79.   HAL_Init();
  80.  
  81.   /* USER CODE BEGIN Init */
  82.  
  83.   /* USER CODE END Init */
  84.  
  85.   /* Configure the system clock */
  86.   SystemClock_Config();
  87.  
  88.   /* USER CODE BEGIN SysInit */
  89.  
  90.   /* USER CODE END SysInit */
  91.  
  92.   /* Initialize all configured peripherals */
  93.   MX_GPIO_Init();
  94.   MX_I2C1_Init();
  95.   MX_TIM2_Init();
  96.   /* USER CODE BEGIN 2 */
  97.  
  98.   HAL_TIM_Base_Start_IT(&htim2);
  99.  
  100.   I2C_LCD_Init(MyI2C_LCD);
  101.   I2C_LCD_SetCursor(MyI2C_LCD, 0, 0);
  102.   I2C_LCD_WriteString(MyI2C_LCD, "Hello I2C LCD2");
  103.   I2C_LCD_SetCursor(MyI2C_LCD, 0, 1);
  104.   I2C_LCD_WriteString(MyI2C_LCD, "LCD2 Line 2 Test");
  105.   I2C_LCD_SetCursor(MyI2C_LCD, 0, 2);
  106.   I2C_LCD_WriteString(MyI2C_LCD, "LCD2 Line 3 Test");
  107.   I2C_LCD_SetCursor(MyI2C_LCD, 0, 3);
  108.   I2C_LCD_WriteString(MyI2C_LCD, "LCD2 Line 4 Test");
  109.   /* USER CODE END 2 */
  110.  
  111.   /* Infinite loop */
  112.   /* USER CODE BEGIN WHILE */
  113.   while (1)
  114.   {
  115.     /* USER CODE END WHILE */
  116.  
  117.     /* USER CODE BEGIN 3 */
  118.   }
  119.   /* USER CODE END 3 */
  120. }
  121.  
  122. /**
  123.   * @brief System Clock Configuration
  124.   * @retval None
  125.   */
  126. void SystemClock_Config(void)
  127. {
  128.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  129.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  130.  
  131.   /** Initializes the RCC Oscillators according to the specified parameters
  132.   * in the RCC_OscInitTypeDef structure.
  133.   */
  134.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  135.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  136.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  137.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  138.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  139.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  140.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  141.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142.   {
  143.     Error_Handler();
  144.   }
  145.  
  146.   /** Initializes the CPU, AHB and APB buses clocks
  147.   */
  148.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  149.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  150.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  151.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  152.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  153.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  154.  
  155.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  156.   {
  157.     Error_Handler();
  158.   }
  159.  
  160.   /** Enables the Clock Security System
  161.   */
  162.   HAL_RCC_EnableCSS();
  163. }
  164.  
  165. /**
  166.   * @brief I2C1 Initialization Function
  167.   * @param None
  168.   * @retval None
  169.   */
  170. static void MX_I2C1_Init(void)
  171. {
  172.  
  173.   /* USER CODE BEGIN I2C1_Init 0 */
  174.  
  175.   /* USER CODE END I2C1_Init 0 */
  176.  
  177.   /* USER CODE BEGIN I2C1_Init 1 */
  178.  
  179.   /* USER CODE END I2C1_Init 1 */
  180.   hi2c1.Instance = I2C1;
  181.   hi2c1.Init.ClockSpeed = 100000;
  182.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  183.   hi2c1.Init.OwnAddress1 = 0;
  184.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  185.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  186.   hi2c1.Init.OwnAddress2 = 0;
  187.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  188.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  189.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  190.   {
  191.     Error_Handler();
  192.   }
  193.   /* USER CODE BEGIN I2C1_Init 2 */
  194.  
  195.   /* USER CODE END I2C1_Init 2 */
  196.  
  197. }
  198.  
  199. /**
  200.   * @brief TIM2 Initialization Function
  201.   * @param None
  202.   * @retval None
  203.   */
  204. static void MX_TIM2_Init(void)
  205. {
  206.  
  207.   /* USER CODE BEGIN TIM2_Init 0 */
  208.  
  209.   /* USER CODE END TIM2_Init 0 */
  210.  
  211.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  212.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  213.  
  214.   /* USER CODE BEGIN TIM2_Init 1 */
  215.  
  216.   /* USER CODE END TIM2_Init 1 */
  217.   htim2.Instance = TIM2;
  218.   htim2.Init.Prescaler = 10000-1;
  219.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  220.   htim2.Init.Period = 7200-1;
  221.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  222.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  223.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  224.   {
  225.     Error_Handler();
  226.   }
  227.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  228.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  229.   {
  230.     Error_Handler();
  231.   }
  232.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  233.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  234.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  235.   {
  236.     Error_Handler();
  237.   }
  238.   /* USER CODE BEGIN TIM2_Init 2 */
  239.  
  240.   /* USER CODE END TIM2_Init 2 */
  241.  
  242. }
  243.  
  244. /**
  245.   * @brief GPIO Initialization Function
  246.   * @param None
  247.   * @retval None
  248.   */
  249. static void MX_GPIO_Init(void)
  250. {
  251.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  252. /* USER CODE BEGIN MX_GPIO_Init_1 */
  253. /* USER CODE END MX_GPIO_Init_1 */
  254.  
  255.   /* GPIO Ports Clock Enable */
  256.   __HAL_RCC_GPIOC_CLK_ENABLE();
  257.   __HAL_RCC_GPIOD_CLK_ENABLE();
  258.   __HAL_RCC_GPIOB_CLK_ENABLE();
  259.  
  260.   /*Configure GPIO pin Output Level */
  261.   HAL_GPIO_WritePin(LED13_GPIO_Port, LED13_Pin, GPIO_PIN_RESET);
  262.  
  263.   /*Configure GPIO pin Output Level */
  264.   HAL_GPIO_WritePin(Crystal_Error_GPIO_Port, Crystal_Error_Pin, GPIO_PIN_RESET);
  265.  
  266.  
  267.   /*Configure GPIO pin : LED13_Pin */
  268.   GPIO_InitStruct.Pin = LED13_Pin;
  269.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  270.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  271.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  272.   HAL_GPIO_Init(LED13_GPIO_Port, &GPIO_InitStruct);
  273.  
  274.   /*Configure GPIO pin : Crystal_Error_Pin */
  275.   GPIO_InitStruct.Pin = Crystal_Error_Pin;
  276.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  277.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  278.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  279.   HAL_GPIO_Init(Crystal_Error_GPIO_Port, &GPIO_InitStruct);
  280.  
  281. /* USER CODE BEGIN MX_GPIO_Init_2 */
  282. /* USER CODE END MX_GPIO_Init_2 */
  283. }
  284.  
  285. /* USER CODE BEGIN 4 */
  286. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
  287. {
  288.      if(htim->Instance == TIM2) //check if the interrupt comes from TIM2
  289.      {
  290.          HAL_GPIO_TogglePin(LED13_GPIO_Port, LED13_Pin);
  291.      }
  292.  
  293. }
  294.  
  295. void HAL_RCC_CSSCallback(void) {
  296.  
  297. // Ловит сбой HSE-генератора и принимает надлежащие меры
  298.     HAL_GPIO_WritePin(Crystal_Error_GPIO_Port, Crystal_Error_Pin, GPIO_PIN_SET);
  299. }
  300. /* USER CODE END 4 */
  301.  
  302. /**
  303.   * @brief  This function is executed in case of error occurrence.
  304.   * @retval None
  305.   */
  306. void Error_Handler(void)
  307. {
  308.   /* USER CODE BEGIN Error_Handler_Debug */
  309.   /* User can add his own implementation to report the HAL error return state */
  310.   __disable_irq();
  311.   while (1)
  312.   {
  313.   }
  314.   /* USER CODE END Error_Handler_Debug */
  315. }
  316.  
  317. #ifdef  USE_FULL_ASSERT
  318. /**
  319.   * @brief  Reports the name of the source file and the source line number
  320.   *         where the assert_param error has occurred.
  321.   * @param  file: pointer to the source file name
  322.   * @param  line: assert_param error line source number
  323.   * @retval None
  324.   */
  325. void assert_failed(uint8_t *file, uint32_t line)
  326. {
  327.   /* USER CODE BEGIN 6 */
  328.   /* User can add his own implementation to report the file name and line number,
  329.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  330.   /* USER CODE END 6 */
  331. }
  332. #endif /* USE_FULL_ASSERT */
  333.  
Tags: STM32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement