em là dân mới vào nghề,em muốn quét bàn phím 3x4 hiển thị lên LCD các số 1,2,3...11
sau 1 đêm loay hoay chỉnh sửa kết quả là em vẫn ko thể hiển thị chinh xác đc.
mong mọi người giúp em với
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<intrins.h>
#include<stdlib.h>
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
sbit cot0= P1^0;
sbit cot1= P1^1;
sbit cot2= P1^2;
sbit hang0=P1^4;
sbit hang1=P1^5;
sbit hang2=P1^6;
sbit hang3=P1^7;
sbit led =P3^4;
char m_hang[4];
char m_cot[3];
char quet[3]={0xfe,0xfd,0xfb};
unsigned long str;
unsigned long d=0;
char x;
void tre(unsigned long t){
unsigned int i;
for(i=0;i<=t;i++);
}
void delay_short(){
unsigned int i;
for(i=0;i<=3;i++);
}
void delay(void)
{
TMOD = 0x01; //Timer 0 che do 1
TH0 = (65536-30000)/256;
TL0 = (65536-30000)%256;
TR0 = 1;
while (!TF0);
TR0 = 0;
TF0 = 0;
}
void ktra_coban(){
P2 = 0xff; // P2 muc cao LCD dang hoat dong;
RS = 0; // Chon thanh ghi lenh
RW = 1; // Doc tu LCD
do {
EN = 1; // Cho phep
delay_short();
EN = 0;
x=P2;
x=x&0x80; // Xoa 7 bit thap cua P2;
}
while(x==0x80);
}
void ghi_lenh(unsigned char command){
ktra_coban();
delay();
P2 = command;
RS = 0; //Chon thanh ghi lenh
RW = 0; //ghi vao LCD
EN = 1;
delay_short();
EN = 0;
}
void ghi_kytu(unsigned char kytu){
ktra_coban();
delay();
P2 = kytu;
RS = 1; // Chon thanh ghi du lieu
RW = 0; // Ghi vao LCD
EN =1;
delay_short();
EN = 0;
}
void ghi_chuoi(char *s){
while(*s){
ghi_kytu(*s);
s++;
}
}
void init(){ //khoi tao LCD
ghi_lenh(0x01); //xoa man hinh,dua tro ve dau
ghi_lenh(0x38); //che do 8bit,2dong
ghi_lenh(0x06); //hien thi tang
ghi_lenh(0x0e); //bat hien thi,con tro,tat nhay tro
}
void write_number(unsigned long number){ //Ham tao mot day so dem;
int j,i;
unsigned char array[12]; //Day 12 phan tu tren mot dong;
for(j=0 ; j<12 ; j++)
{
array[j] = number%10; //chia lay du roi thuc hien dua vao mang
number /= 10;
if(number==0)
{
i=j;
break;
}
}
for(j=i ; j>=0 ; j--){ //Hien thi tu trai qua;
ghi_kytu(array[j]+0x30); // cong ma ASCII hien thi len LCD
}
}
void main()
{ unsigned char i,j,k;
init();
ghi_lenh(0x01);
ghi_lenh(0x80);
ghi_chuoi("so:");
while(1)
{ led=0;
hang0=1;
hang1=1;
hang2=1;
hang3=1;
m_hang[0]=hang0;
m_hang[1]=hang1;
m_hang[2]=hang2;
m_hang[3]=hang3;
k=0xfe;
for(i=0;i<=2;i++)
{
P1=quet[i];
//P1=k;
//k=_crol_(k,1);
for(j=0;j<=3;j++)
{
if(m_hang[j]==0)
{
led=1;
str=j*3+i;
write_number(str);
tre(5000);
}
}
}
}}
mophong:
111111.rar
sau 1 đêm loay hoay chỉnh sửa kết quả là em vẫn ko thể hiển thị chinh xác đc.
mong mọi người giúp em với
#include<reg52.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<intrins.h>
#include<stdlib.h>
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
sbit cot0= P1^0;
sbit cot1= P1^1;
sbit cot2= P1^2;
sbit hang0=P1^4;
sbit hang1=P1^5;
sbit hang2=P1^6;
sbit hang3=P1^7;
sbit led =P3^4;
char m_hang[4];
char m_cot[3];
char quet[3]={0xfe,0xfd,0xfb};
unsigned long str;
unsigned long d=0;
char x;
void tre(unsigned long t){
unsigned int i;
for(i=0;i<=t;i++);
}
void delay_short(){
unsigned int i;
for(i=0;i<=3;i++);
}
void delay(void)
{
TMOD = 0x01; //Timer 0 che do 1
TH0 = (65536-30000)/256;
TL0 = (65536-30000)%256;
TR0 = 1;
while (!TF0);
TR0 = 0;
TF0 = 0;
}
void ktra_coban(){
P2 = 0xff; // P2 muc cao LCD dang hoat dong;
RS = 0; // Chon thanh ghi lenh
RW = 1; // Doc tu LCD
do {
EN = 1; // Cho phep
delay_short();
EN = 0;
x=P2;
x=x&0x80; // Xoa 7 bit thap cua P2;
}
while(x==0x80);
}
void ghi_lenh(unsigned char command){
ktra_coban();
delay();
P2 = command;
RS = 0; //Chon thanh ghi lenh
RW = 0; //ghi vao LCD
EN = 1;
delay_short();
EN = 0;
}
void ghi_kytu(unsigned char kytu){
ktra_coban();
delay();
P2 = kytu;
RS = 1; // Chon thanh ghi du lieu
RW = 0; // Ghi vao LCD
EN =1;
delay_short();
EN = 0;
}
void ghi_chuoi(char *s){
while(*s){
ghi_kytu(*s);
s++;
}
}
void init(){ //khoi tao LCD
ghi_lenh(0x01); //xoa man hinh,dua tro ve dau
ghi_lenh(0x38); //che do 8bit,2dong
ghi_lenh(0x06); //hien thi tang
ghi_lenh(0x0e); //bat hien thi,con tro,tat nhay tro
}
void write_number(unsigned long number){ //Ham tao mot day so dem;
int j,i;
unsigned char array[12]; //Day 12 phan tu tren mot dong;
for(j=0 ; j<12 ; j++)
{
array[j] = number%10; //chia lay du roi thuc hien dua vao mang
number /= 10;
if(number==0)
{
i=j;
break;
}
}
for(j=i ; j>=0 ; j--){ //Hien thi tu trai qua;
ghi_kytu(array[j]+0x30); // cong ma ASCII hien thi len LCD
}
}
void main()
{ unsigned char i,j,k;
init();
ghi_lenh(0x01);
ghi_lenh(0x80);
ghi_chuoi("so:");
while(1)
{ led=0;
hang0=1;
hang1=1;
hang2=1;
hang3=1;
m_hang[0]=hang0;
m_hang[1]=hang1;
m_hang[2]=hang2;
m_hang[3]=hang3;
k=0xfe;
for(i=0;i<=2;i++)
{
P1=quet[i];
//P1=k;
//k=_crol_(k,1);
for(j=0;j<=3;j++)
{
if(m_hang[j]==0)
{
led=1;
str=j*3+i;
write_number(str);
tre(5000);
}
}
}
}}
mophong:
111111.rar
Comment