📄 bsp_keyboard.c
字号:
#include <includes.h>
CPU_INT08U Key_Flag;
CPU_INT08U I2C_Read_Counter = 0;
CPU_INT08U I2C_Write_Counter = 0;
CPU_INT08U Keypad_Data_Read;
CPU_INT08U Column_Value = 0;
CPU_INT08U Row_Counter;
CPU_INT08U Row_Found = 0;
CPU_INT08U Row_Value;
CPU_INT08U Brightness_Global=8;
CPU_INT08U I2C_Data_Read_Buffer[2];
CPU_INT08U I2C_Data_Write_Buffer[2];
/*********************************************************************************************************
* EINT2_Handler()
*
* Description : External Interrupt 2 Handler (Handles Interrupt from the Keypad Controller (PCA9555)
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
void EINT2_Handler (void)
{
VICIntEnClear = (1 << VIC_EINT2);
EXTINT = 0x04; // clear interrupt
Key_Flag=1; // Set Keypad Flag when interrupt detected = key pressed
}
/*********************************************************************************************************
* EINT_Init()
*
* Description : External Interrupt 2 Initialization
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
void EINT_Init(void){
CPU_INT32U pinsel;
pinsel = PINSEL4; // EINT function B[25:24] = 01
pinsel &= 0xFDFFFFFF; // Reset B[25]
pinsel |= 0x01000000; // Set B[24]
PINSEL4 = pinsel;
Key_Flag =0;
EXTMODE = 0x04; // EINT2 is edge sensitive
EXTPOLAR = 0x00; // EINT2 is falling edge sensitive
VICIntSelect &= ~(1 << VIC_EINT2);
VICVectAddr16 = (CPU_INT32U)EINT2_Handler; // Set the vector address
VICIntEnable = (1 << VIC_EINT2); // Enable the timer interrupt source
EXTINT = 0x04;
}
/*********************************************************************************************************
* Keypad_Write()
*
* Description : Function to write to the Keypad Controller (PCA9555)
*
* Argument(s) : Address of the register to be accessed and data to write to that register
*
* Return(s) : none.
*********************************************************************************************************/
void Keypad_Write(CPU_INT08U Reg_Ptr, CPU_INT08U Data_Write)
{
I2C_Data_Write_Buffer[0] = Reg_Ptr;
I2C_Data_Write_Buffer[1] = Data_Write;
I2C_Write(PCA9555_ADDRESS,I2C_Data_Write_Buffer, 2); // Write Port1 to Data_Write
}
/*********************************************************************************************************
* Keypad_Read()
*
* Description : Function to read from the Keypad Controller (PCA9555)
*
* Argument(s) : Address of the register to be accessed and data to be read from that register
*
* Return(s) : none.
*********************************************************************************************************/
CPU_INT08U Keypad_Read(void)
{
I2C_ReadReg(PCA9555_ADDRESS, I2C_Data_Read_Buffer,1,0);
return I2C_Data_Read_Buffer[0];
}
/*********************************************************************************************************
* Keypad_Init()
*
* Description : Initialize the Keypad Controller (PCA9555)
* Registers to be written:
* 0x02: Program Port 0 (MSB = 0.7 - LSB = 0.0) ; 0: Port = 0 1: Port = 1
* 0x03: Program Port 1 (MSB = 1.7 - LSB = 1.0) ; 0: Port = 0 1: Port = 1
* 0x06: Configure Port 0 (MSB = 0.7 - LSB = 0.0) ; 0: Port = output 1: Port = input
* 0x07: Configure Port 1 (MSB = 1.7 - LSB = 1.0) ; 0: Port = output 1: Port = input
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
void Keypad_Init(void)
{
Keypad_Write(0x06, P0_7_LED_OUT_REG & P0_6_BACKLIGHT_OUT_REG); // Program output port 0
Keypad_Write(0x07, P1_7_6_LED_OUT_REG & P1_KPAD_ROW_OUT_REG); // Program output port 1
Keypad_Write(0x03,0xC0); // Program Port1 - LEDs off and Kpad Column = 0
}
/*********************************************************************************************************
* Decode_Column()
*
* Description : Decode the column of the pressed key
* Input Register has been read in CPU_INT08U KeypadCheck(void) in app_keyboard.c
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
void Decode_Column(void)
{
I2C_Read_Counter+=1;
switch (Keypad_Data_Read | P1_KPAD_COLUMN_MASK) // Check value of the PCA9555's Input Register and store Column value accordingly
{
case 0xFE: Column_Value = 0;
break;
case 0xFD: Column_Value = 1;
break;
case 0xFB: Column_Value = 2;
break;
case 0xF7: Column_Value = 3;
break;
case 0xEF: Column_Value = 4;
break;
case 0xDF: Column_Value = 5;
break;
default: break;
}
}
/*********************************************************************************************************
* Decode_Row()
*
* Description : Decode the row of the pressed key
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
void Decode_Row(void)
{
CPU_INT08U Keypad_Data_Read;
CPU_INT08U Keypad_Data_Read_Masked;
do // Perform the loop to check ALL the rows (0 to 5) - Write to the Output register accordingly
{
if (Row_Counter == 0)
{
Keypad_Write(0x03,0xFE);
}
if (Row_Counter == 1)
{
Keypad_Write(0x03,0xFC);
}
if (Row_Counter == 2)
{
Keypad_Write(0x03,0xF8);
}
if (Row_Counter == 3)
{
Keypad_Write(0x03,0xF0);
}
if (Row_Counter == 4)
{
Keypad_Write(0x03,0xE0);
}
if (Row_Counter == 5)
{
Keypad_Write(0x03,0xC0);
}
I2C_Write_Counter+=1;
Keypad_Data_Read = Keypad_Read(); // Read Input Register
I2C_Read_Counter+=1;
Keypad_Data_Read_Masked = Keypad_Data_Read | P1_KPAD_COLUMN_MASK;
if (Keypad_Data_Read_Masked != 0xFF) // Check if Read Data is different from 0xFF = Row has been found when no 0xFF
{
if (Row_Found == 0)
{
Row_Value = Row_Counter; // Store Row Value
Row_Found = 1;
}
}
Row_Counter+=1;
} while (Row_Counter <= KPAD_NB_ROW-1);
}
/*********************************************************************************************************
* Keypad_Map()
*
* Description : Map the Row and Column values found in Decode_Row() and Decode_Column() to a specific key
*
* Argument(s) : none.
*
* Return(s) : Pressed Key.
*********************************************************************************************************/
CPU_INT08U Keypad_Map(void)
{
CPU_INT08U lKey = 0;
if (Column_Value == 5)
{
if (Row_Value == 0) lKey = KEY_F4;
else if (Row_Value == 1) lKey = KEY_ARW_UP;
else if (Row_Value == 2) lKey = KEY_PLUS;
else if (Row_Value == 3) lKey = KEY_F3;
else if (Row_Value == 4) lKey = KEY_F2;
}
else if (Column_Value == 4)
{
if (Row_Value == 0) lKey = KEY_MENU;
else if (Row_Value == 1) lKey = KEY_ARW_DN;
else if (Row_Value == 3) lKey = KEY_ARW_RT;
else if (Row_Value == 4) lKey = KEY_OK;
else if (Row_Value == 5) lKey = KEY_ARW_LT;
}
else if (Column_Value == 3)
{
if (Row_Value == 0) lKey = KEY_ERROR;
else if (Row_Value == 1) lKey = KEY_3;
else if (Row_Value == 2) lKey = KEY_2;
else if (Row_Value == 3) lKey = KEY_1;
else if (Row_Value == 5) lKey = KEY_SEND;
}
else if (Column_Value == 2)
{
if (Row_Value == 0) lKey = KEY_ERROR;
else if (Row_Value == 1) lKey = KEY_ERROR;
else if (Row_Value == 2) lKey = KEY_7;
else if (Row_Value == 3) lKey = KEY_6;
else if (Row_Value == 4) lKey = KEY_5;
else if (Row_Value == 5) lKey = KEY_4;
}
else if (Column_Value == 1)
{
if (Row_Value == 0) lKey = KEY_ERROR;
else if (Row_Value == 2) lKey = KEY_0;
else if (Row_Value == 4) lKey = KEY_9;
else if (Row_Value == 5) lKey = KEY_8;
}
else if (Column_Value == 0)
{
lKey = KEY_END;
}
return lKey;
}
/*********************************************************************************************************
* Keypad_Decoder()
*
* Description : Keypad_Decoder Function
* 1) Decode the column
* 2) Decode the row
* 3) Map the coordinate to a key
*
* Argument(s) : none.
*
* Return(s) : Pressed Key.
*********************************************************************************************************/
CPU_INT08U Keypad_Decoder (void)
{
Decode_Column();
Decode_Row();
return Keypad_Map();
}
/*********************************************************************************************************
* Green_LED_On_Off()
*
* Description : Switch on/off the Green LED in the Keypad
* 1) When a key is pressed (F4 in the Hands-on), Green LED goes on
* 2) When the same key is pressed again, Green LED goes off
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************/
#define PROGRAM_KEYPAD_GREEN_LED_ON 0x00 // Exercise 2: actual value to be found
#define PROGRAM_KEYPAD_GREEN_LED_OFF 0x00 // Exercise 2: actual value to be found
CPU_INT08U F4_Flag = 0;
void Green_LED_On_Off (void)
{
switch (F4_Flag)
{
case 0: // Add Code here - Exercise 2
break;
case 1: // Add Code here - Exercise 2
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -