/** * @brief Check to see if a key is pressed, and if one is, which. * * @notes Check to see if a key is pressed. If one is, determine which it s. * @retval The key that is pressed. */ uint8_t KEYPAD_Check(void){ uint8_t pressed_button = 0; GPIO_InitTypeDef GPIO_InitStructure; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_ROW_4)) pressed_button = 4; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_ROW_3)) pressed_button = 3; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_ROW_2)) pressed_button = 2; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_ROW_1)) pressed_button = 1; GPIO_InitStructure.GPIO_Pin = KEYPAD_ROW_4 | KEYPAD_ROW_1 | KEYPAD_ROW_2 | KEYPAD_ROW_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_Init(KEYPAD_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = KEYPAD_COL_1 | KEYPAD_COL_2 | KEYPAD_COL_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_Init(KEYPAD_GPIO, &GPIO_InitStructure); if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_COL_1)) pressed_button += 10; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_COL_2)) pressed_button += 20; if(!GPIO_ReadInputDataBit(KEYPAD_GPIO, KEYPAD_COL_3)) pressed_button += 30; KEYPAD_Init(); switch(pressed_button){ case 11: return 1; case 12: return 4; case 13: return 7; case 14: return 14; case 21: return 2; case 22: return 5; case 23: return 8; case 24: return 0; case 31: return 3; case 32: return 6; case 33: return 9; case 34: return 34; } return 99; }
/***************************************************KEYPAD ***************************************** Displays keys pressed on 4x4 keypad on the uart *****************************************************TEST*******************************************/ void keypad_test() { uint8_t key; UART_Printf("\n\rConnections: Port 2"); UART_Printf("\n\rMake connections and hit 'k' to test!"); while(UART_RxChar()!='k'); KEYPAD_Init(); while(1) { key = KEYPAD_GetKey(); UART_Printf("\n\r Key:%c", key); } }
int main() { uint8_t key; SystemInit(); UART0_Init(9600); // Initialize UART0 at 9600 baud rate KEYPAD_Init(P2_0,P2_1,P2_2,P2_3,P2_4,P2_5,P2_6,P2_7); // Keypad is connected to P2_0 - P2_7 while (1) { key = KEYPAD_GetKey(); // Read the Ascii value of Key UART0_Printf("\n\r Key:%c", key); // Transmit the key pressed on UART } }
/***************************************************KEYPAD ***************************************** Displays keys pressed on 4x4 keypad on the uart *****************************************************TEST*******************************************/ void keypad_test() { uint8_t key; UART_Printf("\n\rConnections: PORTD"); UART_Printf("\n\rMake connections and hit 'k' to test!"); while(UART_RxChar()!='k'); KEYPAD_Init(PD_0,PD_1,PD_2,PD_3,PD_4,PD_5,PD_6,PD_7); while(1) { key = KEYPAD_GetKey(); UART_Printf("\n\r Key:%c", key); } }
/* Program to demonstrate the hex-Keypad interface*/ int main() { uint8_t key; LCD_Init(8,2,16); /*Initialize the 2x16 LCD in 8-bit mode */ KEYPAD_Init(); /*Configure the ROWs and COLUMNs for keypad scanning*/ while(1) { key = KEYPAD_GetKey(); /*Get the Ascii value of the key Pressed */ LCD_DisplayChar(key); /*Display the key pressed */ } return 0; }
void sMCIfaceKEYPAD_init() { KEYPAD_Init(); }
int main(void) { TRIS_KEY1 = 1; TRIS_KEY2 = 1; TRIS_LED1 = 0; TRIS_LED2 = 0; LAT_LED1 = 0; LAT_LED2 = 0; /* External Interrupts Configuration */ INTCONbits.INT0E = 1; /* Enable Interrupt 0 (RB0 as interrupt) */ INTCON2bits.INTEDG0 = 1; /* Cause Interrupt 0 at rising edge */ INTCONbits.INT0F = 0; /* Clear Interrupt 0 flag */ INTCON3bits.INT1E = 1; /* Enable Interrupt 1 (RB1 as interrupt) */ INTCON2bits.INTEDG1 = 1; /* Cause Interrupt 1 at rising edge */ INTCON3bits.INT1F = 0; /* Clear Interrupt 0 flag */ ei(); /* Global Interrupt Enable */ /* Initialize LCD 16 cols x 2 rows */ HD44780_Init(16, 2); HD44780_Puts(3, 0, "PIC18F4520"); HD44780_Puts(0, 1, "16x2 HD44780 LCD"); Delayms(2000); SSEG_Init(); KEYPAD_Init(); OpenADC(ADC_FOSC_4 & ADC_RIGHT_JUST & ADC_0_TAD, ADC_CH0 & ADC_REF_VDD_VSS & ADC_INT_OFF, ADC_1ANA); Delay(50); /* Loop forever */ while(1) { if (state == STATE_LCD) { counter = 0; lcd_flag = 0; while(lcd_flag == 0) { HD44780_Clear(); Delayms(250); HD44780_Clear(); HD44780_Puts(0, 0, "1.LCD TEST"); HD44780_Puts(0, 1, "<<ADC LED>>"); Delayms(250); counter++; if(counter > 3) {counter = 0; break;} } HD44780_Clear(); HD44780_Puts(0, 0, "1.LCD TEST"); HD44780_Puts(0, 1, "<<ADC LED>>"); while(lcd_flag == 0){ HD44780_Clear(); HD44780_Puts(0, 0, "PRESS KEY1 FOR"); HD44780_Puts(0, 1, "LEFT"); Delayms(500); HD44780_Clear(); HD44780_Puts(0, 0, "PRESS KEY2 FOR"); HD44780_Puts(0, 1, "RIGHT"); Delayms(500); counter++; if(counter > 3) {counter = 0; break;} } } else if (state == STATE_LED) { counter = 0; led_flag = 0; while(led_flag == 0) { HD44780_Clear(); Delayms(250); HD44780_Clear(); HD44780_Puts(0, 0, "2.LED TEST"); HD44780_Puts(0, 1, "<<LCD SSEG>>"); Delayms(250); counter++; if(counter > 3) {counter = 0; break;} } HD44780_Clear(); HD44780_Puts(0, 0, "2.LED TEST"); HD44780_Puts(0, 1, "<<LCD SSEG>>"); while(led_flag == 0) { LAT_LED1 = 1; LAT_LED2 = 0; Delayms(50); LAT_LED1 = 0; LAT_LED2 = 1; Delayms(50); } LAT_LED2 = 0; } else if (state == STATE_SSEG) { counter = 0; sseg_flag = 0; while(sseg_flag == 0) { HD44780_Clear(); Delayms(250); HD44780_Clear(); HD44780_Puts(0, 0, "3.SSEG TEST"); HD44780_Puts(0, 1, "<<LED KEYPAD>>"); Delayms(250); counter++; if(counter > 3) {counter = 0; break;} } HD44780_Clear(); HD44780_Puts(0, 0, "3.SSEG TEST"); HD44780_Puts(0, 1, "<<LED KEYPAD>>"); while(sseg_flag == 0) { SSEG_Print(1,sseg_counter%10); Delayms(10); SSEG_Print(2,sseg_counter/10); Delayms(10); sseg_counter++; if(sseg_counter > 99) sseg_counter = 0; } LATCbits.LATC0 = 0; LATCbits.LATC5 = 0; } else if (state == STATE_KEYPAD) { counter = 0; keypad_flag = 0; while(keypad_flag == 0) { HD44780_Clear(); Delayms(250); HD44780_Clear(); HD44780_Puts(0, 0, "4.KEYPAD TEST"); HD44780_Puts(0, 1, "<<SSEG ADC>>"); Delayms(250); counter++; if(counter > 3) {counter = 0; break;} } HD44780_Clear(); HD44780_Puts(0, 0, "4.KEYPAD TEST"); HD44780_Puts(0, 1, "<<SSEG ADC>>"); while(keypad_flag == 0) { int_key = KEYPAD_Read(); char_key = (char)KEYPAD_KeytoASCII(int_key); HD44780_CursorSet(0,0); if(int_key != 0xFF) printf("4.KEYPAD TEST: %c", char_key); Delayms(50); } } else if (state == STATE_ADC) { counter = 0; adc_flag = 0; while(adc_flag == 0) { HD44780_Clear(); Delayms(250); HD44780_Clear(); HD44780_Puts(0, 0, "5.ADC TEST"); HD44780_Puts(0, 1, "<<KEYPAD LCD>>"); Delayms(250); counter++; if(counter > 3) {counter = 0; break;} } HD44780_Clear(); HD44780_Puts(0, 0, "5.ADC TEST"); HD44780_Puts(0, 1, "<<KEYPAD LCD>>"); while(adc_flag == 0){ ConvertADC(); while( BusyADC() ); adcin = 1023 - ReadADC(); dcf_old = dcf; dcf = (1-lamda)*dcf + lamda*adcin; if(dcf != dcf_old) { HD44780_CursorSet(0,0); printf("5.ADC TEST: %4d", (int)dcf); } } } else state = STATE_LCD; } }