Ma3ix
Member level 2
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 :
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
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
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: