Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

atmega64l programming

atmega64l programming

2009-07-16 by iijfet

hi,i'm so deprss.i want to have a double-usart by atmega64l.

but i program it,exp for test i make PORTC=255,it program,but these

pins of micro are not 1-logic,and these voltage are 0;

i don't know really the correct fusebit configuration at usart state.i

know M103 must set to 0,but what about WTDON,CLK,.. .

every day i go to the center of city(Tehran) but when 3time i program

these fusebits,micro isn't programed.

Moving average filter

2009-07-17 by Антощенков Роман Викторович

Hello!

I using WinAVR 20090313 and ATMega32.
I write code for time critical application.
There is a moving average filter algorithm.

    unsigned char data    = 0;
    unsigned char i       = 0;
    unsigned char mafinit = 0;
    unsigned int  mafsum  = 0;
    unsigned char mafdata[16];

    if (!mafinit)
    {
      for (i = 0; i < 16; i++)
        mafdata[i] = data;
      mafinit = 1;
    }

    for (i = 0; i < 15; i++)
    {
      mafdata[i] = mafdata[i+1];
      mafsum += mafdata[i];
    }
    mafdata[15] = data;
    mafsum += data;
    data = mafsum / 16;

Are there any ways to speedup algorithm?

-- 
Best regards,
Roman Antoshchenkov
mailto:djantoxa@rambler.ru

Re: [AVR-Chat] Moving average filter

2009-07-17 by Andreas Stemmer

Hallo Roman,

there are two things you could do to speed up the calculation significantly. First of all, it's not necessary to shift the data through the array with each update cycle. Instead, keep the data in its position and just use an index to the oldest element (like a ring buffer).

It's also not necessary to calculate the sum which each update. Instead, save the old sum and just remove the oldest element and add the new element with each update.

Something like (untested!):

#define FILTERSIZE 16
static unsigned char mafData[FILTERSIZE];
static unsigned char mafIdx;
static unsigned int mafSum;

void mafInit(void)
{
    int i;
    for (i=0; i<FILTERSIZE; i++)
        mafData[i] = 0;
    mafSum = 0;
}

unsigned char mafUpdate(unsigned char data)
{
    /* remove oldest element */
    mafSum -= mafData[mafIdx];

    /* add new element */
    mafData[mafIdx] = data;
    mafSum += mafData[mafIdx];

    /* increment index */
    if (++mafIdx >= FILTERSIZE)
        mafIdx = 0;

    /* return moving average */
    return (mafSum/FILTERSIZE);
}

Depending on your data, returning the filter value as float or at least doing some proper rounding after division could be sensible.

Andreas


-------- Original-Nachricht --------
Show quoted textHide quoted text
> Datum: Fri, 17 Jul 2009 12:07:14 +0300
> Von: "Антощенков Роман Викторович" <djantoxa@rambler.ru>
> An: AVR-Chat@yahoogroups.com
> Betreff: [AVR-Chat] Moving average filter

> Hello!
> 
> I using WinAVR 20090313 and ATMega32.
> I write code for time critical application.
> There is a moving average filter algorithm.
> 
>     unsigned char data    = 0;
>     unsigned char i       = 0;
>     unsigned char mafinit = 0;
>     unsigned int  mafsum  = 0;
>     unsigned char mafdata[16];
> 
>     if (!mafinit)
>     {
>       for (i = 0; i < 16; i++)
>         mafdata[i] = data;
>       mafinit = 1;
>     }
> 
>     for (i = 0; i < 15; i++)
>     {
>       mafdata[i] = mafdata[i+1];
>       mafsum += mafdata[i];
>     }
>     mafdata[15] = data;
>     mafsum += data;
>     data = mafsum / 16;
> 
> Are there any ways to speedup algorithm?
> 
> -- 
> Best regards,
> Roman Antoshchenkov
> mailto:djantoxa@rambler.ru

Re: [AVR-Chat] Moving average filter

2009-07-17 by Andreas Stemmer

Andreas Stemmer wrote:
> It's also not necessary to calculate the sum which each update. Instead,
> save the old sum and just remove the oldest element and add the new element
> with each update.

Just an additional note: this works fine with integers - with floats however, this could lead to numerical problems... I don't know how much they matter in reality.

Andreas

Re[2]: [AVR-Chat] Moving average filter

2009-07-17 by Антощенков Роман Викторович

Hello, Andreas.


>   
> Hallo Roman,

> there are two things you could do to speed up the calculation
> significantly. First of all, it's not necessary to shift the data
> through the array with each update cycle. Instead, keep the data in
> its position and just use an index to the oldest element (like a ring buffer).

> It's also not necessary to calculate the sum which each update.
> Instead, save the old sum and just remove the oldest element and add the new element with each update.

Thanks for fast reply. It is a good solution

-- 
Best regards,
Roman Antoshchenkov
mailto:djantoxa@rambler.ru

Re: [AVR-Chat] Moving average filter

2009-07-17 by David Kelly

On Fri, Jul 17, 2009 at 12:07:14PM +0300, ???????????????????? ?????????? ???????????????????? wrote:
> Hello!
> 
> I using WinAVR 20090313 and ATMega32.
> I write code for time critical application.
> There is a moving average filter algorithm.

[...]

Do not start a new thread with an edited Reply to another thread.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Re: [AVR-Chat] Moving average filter

2009-07-17 by Adeilton Oliveira

Hello.

I don't know how the division operation is internally implemented, but I 
think in your case you can use >>4 instead of /16.



?????????? ????? ?????????? wrote:
Show quoted textHide quoted text
>
> Hello!
>
> I using WinAVR 20090313 and ATMega32.
> I write code for time critical application.
> There is a moving average filter algorithm.
>
> unsigned char data = 0;
> unsigned char i = 0;
> unsigned char mafinit = 0;
> unsigned int mafsum = 0;
> unsigned char mafdata[16];
>
> if (!mafinit)
> {
> for (i = 0; i < 16; i++)
> mafdata[i] = data;
> mafinit = 1;
> }
>
> for (i = 0; i < 15; i++)
> {
> mafdata[i] = mafdata[i+1];
> mafsum += mafdata[i];
> }
> mafdata[15] = data;
> mafsum += data;
> data = mafsum / 16;
>
> Are there any ways to speedup algorithm?
>
> -- 
> Best regards,
> Roman Antoshchenkov
> mailto:djantoxa@rambler.ru <mailto:djantoxa%40rambler.ru>
>
>

Re: [AVR-Chat] Moving average filter

2009-07-18 by dpharris@telus.net

Use a revolving buffer to store your last 16 values.  

Do an initial sum from the first 16 values.  

Then at each step: 

  if(++i>15) i=0;   // get the next mafdata slot
  mafsum = mafsum + data - mafdata[i];  // update running sum
  mafdata[i] = data;   // remember latest value in slot

  data = mafsum / 16;  // calculate running average
  
  Quoting Adeilton Oliveira <adeiltonjunior@terra.com.br>:

David
Show quoted textHide quoted text
> Hello.
> 
> I don't know how the division operation is internally implemented, but I 
> think in your case you can use >>4 instead of /16.
> 
> 
> 
> ?????????? ????? ?????????? wrote:
> >
> > Hello!
> >
> > I using WinAVR 20090313 and ATMega32.
> > I write code for time critical application.
> > There is a moving average filter algorithm.
> >
> > unsigned char data = 0;
> > unsigned char i = 0;
> > unsigned char mafinit = 0;
> > unsigned int mafsum = 0;
> > unsigned char mafdata[16];
> >
> > if (!mafinit)
> > {
> > for (i = 0; i < 16; i++)
> > mafdata[i] = data;
> > mafinit = 1;
> > }
> >
> > for (i = 0; i < 15; i++)
> > {
> > mafdata[i] = mafdata[i+1];
> > mafsum += mafdata[i];
> > }
> > mafdata[15] = data;
> > mafsum += data;
> > data = mafsum / 16;
> >
> > Are there any ways to speedup algorithm?
> >
> > -- 
> > Best regards,
> > Roman Antoshchenkov
> > mailto:djantoxa@rambler.ru <mailto:djantoxa%40rambler.ru>
> >
> > 
> 
> 
> 
> ------------------------------------
> 
> Yahoo! Groups Links
> 
> 
> 
>

Re: [AVR-Chat] Moving average filter

2009-07-18 by Roy E. Burrage

How about just modifying an Atmel application note?

       http://www.atmel.com/dyn/products/app_notes.asp?family_id=607

Look at AVR222 and 223.  Both address digital filters.




dpharris@telus.net wrote:
> Use a revolving buffer to store your last 16 values.  
>
> Do an initial sum from the first 16 values.  
>
> Then at each step: 
>
>   if(++i>15) i=0;   // get the next mafdata slot
>   mafsum = mafsum + data - mafdata[i];  // update running sum
>   mafdata[i] = data;   // remember latest value in slot
>
>   data = mafsum / 16;  // calculate running average
>   
>   Quoting Adeilton Oliveira <adeiltonjunior@terra.com.br>:
>
> David
>
>   
>> Hello.
>>
>> I don't know how the division operation is internally implemented, but I 
>> think in your case you can use >>4 instead of /16.
>>
>>
>>
>> ?????????? ????? ?????????? wrote:
>>     
>>> Hello!
>>>
>>> I using WinAVR 20090313 and ATMega32.
>>> I write code for time critical application.
>>> There is a moving average filter algorithm.
>>>
>>> unsigned char data = 0;
>>> unsigned char i = 0;
>>> unsigned char mafinit = 0;
>>> unsigned int mafsum = 0;
>>> unsigned char mafdata[16];
>>>
>>> if (!mafinit)
>>> {
>>> for (i = 0; i < 16; i++)
>>> mafdata[i] = data;
>>> mafinit = 1;
>>> }
>>>
>>> for (i = 0; i < 15; i++)
>>> {
>>> mafdata[i] = mafdata[i+1];
>>> mafsum += mafdata[i];
>>> }
>>> mafdata[15] = data;
>>> mafsum += data;
>>> data = mafsum / 16;
>>>
>>> Are there any ways to speedup algorithm?
>>>
>>> -- 
>>> Best regards,
>>> Roman Antoshchenkov
>>> mailto:djantoxa@rambler.ru <mailto:djantoxa%40rambler.ru>
>>>
>>>
>>>       
>>
>> ------------------------------------
>>
>> Yahoo! Groups Links
>>
>>
>>
>>
>>     
>
>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.392 / Virus Database: 270.13.19/2245 - Release Date: 07/18/09 05:57:00
>
>   


[Non-text portions of this message have been removed]

Re: [AVR-Chat] Moving average filter

2009-07-19 by David Kelly

On Jul 17, 2009, at 5:43 PM, Adeilton Oliveira wrote:

> I don't know how the division operation is internally implemented,  
> but I
> think in your case you can use >>4 instead of /16.


Its a simple matter to examine the generated assembly with the  
debugger in AVR Studio. Or to ask avr-gcc (WinAVR) to leave the  
assembly laying around after a compile, or to generate a disassembly  
from the object file.

Think you will find avr-gcc is smart enough to >>4 for a constant / 
16. I know it will replace /2 with a right shift.

Meanwhile using the division in one's source is usually the right  
thing to do for human comprehension. Replacing division with shifts  
is one of those things that makes assembly harder for someone other  
than the original author to understand. Makes it harder for the  
original author to understand 6 months after it was written.

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.