Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20563

BASCOM-AVR : pulse length capture : REPLY

$
0
0
You must clear timer1 on the Int0 ( rising edge ), not just enable the timer interrupt then read the timer on the falling edge in Int1 If you want to read the timer hi-lo as two separate bytes, Atmel says that you MUST read the low byte first, followed by the hi byte. This to prevent a rollover from lo to hi affecting the result. Bascom does this for you if you do a 16 bit read of timer1. Just as a further note, you do not need to disable the interrupts when in the interrupt routine, the AVR does this automatically for you. And of course that means you dont need to re-enable them afterwards, either. [code:1:d3081a8ecf] Int0_Isr: timer1 = 0 'this does a write to the timer1 hi-lo registers in the correct order return Int1_isr: 'Reset Portb.4 'test I = Timer1 'do 16 bit read of timer 1 in correct order N = Ovf_1 * 65536 N = N + I Ovf_1 = 0 Return [/code:1:d3081a8ecf] At the speeds you are talking - 20-200 Hz, Bascom is plenty fast enough that you dont need to do special things like nosave. Leave that for when you write your own ISRs in ASM. But as others have pointed out, its actually simpler to just poll the line, and clear the timer on a rising edge, then read it on a falling edge directly in main code. Even simpler is to let the timer free run, read it on the rising edge, then read it again on the falling edge, and subtract the two values as words. That also gives the possibility to poll several lines, and read the period of each one, using only one timer. You do have to ensure the timer does not roll over more than 65535 counts in one period for this to work. But you can usually avoid this by setting the prescaler properly. There are about another dozen ways to do this.

Viewing all articles
Browse latest Browse all 20563

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>