trước hết bạn biết rằng ngắt rb là ngắt khi có thay đổi ở b4..b7 vậy khi bạn ấn nút sẽ có ngắt và bạn nhả nút cũng tạo thay đổi so với thời điểm trước đó nên cũng ngắt .
thứ 2 trong while của bạn chẳng có lệnh nào để nhìn thấy mode bằng bao nhiêu.
tôi thử VD sau:
nút bấm vào b4, trở kéo lên.
xuat ra port gia tri mode để test nhé
#include <16f877a.h>
#device *=16
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, NOPUT
int8 mode;
#int_rb
void interrupts_rb()
{
//toi lam nut nhan o b4
if(!input(pin_b4))mode++;
}
void main()
{
set_tris_b(0xff);
set_tris_c(0x00);
set_tris_d(0x00);
enable_interrupts(global);
enable_interrupts(int_rb);
while(1)
{
if(mode==4)output_high(pin_c0);else output_low(pin_c0);
output_d(mode);
}
}
|