AVR Ring Buffer

Hello, everyone I’m new here but not new at embedded things. So I was newbie and know how hard is to start something. So I will make some code snippets for you guys. So here is my first [snippet. Sorry guys for link cause I’m too lazy to copy it here… And Sorry if I posted to incorrect category I saw in PIC’s Code Snippets but in AVR dont :smiley:

Aurimas](http://blog.gcds.lt/2011/03/23/ring-buffer/)

cool

what does PG2 do? if you don’t need PG2 and want another way of doing “critical sections”, look up atomic blocks

http://www.nongnu.org/avr-libc/user-man … tomic.html

I just forgot to remove it was status led just incase if it get stuck inside :smiley: But it never shows up :stuck_out_tongue:

When did your code get stuck? I don’t see any loops, so you might have had a loop in your interrupt code.

Also, where did you learn this ring buffer technique? It’s usually done with a read and write pointer, instead of a single pointer and length. I’m not sure about the advantages of the methods but this is the first time I’ve seen it done your way.

frank26080115:
When did your code get stuck? I don’t see any loops, so you might have had a loop in your interrupt code.

Also, where did you learn this ring buffer technique? It’s usually done with a read and write pointer, instead of a single pointer and length. I’m not sure about the advantages of the methods but this is the first time I’ve seen it done your way.

Sorry, It not get stuck just mistake in writing :smiley:

I don’t know this should be like this. You create like 10 byte variable filling it and updating dataindex and datalength and when you get full you starting from index 0 and so on…

lots of examples of ring buffer code on avrfreaks.net, forum, projects section.

Most ring buffers have a “head” pointer and a “tail” pointer. the buffer size is a power of two so it’s easy to use an AND operation to wrap the pointers in a circular manner.

You can use two index numbers in the same manner.

When head == tail, the buffer is empty. Storing into the buffer advances the head. Retrieving advances the tail pointer or index.

Beware interrupts and use of the volatile declaration if interrupts alter the buffer contents.