Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ultrasonic Sample Code Explanation

Status
Not open for further replies.

guil

Newbie level 3
Joined
Oct 11, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,309
Hi,
Can somebody explain to me the sample code here -> link.
Particularly in this line:
Code:
for(i = 0 ; i < BUFSIZE ; i++)  // for all samples in buffer
 {
       cm += buf[i] ;  // add to sum
 }
 cm /= BUFSIZE ;             // average samples

Why he divide it by 10? I am confused.

And also in this line:
Code:
buf[idx] = TMR1H ;              // build a 16 bit value from timer1
buf[idx] <<= 8 ;                // MSB
buf[idx] += TMR1L ;             // LSB

When TMR1H > 0, I am expecting TMR1L = 0xff always, since the timer counts from 0x0000 to 0xffff. Am I right?

thanks in advance.
Ilms
 

cm is the sum of 10 samples. Dividing by 10 would give you the average.

Timer 1 would count from 0x0100 to 0x01ff,
Then from 0x0200 to 0x2ff etc.
So TMR1L would not always be 0xff when TMR1H > 0.
 
  • Like
Reactions: guil

    guil

    Points: 2
    Helpful Answer Positive Rating
cm is the sum of 10 samples. Dividing by 10 would give you the average.

Thank you btbass. So quick!
Yes, I know it was the average of the 10 samples in .1sec period approximately. But he should not averaging it unless the buffer gets full (buf[0...9] filled up). Right?
And he will not displaying it until averaging is complete.
Although, 10ms is fast enough for human to noticed. The logic is somewhat confusing.

regards
guil
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top