main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "gpio.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. void testAquit();
  32. void testLed();
  33. void testSortie();
  34. void testSPI();
  35. void testVent();
  36. void testtemp();
  37. void TestOn();
  38. void TestOFF();
  39. void TestPWM();
  40. /* USER CODE END PD */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43. /* USER CODE END PM */
  44. /* Private variables ---------------------------------------------------------*/
  45. /* USER CODE BEGIN PV */
  46. /* USER CODE END PV */
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. /* USER CODE BEGIN PFP */
  50. /* USER CODE END PFP */
  51. /* Private user code ---------------------------------------------------------*/
  52. /* USER CODE BEGIN 0 */
  53. /* USER CODE END 0 */
  54. /**
  55. * @brief The application entry point.
  56. * @retval int
  57. */
  58. int main(void)
  59. {
  60. /* USER CODE BEGIN 1 */
  61. /* USER CODE END 1 */
  62. /* MCU Configuration--------------------------------------------------------*/
  63. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  64. HAL_Init();
  65. /* USER CODE BEGIN Init */
  66. /* USER CODE END Init */
  67. /* Configure the system clock */
  68. SystemClock_Config();
  69. /* USER CODE BEGIN SysInit */
  70. /* USER CODE END SysInit */
  71. /* Initialize all configured peripherals */
  72. MX_GPIO_Init();
  73. /* USER CODE BEGIN 2 */
  74. /* testRTC();
  75. testBatteryRTC();
  76. testEeprom();
  77. testAquit();
  78. testLed();
  79. testSortie();
  80. testSPI();
  81. testVent();
  82. testtemp();
  83. TestOn();
  84. TestOFF();
  85. TestPWM();
  86. /* USER CODE END 2 */
  87. /* Infinite loop */
  88. /* USER CODE BEGIN WHILE */
  89. while (1)
  90. {
  91. /* USER CODE END WHILE */
  92. /* USER CODE BEGIN 3 */
  93. }
  94. /* USER CODE END 3 */
  95. }
  96. /**
  97. * @brief System Clock Configuration
  98. * @retval None
  99. */
  100. void SystemClock_Config(void)
  101. {
  102. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  103. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  104. /** Configure the main internal regulator output voltage
  105. */
  106. __HAL_RCC_PWR_CLK_ENABLE();
  107. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  108. /** Initializes the CPU, AHB and APB busses clocks
  109. */
  110. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  111. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  112. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  113. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  114. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  115. {
  116. Error_Handler();
  117. }
  118. /** Initializes the CPU, AHB and APB busses clocks
  119. */
  120. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  121. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  122. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  123. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  124. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  125. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  126. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. }
  131. /* USER CODE BEGIN 4 */
  132. /*
  133. void testAquit(){
  134. while (HAL_GPIO_ReadPin(aquit_GPIO_Port,aquit_Pin));
  135. HAL_Delay(1);
  136. }
  137. void testLed(){
  138. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,0);
  139. HAL_GPIO_WritePin(LED_orange_GPIO_Port,LED_orange_Pin,0);
  140. HAL_GPIO_WritePin(LED_rouge_GPIO_Port,LED_rouge_Pin,0);
  141. HAL_Delay(1000);
  142. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,1);
  143. HAL_GPIO_WritePin(LED_orange_GPIO_Port,LED_orange_Pin,1);
  144. HAL_GPIO_WritePin(LED_rouge_GPIO_Port,LED_rouge_Pin,1);
  145. HAL_Delay(1000);
  146. }
  147. void testSortie(){
  148. HAL_GPIO_WritePin(S1_GPIO_Port,S1_Pin,0);
  149. HAL_GPIO_WritePin(S2_GPIO_Port,S2_Pin,0);
  150. HAL_GPIO_WritePin(S3_GPIO_Port,S3_Pin,0);
  151. HAL_GPIO_WritePin(S4_GPIO_Port,S4_Pin,0);
  152. HAL_GPIO_WritePin(S5_GPIO_Port,S5_Pin,0);
  153. HAL_GPIO_WritePin(S6_GPIO_Port,S6_Pin,0);
  154. HAL_GPIO_WritePin(S7_GPIO_Port,S7_Pin,0);
  155. HAL_GPIO_WritePin(S8_GPIO_Port,S8_Pin,0);
  156. HAL_GPIO_WritePin(S9_GPIO_Port,S9_Pin,0);
  157. HAL_GPIO_WritePin(S10_GPIO_Port,S10_Pin,0);
  158. HAL_Delay(1000);
  159. HAL_GPIO_WritePin(S1_GPIO_Port,S1_Pin,1);
  160. HAL_GPIO_WritePin(S2_GPIO_Port,S2_Pin,1);
  161. HAL_GPIO_WritePin(S3_GPIO_Port,S3_Pin,1);
  162. HAL_GPIO_WritePin(S4_GPIO_Port,S4_Pin,1);
  163. HAL_GPIO_WritePin(S5_GPIO_Port,S5_Pin,1);
  164. HAL_GPIO_WritePin(S6_GPIO_Port,S6_Pin,1);
  165. HAL_GPIO_WritePin(S7_GPIO_Port,S7_Pin,1);
  166. HAL_GPIO_WritePin(S8_GPIO_Port,S8_Pin,1);
  167. HAL_GPIO_WritePin(S9_GPIO_Port,S9_Pin,1);
  168. HAL_GPIO_WritePin(S10_GPIO_Port,S10_Pin,1);
  169. HAL_Delay(1000);
  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 This function is executed in case of error occurrence.
  220. * @retval None
  221. */
  222. void Error_Handler(void)
  223. {
  224. /* USER CODE BEGIN Error_Handler_Debug */
  225. /* User can add his own implementation to report the HAL error return state */
  226. /* USER CODE END Error_Handler_Debug */
  227. }
  228. #ifdef USE_FULL_ASSERT
  229. /**
  230. * @brief Reports the name of the source file and the source line number
  231. * where the assert_param error has occurred.
  232. * @param file: pointer to the source file name
  233. * @param line: assert_param error line source number
  234. * @retval None
  235. */
  236. void assert_failed(uint8_t *file, uint32_t line)
  237. {
  238. /* USER CODE BEGIN 6 */
  239. /* User can add his own implementation to report the file name and line number,
  240. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  241. /* USER CODE END 6 */
  242. }
  243. #endif /* USE_FULL_ASSERT */
  244. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/