This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using ring buffer to store 16 bits ADC data

Ring buffer operates on uint8_t , but I would like to use ring buffer to store 16 bits ADC data.  What is the simplest  solution?

Converting 16 bits data into 8 bits, store the data into ring buffer, get data back from ring buffer and then convert the data back to 16 bits, seems like a lot of work.

Is it possible to modify the ring buffer to use 16 bits instead of 8 bit for data storage?   Is it simpler to just write a simple ring buffer;  do you have an example?

I just need a simple ring buffer to store certain amount of ADC data before I process the data.   A simple buffer would not work.

Parents
  • I need to put in 640 bytes into the ring buffer at a time and take out 1400 bytes once the buffer exceeds 1400 bytes.  Do you have any example?

    Also do you have an example of typecasting the 16 bit data into 8 bit, store the data in ring buffer, take out the date and then typecast the data back to 16 bits.

    I am a beginner in C.

    Thank You

  • raymond said:
    I am a beginner in C.

    No problem at all! :) 
    If so, I would suggest that you look into creating the ring-buffer yourself (or finding an existing 16 bit ring buffer implementation) instead of modifying the 8bit ring buffer from the SDK. I recommend this because you will have much more control over what is actually happening in your own implementation, and so that you may avoid the testing of the modified ring buffer to ensure that you did not break it.

    raymond said:
    I need to put in 640 bytes into the ring buffer at a time and take out 1400 bytes once the buffer exceeds 1400 bytes.  Do you have any example?

    I do not have any other implementation of a ring buffer other than the one we provide in the SDK, unfortunately.
    A brief google search yielded this article on how to implement a simple ring buffer in C, that might be helpful for you to take a look at.

    raymond said:
    Also do you have an example of typecasting the 16 bit data into 8 bit, store the data in ring buffer, take out the date and then typecast the data back to 16 bits.

    Unfortunately I do not have an example that showcases this, but it follows the regular syntax for typecasting in C.
    However, typecasting between uint8_t and uint16_t may prove cumbersome due to endianness in C.
    Are you familiar with bit shifting? Bit shifting would be the easiest way for you to do this.
    It is an important concept in embedded C programming. To avoid the edianness pitfalls, you should instead use bit shifting to convert your uint8_t to uint16_t, and reverse. A brief google search yielded this tutorial, which might be useful for you to have a look at. This way, you can create a function for merging two uint8_t to an uint16_t, and a function for the opposite.

    Please do not hesitate to ask if any part of my comment is unclear, or if you have any other issues or questions!

    Best regards,
    Karl

Reply
  • raymond said:
    I am a beginner in C.

    No problem at all! :) 
    If so, I would suggest that you look into creating the ring-buffer yourself (or finding an existing 16 bit ring buffer implementation) instead of modifying the 8bit ring buffer from the SDK. I recommend this because you will have much more control over what is actually happening in your own implementation, and so that you may avoid the testing of the modified ring buffer to ensure that you did not break it.

    raymond said:
    I need to put in 640 bytes into the ring buffer at a time and take out 1400 bytes once the buffer exceeds 1400 bytes.  Do you have any example?

    I do not have any other implementation of a ring buffer other than the one we provide in the SDK, unfortunately.
    A brief google search yielded this article on how to implement a simple ring buffer in C, that might be helpful for you to take a look at.

    raymond said:
    Also do you have an example of typecasting the 16 bit data into 8 bit, store the data in ring buffer, take out the date and then typecast the data back to 16 bits.

    Unfortunately I do not have an example that showcases this, but it follows the regular syntax for typecasting in C.
    However, typecasting between uint8_t and uint16_t may prove cumbersome due to endianness in C.
    Are you familiar with bit shifting? Bit shifting would be the easiest way for you to do this.
    It is an important concept in embedded C programming. To avoid the edianness pitfalls, you should instead use bit shifting to convert your uint8_t to uint16_t, and reverse. A brief google search yielded this tutorial, which might be useful for you to have a look at. This way, you can create a function for merging two uint8_t to an uint16_t, and a function for the opposite.

    Please do not hesitate to ask if any part of my comment is unclear, or if you have any other issues or questions!

    Best regards,
    Karl

Children
No Data
Related