mình viết chương trình lập trình C xuất một số khi nhấn 1 phím số tương ứng ra LCD. LCD và mạch thì mình đã test chạy tốt nhưng không hiểu chương trình mình viết sai chỗ nào mà khi chạy LCD chỉ sáng lên chứ không có hiển thị số? Mong các bác giúp mình. thanks!
#include <AT89X52.H>
#include "lcd.h"
//chuong trinh quet phim nhan 4x3 (4 hang 3 cot)
//p2.0-2.2 cot
//p2.4-2.7 hang
sbit ROW_1=P2^0;
sbit ROW_2=P2^1;
sbit ROW_3=P2^2;
sbit ROW_4=P2^3;
sbit COL_1=P2^4;
sbit COL_2=P2^5;
sbit COL_3=P2^6;
sbit COL_4=P2^7;
char str1[10] = "1234567890";
int sttphim;
//int t;
//===============================
void delay(unsigned int time)
{
while(time--)
{
int k = 121;
while(k--){}
}
}
/************************sua chua***********************/
void Quet_Phim(void) {
// Hang lam dau vao, Cot lam dau ra muc thap
ROW_1 = 1;
ROW_2 = 1;
ROW_3 = 1;
ROW_4 = 1;
COL_1 = 0;
COL_2 = 0;
COL_3 = 0;
COL_4 = 0;
// t = 0;
//Kiem tra xem co phim nao dc bam hay ko?
while (1)
{
if ((!ROW_1) || (!ROW_2) || (!ROW_3) || (!ROW_4)) {
delay(30);
//Neu hang 1 co phim dc bam
if (!ROW_1) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_1 = 0;
if (!COL_1) { //1
while(!COL_1);
delay(50);
sttphim = 1;
break;
//t = 1;
}
else if (!COL_2) { //2
while(!COL_2);
delay(50);
sttphim = 2;
//t = 1;
break;
}
else if (!COL_3) { //3
while(!COL_3);
delay(50);
sttphim = 3;
//t = 1;
break;
}
else if (!COL_4) { //4
while(!COL_4);
delay(50);
sttphim = 4;
//t = 1;
break;
}
};
//Neu hang 2 co phim dc bam
if (!ROW_2) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_2 = 0;
if (!COL_1) { //5
while(!COL_1);
delay(50);
sttphim = 5;
//t = 1;
break;
}
else if (!COL_2) { //6
while(!COL_2);
delay(50);
sttphim = 6;
//t = 1;
break;
}
else if (!COL_3) { //7
while(!COL_3);
delay(50);
sttphim = 7;
//t = 1;
break;
}
else if (!COL_4) { //8
while(!COL_4);
delay(50);
sttphim = 8;
//t = 1;
break;
}
};
//Neu hang 3 co phim dc bam
if (!ROW_3) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_3 = 0;
if (!COL_1) { //9
delay(50);
while(!COL_1);
delay(50);
sttphim = 9;
//t = 1;
break;
}
else if (!COL_2) { //0
delay(30);
while(!COL_2);
delay(30);
sttphim = 10;
//t = 1;
break;
}
else if (!COL_3) {
delay(30);
while(!COL_3);
delay(30);
//t = 1;
}
else if (!COL_4) {
delay(30);
while(!COL_3);
delay(30);
//t = 1;
}
};
}
}
}
/* //Neu hang 4 co phim dc bam
if (!ROW_4) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_4 = 0;
if (!COL_1) {
delay(50);
while(!COL_1);
delay(50);
}
else if (!COL_2) {
delay(30);
while(!COL_2);
delay(30);
mod--;
if (mod <= 1) mod = 1;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
else if (!COL_3) {
delay(30);
while(!COL_3);
delay(30);
mod++;
if (mod >= 4) mod = 4;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
else if (!COL_4) {
delay(30);
while(!COL_3);
delay(30);
en_ex = !en_ex;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
// send data
if (!en_ex) {
if ((n1 != 10) && (n2 != 10) && (n3 != 10) && (n4 != 10)) {
ID_H = n1*10 + n2;
ID_L = n3*10 + n4;
ma_ID = ID_H*100 + ID_L;
Send_DATA(ID_H,ID_L);
};
}
n1 = n2 = n3 = n4 =10;
mod = 1;
//nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
};
}
}
/************************sua chua***********************/
void main()
{
unsigned int i;
LCD_Init();
LCD_Clear();
delay(150);
for(i = 0; i<16;i++)
{
LCD_Position(0,i);
quetphim();
LCD_PutChar(str1[sttphim]);
delay(10);
}
for(i = 0; i<16;i++)
{
LCD_Position(1,i);
quetphim();
LCD_PutChar(str1[sttphim]);
delay(10);
}
delay(2000);
while(1){}
}
#include <AT89X52.H>
#include "lcd.h"
//chuong trinh quet phim nhan 4x3 (4 hang 3 cot)
//p2.0-2.2 cot
//p2.4-2.7 hang
sbit ROW_1=P2^0;
sbit ROW_2=P2^1;
sbit ROW_3=P2^2;
sbit ROW_4=P2^3;
sbit COL_1=P2^4;
sbit COL_2=P2^5;
sbit COL_3=P2^6;
sbit COL_4=P2^7;
char str1[10] = "1234567890";
int sttphim;
//int t;
//===============================
void delay(unsigned int time)
{
while(time--)
{
int k = 121;
while(k--){}
}
}
/************************sua chua***********************/
void Quet_Phim(void) {
// Hang lam dau vao, Cot lam dau ra muc thap
ROW_1 = 1;
ROW_2 = 1;
ROW_3 = 1;
ROW_4 = 1;
COL_1 = 0;
COL_2 = 0;
COL_3 = 0;
COL_4 = 0;
// t = 0;
//Kiem tra xem co phim nao dc bam hay ko?
while (1)
{
if ((!ROW_1) || (!ROW_2) || (!ROW_3) || (!ROW_4)) {
delay(30);
//Neu hang 1 co phim dc bam
if (!ROW_1) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_1 = 0;
if (!COL_1) { //1
while(!COL_1);
delay(50);
sttphim = 1;
break;
//t = 1;
}
else if (!COL_2) { //2
while(!COL_2);
delay(50);
sttphim = 2;
//t = 1;
break;
}
else if (!COL_3) { //3
while(!COL_3);
delay(50);
sttphim = 3;
//t = 1;
break;
}
else if (!COL_4) { //4
while(!COL_4);
delay(50);
sttphim = 4;
//t = 1;
break;
}
};
//Neu hang 2 co phim dc bam
if (!ROW_2) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_2 = 0;
if (!COL_1) { //5
while(!COL_1);
delay(50);
sttphim = 5;
//t = 1;
break;
}
else if (!COL_2) { //6
while(!COL_2);
delay(50);
sttphim = 6;
//t = 1;
break;
}
else if (!COL_3) { //7
while(!COL_3);
delay(50);
sttphim = 7;
//t = 1;
break;
}
else if (!COL_4) { //8
while(!COL_4);
delay(50);
sttphim = 8;
//t = 1;
break;
}
};
//Neu hang 3 co phim dc bam
if (!ROW_3) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_3 = 0;
if (!COL_1) { //9
delay(50);
while(!COL_1);
delay(50);
sttphim = 9;
//t = 1;
break;
}
else if (!COL_2) { //0
delay(30);
while(!COL_2);
delay(30);
sttphim = 10;
//t = 1;
break;
}
else if (!COL_3) {
delay(30);
while(!COL_3);
delay(30);
//t = 1;
}
else if (!COL_4) {
delay(30);
while(!COL_3);
delay(30);
//t = 1;
}
};
}
}
}
/* //Neu hang 4 co phim dc bam
if (!ROW_4) {
COL_1 = 1;
COL_2 = 1;
COL_3 = 1;
COL_4 = 1;
ROW_4 = 0;
if (!COL_1) {
delay(50);
while(!COL_1);
delay(50);
}
else if (!COL_2) {
delay(30);
while(!COL_2);
delay(30);
mod--;
if (mod <= 1) mod = 1;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
else if (!COL_3) {
delay(30);
while(!COL_3);
delay(30);
mod++;
if (mod >= 4) mod = 4;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
else if (!COL_4) {
delay(30);
while(!COL_3);
delay(30);
en_ex = !en_ex;
nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
// send data
if (!en_ex) {
if ((n1 != 10) && (n2 != 10) && (n3 != 10) && (n4 != 10)) {
ID_H = n1*10 + n2;
ID_L = n3*10 + n4;
ma_ID = ID_H*100 + ID_L;
Send_DATA(ID_H,ID_L);
};
}
n1 = n2 = n3 = n4 =10;
mod = 1;
//nhap_nhay_1 = nhap_nhay_2 = nhap_nhay_3 = nhap_nhay_4 = 0;
}
};
}
}
/************************sua chua***********************/
void main()
{
unsigned int i;
LCD_Init();
LCD_Clear();
delay(150);
for(i = 0; i<16;i++)
{
LCD_Position(0,i);
quetphim();
LCD_PutChar(str1[sttphim]);
delay(10);
}
for(i = 0; i<16;i++)
{
LCD_Position(1,i);
quetphim();
LCD_PutChar(str1[sttphim]);
delay(10);
}
delay(2000);
while(1){}
}
Comment