main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "i2c.h"
  24. #include "spi.h"
  25. #include "tim.h"
  26. #include "gpio.h"
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. /* USER CODE END PTD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. void testAquit();
  36. void testLed();
  37. void testSortie();
  38. //void testSPI();
  39. //void testVent();
  40. //void testtemp();
  41. //void TestOn();
  42. //void TestOFF();
  43. //void TestPWM();
  44. /* USER CODE END PD */
  45. /* Private macro -------------------------------------------------------------*/
  46. /* USER CODE BEGIN PM */
  47. /* USER CODE END PM */
  48. /* Private variables ---------------------------------------------------------*/
  49. /* USER CODE BEGIN PV */
  50. /* USER CODE END PV */
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. /* USER CODE BEGIN PFP */
  54. /* USER CODE END PFP */
  55. /* Private user code ---------------------------------------------------------*/
  56. /* USER CODE BEGIN 0 */
  57. /* USER CODE END 0 */
  58. /**
  59. * @brief The application entry point.
  60. * @retval int
  61. */
  62. int main(void)
  63. {
  64. /* USER CODE BEGIN 1 */
  65. /* USER CODE END 1 */
  66. /* MCU Configuration--------------------------------------------------------*/
  67. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  68. HAL_Init();
  69. /* USER CODE BEGIN Init */
  70. /* USER CODE END Init */
  71. /* Configure the system clock */
  72. SystemClock_Config();
  73. /* USER CODE BEGIN SysInit */
  74. /* USER CODE END SysInit */
  75. /* Initialize all configured peripherals */
  76. MX_GPIO_Init();
  77. MX_I2C3_Init();
  78. MX_I2C2_Init();
  79. MX_SPI1_Init();
  80. MX_TIM9_Init();
  81. MX_TIM1_Init();
  82. MX_ADC1_Init();
  83. MX_TIM2_Init();
  84. /* USER CODE BEGIN 2 */
  85. //testRTC();
  86. //testBatteryRTC();
  87. //testEeprom();
  88. //testAquit();
  89. //testLed();
  90. testSortie();
  91. //testSPI();
  92. //testVent();
  93. //testtemp();
  94. //TestOn();
  95. //TestOFF();
  96. //TestPWM();
  97. /* USER CODE END 2 */
  98. /* Infinite loop */
  99. /* USER CODE BEGIN WHILE */
  100. while (1)
  101. {
  102. /* USER CODE END WHILE */
  103. /* USER CODE BEGIN 3 */
  104. }
  105. /* USER CODE END 3 */
  106. }
  107. /**
  108. * @brief System Clock Configuration
  109. * @retval None
  110. */
  111. void SystemClock_Config(void)
  112. {
  113. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  115. /** Configure the main internal regulator output voltage
  116. */
  117. __HAL_RCC_PWR_CLK_ENABLE();
  118. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  119. /** Initializes the CPU, AHB and APB busses clocks
  120. */
  121. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  122. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  123. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  124. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  125. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  126. {
  127. Error_Handler();
  128. }
  129. /** Initializes the CPU, AHB and APB busses clocks
  130. */
  131. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  132. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  133. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  134. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  135. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  136. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  137. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. }
  142. /* USER CODE BEGIN 4 */
  143. void testAquit(){
  144. HAL_Delay(1);
  145. while (HAL_GPIO_ReadPin(aquit_GPIO_Port,aquit_Pin));
  146. HAL_Delay(1);
  147. }
  148. void testLed(){
  149. while(1){
  150. HAL_GPIO_TogglePin(LED_vert_GPIO_Port,LED_vert_Pin);
  151. HAL_GPIO_TogglePin(LED_orange_GPIO_Port,LED_orange_Pin);
  152. HAL_GPIO_TogglePin(LED_rouge_GPIO_Port,LED_rouge_Pin);
  153. HAL_Delay(500);
  154. }
  155. }
  156. void testSortie(){
  157. while(1){
  158. HAL_GPIO_TogglePin(S1_GPIO_Port,S1_Pin);
  159. HAL_GPIO_TogglePin(S2_GPIO_Port,S2_Pin);
  160. HAL_GPIO_TogglePin(S3_GPIO_Port,S3_Pin);
  161. HAL_GPIO_TogglePin(S4_GPIO_Port,S4_Pin);
  162. HAL_GPIO_TogglePin(S5_GPIO_Port,S5_Pin);
  163. HAL_GPIO_TogglePin(S6_GPIO_Port,S6_Pin);
  164. HAL_GPIO_TogglePin(S7_GPIO_Port,S7_Pin);
  165. HAL_GPIO_TogglePin(S8_GPIO_Port,S8_Pin);
  166. HAL_GPIO_TogglePin(S9_GPIO_Port,S9_Pin);
  167. HAL_GPIO_TogglePin(S10_GPIO_Port,S10_Pin);
  168. HAL_Delay(2000);
  169. }
  170. }
  171. void testSPI(){
  172. uint8_t data[1];
  173. HAL_SPI_Receive(&hspi1,data,1,1000);
  174. data[1]++;
  175. HAL_SPI_Transmit(&hspi1,data,1,1000);
  176. }
  177. void testVent(){
  178. TIM_OC_InitTypeDef sConfigOC;
  179. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  180. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  181. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  182. for (int i = 0; i<31250-1;i=i+3125){
  183. sConfigOC.Pulse = i;
  184. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  185. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  186. HAL_Delay(1000);
  187. }
  188. }
  189. void testtemp(){
  190. int i;
  191. while(1){
  192. HAL_ADC_Start(&hadc1);
  193. HAL_ADC_PollForConversion(&hadc1,1000);
  194. i=HAL_ADC_GetValue(&hadc1);
  195. }
  196. }
  197. void TestPWM(){
  198. TIM_OC_InitTypeDef sConfigOC;
  199. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  200. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  201. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  202. for (int i = 0; i<31250-1;i=i+3125){
  203. sConfigOC.Pulse = i;
  204. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
  205. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  206. HAL_Delay(1000);
  207. }
  208. }
  209. uint32_t buffer[1];
  210. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
  211. uint32_t temp = buffer[0];
  212. HAL_ADC_Stop_DMA(&hadc1);
  213. }
  214. void TestADC(){
  215. HAL_ADC_Start_DMA(&hadc1,buffer,1);
  216. }
  217. /* USER CODE END 4 */
  218. /**
  219. * @brief Period elapsed callback in non blocking mode
  220. * @note This function is called when TIM5 interrupt took place, inside
  221. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  222. * a global variable "uwTick" used as application time base.
  223. * @param htim : TIM handle
  224. * @retval None
  225. */
  226. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  227. {
  228. /* USER CODE BEGIN Callback 0 */
  229. /* USER CODE END Callback 0 */
  230. if (htim->Instance == TIM5) {
  231. HAL_IncTick();
  232. }
  233. /* USER CODE BEGIN Callback 1 */
  234. /* USER CODE END Callback 1 */
  235. }
  236. /**
  237. * @brief This function is executed in case of error occurrence.
  238. * @retval None
  239. */
  240. void Error_Handler(void)
  241. {
  242. /* USER CODE BEGIN Error_Handler_Debug */
  243. /* User can add his own implementation to report the HAL error return state */
  244. /* USER CODE END Error_Handler_Debug */
  245. }
  246. #ifdef USE_FULL_ASSERT
  247. /**
  248. * @brief Reports the name of the source file and the source line number
  249. * where the assert_param error has occurred.
  250. * @param file: pointer to the source file name
  251. * @param line: assert_param error line source number
  252. * @retval None
  253. */
  254. void assert_failed(uint8_t *file, uint32_t line)
  255. {
  256. /* USER CODE BEGIN 6 */
  257. /* User can add his own implementation to report the file name and line number,
  258. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  259. /* USER CODE END 6 */
  260. }
  261. #endif /* USE_FULL_ASSERT */
  262. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/