PDA

View Full Version : pic16f877a không nhận bàn phím ma trận


huyetdocvanly
17-05-2013, 12:50 AM
tình hình là mình làm cái mạch khóa điện tử chạy mô phỏng thì ok nhưng làm mạch thật thì pic không nhận bàn phím ma trận.đã kiểm tra là co tín hiệu vào pic nhưng nó không hoạtt động
moị người tìm lỗi giúp với .bàn phim nối vào portb
dây là code viết bằng ccs

#include <16f877a.h>
#device *=16
#device adc=8

#FUSES NOWDT
#FUSES XT
#FUSES NOPUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOCPD
#FUSES NOWRT
#FUSES RESERVED
#use delay(clock=4000000)
#include <LCD.C>
#use Fast_IO( B )
#BYTE PORTB=0x06
#BIT B0=PORTB.0
#BIT B1=PORTB.1
#BIT B2=PORTB.2
#BIT B3=PORTB.3
#BIT B4=PORTB.4
#BIT B5=PORTB.5
#BIT B6=PORTB.6
#BIT B7=PORTB.7
unsigned int nhappass() {
unsigned int nhap=0;
int kt=1;
//lcd_init();
while(kt){
b4=1;b5=0;b6=0;b7=0;
switch(input_b()){
case 0x11 :{ delay_ms(50);
if(input_b()==0x11){

nhap=nhap*10 +7;
lcd_putc("*");
}
while(input_b()!=0x10){} delay_ms(50);
break;
}

case 0x12 :{
delay_ms(50);
if(input_b()==0x12){
nhap=nhap*10 +4;
lcd_putc("*");}
while(input_b()!=0x10){} delay_ms(50);
break;

}
case 0x14 :{
delay_ms(50);
if(input_b()==0x14){
nhap=nhap*10 +1;
lcd_putc("*");}
while(input_b()!=0x10){} delay_ms(50);
break;
}
case 0x18 :{
delay_ms(50);
if(input_b()==0x18){
if(nhap!=0) {lcd_putc("\b"); lcd_putc(" ");lcd_putc("\b");nhap=nhap/10;}}
while(input_b()!=0x10){} delay_ms(50);
break;

}
}
b4=0;b5=1;b6=0;b7=0;
switch(input_b()){
case 0x21 :{
delay_ms(50);
if(input_b()==0x21){
nhap=nhap*10 +8;
lcd_putc("*");}
while(input_b()!=0x20){} delay_ms(50);
break;

}
case 0x22 :{
delay_ms(50);
if(input_b()==0x22){
nhap=nhap*10 +5;
lcd_putc("*");}
while(input_b()!=0x20){} delay_ms(50);
break;

}
case 0x24 :{
delay_ms(50);
if(input_b()==0x24){
nhap=nhap*10 +2;
lcd_putc("*");}
while(input_b()!=0x20){} delay_ms(50);
break;
}
case 0x28 :{
delay_ms(50);
if(input_b()==0x28){
nhap=nhap*10 ;
lcd_putc("*");}
while(input_b()!=0x20){} delay_ms(50);
break;
}
}
b4=0;b5=0;b6=1;b7=0;
switch(input_b()){
case 0x41 :{
delay_ms(50);
if(input_b()==0x41){
nhap=nhap*10 +9;
lcd_putc("*");}
while(input_b()!=0x40){} delay_ms(50);
break;

}
case 0x42 :{
delay_ms(50);
if(input_b()==0x42){
nhap=nhap*10 +6;
lcd_putc("*");}
while(input_b()!=0x40){} delay_ms(50);
break;

}
case 0x44 :{
delay_ms(50);
if(input_b()==0x44){
nhap=nhap*10 +3;
lcd_putc("*");}
while(input_b()!=0x40){} delay_ms(50);
break;
}
case 0x48 :{
delay_ms(50);
if(input_b()==0x48){
kt=0;}
while(input_b()!=0x40){} delay_ms(50);

break;
}
}

}
lcd_putc("\f");
return nhap;
}


void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

set_tris_b(0x0f);
set_tris_c(0x00);
output_c(0x00);
unsigned int mk=123456;
lcd_init();
int dung=1;
while(dung){
lcd_putc("** Welcome **");
output_low(pin_c1);output_low(pin_c0);
int ketthuc=1,tt=0;
int sai=0;
while(ketthuc){
b7=1;
switch (input_b()&0x0f){
case 0x01 :{
delay_ms(50);
if((input_b()&0x0f)==0x01){
while(sai<3){lcd_putc("\f");lcd_putc("nhap pass:");
unsigned int ktra1 =nhappass();
if(ktra1==mk){output_high(pin_c0);ketthuc=0;tt=1;l cd_putc("pass chinh xac!");break; }
else{lcd_putc("sai pass !");sai++ ;if(sai<3){delay_ms(1000);} }}
if(sai>=3){ output_high(pin_c1);ketthuc=0;dung=0;break;}
else break;
}delay_ms(50);
}
case 0x02 :{
delay_ms(50);
if((input_b()&0x0f)==0x02){
while(sai<3){lcd_putc("\f");lcd_putc("pass cu:");
unsigned int ktra2 =nhappass();
if(ktra2==mk){ unsigned int pm1,pm2;
do {lcd_putc("\f");lcd_putc("pass moi:");
pm1=nhappass();lcd_putc("\f");lcd_putc("nhap lai:");
pm2=nhappass();if(pm1!=pm2){lcd_putc("\f");lcd_putc("khong khop !");delay_ms(1000);}}while(pm1!=pm2);
mk=pm1;lcd_putc("\f");lcd_putc("doi thanh cong");ketthuc=0;tt=1;break;}
else{lcd_putc("nhap sai:");sai++;if(sai<3){delay_ms(1000);}}
}
if(sai>=3){output_high(pin_c1);ketthuc=0;dung=0;break;}
else break;
}delay_ms(50);
}
}
}
while(tt){
b7=1;b6=0;b5=0;b4=0;
switch (input_b()&0x0f){
case 0x02 :{
delay_ms(50);
if((input_b()&0x0f)==0x02){
unsigned int pm1,pm2;
do {lcd_putc("\f");lcd_putc("pass moi:");
pm1=nhappass();lcd_putc("\f");lcd_putc("nhap lai:");
pm2=nhappass();if(pm1!=pm2){lcd_putc("\f");lcd_putc("khong khop !");delay_ms(1000);}}while(pm1!=pm2);
mk=pm1;lcd_putc("\f");lcd_putc("doi thanh cong");break;
}delay_ms(50);
}
case 0x04:{
delay_ms(50);
if((input_b()&0x0f)==0x04){
tt=0;lcd_putc("\f");break;}
}
}

}
}
while(1){lcd_putc("\f");delay_ms(25);lcd_putc("ban nhap sai !!!");delay_ms(30);}
}

ltdienvp
08-04-2016, 04:17 PM
Bài viết rất bổ ích tks bạn nhiều.