利用外部IO口中斷編寫程序,識別4*4矩陣鍵盤中哪個按鍵被按下。
優點:快速
本程序使用stm32f103zet6編寫,程序中沒有進行手動延時,但經實測,識別效果出錯率很小。
stm32單片機源程序:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include "lcd.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- SRAM_HandleTypeDef hsram1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_FSMC_Init(void);
- static void MX_NVIC_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- // HAL_Delay(10);
- // LCD_ShowString(30,60,200,16,16,(uint8_t*)"OK");
- //檢測第一排
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0)
- {if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"E");
- }
-
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"I");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"M");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"A");
- }
- }
- }
- //檢測第二排
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)==0)
- {if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"B");
- }
-
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"F");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"J");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"N");
- }
- }
- }
- //檢測第三排
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2)==0)
- {if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"C");
- }
-
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"G");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"K");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"O");
- }
- }
- }
- //檢測第四排
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3)==0)
- {if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"D");
- }
-
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"H");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"L");
- }
- }
- //
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1)==0)
- {
- LCD_Clear(WHITE);
- LCD_ShowString(30,30,200,16,16,(uint8_t*)"P");
- }
- }
- }
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_FSMC_Init();
- /* Initialize interrupts */
- MX_NVIC_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init();
- POINT_COLOR=BLUE;
- ////檢測第一排按鍵
- //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,0);
- //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1);
- //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,1);
- //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,0);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
-
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,0);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
-
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,0);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
-
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,1);
- HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,0);
-
-
-
-
-
-
- // HAL_Delay(500);
- // LCD_ShowString(30,30,200,16,16,(uint8_t*)"A");
- //
- // //檢測第一排
- // if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==0)
- // {
- //
- //if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==0)
- //{
- // if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==0)
- // {
- // LCD_Clear(WHITE);
- // LCD_ShowString(30,30,200,16,16,(uint8_t*)"A");
- // }
- //}
- //if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- //{
- // if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5)==0)
- // {
- // LCD_Clear(WHITE);
- // LCD_ShowString(30,30,200,16,16,(uint8_t*)"E");
- // }}
- //
- //if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- //{
- // if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==0)
- // {
- // LCD_Clear(WHITE);
- // LCD_ShowString(30,30,200,16,16,(uint8_t*)"I");
- // }
- //}
- //if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- //{
- // if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
- // {
- // LCD_Clear(WHITE);
- // LCD_ShowString(30,30,200,16,16,(uint8_t*)"M");
- // }
- //}
- //
- // }
- //
- // //檢測第二排
- //
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /**
- * @brief NVIC Configuration.
- * @retval None
- */
- static void MX_NVIC_Init(void)
- {
- /* EXTI9_5_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
- /* EXTI1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI1_IRQn);
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOG_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
- /*Configure GPIO pin : PC1 */
- GPIO_InitStruct.Pin = GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pins : PA0 PA1 PA2 PA3 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : two_Pin three_Pin four_Pin */
- GPIO_InitStruct.Pin = two_Pin|three_Pin|four_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : PB0 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* FSMC initialization function */
- static void MX_FSMC_Init(void)
- {
- FSMC_NORSRAM_TimingTypeDef Timing;
- /** Perform the SRAM1 memory initialization sequence
- */
- hsram1.Instance = FSMC_NORSRAM_DEVICE;
- hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
- /* hsram1.Init */
- hsram1.Init.NSBank = FSMC_NORSRAM_BANK4;
- hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
- hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
- hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
- hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
- hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
- hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
- hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
- hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
- hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
- hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
- hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
- hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
- /* Timing */
- Timing.AddressSetupTime = 15;
- Timing.AddressHoldTime = 15;
- Timing.DataSetupTime = 255;
- Timing.BusTurnAroundDuration = 15;
- Timing.CLKDivision = 16;
- Timing.DataLatency = 17;
- Timing.AccessMode = FSMC_ACCESS_MODE_A;
- /* ExtTiming */
- if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /** Disconnect NADV
- */
- __HAL_AFIO_FSMCNADV_DISCONNECTED();
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼
所有資料51hei提供下載:
代碼.7z
(2.91 MB, 下載次數: 74)
2022-12-16 04:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|