BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Thu Apr 26, 2018 2:53 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: CIRCULAR BUFFER
PostPosted: Fri Feb 07, 2014 4:19 pm 
Offline
Citizen

Joined: Thu Feb 06, 2014 7:37 pm
Posts: 19
HELLO: I have searched the web and found a lot of great information,unfortunately I am still lost...I am using a nano 28 and an MCP3551 ADC..Every thing is working,I just need to rid the noise from the data,I usually do a simple averaging routine,now I want to expand my know how and learn coding for a running average (circular buffer) type filter..If anyone can suggest a tutorial or a book,even a sample code will be great...I tried to use a C based routine and convert it over to BASIC,I did not have any luck...Thank you for any ideas.


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Mon Feb 10, 2014 4:17 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
I am not sure if I understand your question fully, but for example some of the code for Basic Atom Pros, that we use to average analog inputs, that look something like:
Code:
index var byte
buffer var word(8)

sum var word

; init values to some reasonable place... Alternatively could init by running through code 8 times to do new readings instead...
for index = 0 to 7
  buffer(index) = 542
next
sum = 4336

; Read in the next value... something like:
sum = sum - buffer(index)  ; remove the previous value from our sum
adin 2, buffer(index)          ; get the new value
sum = sum + buffer(index)  ; add in the new value to our buffer

; finally increment the index and limit its range to 0 to 7.
index = (index + 1) & 7

; Now your average value is simply sum/8

Note: this was cut and paste, plus typing on the fly, so there are probably some issues with it...


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Tue Feb 11, 2014 1:21 am 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
HI,


In cases where your getting bad results with an analogue input to any micro controller it may be best to see why the results are unstable as curing this is often the best solution,

Take some raw readings and see how much they are varying from what you expect, if for example you expecting to see 2.5V from a sensor check the actual source with a meter and see if its changing and if so does that change match the readings, you can then look to see why the sensor is giving such poor results, if the reading from the sensor is stable then replace the sensor with a POT to give an output to the Nano that's the same as your experiencing from the sensor and see if that's stable then, this will give you an idea where to look for the problem and you can then look at a solution in hardware, for example it may be the power supplies to the sensor that are incorrect or unstable or the analog lines may be running next to a source of interference like a wire feeding a DC motor with a PWM signal for example depending on the project, decoupling of the power supply rails can often cause spurious analogue readings so check to see if you have DC Electrolytic reservoir capacitors on the PCB and that each IC in some cases needs to have a 0.1uF ceramic decoupling capacitor right across its power supply pins,

A quick check on these things will often allow you to cure the problem in hardware and that's a far better than solution than masking a hardware problem with software,

If you have got all this sorted then before you look at circular buffer averaging take a look at the ADIN16 command, this will take 64 readings of the ADC and return the result, you can then right shift the result to get an average reading or depending on how many time you shift the result you can get more effective resolution from the ADC input, the down side to this method is the reduction in the sampling speed as you have to wait for 64 readings before you have a result but this may not be a problem depending on the application, the circular buffer will give you a rolling average once you have collected enough reading to fill the buffer BUT the lack of response time to a step change in the analogue signal may be a side effect that's not good again depending on the exact application,


I hope this helps, keep us posted on the results,

Many thanks

Dave
BasicMicroUK

http://www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Tue Feb 11, 2014 2:57 am 
Offline
Citizen

Joined: Thu Feb 06, 2014 7:37 pm
Posts: 19
THANKS FOR THE REPLY'S: I will experiment with kurteck code example,that looks like what I am trying to do...thank you very much for any help.


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Wed Feb 12, 2014 3:19 am 
Offline
Master

Joined: Sun Jan 03, 2010 4:01 pm
Posts: 134
Location: Eau Claire, WI
@Dave,

Thanks for the responses as I will be working on a project shortly with similar needs (average value)

Just to clarify I thought the ADIN16 was a single read command; in other words the ADIN16 command only had one sample?

Any clarification would be very helpful as I'm going to continue work on a load cell project for weighing but I may need to switch to a discrete ADC if I can't get the Nano-18s to produce a stable reading (if that sound negative that is not my intent; only to point out that in certain cases a discrete ADC will typically out perform a integrated ADC within a micro-controller as that Nano-18s offer a great value).

Thanks-Brian


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Wed Feb 12, 2014 8:19 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
Hi Brian,

Well the basic ADIN command will take one reading of the selected analogue port and returns a value between 0-1023 for the 0-5 Volts input, when you use the ADIN16 command it will take 64 readings and then return the total value of each reading so this will take away most measurement noise BUT the time to convert is longer,

You can use this in 2 ways, the raw value can be divided to get to the original resolution with averaging or you can divide less and get an effective higher resolution and that's useful when you want to convert units for example to get an output reading in Volts with the range 0.00 to 5.00 or to scale to some other units such as Kg for a load cell input,

I set up a little test to demonstrate these two commands, please see :- http://youtu.be/Fic6tmUk08U

Its a little messy as it was just done on the spur of the moment but it shows the two commands in use and its easy to follow,

The reading stability can be effected by a number of external influences such as interference on the analogue input line and even an earth loop can cause common mode problems where to 0 reference of the input is lifted slightly, in the normal set-up the ADC converter uses the Nano-18 supply lines Vdd and Vss as the reference for the ADC conversion so the input signal in the range 0-5.00 Volts is divided by 1024 giving 0.00488V per returned ADC count BUT if you are wanting to use a sensor with a lower output than 5V you can specify that the ADC unit uses an external referance input and so if you required for example a 0-2.5V input range by using a 2.5V precision voltage reference IC to generate a clean VREF+ input to the Nano-18 you will then reduce noise in the measurement as now your using a stable voltage for the ADC rather than a noisy digital supply line and in the case of the 2.5V example you double your resolution to 0.00244 Volts per ADC count,

I hope this helps,

Many thanks

Dave
BasicMicroUK
http://www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Fri Feb 14, 2014 12:31 am 
Offline
Master

Joined: Sun Jan 03, 2010 4:01 pm
Posts: 134
Location: Eau Claire, WI
Thanks Dave for the response.

I'll take a look at the link your provided this weekend.

I do have some precision reference voltages sources I was going to attempt to use along with some new Instrumentation Amplifiers as load cells produce very low voltages (I believe it is less than 10mV full scale).

Thanks again-Brian


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Fri Feb 14, 2014 12:31 am 
Offline
Master

Joined: Sun Jan 03, 2010 4:01 pm
Posts: 134
Location: Eau Claire, WI
Thanks Dave for the response.

I'll take a look at the link your provided this weekend.

I do have some precision reference voltages sources I was going to attempt to use along with some new Instrumentation Amplifiers as load cells produce very low voltages (I believe it is less than 10mV full scale).

Thanks again-Brian


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Fri Aug 29, 2014 1:45 pm 
Offline
New User

Joined: Fri Aug 29, 2014 1:41 pm
Posts: 1
I discovered glitches in the code. Some were due to syntax changes by BasicMicro. Some were just outright bloopers. No one mentioned these?!







_________________________________________________________________________________________________________________
camila


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Tue Sep 02, 2014 9:16 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
HI

YES there was a note onthe code that it was not for Nano to start with and was a cut and paste job so its likley there would be errors but it gives the basic idea,

If you now have a debugged version please share it here on the forum for others to benefit from,

BasicMicroUK have now purchased the NANO range and are looking after new sales and technical support,

We hope to support the Nano range for a long time to come and we are just about to launch a Nano-18X, A 20Mhz version of the Nano-18 like the Nano-28X and Nano-40X, this will have a NEW IDE software BMUK Studio covering all of the Nano range, we will be adding more functions to this software in due course and a MAP command the same as used in the Arduino IDE to scale values will be ready on the first version 3.0.0.0 as well as Nano-18X support,

All the original Nano development PCB's are now back in stock and we are working on more hardware such as a Nano-28 MOSFET board,

You can view our website at www.basicmicro.co.uk and we have an eBay shop at http://stores.ebay.co.uk/Basic-Micro-UK ... 7675.l2563

I hope this helps,

Thanks
Dave
BasicMicroUK
www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: CIRCULAR BUFFER
PostPosted: Fri Sep 05, 2014 3:34 am 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
In your project your are reading a low voltage of 10 mv. My suggestion to you it is the use of an op_amp to bring the input signal to level so can read it better. You may set up the gain of 50 and it will bring the voltage to .5 volt then dived the counts by 50. I used this technique to read thermocouple inputs and it worked very good.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO