Summary of Matrix scankey 3×4 for Lcd 2 line For CSS Compiler
This article provides C source code for a PIC16F877-based keypad-driven electric power display using a 4-bit LCD interface. It initializes MCU peripherals, scans a 3x4 keypad, collects numeric inputs into a buffer, converts values to display digits, and shows them on a two-line LCD. Functions include initialization, keypad scan/display, simple hex-to-BCD digit extraction, and a calculate routine handling input storage and a decrement loop for key 11.
Parts used in the Electric Power Display Project:
- PIC16F877 microcontroller
- 4-bit LCD module (included via lcd_4bit.c)
- 3x4 keypad (handled via get_key3x4.c)
- Resistors for pull-ups on keypad rows/columns
- PCB or breadboard
- Power supply (4 MHz clock referenced, Vcc appropriate for PIC)
- Connecting wires
#include <16F877.h>
#device adc=8
#FUSES NOWDT ,XT
#use delay(clock=4000000)
char const s[10]={'0','1','2','3','4','5','6','7','8','9'};
void init_mcu (void);
void display (void);
void display1 (void);
void calculate (void);
void hex_bcd2(int8 k);
/*-------define keypad------------*/
#define row0 pin_b0 // input
#define row1 pin_b1 // input
#define row2 pin_b2 // input
#define row3 pin_b3 // input
#define col0 pin_b4 // output
#define col1 pin_b5 // output
#define col2 pin_b6 // output
/*------set variable -----------*/
int8 digit=0;
int16 value1,value2;
int8 set[34];
int8 i=0,k=0;
#include "lcd_4bit.c"
#include "get_key3x4.c"
void main(void){
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_B(0x0f);
set_tris_D(0x00);
output_high(col0);
output_high(col1);
output_high(col2);
output_high(row0);
output_high(row1);
output_high(row2);
output_high(row3);
value1 = 0;
value2 = 0;
i = 0;
set[0]=0;
lcd_init();
init_mcu();
delay_ms(2000);
lcd_clear();
while(true){
scan_key();
display();
}
}
void init_mcu (void){
lcd_row_col(1,0);
lcd_putc("Electric Power ");
lcd_row_col(2,0);
lcd_putc("Kchimicro.PIC ");
}
void hex_bcd2(int8 k){
k%=10;
a=0;b=0;
b=k%10;
b=k;
}
void hex_bcd1(int16 k){
a=0; b=0;c=0;d=0;e=0;
while(k>=10000){
a++;
k -= 10000;}
while(k>=1000){
b++;
k -= 1000;}
while(k>=100){
c++;
k -= 100;}
while(k>=10){
d++;
k-=10;}
e=k;
}
void display (void){
lcd_row_col(1,0);
hex_bcd2(set[i]);
lcd_row_col(1,i-1);
lcd_putc(s[b]);
delay_ms(10);
/*--------------------------------*/
if (i > 16){
hex_bcd2(set[i]);
lcd_row_col(2,i-17);
lcd_putc(s[b]);}
delay_ms(10);
}
void calculate (void){
if (k<10){
if (digit++>31){digit=0;i = 0;}
else {i++;set[i] = k;}}
if(k== 11){
do{
value1--;
display();
delay_ms(500);
}while(value1>0);
digit=0;value1 = 0; i=0;
}
}
- What microcontroller is used in the project?
The project uses a PIC16F877 microcontroller. - How is the keypad interfaced to the microcontroller?
The project uses a 3x4 keypad with rows on PORTB pins B0-B3 and columns on PORTB pins B4-B6, scanned via get_key3x4.c. - How is the LCD connected and driven?
The LCD is driven in 4-bit mode using code included from lcd_4bit.c. - Does the code use analog inputs?
No, the code disables analog inputs with setup_adc_ports(NO_ANALOGS) and setup_adc(ADC_OFF). - What clock frequency is configured for the PIC?
The code uses a 4 MHz clock as specified by #use delay(clock=4000000). - How are entered digits stored?
Digits are stored in the set array with index i; each new key press assigns set[i] = k in calculate(). - What happens when key value equals 11?
When k equals 11, calculate() decrements value1 in a loop, calling display() and delaying 500 ms until value1 is zero, then resets digit, value1, and i. - How does the code display individual digits?
hex_bcd2 extracts a single digit into b and display() uses the s character array to print that digit to the LCD at positions based on i.