Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"
  4.  
  5. /* USER CODE BEGIN Includes */
  6.  
  7. /* USER CODE END Includes */
  8.  
  9. /* Private variables ---------------------------------------------------------*/
  10. UART_HandleTypeDef huart2;
  11.  
  12. /* USER CODE BEGIN PV */
  13. /* Private variables ---------------------------------------------------------*/
  14.  
  15. /* USER CODE END PV */
  16.  
  17. /* Private function prototypes -----------------------------------------------*/
  18. void SystemClock_Config(void);
  19. static void MX_GPIO_Init(void);
  20. static void MX_USART2_UART_Init(void);
  21.  
  22. /* USER CODE BEGIN PFP */
  23. /* Private function prototypes -----------------------------------------------*/
  24. uint32_t a = 0, b = 0, c = 0;
  25. #define ROZMIAR 30
  26. char bufor_kolowy[ROZMIAR];
  27. uint8_t zajety = 0;
  28. uint8_t wolny = 0;
  29. char odebrane[1];
  30. uint8_t data[50];
  31. uint8_t size;
  32. uint8_t komendy = 0;
  33. uint8_t pelny = 0;
  34. uint8_t przycisk = 0;
  35. #define LED_ON "LED[ON];" //zapalenie diody
  36. #define LED_OFF "LED[OFF];" //zgaszenie diody
  37. #define LED_BLINK "LED_BLINK;" //mrugnięcie 10 razy
  38. #define LED_1 "1;" //zapalenie diody
  39. #define LED_0 "0;" //zgaszenie diody
  40. #define ZAPAL "ZAPAL[%d,%d,%d];", &a, &b, &c //a - czas świecenia, b - czas zgaszenia, c - ilosc powtorzen
  41. char komenda[30]; //nasza komenda
  42. void dodaj_znak() {
  43. uint8_t poprzedni_wolny = wolny; //pomocnicza zapisuje obecny stan wskaznika wolny
  44. wolny++;
  45. if (wolny > (ROZMIAR - 1)) {
  46. wolny = 0;
  47. }
  48.  
  49. if ((poprzedni_wolny == zajety) || (wolny != zajety)) // mozna dodac znak/ czy bufor jest pełny
  50. {
  51. bufor_kolowy[poprzedni_wolny] = (char) odebrane[0];
  52. if (bufor_kolowy[poprzedni_wolny] == ';') {
  53. komendy++;
  54. }
  55. } else {
  56. wolny = poprzedni_wolny;
  57. pelny = 1;
  58. }
  59. }
  60. void wykonaj_komende() {
  61. uint8_t polecenie = 0;
  62.  
  63. if (komendy > 0) {
  64. komendy--;
  65. uint8_t pierwszy_zajety = zajety; // 1 znak w poleceniu
  66.  
  67. for (uint8_t i = 0; i < ROZMIAR; i++) {
  68. komenda[i] = bufor_kolowy[pierwszy_zajety];
  69. if (komenda[i] == ';') {
  70.  
  71. zajety = pierwszy_zajety + 1;
  72. komenda[i + 1] = '\0';
  73. if (pierwszy_zajety > (ROZMIAR - 1)) {
  74. pierwszy_zajety = 0;
  75. }
  76. break;
  77. }
  78. pierwszy_zajety++;
  79. if (pierwszy_zajety > (ROZMIAR - 1)) {
  80. pierwszy_zajety = 0;
  81. }
  82.  
  83. }
  84.  
  85. if ((strcmp(LED_ON, komenda)) == 0 || (strcmp(LED_1, komenda)) == 0)
  86. polecenie = 1;
  87. if ((strcmp(LED_OFF, komenda)) == 0 || (strcmp(LED_0, komenda)) == 0)
  88. polecenie = 2;
  89. if ((strcmp(LED_BLINK, komenda)) == 0)
  90. polecenie = 3;
  91. //if (sscanf(komenda, "ZAPAL[%d,%d,%d];", &a, &b, &c))
  92. if (sscanf(komenda, ZAPAL))
  93. polecenie = 4;
  94. switch (polecenie) {
  95. case 1: {
  96. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 1);
  97. break;
  98. }
  99. case 2: {
  100. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 0);
  101. break;
  102. }
  103. case 3: {
  104. uint8_t ile = 10;
  105. while (ile) {
  106. ile--;
  107. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 1);
  108. sysTick(100);
  109. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 0);
  110. sysTick(500);
  111.  
  112. }
  113. break;
  114. }
  115. //-------------------------------------------------------------
  116. case 4: {
  117.  
  118. for (int i = 0; i < c; i++) {
  119. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 1);
  120.  
  121. sysTick(a);
  122. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, 0);
  123.  
  124. sysTick(b);
  125.  
  126. }
  127. break;
  128. }
  129.  
  130. default: {
  131. size = sprintf(data, "Komenda nie rozpoznana\n\r");
  132. HAL_UART_Transmit_IT(&huart2, data, size);
  133. break;
  134. }
  135.  
  136. } //switch
  137.  
  138. }
  139. }
  140. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  141.  
  142. if (huart->Instance == USART2) {
  143. dodaj_znak();
  144. HAL_UART_Receive_IT(&huart2, &odebrane, 1);
  145. }
  146.  
  147. }
  148. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) //wcisniecie przycisku
  149. {
  150. if (GPIO_Pin == B1_Pin) {
  151. if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin))
  152. przycisk = 1;
  153. }
  154. }
  155. /* USER CODE END PFP */
  156.  
  157. /* USER CODE BEGIN 0 */
  158.  
  159. /* USER CODE END 0 */
  160.  
  161. /**
  162. * @brief The application entry point.
  163. *
  164. * @retval None
  165. */
  166. int main(void) {
  167. /* USER CODE BEGIN 1 */
  168.  
  169. /* USER CODE END 1 */
  170.  
  171. /* MCU Configuration----------------------------------------------------------*/
  172.  
  173. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  174. HAL_Init();
  175.  
  176. /* USER CODE BEGIN Init */
  177.  
  178. /* USER CODE END Init */
  179.  
  180. /* Configure the system clock */
  181. SystemClock_Config();
  182.  
  183. /* USER CODE BEGIN SysInit */
  184.  
  185. /* USER CODE END SysInit */
  186.  
  187. /* Initialize all configured peripherals */
  188. MX_GPIO_Init();
  189. MX_USART2_UART_Init();
  190. /* USER CODE BEGIN 2 */
  191. for (uint8_t i = 0; i < ROZMIAR; i++) {
  192. bufor_kolowy[i] = '-';
  193. }
  194.  
  195. size = sprintf(data, "hello stm\n\r");
  196. HAL_UART_Transmit_IT(&huart2, data, size);
  197.  
  198. HAL_UART_Receive_IT(&huart2, &odebrane, 1);
  199. /* USER CODE END 2 */
  200.  
  201. /* Infinite loop */
  202. /* USER CODE BEGIN WHILE */
  203. while (1) {
  204.  
  205. /* USER CODE END WHILE */
  206.  
  207. /* USER CODE BEGIN 3 */
  208. if (pelny) {
  209. size = sprintf(data, "Bufor pelny\n\r");
  210. HAL_UART_Transmit_IT(&huart2, data, size);
  211.  
  212. } else if (przycisk) {
  213. przycisk = 0;
  214. size = sprintf(data, "%s\n\r", &bufor_kolowy);
  215. HAL_UART_Transmit_IT(&huart2, data, size);
  216.  
  217. }
  218. wykonaj_komende();
  219. }
  220. /* USER CODE END 3 */
  221.  
  222. }
  223.  
  224. /**
  225. * @brief System Clock Configuration
  226. * @retval None
  227. */
  228. void SystemClock_Config(void) {
  229.  
  230. RCC_OscInitTypeDef RCC_OscInitStruct;
  231. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  232.  
  233. /**Initializes the CPU, AHB and APB busses clocks
  234. */
  235. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  236. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  237. RCC_OscInitStruct.HSICalibrationValue = 16;
  238. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  239. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  240. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  241. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  242. _Error_Handler(__FILE__, __LINE__);
  243. }
  244.  
  245. /**Initializes the CPU, AHB and APB busses clocks
  246. */
  247. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  248. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  249. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  250. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  251. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  252. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  253.  
  254. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  255. _Error_Handler(__FILE__, __LINE__);
  256. }
  257.  
  258. /**Configure the Systick interrupt time
  259. */
  260. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  261.  
  262. /**Configure the Systick
  263. */
  264. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  265.  
  266. /* SysTick_IRQn interrupt configuration */
  267. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  268. }
  269.  
  270. /* USART2 init function */
  271. static void MX_USART2_UART_Init(void) {
  272.  
  273. huart2.Instance = USART2;
  274. huart2.Init.BaudRate = 9600;
  275. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  276. huart2.Init.StopBits = UART_STOPBITS_1;
  277. huart2.Init.Parity = UART_PARITY_NONE;
  278. huart2.Init.Mode = UART_MODE_TX_RX;
  279. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  280. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  281. if (HAL_UART_Init(&huart2) != HAL_OK) {
  282. _Error_Handler(__FILE__, __LINE__);
  283. }
  284.  
  285. }
  286.  
  287. /** Configure pins as
  288. * Analog
  289. * Input
  290. * Output
  291. * EVENT_OUT
  292. * EXTI
  293. */
  294. static void MX_GPIO_Init(void) {
  295.  
  296. GPIO_InitTypeDef GPIO_InitStruct;
  297.  
  298. /* GPIO Ports Clock Enable */
  299. __HAL_RCC_GPIOC_CLK_ENABLE()
  300. ;
  301. __HAL_RCC_GPIOD_CLK_ENABLE()
  302. ;
  303. __HAL_RCC_GPIOA_CLK_ENABLE()
  304. ;
  305. __HAL_RCC_GPIOB_CLK_ENABLE()
  306. ;
  307.  
  308. /*Configure GPIO pin Output Level */
  309. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  310.  
  311. /*Configure GPIO pin : B1_Pin */
  312. GPIO_InitStruct.Pin = B1_Pin;
  313. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  314. GPIO_InitStruct.Pull = GPIO_NOPULL;
  315. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  316.  
  317. /*Configure GPIO pin : LD2_Pin */
  318. GPIO_InitStruct.Pin = LD2_Pin;
  319. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  320. GPIO_InitStruct.Pull = GPIO_NOPULL;
  321. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  322. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  323.  
  324. /* EXTI interrupt init*/
  325. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  326. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  327.  
  328. }
  329.  
  330. /* USER CODE BEGIN 4 */
  331.  
  332. /* USER CODE END 4 */
  333.  
  334. /**
  335. * @brief This function is executed in case of error occurrence.
  336. * @param file: The file name as string.
  337. * @param line: The line in file as a number.
  338. * @retval None
  339. */
  340. void _Error_Handler(char *file, int line) {
  341. /* USER CODE BEGIN Error_Handler_Debug */
  342. /* User can add his own implementation to report the HAL error return state */
  343. while (1) {
  344. }
  345. /* USER CODE END Error_Handler_Debug */
  346. }
  347.  
  348. #ifdef USE_FULL_ASSERT
  349. /**
  350. * @brief Reports the name of the source file and the source line number
  351. * where the assert_param error has occurred.
  352. * @param file: pointer to the source file name
  353. * @param line: assert_param error line source number
  354. * @retval None
  355. */
  356. void assert_failed(uint8_t* file, uint32_t line)
  357. {
  358. /* USER CODE BEGIN 6 */
  359. /* User can add his own implementation to report the file name and line number,
  360. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  361. /* USER CODE END 6 */
  362. }
  363. #endif /* USE_FULL_ASSERT */
  364.  
  365. /**
  366. * @}
  367. */
  368.  
  369. /**
  370. * @}
  371. */
  372.  
  373. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement