Đây là code chương trình hiển thị kí tự nhập từ bàn phím ma trận 4x4 lên TEXT LCD 16x2, mình nạp vào mạch thì LCD chỉ hiển ra chữ "Hãy nhấn phím" còn nhấn phím thì không thấy gì, ai biết nhờ xem lỗi gì. Thanks nhiều:
#include <REGX51.H>
#include <string.h>
sbit H1=P2^0;
sbit H2=P2^1;
sbit H3=P2^2;
sbit H4=P2^3;
sbit C1=P2^4;
sbit C2=P2^5;
sbit C3=P2^6;
sbit C4=P2^7;
/*===========khai bao bien toan cuc============*/
sfr LCDdata = 0x90; // cong P1, 8 bit du lieu.
sbit BF = 0x97; // co ban, bit DB7.
sbit RS = P3^0; // chon thanh ghi
sbit RW = P3^1; // doc/ghi
sbit EN = P3^2; //cho phep chot du lieu
/*===========cac chuong trinh con cua LCD==========*/
/*--------------kiem tra su san sang cua LCD------------------------*/
void wait(void)
{
RS=0; //chon thanh ghi lenh
RW=1; //doc tu LCD
LCDdata=0xff; //gia tri 0xff
while(BF) //kiem tra co ban
{
EN=0; //dua xung cao xuong thap de chot.
EN=1; //dua chan cho phep len cao
}
}
/*------------------------thiet lap lenh cho LCD--------------------------*/
void LCDcontrol(unsigned char x)
{
RS=0;//chon thanh ghi lenh
RW=0;//ghi len LCD.
LCDdata=x ;// gia tri x
EN=1;//cho phep muc cao
EN=0;//xung cao xuong thap
wait();//doi LCD san sang.
}
/*------------------------------Khoi tao LCD-------------------------*/
void LCDinit(void)
{
LCDcontrol(0x38);//2 dong va ma tran 5x7
LCDcontrol(0x0e);//bat man hinh, bat con tro
LCDcontrol(0x01);//xoa man hinh
}
/*-------------------------thiet lap du lieu cho LCD-----------------*/
void LCDwrite(unsigned char c)
{
RS=1;//ghi du lieu
RW=0;//ghi du lieu len LCD
LCDdata=c;//gia tri C
EN=1;//cho phep muc cao
EN=0;//xung cao xuong thap
wait();//cho
}
void LCDwrites(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++){
LCDwrite(*(s+count));
}
}
/*=====================chuong trinh tre================*/
void delay(long time)
{
while(time--);
}
/***************************Ham quet phim************************/
void quetphim()
{ unsigned char phimnhan;
if(H1==0) //neu la C1
{
C1=1;
C2=1;
C3=1;
C4=1;
H1=0;
if(!C1) phimnhan=0;
if(!C2) phimnhan=1;
if(!C3) phimnhan=2;
if(!C4) phimnhan='A';
}
else if(H2==0) //neu la C2
{
C1=1;
C2=1;
C3=1;
C4=1;
H2=0;
if(!C1) phimnhan=4;
if(!C2) phimnhan=5;
if(!C3) phimnhan=6;
if(!C4) phimnhan='B';
}
if(H3==0)
{
C1=1;
C2=1;
C3=1;
C4=1;
H3=0;
if(!C1) phimnhan=7;
if(!C2) phimnhan=8;
if(!C3) phimnhan=9;
if(!C4) phimnhan='C';
}
if(H4==0)
{
C1=1;
C2=1;
C3=1;
C4=1;
H4=0;
if(!C1) phimnhan='*';
if(!C2) phimnhan=0;
if(!C3) phimnhan='#';
if(!C4) phimnhan='D';
}
}
}
}
void viewphim(unsigned char phimnhan)
{
if(phimnhan<10){
LCDwrite(phimnhan+48);
}
if(phimnhan='A'){
LCDwrite(65);
}
if(phimnhan='B'){
LCDwrite(66);
}
if(phimnhan='C'){
LCDwrite(67);
}
if(phimnhan='D'){
LCDwrite(68);
}
if(phimnhan='*'){
LCDwrite(42);
}
if(phimnhan='#'){
LCDwrite(35);
void main(void)
{
unsigned char phimnhan=0;
LCDinit();
LCDwrites("Hay Nhan Phim");
LCDcontrol(0xC0);
while(1){
phimnhan=Phim();
if(phimnhan){
LCDcontrol(0x01);
LCDwrites("Hay Nhan Phim");
LCDcontrol(0xC0);
viewphim(phimnhan);
}
}
}
#include <REGX51.H>
#include <string.h>
sbit H1=P2^0;
sbit H2=P2^1;
sbit H3=P2^2;
sbit H4=P2^3;
sbit C1=P2^4;
sbit C2=P2^5;
sbit C3=P2^6;
sbit C4=P2^7;
/*===========khai bao bien toan cuc============*/
sfr LCDdata = 0x90; // cong P1, 8 bit du lieu.
sbit BF = 0x97; // co ban, bit DB7.
sbit RS = P3^0; // chon thanh ghi
sbit RW = P3^1; // doc/ghi
sbit EN = P3^2; //cho phep chot du lieu
/*===========cac chuong trinh con cua LCD==========*/
/*--------------kiem tra su san sang cua LCD------------------------*/
void wait(void)
{
RS=0; //chon thanh ghi lenh
RW=1; //doc tu LCD
LCDdata=0xff; //gia tri 0xff
while(BF) //kiem tra co ban
{
EN=0; //dua xung cao xuong thap de chot.
EN=1; //dua chan cho phep len cao
}
}
/*------------------------thiet lap lenh cho LCD--------------------------*/
void LCDcontrol(unsigned char x)
{
RS=0;//chon thanh ghi lenh
RW=0;//ghi len LCD.
LCDdata=x ;// gia tri x
EN=1;//cho phep muc cao
EN=0;//xung cao xuong thap
wait();//doi LCD san sang.
}
/*------------------------------Khoi tao LCD-------------------------*/
void LCDinit(void)
{
LCDcontrol(0x38);//2 dong va ma tran 5x7
LCDcontrol(0x0e);//bat man hinh, bat con tro
LCDcontrol(0x01);//xoa man hinh
}
/*-------------------------thiet lap du lieu cho LCD-----------------*/
void LCDwrite(unsigned char c)
{
RS=1;//ghi du lieu
RW=0;//ghi du lieu len LCD
LCDdata=c;//gia tri C
EN=1;//cho phep muc cao
EN=0;//xung cao xuong thap
wait();//cho
}
void LCDwrites(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++){
LCDwrite(*(s+count));
}
}
/*=====================chuong trinh tre================*/
void delay(long time)
{
while(time--);
}
/***************************Ham quet phim************************/
void quetphim()
{ unsigned char phimnhan;
if(H1==0) //neu la C1
{
C1=1;
C2=1;
C3=1;
C4=1;
H1=0;
if(!C1) phimnhan=0;
if(!C2) phimnhan=1;
if(!C3) phimnhan=2;
if(!C4) phimnhan='A';
}
else if(H2==0) //neu la C2
{
C1=1;
C2=1;
C3=1;
C4=1;
H2=0;
if(!C1) phimnhan=4;
if(!C2) phimnhan=5;
if(!C3) phimnhan=6;
if(!C4) phimnhan='B';
}
if(H3==0)
{
C1=1;
C2=1;
C3=1;
C4=1;
H3=0;
if(!C1) phimnhan=7;
if(!C2) phimnhan=8;
if(!C3) phimnhan=9;
if(!C4) phimnhan='C';
}
if(H4==0)
{
C1=1;
C2=1;
C3=1;
C4=1;
H4=0;
if(!C1) phimnhan='*';
if(!C2) phimnhan=0;
if(!C3) phimnhan='#';
if(!C4) phimnhan='D';
}
}
}
}
void viewphim(unsigned char phimnhan)
{
if(phimnhan<10){
LCDwrite(phimnhan+48);
}
if(phimnhan='A'){
LCDwrite(65);
}
if(phimnhan='B'){
LCDwrite(66);
}
if(phimnhan='C'){
LCDwrite(67);
}
if(phimnhan='D'){
LCDwrite(68);
}
if(phimnhan='*'){
LCDwrite(42);
}
if(phimnhan='#'){
LCDwrite(35);
void main(void)
{
unsigned char phimnhan=0;
LCDinit();
LCDwrites("Hay Nhan Phim");
LCDcontrol(0xC0);
while(1){
phimnhan=Phim();
if(phimnhan){
LCDcontrol(0x01);
LCDwrites("Hay Nhan Phim");
LCDcontrol(0xC0);
viewphim(phimnhan);
}
}
}
Comment