|
|
View previous topic :: View next topic |
Author |
Message |
ssx Guest
|
Delaying inside both main and interrupt functions? |
Posted: Tue Feb 24, 2009 3:45 pm |
|
|
Code: | #include <16F628A.h>
#fuses NOWDT,INTRC_IO,NOPUT,NOBROWNOUT,NOMCLR,NOLVP,NOCPD
#use delay(clock=4000000)
#use fast_io(b)
#int_ext
void ext_intrpt(){
output_high(pin_a1);
enable_interrupts(int_timer1);
set_timer1(15536);
}
#int_timer1
void timer1_intrpt()
{
output_low(pin_a1);
disable_interrupts(int_timer1);
}
void main()
{
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_2);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,27,1);
set_pwm1_duty(14);
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
set_tris_a(0x00);
set_tris_b(0x01);
output_a(0x00);
while(1){
set_pwm1_duty(14);
delay_us(999);
set_pwm1_duty(0);
delay_ms(6);
}
} |
There is an Ir receiver at RB0(using as external interrupt). I wanted to flash the led (at RA1) 100ms if RB0 gets "0V" from the receiver. But its status is always on.
(there isn't ir transmitter at this time. RB0 is always High.)
I used timer1 for 100ms timing of led flash. Because using delay_ms function inside external interrupt creates another problem like this.
What should i do?
Thanks. |
|
|
Ttelmah Guest
|
|
Posted: Tue Feb 24, 2009 4:12 pm |
|
|
Clear the timer1 interrupt before exiting the external interrupt function. Problem is that if a timer interrupt has already occurred, the interrupt flag will be set, and the timer will be called immediately. This will then set the signal low again.
Remember also that the external interrupt on the PIC, is an _edge_ triggered interrupt, not a 'level' triggered interrupt. If B0, is held low, it'll never trigger...
Best Wishes |
|
|
ssx Guest
|
|
Posted: Tue Feb 24, 2009 5:23 pm |
|
|
Thanks for the reply.
I cleared the timer1 but still not solved. Perhaps using internal rc oscillator causes this? When i changed delay_ms(6) to delay_ms(1) the led turns off at the start. If receiver sees transmitter, led turns on but stucks sometime or doesn't work properly.
At first i was using the code that i posted below. It's working properly with delay_ms(6) or less. But changing it as delay_ms(7) or greater value, the led is always goes on.
Code: | #include <16F628A.h>
#fuses NOWDT,INTRC_IO,NOPUT,NOBROWNOUT,NOMCLR,NOLVP,NOCPD
#use delay(clock=4000000)
#use fast_io(b)
#int_ext
void ext_intrpt(){
output_high(pin_a1);
delay_ms(100);
output_low(pin_a1);
}
#use delay(clock=4000000)
void main()
{
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_2);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,27,1);
set_pwm1_duty(14);
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
set_tris_a(0x00);
set_tris_b(0x01);
output_a(0x00);
while(1){
set_pwm1_duty(14);
delay_us(999);
set_pwm1_duty(0);
delay_ms(6); //delay_ms(7) stucks
}
} |
I hope it gives an idea about the problem..
Thanks. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|