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.

circular buffering-problem

Status
Not open for further replies.

Ma3ix

Member level 2
Joined
Dec 17, 2015
Messages
43
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
375
Hi, I need help for my code. Im trying to do filtering (running average from circular buffer) from adc reading.
I wrote this code and tested it from bascom 8051 compiler and it works fine :

Code:
**circular buffer**

Dim array(3) As Byte
Dim P As Byte
Dim sum As Byte
Dim X As Byte
Dim counter As Byte
Dim average As Single

counter = 0
P = 0
sum = 0

Do

sum = sum - array(p)
Input "Input number:" , X
array(p) = X
sum = Sum + X
counter=counter + x
P = P + 1
P = P Mod 3
   If counter = 3 Then
      	average = Sum / 3
       	
	counter = 2
   End If
Loop

** 
If I enter 5 numbers , for example array= (x1=1, x2=3; x3=5; x4=7; x5=8)
i will get 3 averages:
average_1= x1 + x2 + x3
average_2= x2 + x3 + x4
average_3= x3 + x4 + x5
which is fine cause I want to replace the oldest input with newest.


When I put this code in my project(thermometer) it does not work!Only first average is fine on display, I guess that second line of this code "sum=sum-array(p)"
does not work with project.:/
Project works fine without circular buffering.

The logic is same

Do

- reading from senzor
- circular buffering (X is reading from ADC)
- do calculation for temperature
- send data to displays

loop


im coding in Bascom 8051 :(
 
Last edited by a moderator:

I wrote this code and tested it from bascom 8051 compiler and it works fine
Means it's compiled without errors? O.K. but does it mean the code is correct?

Suggest to trace code operation with pencil and paper method, just writing down the results for every loop iteration. You surely see why it's not working as expected.
 

Means it's compiled without errors? O.K. but does it mean the code is correct?

Suggest to trace code operation with pencil and paper method, just writing down the results for every loop iteration. You surely see why it's not working as expected.

I tested this part of code in Bascom simulator and it works (giving me correct results for running average)..

In Proteus simulation and in reality it gives me totally incorrect results:roll:

It seems to me that mcu does not remember elements of array after first average?:idea:
 

Looks definitely wrong
Code:
counter=counter + x
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top