main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* USER CODE BEGIN 1 */
  64. /* USER CODE END 1 */
  65. /* MCU Configuration--------------------------------------------------------*/
  66. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  67. HAL_Init();
  68. /* USER CODE BEGIN Init */
  69. /* USER CODE END Init */
  70. /* Configure the system clock */
  71. SystemClock_Config();
  72. /* USER CODE BEGIN SysInit */
  73. /* USER CODE END SysInit */
  74. /* Initialize all configured peripherals */
  75. MX_GPIO_Init();
  76. MX_I2C3_Init();
  77. MX_I2C2_Init();
  78. MX_SPI1_Init();
  79. MX_TIM9_Init();
  80. MX_TIM1_Init();
  81. MX_ADC1_Init();
  82. MX_TIM2_Init();
  83. /* USER CODE BEGIN 2 */
  84. // testRTC();
  85. // testBatteryRTC();
  86. // testEeprom();
  87. // testAquit();
  88. // testLed();
  89. // testSortie();
  90. testSPI();
  91. // testVent();
  92. // testtemp();
  93. // TestOn();
  94. // TestOFF();
  95. // TestPWM();
  96. /* USER CODE END 2 */
  97. /* Infinite loop */
  98. /* USER CODE BEGIN WHILE */
  99. while (1) {
  100. /* USER CODE END WHILE */
  101. /* USER CODE BEGIN 3 */
  102. }
  103. /* USER CODE END 3 */
  104. }
  105. /**
  106. * @brief System Clock Configuration
  107. * @retval None
  108. */
  109. void SystemClock_Config(void) {
  110. RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
  111. RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
  112. /** Configure the main internal regulator output voltage
  113. */
  114. __HAL_RCC_PWR_CLK_ENABLE()
  115. ;
  116. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  117. /** Initializes the CPU, AHB and APB busses clocks
  118. */
  119. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  120. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  121. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  123. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  124. Error_Handler();
  125. }
  126. /** Initializes the CPU, AHB and APB busses clocks
  127. */
  128. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  129. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  130. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  131. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  132. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  133. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  134. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
  135. Error_Handler();
  136. }
  137. }
  138. /* USER CODE BEGIN 4 */
  139. void testAquit() {
  140. while (HAL_GPIO_ReadPin(aquit_GPIO_Port, aquit_Pin))
  141. ;
  142. HAL_Delay(1);
  143. }
  144. void testLed() {
  145. while (1) {
  146. HAL_GPIO_TogglePin(LED_vert_GPIO_Port, LED_vert_Pin);
  147. HAL_GPIO_TogglePin(LED_orange_GPIO_Port, LED_orange_Pin);
  148. HAL_GPIO_TogglePin(LED_rouge_GPIO_Port, LED_rouge_Pin);
  149. HAL_Delay(1000);
  150. }
  151. }
  152. void testSortie() {
  153. while (1) {
  154. HAL_GPIO_TogglePin(S1_GPIO_Port, S1_Pin);
  155. HAL_GPIO_TogglePin(S2_GPIO_Port, S2_Pin);
  156. HAL_GPIO_TogglePin(S3_GPIO_Port, S3_Pin);
  157. HAL_GPIO_TogglePin(S4_GPIO_Port, S4_Pin);
  158. HAL_GPIO_TogglePin(S5_GPIO_Port, S5_Pin);
  159. HAL_GPIO_TogglePin(S6_GPIO_Port, S6_Pin);
  160. HAL_GPIO_TogglePin(S7_GPIO_Port, S7_Pin);
  161. HAL_GPIO_TogglePin(S8_GPIO_Port, S8_Pin);
  162. HAL_GPIO_TogglePin(S9_GPIO_Port, S9_Pin);
  163. HAL_GPIO_TogglePin(S10_GPIO_Port, S10_Pin);
  164. HAL_Delay(1000);
  165. }
  166. }
  167. void testSPI() {
  168. uint8_t data[1];
  169. while(1){
  170. while(HAL_SPI_Receive(&hspi1, data, 1, 1000)!=HAL_ERROR);
  171. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,1);
  172. data[0]++;
  173. HAL_SPI_Transmit(&hspi1, data, 1, 1000);
  174. HAL_Delay(100);
  175. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,0);
  176. }
  177. }
  178. void testVent() {
  179. TIM_OC_InitTypeDef sConfigOC;
  180. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  181. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  182. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  183. for (int i = 0; i < 31250 - 1; i = i + 3125) {
  184. sConfigOC.Pulse = i;
  185. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  186. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  187. HAL_Delay(1000);
  188. }
  189. }
  190. void testtemp() {
  191. int i;
  192. while (1) {
  193. HAL_ADC_Start(&hadc1);
  194. HAL_ADC_PollForConversion(&hadc1, 1000);
  195. i = HAL_ADC_GetValue(&hadc1);
  196. }
  197. }
  198. void TestPWM() {
  199. TIM_OC_InitTypeDef sConfigOC;
  200. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  201. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  202. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  203. for (int i = 0; i < 31250 - 1; i = i + 3125) {
  204. sConfigOC.Pulse = i;
  205. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
  206. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  207. HAL_Delay(1000);
  208. }
  209. }
  210. uint32_t buffer[1];
  211. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
  212. uint32_t temp = buffer[0];
  213. HAL_ADC_Stop_DMA(&hadc1);
  214. }
  215. void TestADC() {
  216. HAL_ADC_Start_DMA(&hadc1, buffer, 1);
  217. }
  218. /* USER CODE END 4 */
  219. /**
  220. * @brief This function is executed in case of error occurrence.
  221. * @retval None
  222. */
  223. void Error_Handler(void) {
  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****/