PDA

View Full Version : Giúp gỡ rối Ngắt RB!


sontvak42
25-07-2012, 04:33 PM
Ai có thể giúp em gỡ rối phần ngắt RB này cái không ạ.!
Tại sao khi chương trình của em viết khi nó nhảy vào chương trình phục vụ ngắt thì lại không thực hiện ở phần case 2 nữa, màn hình LCD cũng bị treo luôn không làm cách nào khôi phục được.
Mong các pro giúp đỡ em với!
Em cám ơn nhiều.

Code :

#include <16F877A.h>
#include <def_877a.h>
#device adc=10
#FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50%
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)

#include <lcd_nokia3310.c>
int8 model;
float adc;

#int_RB
void RB_isr()
{
output_c(0xff);
model =model+1;
clear_interrupt(int_rb);
}



void main()
{
set_tris_a(0xff); // all input
set_tris_b(0xff);
// port_b_pullups(1); // treo tro port b
set_tris_c(0x00);
set_tris_d(0x00); // all output

enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
ext_int_edge(0,H_to_L);
setup_adc_ports(AN0);
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);

// TODO: USER CODE!!

model =1;


while(true)

{
nokia_init();
// if(RB7==0)
// model++;
if(model==3)
model =1;


switch (model)
{
case 1:
{

nokia_clear_screen();
adc = read_adc();
nokia_gotoxy(0,0);
printf(nokia_printchar,"ADC!");
nokia_gotoxy(17,2);
printf(nokia_printchar,"%4.3f",adc);
delay_us(10);
}
break;
case 2:
{

nokia_clear_screen();
nokia_gotoxy(17,2);
printf(nokia_printchar,"Interrupts");
delay_us(10);
}
break;
}
}
}

tdm
27-07-2012, 02:43 PM
Ai có thể giúp em gỡ rối phần ngắt RB này cái không ạ.!
Tại sao khi chương trình của em viết khi nó nhảy vào chương trình phục vụ ngắt thì lại không thực hiện ở phần case 2 nữa, màn hình LCD cũng bị treo luôn không làm cách nào khôi phục được.
Mong các pro giúp đỡ em với!
Em cám ơn nhiều.

Code :

#include <16F877A.h>
#include <def_877a.h>
#device adc=10
#FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50%
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)

#include <lcd_nokia3310.c>
int8 model;
float adc;

#int_RB
void RB_isr()
{
output_c(0xff);
model =model+1;
clear_interrupt(int_rb);
}



void main()
{
set_tris_a(0xff); // all input
set_tris_b(0xff);
// port_b_pullups(1); // treo tro port b
set_tris_c(0x00);
set_tris_d(0x00); // all output

enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
ext_int_edge(0,H_to_L);
setup_adc_ports(AN0);
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);

// TODO: USER CODE!!

model =1;


while(true)

{
nokia_init();
// if(RB7==0)
// model++;
if(model==3)
model =1;


switch (model)
{
case 1:
{

nokia_clear_screen();
adc = read_adc();
nokia_gotoxy(0,0);
printf(nokia_printchar,"ADC!");
nokia_gotoxy(17,2);
printf(nokia_printchar,"%4.3f",adc);
delay_us(10);
}
break;
case 2:
{

nokia_clear_screen();
nokia_gotoxy(17,2);
printf(nokia_printchar,"Interrupts");
delay_us(10);
}
break;
}
}
}
tham khảo code sau:

#include <16F877A.h>
#include <def_877a.h>
#device adc=10
#FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50%
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)

#include <lcd_nokia3310.c>
int8 model;
float adc;

#int_RB
void RB_isr()
{
if( !input(PIN_B4) )
{
output_c(0xff);
model =model+1;
if(model==3)
model =1;
nokia_clear_screen();
switch (model)
{
case 1:
{
adc = read_adc();
nokia_gotoxy(0,0);
printf(nokia_printchar,"ADC!");
nokia_gotoxy(17,2);
printf(nokia_printchar,"%4.3f",adc);
delay_us(10);
}
break;
case 2:
{
nokia_gotoxy(17,2);
printf(nokia_printchar,"Interrupts");
delay_us(10);
}
break;
}
clear_interrupt(int_rb);
}
}



void main()
{
set_tris_a(0xff); // all input
set_tris_b(0xff);
port_b_pullups(true); // treo tro port b
set_tris_c(0x00);
set_tris_d(0x00); // all output


ext_int_edge(H_to_L);
setup_adc_ports(AN0);
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);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
// TODO: USER CODE!!

model =1;
nokia_init();
nokia_gotoxy(0,0);
printf(nokia_printchar,"ADC!");
while(true)

{
switch (model)
{
case 1:
{
adc = read_adc();
nokia_gotoxy(17,2);
printf(nokia_printchar,"%4.3f",adc);
delay_ms(100);//thoi gian lay mau adc
}
break;
}
}
}

sontvak42
02-08-2012, 09:57 AM
Cho em hỏi cái là: Tại sao mình không khởi tạo một biến tạm để thực hiện chế độ switch mà lại chuyển hết toàn bộ phần switch...case vào trong chương trình ngắt vậy anh? theo em được hiểu là chương trình thực hiện ngắt thì nó càng ít lệnh thực hiện trong đó thì càng tốt mà anh.
Với lại trong phần while(1) của anh tại sao chỉ mỗi cái case 1 là sao hả anh? như vậy mặc nhiên chương trình sẽ chạy thế nào?
Em cám ơn anh nhiều!

tdm
04-08-2012, 12:33 AM
Cho em hỏi cái là: Tại sao mình không khởi tạo một biến tạm để thực hiện chế độ switch mà lại chuyển hết toàn bộ phần switch...case vào trong chương trình ngắt vậy anh? theo em được hiểu là chương trình thực hiện ngắt thì nó càng ít lệnh thực hiện trong đó thì càng tốt mà anh.
Với lại trong phần while(1) của anh tại sao chỉ mỗi cái case 1 là sao hả anh? như vậy mặc nhiên chương trình sẽ chạy thế nào?
Em cám ơn anh nhiều!
tùy vào ứng dụng mà người dùng có thể xử lí trong ngắt hay ngoài ngắt.
với ứng dụng trên của bạn khi mode=2 chỉ làm mỗi việc printf(nokia_printchar,"Interrupts"); nên tôi xử lí trong ngắt luôn.
nếu xử lí ngoài ngắt thì khi mode=2 nó lặp lại printf(nokia_printchar,"Interrupts"); một cách vô ích

sontvak42
04-08-2012, 10:25 AM
tùy vào ứng dụng mà người dùng có thể xử lí trong ngắt hay ngoài ngắt.
với ứng dụng trên của bạn khi mode=2 chỉ làm mỗi việc printf(nokia_printchar,"Interrupts"); nên tôi xử lí trong ngắt luôn.
nếu xử lí ngoài ngắt thì khi mode=2 nó lặp lại printf(nokia_printchar,"Interrupts"); một cách vô ích
Vâng em hiểu rồi, cám ơn anh nhiều!