Mr.Bi
03-11-2007, 04:30 PM
#include <16F877A.h>
#include <DEFINE_16F877A.h>
#include <DEFINE_16F87x.h>
#fuses NOWDT,XT,NOPROTECT,NOLVP,PUT
#use delay(clock=4000000)
#define RS RC0
#define RW RC1
#define E RC2
#define LCD PORTD
#define mo_khoa RA0
#define kichSCR RA1
#define OK RA2
#define CANCEL RA3
const unsigned char num[] = {' ','0','1','2','3','4','5','6','7','8','9'} ;
const unsigned char pass[] = {' ','3','9','5','2','6','1','0'} ; // password
unsigned char key[] ;
unsigned char a = 0 ; // bien tang cac so nhap vao key [a++]
unsigned char i ; // bien hien thi cac so da nhan , sau khi nhan OK
//----------------------------
void delay_ms_MAIN(unsigned int j)
{ unsigned int k , l;
for( k=0 ; k<=j;k++)
{ l = j ; while(l!=0) l-- ; }
}
//----------------------------
void delay_ms_INT(unsigned int j)
{ unsigned int k , l;
for( k=0 ; k<=j;k++)
{ l = j ; while(l!=0) l-- ; }
}
void ham_DK_MAIN()
{ RS = 0 ; RW = 0 ; E =1 ; E=0 ; delay_ms_MAIN(2);}
void ham_HienThi_MAIN()
{ RS = 1 ; RW = 0 ; E =1 ; E=0 ; delay_ms_MAIN(1);}
//----------------
void ham_DK_INT()
{ RS = 0 ; RW = 0 ; E =1 ; E=0 ; delay_ms_INT(2);}
void ham_HienThi_INT()
{ RS = 1 ; RW = 0 ; E =1 ; E=0 ; delay_ms_INT(1);}
//----------------------
void quetphim_hienthi()
{ PORTB = 0b11101111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[1] ; ham_HienThi_INT();
key[a++] = num[1]; // key[a++] = '0'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[2] ; ham_HienThi_INT();
key[a++] = num[2]; // key[a++] = '1'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[3] ; ham_HienThi_INT();
key[a++] = num[3]; // key[a++] = '2'
} ;
PORTB = 0b11011111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[4] ; ham_HienThi_INT();
key[a++] = num[4]; // key[a++] = '3'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[5] ; ham_HienThi_INT();
key[a++] = num[5]; // key[a++] = '4'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[6] ; ham_HienThi_INT();
key[a++] = num[6]; // key[a++] = '5'
} ;
PORTB = 0b10111111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[7] ; ham_HienThi_INT();
key[a++] = num[7]; // key[a++] = '6'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[8] ; ham_HienThi_INT();
key[a++] = num[8]; // key[a++] = '7'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[9] ; ham_HienThi_INT();
key[a++] = num[9]; // key[a++] = '8'
} ;
PORTB = 0b01111111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[10] ; ham_HienThi_INT();
key[a++] = num[10]; // key[a++] = '9'
} ;
}
#int_RB
void ngat_RB()
{ while(OK!=0) { quetphim_hienthi();}
delay_ms_INT(20);
RBIF =0;
}
main()
{ set_tris_A(0b00001100); output_A(0b001100);
set_tris_B(0x0F); output_B(0x0F); // port B = keypad
set_tris_C(0);
set_tris_D(0);
delay_ms_MAIN(100); // thoi gian LCD khoi dong
LCD = 0x38 ; ham_DK_MAIN(); // modify LCD 2 dong matrix 5x7
LCD = 0x0C ; ham_DK_MAIN(); // tat con tro bat hien thi
enable_interrupts(global);
enable_interrupts(int_RB);
ext_int_edge(H_to_L);
while(1)
{
LCD = 0xC4 ; ham_DK_MAIN(); // hien chu dong 2 cot 5
LCD = 'R' ; ham_HienThi_MAIN();
LCD = 'e' ; ham_HienThi_MAIN();
LCD = 'a' ; ham_HienThi_MAIN();
LCD = 'd' ; ham_HienThi_MAIN();
LCD = 'y' ; ham_HienThi_MAIN();
LCD = '!' ; ham_HienThi_MAIN();
} ;
}
http://i189.photobucket.com/albums/z78/NgCongMinhDN/matranphim.jpg
hiện tượng xảy ra thế này
- khi cho dòng lệnh enable_interrupts(global) - cho phép ngắt - thì LCD ko hiện dòng chữ gì cả ? ngay cả khi ta reset để cố tình cho ct chỉ chạy trong MAIN (tức là phải có dòng "ready!'
- khi disable_interrupts(global) thì dòng "ready!" hiện ra ( có thể hiểu là ct chạy đúng theo ý đồ )
tại sao vậy ?
- chả lẽ set_tris các port bị sai so với phần cứng ?
- hay khởi tạo ngắt chưa đúng , nên ct nhảy ngắt lung tung ?
#include <DEFINE_16F877A.h>
#include <DEFINE_16F87x.h>
#fuses NOWDT,XT,NOPROTECT,NOLVP,PUT
#use delay(clock=4000000)
#define RS RC0
#define RW RC1
#define E RC2
#define LCD PORTD
#define mo_khoa RA0
#define kichSCR RA1
#define OK RA2
#define CANCEL RA3
const unsigned char num[] = {' ','0','1','2','3','4','5','6','7','8','9'} ;
const unsigned char pass[] = {' ','3','9','5','2','6','1','0'} ; // password
unsigned char key[] ;
unsigned char a = 0 ; // bien tang cac so nhap vao key [a++]
unsigned char i ; // bien hien thi cac so da nhan , sau khi nhan OK
//----------------------------
void delay_ms_MAIN(unsigned int j)
{ unsigned int k , l;
for( k=0 ; k<=j;k++)
{ l = j ; while(l!=0) l-- ; }
}
//----------------------------
void delay_ms_INT(unsigned int j)
{ unsigned int k , l;
for( k=0 ; k<=j;k++)
{ l = j ; while(l!=0) l-- ; }
}
void ham_DK_MAIN()
{ RS = 0 ; RW = 0 ; E =1 ; E=0 ; delay_ms_MAIN(2);}
void ham_HienThi_MAIN()
{ RS = 1 ; RW = 0 ; E =1 ; E=0 ; delay_ms_MAIN(1);}
//----------------
void ham_DK_INT()
{ RS = 0 ; RW = 0 ; E =1 ; E=0 ; delay_ms_INT(2);}
void ham_HienThi_INT()
{ RS = 1 ; RW = 0 ; E =1 ; E=0 ; delay_ms_INT(1);}
//----------------------
void quetphim_hienthi()
{ PORTB = 0b11101111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[1] ; ham_HienThi_INT();
key[a++] = num[1]; // key[a++] = '0'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[2] ; ham_HienThi_INT();
key[a++] = num[2]; // key[a++] = '1'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[3] ; ham_HienThi_INT();
key[a++] = num[3]; // key[a++] = '2'
} ;
PORTB = 0b11011111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[4] ; ham_HienThi_INT();
key[a++] = num[4]; // key[a++] = '3'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[5] ; ham_HienThi_INT();
key[a++] = num[5]; // key[a++] = '4'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[6] ; ham_HienThi_INT();
key[a++] = num[6]; // key[a++] = '5'
} ;
PORTB = 0b10111111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[7] ; ham_HienThi_INT();
key[a++] = num[7]; // key[a++] = '6'
} ;
if(RB1==0) { delay_ms_INT(20) ; while (RB1==0) continue ; delay_ms_INT(20);
LCD = num[8] ; ham_HienThi_INT();
key[a++] = num[8]; // key[a++] = '7'
} ;
if(RB2==0) { delay_ms_INT(20) ; while (RB2==0) continue ; delay_ms_INT(20);
LCD = num[9] ; ham_HienThi_INT();
key[a++] = num[9]; // key[a++] = '8'
} ;
PORTB = 0b01111111 ;
if(RB0==0) { delay_ms_INT(20) ; while (RB0==0) continue ; delay_ms_INT(20);
LCD = num[10] ; ham_HienThi_INT();
key[a++] = num[10]; // key[a++] = '9'
} ;
}
#int_RB
void ngat_RB()
{ while(OK!=0) { quetphim_hienthi();}
delay_ms_INT(20);
RBIF =0;
}
main()
{ set_tris_A(0b00001100); output_A(0b001100);
set_tris_B(0x0F); output_B(0x0F); // port B = keypad
set_tris_C(0);
set_tris_D(0);
delay_ms_MAIN(100); // thoi gian LCD khoi dong
LCD = 0x38 ; ham_DK_MAIN(); // modify LCD 2 dong matrix 5x7
LCD = 0x0C ; ham_DK_MAIN(); // tat con tro bat hien thi
enable_interrupts(global);
enable_interrupts(int_RB);
ext_int_edge(H_to_L);
while(1)
{
LCD = 0xC4 ; ham_DK_MAIN(); // hien chu dong 2 cot 5
LCD = 'R' ; ham_HienThi_MAIN();
LCD = 'e' ; ham_HienThi_MAIN();
LCD = 'a' ; ham_HienThi_MAIN();
LCD = 'd' ; ham_HienThi_MAIN();
LCD = 'y' ; ham_HienThi_MAIN();
LCD = '!' ; ham_HienThi_MAIN();
} ;
}
http://i189.photobucket.com/albums/z78/NgCongMinhDN/matranphim.jpg
hiện tượng xảy ra thế này
- khi cho dòng lệnh enable_interrupts(global) - cho phép ngắt - thì LCD ko hiện dòng chữ gì cả ? ngay cả khi ta reset để cố tình cho ct chỉ chạy trong MAIN (tức là phải có dòng "ready!'
- khi disable_interrupts(global) thì dòng "ready!" hiện ra ( có thể hiểu là ct chạy đúng theo ý đồ )
tại sao vậy ?
- chả lẽ set_tris các port bị sai so với phần cứng ?
- hay khởi tạo ngắt chưa đúng , nên ct nhảy ngắt lung tung ?