我需要一点帮助。我想将数组中的元素向上移动一个元素,以便新位置 1 包含位置 1 中的旧值,new-2 包含 old-1,依此类推。旧的最后一个值被丢弃,第一个位置的新值是我每秒给出的新值。
我使用大小为 10 的数组:
uint32_t TEST[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
我使用的代码必须每秒执行一次。
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* Code for shifting the elements */
for (int i = 0; i < 9; i++)
{
TEST[i+1] = TEST[i];
}
TEST[0] = HAL_ADC_GetValue(&hadc);
}
Ringbuffer会减少数据的复制,如果你希望last_pos指向最新的样本,你可以在读取之前增加它。
uint32_t TEST[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int last_pos = 0; /* init to any valid number between 0 and 9 inclusively */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
last_pos = (last_pos + 1) % 10;
TEST[last_pos] = HAL_ADC_GetValue(&hadc);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)