Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
In previous post I use these two SPI chips, SN74HC595 and SN74HC165 to expand the inputs/outputs of an MCU. Here I use these two chip pair to make a matrix keypad. These two 8-bit I/O expansion chip can build up to a 8x8 matrix keypad that yield 64 different keys. But I make only a 4x8 matrix keypad since it's not important.
Simulating Program
The 8-bit output port of the SN74HC595 is a column scanner while the lower nibble input port of the SN74HC165 is a row detector (active high). Whenever any key press found the program assign a specific key in its keypad array.
I separate these two chip using different Slave Select pins. The SN74HC165 doesn't need serial data input. It only need serial clock and Slave Select pins controlled by the MCU. However I think we can cascade this two chip using a shared SPI pins. If I have more time I will test it using this method.
The HD44780 based character LCD can be controlled by a micro-controller, digital circuit, an I/O expansion chip or event by hands. In this example I use an output port of the MCP23017 to control a 16x2 LCD in one direction (write only) in 4-bit data transfer mode.
The LCD connect to GPIOB (output register OLATB). That's,
I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost,
fast processing time for only 24 hours, and fast delivery time using
any carrier options. This double side 10cmx10cm can be fabricate at only
5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.
The MCP23017 can be use as input or output depends on its register configurations. A 4x4 matrix keypad requires only one 8-bit I/O port of its master controller. So I use one general I/O port the GPA of the MCP23017 to scan a 4x4 matrix keypad. Founded key will show on a16x2 character LCD.
The LCD is already place on board. The MCP23017 interfaces to the ATMega644P using only two TWI wires. But it need to place and wire on a breadboard module with the keypad.
I use a remembrance 4x4 keypad module that's ready to connect to GPA.
We need to add the "twi.c" to project before the twi function can be called.
"twi.c"
/*
* twi.c
*
* Created: 2/4/2026 10:22:02 AM
* Author: Admin
*/
#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
voidtwi_init(void){
TWSR|=0x00; //Prescaler Selection Bit
TWBR=0x0F; //Baud Rate Generator
TWCR=(1<<TWEN); //Enable The TWI Module
PORTC|=(1<<0);
PORTC|=(1<<1);
}
voidtwi_start(void){
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
}
voidtwi_write(unsignedchar data){
TWDR=data;
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
}
unsignedchartwi_read(char ACK){
if(ACK==0)
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
else
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
return TWDR;
}
voidtwi_stop(){
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
_delay_us(10);
}
Schematic
Proteus Circuit and Simulation
Keypad Scanning
ATMega644P AVR Board Experiment
AVR Prototype Board Experiment
AVR Prototype Board Experiment
This PCB was offered by PCBWay (pcbway.com).
I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost,
fast processing time for only 24 hours, and fast delivery time using
any carrier options. This double side 10cmx10cm can be fabricate at only
5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.