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

时间:2019-10-01 13:21:48

标签: c arrays integer memcpy

我想将类型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_value附加到sd_buf_write直到后一个数组已满?

1 个答案:

答案 0 :(得分:1)

  

//如何使用memcpy将adc_conv_value连续复制到   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)