Sunday, January 21, 2024

PIC16F887 MCP23017 Key Pad and 7-Segment Display Example using XC8

In previous post, I introduce about using the MCP23017 16-bit I2C I/O expander with PIC16F887. This chip can be interfaced with various types of I/O device. In this example, I a PIC16F887 micro-controller command this I2C chip to scan and find key-press from a 4x4 matrix keypad. Key present will show on a single common cathode seven-segment display.

PIC16F887 MCP23017 Key Pad and 7-Segment Display Example using XC8
Simulating Program in Proteus

The I2C operates at 400kHz serial clock frequency. Lower nibber of GPB is configured as digital output while the higher nibble is configured as digital input detecting key-press. GPA is configured as digital output, driving a 7-Segment display.

PIC16F887 MCP23017 Key Pad and 7-Segment Display Example using XC8
Two 4x4 matrix keypad for Arduino

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on January 21, 2024, 7:12 PM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "config.h"
  10. #include "mcp23017.h"
  11.  
  12. const uint8_t d_7[]={0x3F,0x06,0x5B,0x4F,
  13. 0x66,0x6D,0x7D,0x07,
  14. 0x7F,0x6F,0x77,0x7C,
  15. 0x39,0x5E,0x79,0x71};
  16. const uint8_t key_16[][4]={7,8,9,15,
  17. 4,5,6,14,
  18. 1,2,3,13,
  19. 10,0,11,12};
  20.  
  21. uint8_t keyScan(void){
  22. uint8_t data,temp;
  23. for(uint8_t i=0;i<4;i++){
  24. data=1<<i;
  25. mcp23017_write(OLATB,data);
  26. __delay_ms(10);
  27. data=mcp23017_read(GPIOB);
  28. data>>=4;
  29. if(data==0x01) {temp=key_16[i][0]; break;}
  30. else if(data==0x02) {temp=key_16[i][1]; break;}
  31. else if(data==0x04) {temp=key_16[i][2]; break;}
  32. else if(data==0x08) {temp=key_16[i][3]; break;}
  33. else temp=0xFF;
  34. __delay_ms(10);
  35. }
  36. return temp;
  37. }
  38. void main(void) {
  39. OSCCONbits.IRCF=7;
  40. __delay_ms(100);
  41. mcp23017_init();
  42. uint8_t temp=0xFF;
  43. while(1){
  44. temp=keyScan();
  45. if(temp!=0xFF){
  46. mcp23017_write(OLATA,d_7[temp]);
  47. __delay_ms(100);
  48. }
  49. }
  50. return;
  51. }
  52.  

When key-press is found the micro-controller sends a seven-segment data to GPA of the MCP23017, and it will wait for 100 Milli seconds. A key value of 0xFF is ignored.

Click here to download this example. If you prefer another I2C device, you can choose the PCF8574AP that's described in this post.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)