一、實驗目的與要求
1、熟悉GPIO的使用方法,熟悉HAL固件庫中GPIO庫函數的使用方法
2、掌握矩陣鍵盤的掃描方法
3、掌握動態掃描數碼塊的方法
二、實驗設備
SUN ESMC01實驗儀(含STM32F407模塊)一套、ARM仿真器一套、PC機一臺
三、實驗內容
1、編寫程序:掃描鍵盤,如有按鍵,鍵號顯示于數碼管。
2、連接線路,驗證實驗,掌握數碼管、矩陣鍵盤的掃描方法。
intcount0,count1,count2,a;
const uint8_tSegArray[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void DIR(void);
uint8_t keyi(void);
HAL_UART_Receive_IT(&huart1, (uint8_t *)RxBuffer,4);//啟動串口
while (1)
{
int i;
for (i = 0; i < 8; i++)
RxBuffer[ i] = keyi();uint8_t keyi()
[ i]
{
uint8_t i,j;
GPIOA->ODR = (GPIOA->ODR & 0x00ff); //全"0"->掃描口
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11)&& HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11)) //讀鍵狀態
a=0;
else
a=1;
while (1)
{
if (a == 0) //調用判有無閉合鍵子程序
{
DIR();
continue;
}
i = 0xfe;
j = 0;
while(i != 0xff)
{
GPIOA->ODR = (GPIOA->ODR & 0x00ff)| (i << 8); //鍵掃數據->KR_DIG_GPIO_Port PB15..8
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11) ==0)
{ //0行有鍵閉合
break;
}
else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12)== 0)
{//1行有鍵閉合
j += 8;
break;
}
j++;//列計數器加1
i = ((i<<1) | 1);
}
if (i == 0xff) //完成一次掃描,沒有鍵按下
continue;
do
{
DIR();
}while(a != 0); //判斷釋放否
return j; //鍵號
}
}
數碼管
while(1)
{
uint8_ti = 0xfe;
uint8_t*pBuffer = RxBuffer;
while(i!= 0xff)
{
GPIOB->ODR |= 0xffff;
GPIOC->ODR |= 0xffff;
GPIOB->ODR = (GPIOB->ODR & 0xff00)| SegArray[*pBuffer++];
GPIOC->ODR = (GPIOC->ODR & 0xff00)| i ;
HAL_Delay(1); / /延遲1ms
i =((i << 1) | 0x1);
}
}
|