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 posts the ATMega644P TWI interface with the DS13017 RTC and MCP23017 GPIO expansion chip. Here I will put them altogethers. This TWI bus will drives the DS1307 RTC and the MCP23017 that controls a character LCD.
The master microcontroller read the RTC data from ds1307 and it will send to the MCP23017 based LCD.
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 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.