Code:
// *****GIAO TIEP BAN PHIM VOI LCD***
// RS,RW,E POTRE
// LCD PORT B
// KEYPAD PORT D
//*************************************
#include "16F877A.h"
#include <DEFS_16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#byte portD=0x08
#byte portB=0x06
#byte portE=0x09
#define LCD portb
#define rs re0
#define rw re1
#define e re2
const unsigned char line[]= {'M','O','I',' ','N','H','A','P',' ','S','O',' ',':','#'};
INT8 b=0;
void lenh(void)
{output_low(pin_e0) ;
output_low(pin_e1);
output_high(pin_e2);
output_low(pin_e2);
delay_ms(6);}
void du_lieu(void)
{
output_high(pin_e0);
output_low(pin_e1);
output_high(pin_e2);
output_low(pin_e2);
delay_ms(6);}
void goi()
{b=0;
while(line[b]!='#')
{
output_b(line[b]);
du_lieu();
delay_ms(100);
b++;
}
}
void main()
{ set_tris_e(0);output_e(0);
set_tris_b(0);output_b(0);
OUTPUT_B(0X38);
lenh();
portb=0x0E;
lenh();
output_b(0x82);
lenh();
delay_ms(1);
goi();
delay_ms(1);
portb=0xc0;
lenh();
delay_ms(100);
output_D(0b11111111);
delay_ms(10);
while(true)
{
//====================================output_D(11111110)============//
output_D(0b11111110);
DELAY_MS(10);
if( input(pin_D4)==0 )
{
portb=0x37; //ghi 7
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D5)==0 ) //cho khi D5 xuong 0
{
portb=0x38; //ghi 8
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D6)==0 ) //cho khi D6 xuong 0
{
portb=0x39; //ghi 9
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D7)==0 ) //cho khi D7 xuong 0
{
portb='A'; // ghi a
du_lieu();
DELAY_MS(150);
}
//====================================output_D(11111101)=============//
output_D(0b11111101);
DELAY_MS(10);
if( input(PIN_D4)==0 ) //cho khi D4 xuong 0
{
portb=0x34; //ghi 4
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D5)==0 ) //cho khi D5 xuong 0
{
portb=0x35; //ghi 5
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D6)==0 ) //cho khi D6 xuong 0
{
portb=0x36; //ghi 6
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D7)==0 ) //cho khi D7 xuong 0
{
portb='B'; //ghi b
du_lieu();
DELAY_MS(150);
}
//======================output_D(11111011)==========================//
output_D(0b11111011);
DELAY_MS(10);
if( input(PIN_D4)==0 ) //cho khi D4 xuong 0
{
portb=0x31; //ghi 1
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D5)==0 ) //cho khi D5 xuong 0
{
portb=0x32; //ghi 2
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D6)==0 ) //cho khi D6 xuong 0
{
portb=0x33; //ghi 3
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D7)==0 ) //cho khi D7 xuong 0
{
portb='C'; //ghi c
du_lieu();
DELAY_MS(150);
}
//*******************************
output_D(0b11110111);
DELAY_MS(10);
if( input(PIN_D4)==0 ) //cho khi D4 xuong 0
{
portb='*'; //ghi *
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D5)==0 ) //cho khi D5 xuong 0
{
portb=0x30; //ghi 0
du_lieu();
DELAY_MS(150);
}
if( input(PIN_D6)==0 ) //cho khi D6 xuong 0
{
portb=0X01; //CLEAR
LENH();
portb=0x82;
lenh();
delay_ms(1);
goi();
delay_ms(1);
portb=0xcf;
lenh();
portb=0x04;
lenh();
}
if( input(PIN_D7)==0 ) //cho khi D7 xuong 0
{portb='D'; //ghi d
du_lieu();
DELAY_MS(150);
}
}
}