--- Sandu Teo <teodorstv@yahoo.com> wrote:
> Hello
>
> I am working at a sonar sistem .
> When the sensor detects something, a response is
> sent to the mirocontroller on PORTC.2
> I have enabled PCINT10 which works on that pin.
>
> I send some pulses on PORTC.0 and I start TIMER0
> and after that ,I wait for response on PORTC.2
>
> I have an oscilloscope and I monitor the signal
> sent to PORTC.2
> The sensor sends the right response on that PIN.
> When the respons comes, PCINT0 interrupt routine
> is served and the timer is stopped.
>
> The problem is that the timer does not report the
> right value.
>
> I use Timer0 at 2Mhz on Atmega162 at 16Mhz.
> The time needed to be counted, between sending and
> response is about 700 us.
> In timer0 interrupt routine I increment a counter
> that counts timer overflows.
> I have also tried with Timer1 [16 bit mode]
>
> Any ideea
>
>
Hi;
As i understand, your main problem is to measure the
time between
outgoing and incoming pulse with best resolution.
using a timer interrupt is something wrong because
timer works for a period of time, in interrupt service
routine
the timer stops and starts again, which the processing
time of
interrupt routine is not counted, except you using
timer in CTC mode.
so you need to use timer channel as a counter, and
count number of
logical overflows happened, see the pseoudo code:
assume using 8 bits counter.
send the out-pulse
reset the counter
while incoming pulse not reached
check the counter value
check 7th bit of counter
if set, increment local register counter
and reset counter into (count & 127)
end while
get the counter value and combine with local
counter.
note that because this process is a time-critical
process,
you need to use an external clock like microcontroller
XTAL signal buffered using a 74HC244 buffer.
but in any cases instruction execution time must be
calculated in operations:
clr r24 // clear variable to zero
clr r25 // "
cli // no interrupts during detection
out TCNT0,r1 // clear counter to measure time
sbi PORTC,0 // start sonar signal
loop:
//BEGIN OF REGION #3
sbic PORTC,2 // wait for incoming pulse
rjmp break_loop
sbic TCNT0,7 // check counter overflow
//END OF REGION #3 // time = 3 ticks
//BEGIN OF REGION #1
rjmp no_overflow // in this region (#1) rjmp
skipped, so time=1
cbi TCNT0,7 // reset counter overflow bit
adiw r24,1 // increment local counter
rjmp loop
//END OF REGION #1 // 6 ticks
no_overflow:
//BEGIN OF REGION #2
nop
nop
nop
nop
rjmp loop
//END OF REGION #2 // 6 ticks (MUST be same as REGION
#1)
break_loop:
in r23,TCNT0
sei
you should note that using timer is to implement an
asynchronous
timing operation while other codes are executed, so we
using timer in case of a counter.
but, the timing is very important:
execution time of loop if no signals found (one
iteration) is:
A + B + C1 + C2 - errors
A = (execution time of either region #1 or #2 (MUST
be same))
B = execution time of region #3
C1, C2 are the fixed code parts between clearing
TCNT0 and setting PORTC.0
and the time between detecting PORTC.2 to be
set and get the value of TCNT
errors is the time which is somehow same as (C1+C2)
so for a typical AVR micro, one iteration takes 6 + 3
= 9 ticks
the overflow counting variable counts the each 128
ticks.
so entire result time could be calculated using this
formula:
((r25:r24)*128) + r23 ticks
which using a 16MHz signal, you can measure up to
(65535*128+127) = 8388607 ticks # 524 ms.
but if you want to set a timeout to break the loop if
sonar pulse not returned, insert your code
into region #1, and add more nop operations in region
#2 before rjmp instruction and recalculate
the measure using new times. but in this case, the
minimum detectable time is 5 ticks
( at 16MHz, less than 1/2 micro-second )
i hope this helps;
good luck;
____________________________________________________________________________________
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel bargains.
http://farechase.yahoo.com/promo-generic-14795097Message
Re: [AVR-Chat] Atmega162 shared interrupt PCINT10 and timer
2007-06-12 by Reza
Attachments
- No local attachments were found for this message.