1 原理圖
按鍵同I/O的映射關係:
B1~B4
—PA0、PA8、PB1、PB2
2 部分源碼
實驗:實現LCD第四行0-10隨按鍵滾動。
主循環掃描實現
main.c
:
#include "HeadFile.h"
u8 KeyCount = 0;
u8 KeySta[4] = {
1, 1, 1, 1
};
u8 KeyMap[5] = {
'A', 'B', 'C', 'D'
};
void KeyInit(){
GPIO_InitTypeDef GPIOInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
GPIOInitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
GPIOInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIOInitStructure);
GPIOInitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIOInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIOInitStructure);
}
void KeyScan(){
u8 index = 0;
static u8 KeyBuffer[4] = {
0xFF, 0xFF, 0xFF, 0xFF
};
KeyBuffer[0] = (KeyBuffer[0] << 1) | K1;
KeyBuffer[1] = (KeyBuffer[1] << 1) | K2;
KeyBuffer[2] = (KeyBuffer[2] << 1) | K3;
KeyBuffer[3] = (KeyBuffer[3] << 1) | K4;
for(index = 0; index < 4; index ++){
if(KeyBuffer[index] == 0xFF ){
KeySta[index] = 1;
}else if(KeyBuffer[index] == 0x00){
KeySta[index] = 0;
}
}
}
void KeyDriver(){
u8 index = 0;
static u8 KeyBackup[4] = {
1, 1, 1, 1
};
for(index = 0; index < 4; index ++){
if(KeySta[index] != KeyBackup[index] ){
KeyBackup[index] = KeySta[index];
if(KeySta[index] == 0){
KeyAction(KeyMap[index]);
}
}
}
}
void KeyAction(u8 CMD){
u8 Display;
if(CMD == 'A'){
KeyCount ++;
}else if(CMD == 'B'){
KeyCount += 2;
}else if(CMD == 'C'){
KeyCount += 3;
}else if(CMD == 'D'){
KeyCount += 4;
}
if(KeyCount >= 10){
KeyCount -= 10;
}
Display = '0'+KeyCount;
LCD_DisplayStringLine(Line4,&(Display));
}
外部中斷實現
外部中斷配置過程:
1、初始化IO口(設置輸入狀態和時鐘)
2、設置IO口和中斷線的映射關係
3、設置中斷觸發方式等
4、配置中斷分組
5、編寫中斷服務函數
6、清除中斷標誌位
Tips:中斷服務函數格式:
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3)!=RESET)//判斷某個線上的中斷是否發生
{
中斷邏輯…
EXTI_ClearITPendingBit(EXTI_Line3); //清除 LINE 上的中斷標誌位
}
}
#include "Headfile.h"
u8 Display;
u8 KeyCount = 0;
void EXTIInit(){
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//1、初始化IO口(設置輸入狀態和時鐘)
KeyInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能複用IO
//KEY1
//2、設置IO口和中斷線的映射關係
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
//3、設置中斷觸發方式等
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//4、配置中斷分組
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//KEY2
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//KEY3
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//KEY4
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//5、編寫中斷服務函數
void EXTI0_IRQHandler(void){
if(EXTI_GetITStatus(EXTI_Line0)!=RESET){
KeyCount ++;
if(KeyCount >= 10){
KeyCount -= 10;
}
Display = '0'+KeyCount;
LCD_DisplayStringLine(Line4,&(Display));
//6、清除中斷標誌位
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI9_5_IRQHandler(void){
if(EXTI_GetITStatus(EXTI_Line8)!=RESET){
KeyCount +=2;
if(KeyCount >= 10){
KeyCount -= 10;
}
Display = '0'+KeyCount;
LCD_DisplayStringLine(Line4,&(Display));
EXTI_ClearITPendingBit(EXTI_Line8);
}
}
void EXTI1_IRQHandler(void){
if(EXTI_GetITStatus(EXTI_Line1)!=RESET){
KeyCount +=3;
if(KeyCount >= 10){
KeyCount -= 10;
}
Display = '0'+KeyCount;
LCD_DisplayStringLine(Line4,&(Display));
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void){
if(EXTI_GetITStatus(EXTI_Line2)!=RESET){
KeyCount +=4;
if(KeyCount >= 10){
KeyCount -= 10;
}
Display = '0'+KeyCount;
LCD_DisplayStringLine(Line4,&(Display));
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
注意:外部中斷中不可以使用滴答定時器的延時函數不然會死機!!!