|
|
View previous topic :: View next topic |
Author |
Message |
ahmad-al3omar
Joined: 25 May 2007 Posts: 16
|
width between two pulses |
Posted: Wed Feb 13, 2008 3:19 am |
|
|
hello all;
i am trying to calculate the time difference between two pulse waves , i am using the the pic18f458 and i connect each pulse wave to interrupt 0 and interrupt 1 and i use timer1 to calculate the time between two edges
i am using the following code :
Code: | #include<18f458.h>
#fuses noprotect,nowdt,xt
#use delay(clock=4000000)
#include<lcd.c>
int value;
#int_ext
ext1_isr()
{
set_timer1(0);
}
#int_ext1
ext2_isr()
{
value=get_timer1();
lcd_gotoxy(1,2);
printf(lcd_putc,"value= %d ",value);
}
main()
{
lcd_init();
enable_interrupts(global);
enable_interrupts(int_ext1);
enable_interrupts(int_ext2);
setup_timer_1(t1_internal|t1_div_by_8);
ext_int_edge( l_TO_h);
ext_int_edge( 1, l_TO_h);
while(1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"value= %d ",value);
}
} |
but the output value on the lcd is changed rapidly and not constant on the simulation .
can you help me to find out the way to calculate the difference ???
thank you all _________________ A.T.N |
|
|
kolio
Joined: 06 Feb 2008 Posts: 26
|
|
Posted: Wed Feb 13, 2008 2:24 pm |
|
|
Hi Ahmad,
Don't use lcd_gotoxy() and printf() in ISR. Read about "re-entrant code".
Try this one:
Code: |
#include<18f458.h>
#fuses noprotect,nowdt,xt
#use delay(clock=4000000)
#include<lcd.c>
int value;
int1 Completed;
#int_ext
ext1_isr()
{
if(!Completed) set_timer1(0);
}
#int_ext1
ext2_isr()
{
value=get_timer1();
Completed = 1;
//lcd_gotoxy(1,2);
//printf(lcd_putc,"value= %d ",value);
}
main()
{
lcd_init();
Completed = 0;
enable_interrupts(global);
enable_interrupts(int_ext1);
enable_interrupts(int_ext2);
setup_timer_1(t1_internal|t1_div_by_8);
ext_int_edge( l_TO_h);
ext_int_edge( 1, l_TO_h);
while(1)
{
if(Completed){
lcd_gotoxy(1,1);
printf(lcd_putc,"value= %d ",value);
Completed = 0;
}
}
}
|
In this way you may miss some pulses during printing the result on LCD, but the measurement will be properly displayed. Also pay attention about the duration and edges of the pulses. |
|
|
|
|
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
|