|
|
View previous topic :: View next topic |
Author |
Message |
bittersweet
Joined: 23 Apr 2008 Posts: 22
|
timer2 and external interrupt question |
Posted: Thu May 15, 2008 3:48 pm |
|
|
hi, i want to calculate the time of a pin's high to low situtaion changing, i want to use extarnal interrupt and timer2 here is the code
Code: |
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
#include<lcd.c>
int16 tmrcntr;
int32 time;
void main()
{
enable_interrupts(int_timer2);
ext_int_edge(H_TO_L);
enable_interrupts(int_ext);
enable_interrupts(global);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
setup_timer_2(T2_div_by_16, 255, 16);
set_timer2(0);
set_tris_b(0x11);
lcd_init();
while(1)
{
if(input(pin_b0))
{set_timer2(0);}
}
}
#int_ext
void ext_interrupt()
{
time= ((tmrcntr* 65536)/1000 + ((get_timer2()*16)/1000));
if(time>=6000)
{printf(lcd_putc,"SO LONG");}
if(time>=3000 && time<=6000)
{printf(lcd_putc," LONG");}
if(time<=3000)
{printf(lcd_putc," SHORT");}
set_timer2(0);
tmrcntr=0;
}
#int_timer2
void tmr2Kesme()
{
tmrcntr++;
}
|
i tried the time function calculates the time truely but when rb0 changes high to low i see on display regardless of time SO LONG, LONG and SHORT what is the problem? thanks.. |
|
|
Ttelmah Guest
|
|
Posted: Fri May 16, 2008 2:28 am |
|
|
Without looking very far, remember if you multiply an int16 value by an int16 value, _int16_ arithmetic will be used. Your arithmetic is almost certainly wrapping at several points. Use a cast, to change the data type to int32. Remember also, that division in particular, takes a _long_ time. Your timer value, won't be anything like what you expect, after the first part of the sum has completed...
Consider changing the numbers you use, to represent an easier internal value, rather than trying to work in numbers that are convenient for you. So, why multiply the timer2 value by anything?. Also, the arithmetic seems screwed. You are incrementing tmrcntr, every 16th time the timer2 counter wraps. As such, you have no way to distinguishthe first, second, third, fourth etc., wrap of the counter.....
You need to interrupt _every_ time the counter wraps.
Then scale your tests to suit this. So you could get an integer count for timer2, by using:
time=get_timer2()+(int32)tmrcntr*256;
Then time is just in timer2 counts, and the value in the hardware counter is read first. Just test this against your targets. This will simply be in 16uSec steps.
Best Wishes |
|
|
|
|
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
|