如何将 16 位整数连续附加到固定的 8 位数组

How to continously append a 16 bit integer to a fixed 8 bit array

我正在尝试将 uint16_t 类型的 ADC 值存储到 32KB uint8_t,因为我想将数据写入 SD 卡。目前,我正在努力解决如何将转换后的 16 位整数复制到缓冲区中的问题。

我的代码如下所示:

#define SD_WRITE_BUF_SIZE 32768 //32KB

uint16_t adc_value;  // holds the adc Converted value
uint8_t adc_conv_value[2]; // array to store the 8bit conversion of adc_value;

UINT bytesWritten;

int adc_callback_counter = 0;

uint8_t sd_buf_write[SD_WRITE_BUF_SIZE];

int main void
{
    Mount_SD_CARD(); // Mount SD Card
    Open_SD_CARD_write(); // create file and open for writing
    Start_ADC_Conversion(); // Start ADC Conversion;

    while (1)
    {

        if(adc_callback_counter >= (32768/2))
        {
            write_res = f_write(&myFile, sd_buf_write, SD_WRITE_BUF_SIZE, &bytesWritten);
             if(write_res ==  FR_OK)
             {
                 // Stop ADC
                 ADC_Stop();
                 // reset counter 
                 adc_conv_counter = 0;               
                 // close file
                 f_close(&myFile);
        }

    }
}

ADC_Conversion_Callback()
{

    // convert to 2 8 bit values;
   adc_conv_value[0] = (adc_values & 0xFF);
   adc_conv_value[1] = (adc_values >> 8) & 0xFF;

   // How do I use memcpy to continously copy adc_conv_value to sd_buf_write
   memcpy(sd_buf_write, ???, sizeof(??));

   // Increment counter;
   adc_callback_counter++;      

}

ADC_Conversion_Callback()函数中,如何连续追加adc_conv_valuesd_buf_write直到后一个数组满?

// How do I use memcpy to continously copy adc_conv_value to sd_buf_write

   memcpy(sd_buf_write, ???, sizeof(??));

您可以使用 adc_callback_counter 变量来告诉 memcpy 被调用了多少次。

memcpy((char *)sd_buf_write + (sizeof (adc_conv_value) * adc_callback_counter), adc_conv_value, sizeof(adc_conv_value));

(sizeof (adc_conv_value)* adc_callback_counter) 因为我们每次都在复制sizeof (adc_conv_value),所以起始位置会是sd_buf_write + (sizeof (adc_conv_value) * adc_callback_counter)