Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.29 KB | None | 0 0
  1.  
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f1xx_hal.h"
  42. #include "stm32f1xx_it.h"
  43. /* USER CODE BEGIN Includes */
  44. #include "inttypes.h"
  45. #include "i2c-lcd.h"
  46. #include "string.h"
  47. #include <stdbool.h>
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51. I2C_HandleTypeDef hi2c1;
  52.  
  53. TIM_HandleTypeDef htim4;
  54.  
  55. UART_HandleTypeDef huart1;
  56. UART_HandleTypeDef huart2;
  57.  
  58. /* USER CODE BEGIN PV */
  59. /* Private variables ---------------------------------------------------------*/
  60. bool sel_USART_send = false; // true=raw, false=string
  61. uint8_t buffer_USART2_tx[10];
  62. uint8_t buffer_USART2_rx[10];
  63. uint8_t buffer_USART1_rx[10];
  64. uint32_t water_level,input_flw_int,output_flw_int=0;
  65. uint32_t target_level_pi=0;
  66. uint32_t target_level_kp=0;
  67. uint32_t input_flow_cnt,output_flow_cnt=0;
  68. uint32_t sys_timer=0;
  69. float input_flow,output_flow=0;
  70. char cast_buff [10];
  71. /* USER CODE END PV */
  72.  
  73. /* Private function prototypes -----------------------------------------------*/
  74. void SystemClock_Config(void);
  75. static void MX_GPIO_Init(void);
  76. static void MX_USART1_UART_Init(void);
  77. static void MX_USART2_UART_Init(void);
  78. static void MX_TIM4_Init(void);
  79. static void MX_I2C1_Init(void);
  80. static void MX_NVIC_Init(void);
  81.  
  82. /* USER CODE BEGIN PFP */
  83. /* Private function prototypes -----------------------------------------------*/
  84. char* concat(const char *s1, const char *s2);
  85. uint8_t KeypadGetKey(void); // Get KEY value from MATRIX KEYPAD
  86. uint32_t getUSART1(void); // Get WATER_LEVEL from SLAVE STM32
  87. uint32_t getUSART2(void); // Get TARGET_LEVEL from NANOPI
  88. void setUSART2(uint32_t value, bool mode); //Send WATER_LEVEL to NANOPI
  89. /* USER CODE END PFP */
  90.  
  91. /* USER CODE BEGIN 0 */
  92.  
  93. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  94. {
  95. if(htim->Instance == TIM4){
  96. if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
  97. {
  98.  
  99. input_flow_cnt++;
  100.  
  101.  
  102. }
  103. else if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2)
  104. {
  105.  
  106. output_flow_cnt++;
  107. }
  108. else
  109. {
  110.  
  111. }
  112. }
  113.  
  114.  
  115. }
  116. void HAL_SYSTICK_Callback()
  117. {
  118. sys_timer++;
  119. if(sys_timer >=1000)
  120. {
  121. output_flow=(output_flow_cnt) /7.5;
  122. input_flow=(input_flow_cnt) /7.5;
  123. input_flw_int=round(input_flow);
  124. output_flw_int=round(output_flow);
  125. output_flow_cnt=0;
  126. input_flow_cnt=0;
  127. sys_timer=0;
  128. }
  129. }
  130.  
  131. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  132. {
  133. __NOP();
  134. }
  135.  
  136. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  137. {
  138. if(huart->Instance==USART1)
  139. {
  140.  
  141. water_level=getUSART1();
  142. }
  143.  
  144. if(huart->Instance==USART2)
  145. {
  146. target_level_pi=getUSART2();
  147.  
  148. }
  149. }
  150. /* USER CODE END 0 */
  151.  
  152. /**
  153. * @brief The application entry point.
  154. *
  155. * @retval None
  156. */
  157. int main(void)
  158. {
  159. /* USER CODE BEGIN 1 */
  160.  
  161. /* USER CODE END 1 */
  162.  
  163. /* MCU Configuration----------------------------------------------------------*/
  164.  
  165. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  166. HAL_Init();
  167.  
  168. /* USER CODE BEGIN Init */
  169.  
  170. /* USER CODE END Init */
  171.  
  172. /* Configure the system clock */
  173. SystemClock_Config();
  174.  
  175. /* USER CODE BEGIN SysInit */
  176.  
  177. /* USER CODE END SysInit */
  178.  
  179. /* Initialize all configured peripherals */
  180. MX_GPIO_Init();
  181. MX_USART1_UART_Init();
  182. MX_USART2_UART_Init();
  183. MX_TIM4_Init();
  184. MX_I2C1_Init();
  185.  
  186. /* Initialize interrupts */
  187. MX_NVIC_Init();
  188. /* USER CODE BEGIN 2 */
  189. HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);
  190. HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_2);
  191. HAL_UART_Receive_IT(&huart1, buffer_USART1_rx, 4);
  192. HAL_UART_Receive_IT(&huart2, buffer_USART2_rx, 4);
  193. // HAL_UART_Transmit_IT(&huart2, buffer_USART2_tx, 4);
  194.  
  195. volatile char key;
  196.  
  197. bool text_flag = false;
  198. char razina[2];
  199.  
  200. static uint8_t i=0;
  201.  
  202. HAL_Delay(1000);
  203. lcd_init();
  204. HAL_Delay(1000);
  205. lcd_send_cmd(0x01); //clear screen
  206.  
  207. char *mystr1="Targ.Lvl:";
  208. char *mystr2="Curr.Lvl:";
  209. char *curr_lvl="25";
  210. char * new_str1 ;
  211. char * new_str2 ;
  212. char * targ_lvl;
  213.  
  214. lcd_send_string(mystr1);
  215. lcd_send_cmd(0xC0);// move cursor to second row
  216. lcd_send_string(mystr2);
  217. /* USER CODE END 2 */
  218.  
  219. /* Infinite loop */
  220. /* USER CODE BEGIN WHILE */
  221. while (1)
  222. {
  223.  
  224. /* USER CODE END WHILE */
  225. // Get key pressed
  226. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12); //Toggle the state of pin PC9
  227. HAL_Delay(1000); //delay 100ms
  228.  
  229. key=KeypadGetKey();
  230. HAL_Delay(50);
  231. // LCD print
  232. if (text_flag==true)
  233. {
  234.  
  235. lcd_send_cmd(0x01);
  236.  
  237. if((new_str1 = malloc(strlen(mystr1)+strlen(razina)+1)) != NULL)
  238. {
  239. new_str1[0] = '\0'; // ensures the memory is an empty string
  240. strcat(new_str1,mystr1);
  241. strcat(new_str1,razina);
  242. }
  243.  
  244.  
  245. lcd_send_cmd(0x01);
  246. lcd_send_string(new_str1);
  247.  
  248. if((new_str2 = malloc(strlen(mystr2)+strlen(curr_lvl)+1)) != NULL)
  249. {
  250. new_str2[0] = '\0'; // ensures the memory is an empty string
  251. strcat(new_str2,mystr2);
  252. strcat(new_str2,curr_lvl);
  253. }
  254.  
  255. lcd_send_cmd(0xC0);
  256. lcd_send_string(new_str2);
  257. }
  258.  
  259. // Numerical key pressed
  260. if ((key != KEYPAD_NO_PRESSED && (key != '*') ) && (key != KEYPAD_NO_PRESSED && (key!='#')))
  261. {
  262. HAL_Delay(30);
  263. razina[i]=key;
  264. razina[i+1]='\0';
  265. i++;
  266. if(i>2)
  267. {
  268. i=0;
  269. }
  270. text_flag=true;
  271. }
  272. // Enter (*) pressed
  273.  
  274. else if(key!=KEYPAD_NO_PRESSED && (key =='*'))
  275. {
  276. i=0;
  277. target_level_kp=atoi(razina);
  278.  
  279. /* if((targ_lvl = malloc(strlen(razina))) != NULL)
  280. {
  281. targ_lvl[0] = '\0'; // ensures the memory is an empty string
  282. strcpy(targ_lvl,razina);
  283. }
  284. */
  285. text_flag=true;
  286.  
  287. }
  288.  
  289. else
  290. {
  291. text_flag=false;
  292. }
  293. /* USER CODE BEGIN 3 */
  294.  
  295.  
  296. //setUSART2(water_level,true);
  297.  
  298. // sprintf(buffer_USART2_tx, "Lvl:"%" PRIu32 "\n" "In:" ""%" PRIu32 "\n",input_flw_int);
  299. sprintf(buffer_USART2_tx,"%" PRIu32 "\n",target_level_kp);
  300. HAL_UART_Transmit_IT(&huart2,buffer_USART2_tx,5);
  301. }
  302. /* USER CODE END 3 */
  303.  
  304. }
  305.  
  306. /**
  307. * @brief System Clock Configuration
  308. * @retval None
  309. */
  310. void SystemClock_Config(void)
  311. {
  312.  
  313. RCC_OscInitTypeDef RCC_OscInitStruct;
  314. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  315.  
  316. /**Initializes the CPU, AHB and APB busses clocks
  317. */
  318. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  319. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  320. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  321. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  322. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  323. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  324. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  325. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  326. {
  327. _Error_Handler(__FILE__, __LINE__);
  328. }
  329.  
  330. /**Initializes the CPU, AHB and APB busses clocks
  331. */
  332. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  333. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  334. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  335. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  336. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  337. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  338.  
  339. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  340. {
  341. _Error_Handler(__FILE__, __LINE__);
  342. }
  343.  
  344. /**Configure the Systick interrupt time
  345. */
  346. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  347.  
  348. /**Configure the Systick
  349. */
  350. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  351.  
  352. /* SysTick_IRQn interrupt configuration */
  353. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  354. }
  355.  
  356. /**
  357. * @brief NVIC Configuration.
  358. * @retval None
  359. */
  360. static void MX_NVIC_Init(void)
  361. {
  362. /* TIM4_IRQn interrupt configuration */
  363. HAL_NVIC_SetPriority(TIM4_IRQn, 1, 0);
  364. HAL_NVIC_EnableIRQ(TIM4_IRQn);
  365. /* I2C1_EV_IRQn interrupt configuration */
  366. HAL_NVIC_SetPriority(I2C1_EV_IRQn, 2, 0);
  367. HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
  368. /* I2C1_ER_IRQn interrupt configuration */
  369. HAL_NVIC_SetPriority(I2C1_ER_IRQn, 3, 0);
  370. HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
  371. /* RCC_IRQn interrupt configuration */
  372. HAL_NVIC_SetPriority(RCC_IRQn,1, 0);
  373. HAL_NVIC_EnableIRQ(RCC_IRQn);
  374. /* USART1_IRQn interrupt configuration */
  375. HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);
  376. HAL_NVIC_EnableIRQ(USART1_IRQn);
  377. /* USART2_IRQn interrupt configuration */
  378. HAL_NVIC_SetPriority(USART2_IRQn, 3, 0);
  379. HAL_NVIC_EnableIRQ(USART2_IRQn);
  380. }
  381.  
  382. /* I2C1 init function */
  383. static void MX_I2C1_Init(void)
  384. {
  385.  
  386. hi2c1.Instance = I2C1;
  387. hi2c1.Init.ClockSpeed = 100000;
  388. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  389. hi2c1.Init.OwnAddress1 = 0;
  390. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  391. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  392. hi2c1.Init.OwnAddress2 = 0;
  393. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  394. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  395. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  396. {
  397. _Error_Handler(__FILE__, __LINE__);
  398. }
  399.  
  400. }
  401.  
  402. /* TIM4 init function */
  403. static void MX_TIM4_Init(void)
  404. {
  405.  
  406. TIM_ClockConfigTypeDef sClockSourceConfig;
  407. TIM_MasterConfigTypeDef sMasterConfig;
  408. TIM_IC_InitTypeDef sConfigIC;
  409.  
  410. htim4.Instance = TIM4;
  411. htim4.Init.Prescaler = 71;
  412. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  413. htim4.Init.Period = 65535;
  414. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  415. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  416. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  417. {
  418. _Error_Handler(__FILE__, __LINE__);
  419. }
  420.  
  421. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  422. if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  423. {
  424. _Error_Handler(__FILE__, __LINE__);
  425. }
  426.  
  427. if (HAL_TIM_IC_Init(&htim4) != HAL_OK)
  428. {
  429. _Error_Handler(__FILE__, __LINE__);
  430. }
  431.  
  432. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  433. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  434. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  435. {
  436. _Error_Handler(__FILE__, __LINE__);
  437. }
  438.  
  439. sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  440. sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  441. sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  442. sConfigIC.ICFilter = 15;
  443. if (HAL_TIM_IC_ConfigChannel(&htim4, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  444. {
  445. _Error_Handler(__FILE__, __LINE__);
  446. }
  447.  
  448. if (HAL_TIM_IC_ConfigChannel(&htim4, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
  449. {
  450. _Error_Handler(__FILE__, __LINE__);
  451. }
  452.  
  453. }
  454.  
  455. /* USART1 init function */
  456. static void MX_USART1_UART_Init(void)
  457. {
  458.  
  459. huart1.Instance = USART1;
  460. huart1.Init.BaudRate = 115200;
  461. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  462. huart1.Init.StopBits = UART_STOPBITS_1;
  463. huart1.Init.Parity = UART_PARITY_NONE;
  464. huart1.Init.Mode = UART_MODE_RX;
  465. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  466. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  467. if (HAL_UART_Init(&huart1) != HAL_OK)
  468. {
  469. _Error_Handler(__FILE__, __LINE__);
  470. }
  471.  
  472. }
  473.  
  474. /* USART2 init function */
  475. static void MX_USART2_UART_Init(void)
  476. {
  477.  
  478. huart2.Instance = USART2;
  479. huart2.Init.BaudRate = 115200;
  480. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  481. huart2.Init.StopBits = UART_STOPBITS_1;
  482. huart2.Init.Parity = UART_PARITY_NONE;
  483. huart2.Init.Mode = UART_MODE_TX_RX;
  484. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  485. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  486. if (HAL_UART_Init(&huart2) != HAL_OK)
  487. {
  488. _Error_Handler(__FILE__, __LINE__);
  489. }
  490.  
  491. }
  492.  
  493. /** Configure pins as
  494. * Analog
  495. * Input
  496. * Output
  497. * EVENT_OUT
  498. * EXTI
  499. */
  500. static void MX_GPIO_Init(void)
  501. {
  502.  
  503. GPIO_InitTypeDef GPIO_InitStruct;
  504.  
  505. /* GPIO Ports Clock Enable */
  506. __HAL_RCC_GPIOD_CLK_ENABLE();
  507. __HAL_RCC_GPIOA_CLK_ENABLE();
  508. __HAL_RCC_GPIOB_CLK_ENABLE();
  509.  
  510. /*Configure GPIO pin Output Level */
  511. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
  512.  
  513. /*Configure GPIO pins : PA4 PA5 PA6 PA7 */
  514. GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  515. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  516. GPIO_InitStruct.Pull = GPIO_PULLUP;
  517. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  518.  
  519.  
  520.  
  521. /*Configure GPIO pins : PB1 PB10 PB0 */
  522. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_0;
  523. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  524. GPIO_InitStruct.Pull = GPIO_NOPULL;
  525. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  526. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  527.  
  528. /*Configure GPIO pin : PB12 */
  529. GPIO_InitStruct.Pin = GPIO_PIN_12;
  530. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  531. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  532. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  533. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  534.  
  535. }
  536.  
  537. /* USER CODE BEGIN 4 */
  538. uint8_t KeypadGetKey()
  539. {
  540. // Scan column 0 (column 0 pin is grounded, other column pins is open drain)
  541. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL0,GPIO_PIN_RESET);
  542. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL1,GPIO_PIN_SET);
  543. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL2,GPIO_PIN_SET);
  544.  
  545. // Read rows
  546. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW0))
  547. return '1';
  548. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW1))
  549. return '4';
  550. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW2))
  551. return '7';
  552. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW3))
  553. return '*';
  554. HAL_Delay(30);
  555. // Scan column 1 (column 1 pin is grounded, other column pins is open drain)
  556. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL0,GPIO_PIN_SET);
  557. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL1,GPIO_PIN_RESET);
  558. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL2,GPIO_PIN_SET);
  559.  
  560.  
  561. // Read rows
  562. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW0))
  563. return '2';
  564. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW1))
  565. return '5';
  566. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW2))
  567. return '8';
  568. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW3))
  569. return '0';
  570. HAL_Delay(30);
  571. // Scan column 2 (column 2 pin is grounded, other column pins is open drain)
  572. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL0,GPIO_PIN_SET);
  573. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL1,GPIO_PIN_SET);
  574. HAL_GPIO_WritePin(KEYPAD_GPIO_COL, KEYPAD_PIN_COL2,GPIO_PIN_RESET);
  575.  
  576. // Read rows
  577. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW0))
  578. return '3';
  579. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW1))
  580. return '6';
  581. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW2))
  582. return '9';
  583. if (!HAL_GPIO_ReadPin(KEYPAD_GPIO_ROW, KEYPAD_PIN_ROW3))
  584. return '#';
  585. HAL_Delay(30);
  586. return KEYPAD_NO_PRESSED;
  587. }
  588.  
  589. char* concat(const char *s1, const char *s2)
  590. {
  591. char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
  592. strcpy(result, s1);
  593. strcat(result, s2);
  594. return result;
  595. }
  596. uint32_t getUSART1()
  597. {
  598.  
  599. uint32_t num=0;
  600.  
  601. num |= buffer_USART1_rx[0] << 24;
  602. num |= buffer_USART1_rx[1] << 16;
  603. num |= buffer_USART1_rx[2] << 8;
  604. num |= buffer_USART1_rx[3];
  605.  
  606. return num;
  607. }
  608. uint32_t getUSART2()
  609. {
  610.  
  611.  
  612. uint32_t num=0;
  613. num |= buffer_USART2_rx[0] << 24;
  614. num |= buffer_USART2_rx[1] << 16;
  615. num |= buffer_USART2_rx[2] << 8;
  616. num |= buffer_USART2_rx[3];
  617. return num;
  618. }
  619. void setUSART2(uint32_t value, bool mode)
  620. {
  621. //mode =TRUE->send RAW uint32, FALSE->send as STRING
  622.  
  623.  
  624. if (mode==true)
  625. {
  626. buffer_USART2_tx[0] = value ;
  627. buffer_USART2_tx[1] = value >>8;
  628. buffer_USART2_tx[2] = value >> 16;
  629. buffer_USART2_tx[3] = value >> 24;
  630.  
  631. HAL_UART_Transmit_IT(&huart2,buffer_USART2_tx,5);
  632. }
  633. else
  634. {
  635. sprintf(buffer_USART2_tx, "%" PRIu32 "\n",value);
  636. HAL_UART_Transmit_IT(&huart2,buffer_USART2_tx,5);
  637. }
  638.  
  639. }
  640. /* USER CODE END 4 */
  641.  
  642. /**
  643. * @brief This function is executed in case of error occurrence.
  644. * @param file: The file name as string.
  645. * @param line: The line in file as a number.
  646. * @retval None
  647. */
  648. void _Error_Handler(char *file, int line)
  649. {
  650. /* USER CODE BEGIN Error_Handler_Debug */
  651. /* User can add his own implementation to report the HAL error return state */
  652. while(1)
  653. {
  654. }
  655. /* USER CODE END Error_Handler_Debug */
  656. }
  657.  
  658. #ifdef USE_FULL_ASSERT
  659. /**
  660. * @brief Reports the name of the source file and the source line number
  661. * where the assert_param error has occurred.
  662. * @param file: pointer to the source file name
  663. * @param line: assert_param error line source number
  664. * @retval None
  665. */
  666. void assert_failed(uint8_t* file, uint32_t line)
  667. {
  668. /* USER CODE BEGIN 6 */
  669. /* User can add his own implementation to report the file name and line number,
  670. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  671. /* USER CODE END 6 */
  672. }
  673. #endif /* USE_FULL_ASSERT */
  674.  
  675. /**
  676. * @}
  677. */
  678.  
  679. /**
  680. * @}
  681. */
  682.  
  683. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement