//IAR 7.1
#include "stm32_key.h"
uint8_t key_value = 0xff;
uint8_t key_index = 0xff;
uint8_t key_row_index = 0xff;
const uint16_t ROW_PIN[ROW_COUNT] = {BUTTON_ROW_ONE_PIN,
BUTTON_ROW_TWO_PIN,
BUTTON_ROW_THR_PIN,
BUTTON_ROW_FOR_PIN,
BUTTON_ROW_FIV_PIN};
const uint16_t COLUMN_PIN[COLUMN_COUNT] = {BUTTON_COLUMN_ONE_PIN,
BUTTON_COLUMN_TWO_PIN,
BUTTON_COLUMN_THR_PIN,
BUTTON_COLUMN_FOR_PIN,
BUTTON_COLUMN_FIV_PIN};
const uint16_t ROW_EXTI_PIN_SOURCE[ROW_COUNT] = {ROW_ONE_EXTI_PIN_SOURCE,
ROW_TWO_EXTI_PIN_SOURCE,
ROW_THR_EXTI_PIN_SOURCE,
ROW_FOR_EXTI_PIN_SOURCE,
ROW_FIV_EXTI_PIN_SOURCE};
const uint16_t ROW_EXIT_LINE[ROW_COUNT] = {ROW_ONE_EXTI_LINE,
ROW_TWO_EXTI_LINE,
ROW_THR_EXTI_LINE,
ROW_FOR_EXTI_LINE,
ROW_FIV_EXTI_LINE};
static void stm32_keyrow_init(void)
{
uint8_t row_index;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the BUTTON_ROW Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Configure Button row pin as input */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = BUTTON_ROW_ONE_PIN | BUTTON_ROW_TWO_PIN |
BUTTON_ROW_THR_PIN | BUTTON_ROW_FOR_PIN |
BUTTON_ROW_FIV_PIN;
GPIO_Init(BUTTON_ROW_GPIO_PORT, &GPIO_InitStructure);
for(row_index = 0; row_index < ROW_COUNT; row_index++)
{
/* Connect Button EXTI Line to Button GPIO Pin */
GPIO_EXTILineConfig(BUTTON_ROW_EXTI_PORT_SOURCE, ROW_EXTI_PIN_SOURCE[row_index]);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = ROW_EXIT_LINE[row_index];
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
if(row_index == 0)
{
//NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
}
else if(row_index == 1)
{
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
}
else
{
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
}
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
}
static void stm32_keycolumn_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the BUTTON_COLUMN Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure Button row pin as output */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = BUTTON_COLUMN_ONE_PIN | BUTTON_COLUMN_TWO_PIN |
BUTTON_COLUMN_THR_PIN | BUTTON_COLUMN_FOR_PIN |
BUTTON_COLUMN_FIV_PIN;
GPIO_Init(BUTTON_COLUMN_GPIO_PORT, &GPIO_InitStructure);
}
static void stm32_keycolumn_set(uint8_t column_x)
{
BUTTON_COLUMN_GPIO_PORT->BSRR = COLUMN_PIN[column_x];
}
static void stm32_keycolumn_setall(void)
{
BUTTON_COLUMN_GPIO_PORT->BSRR = (GPIO_Pin_0 | GPIO_Pin_1 |
GPIO_Pin_2 | GPIO_Pin_10 |
GPIO_Pin_11);
}
static void stm32_keycolumn_clr(uint8_t column_x)
{
BUTTON_COLUMN_GPIO_PORT->BRR = COLUMN_PIN[column_x];
}
static void stm32_keycolumn_clrall(void)
{
BUTTON_COLUMN_GPIO_PORT->BRR = (GPIO_Pin_0 | GPIO_Pin_1 |
GPIO_Pin_2 | GPIO_Pin_10 |
GPIO_Pin_11);
}
static void stm32_keycolumn_scan(ButtonRow_TypeDef row_x)
{
uint8_t index;
if(press_ture==1)
{
/*Set all column pin to 1*/
stm32_keycolumn_setall();
Delay_1_Ms(10);
/*Scan all the column pins*/
for(index = 0; index < COLUMN_COUNT; index++)
{
stm32_keycolumn_clr(index);
Delay_1_Ms(15);
if(stm32_row_getstate(row_x) == RESET)
{
Delay_1_Ms(15);
if(stm32_row_getstate(row_x) == RESET)
{
key_value = (row_x * 5) + index;
press_ture=0;
stm32_keycolumn_clrall();
return;
}
else
{
stm32_keycolumn_set(index);
}
}
else
{
stm32_keycolumn_set(index);
//stm32_keycolumn_setall();
}
}
stm32_keycolumn_clrall();
}
}
void stm32_key_init(void)
{
stm32_keyrow_init();
stm32_keycolumn_init();
/*Set all columns output 0*/
stm32_keycolumn_clrall();
}
uint8_t stm32_row_getstate(ButtonRow_TypeDef row_x)
{
return GPIO_ReadInputDataBit(BUTTON_ROW_GPIO_PORT, ROW_PIN[row_x]);
}
void stm32_row_exti_disable(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannel =EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);
}
void stm32_row_exti_Enable(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannel =EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Check_Hand(ButtonRow_TypeDef Row)
{
Delay_1_Ms(50);
while(!stm32_row_getstate(Row))
{
Delay_1_Ms(10);
no_press_count=0;
while(!stm32_row_getstate(Row))
{
stm32_wdg_feed();
no_press_count=0;
}
}
}
void stm32_clear_allClearITPendingBit(void)
{
//EXTI_ClearITPendingBit(EXTI_Line2);
EXTI_ClearITPendingBit(EXTI_Line10);
EXTI_ClearITPendingBit(EXTI_Line4);
EXTI_ClearITPendingBit(EXTI_Line5);
EXTI_ClearITPendingBit(EXTI_Line6);
EXTI_ClearITPendingBit(EXTI_Line7);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_ClearITPendingBit(EXTI_Line9);
}
/*
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
//if(press_ture==1)
//{
Scan all the column pins
stm32_keycolumn_scan(BUTTON_ROW_ONE);
stm32_clear_allClearITPendingBit();
//}
}
}
*/
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line10) != RESET)
{
//if(press_ture==1)
//{
/*Scan all the column pins*/
stm32_keycolumn_scan(BUTTON_ROW_ONE);
stm32_clear_allClearITPendingBit();
//}
}
}
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{
//if(press_ture==1)
//{
/*Scan all the column pins*/
stm32_keycolumn_scan(BUTTON_ROW_TWO);
stm32_clear_allClearITPendingBit();
//}
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) != RESET)
{
//if(press_ture==1)
//{
/*Scan all the column pins*/
stm32_keycolumn_scan(BUTTON_ROW_THR);
stm32_clear_allClearITPendingBit();
// }
}
else if(EXTI_GetITStatus(EXTI_Line6) != RESET)
{
//if(press_ture==1)
//{
/*Scan all the column pins*/
stm32_keycolumn_scan(BUTTON_ROW_FOR);
stm32_clear_allClearITPendingBit();
//}
}
else if(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
//if(press_ture==1)
//{
/*Scan all the column pins*/
stm32_keycolumn_scan(BUTTON_ROW_FIV);
stm32_clear_allClearITPendingBit();
//}
}
EXTI_ClearITPendingBit(EXTI_Line18);
}