main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2017 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 "main.h"
  35. #include "stm32f0xx_hal.h"
  36. #include "i2c.h"
  37. #include "tim.h"
  38. #include "gpio.h"
  39. /* USER CODE BEGIN Includes */
  40. #include "rtc.h"
  41. #include "sortie.h"
  42. /* USER CODE END Includes */
  43. /* Private variables ---------------------------------------------------------*/
  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/
  46. /* USER CODE END PV */
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. void Error_Handler(void);
  50. /* USER CODE BEGIN PFP */
  51. /* Private function prototypes -----------------------------------------------*/
  52. /* USER CODE END PFP */
  53. /* USER CODE BEGIN 0 */
  54. /* USER CODE END 0 */
  55. int main(void) {
  56. /* USER CODE BEGIN 1 */
  57. /* USER CODE END 1 */
  58. /* MCU Configuration----------------------------------------------------------*/
  59. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  60. HAL_Init();
  61. /* Configure the system clock */
  62. SystemClock_Config();
  63. /* Initialize all configured peripherals */
  64. MX_GPIO_Init();
  65. MX_I2C1_Init();
  66. MX_TIM6_Init();
  67. MX_TIM14_Init();
  68. MX_TIM16_Init();
  69. /* USER CODE BEGIN 2 */
  70. int heure = lireHeure(); //on initialise l'heure grace a la fonction de lecture de l\'heure sur la RTC.
  71. char nouvelleHeure = 1; //Variable qui sert à savoir si on est passe à une nouvelle heure. Initialise à 1 car au demarage du programme rien est initialise.
  72. char change = 0; //Variable qui sert à savoir si une des sortie est dans un état non stable.
  73. char tableSortie[10]; //Variable qui sert à savoir quelle sont les états des sortie.
  74. char t3min = 0; //Variable qui sert à savoir si on a passé 3min depuis la derniere fois que l'on a augménté le timer 3
  75. char Xms = 0; //Variable qui sert au temps du timer 3
  76. /* USER CODE END 2 */
  77. /* Infinite loop */
  78. /* USER CODE BEGIN WHILE */
  79. demarreTimer1();
  80. while (1) {
  81. /* USER CODE END WHILE */
  82. /* USER CODE BEGIN 3 */
  83. if (nouvelleHeure) {
  84. nouvelleHeure = 0;
  85. Xms = 0;
  86. lireSortie(heure, tableSortie);
  87. change = getChange(tableSortie);
  88. if (change)
  89. demarreTimer2();
  90. else
  91. eteindreTimer2();
  92. }
  93. if (change) {
  94. if (t3min)
  95. Xms = Xms + 1;
  96. }
  97. HAL_Delay(65); // On dort 65 ms pour pas toujour etre actif
  98. }
  99. /* USER CODE END 3 */
  100. }
  101. /** System Clock Configuration
  102. */
  103. void SystemClock_Config(void) {
  104. RCC_OscInitTypeDef RCC_OscInitStruct;
  105. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  106. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  107. /**Initializes the CPU, AHB and APB busses clocks
  108. */
  109. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  110. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  111. RCC_OscInitStruct.HSICalibrationValue = 16;
  112. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  113. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  114. Error_Handler();
  115. }
  116. /**Initializes the CPU, AHB and APB busses clocks
  117. */
  118. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  119. | RCC_CLOCKTYPE_PCLK1;
  120. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  121. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  122. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  123. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
  124. Error_Handler();
  125. }
  126. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  127. PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  128. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
  129. Error_Handler();
  130. }
  131. /**Configure the Systick interrupt time
  132. */
  133. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  134. /**Configure the Systick
  135. */
  136. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  137. /* SysTick_IRQn interrupt configuration */
  138. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  139. }
  140. /* USER CODE BEGIN 4 */
  141. /* USER CODE END 4 */
  142. /**
  143. * @brief This function is executed in case of error occurrence.
  144. * @param None
  145. * @retval None
  146. */
  147. void Error_Handler(void) {
  148. /* USER CODE BEGIN Error_Handler */
  149. /* User can add his own implementation to report the HAL error return state */
  150. while (1) {
  151. }
  152. /* USER CODE END Error_Handler */
  153. }
  154. #ifdef USE_FULL_ASSERT
  155. /**
  156. * @brief Reports the name of the source file and the source line number
  157. * where the assert_param error has occurred.
  158. * @param file: pointer to the source file name
  159. * @param line: assert_param error line source number
  160. * @retval None
  161. */
  162. void assert_failed(uint8_t* file, uint32_t line)
  163. {
  164. /* USER CODE BEGIN 6 */
  165. /* User can add his own implementation to report the file name and line number,
  166. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  167. /* USER CODE END 6 */
  168. }
  169. #endif
  170. /**
  171. * @}
  172. */
  173. /**
  174. * @}
  175. */
  176. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/