如何将数据格式化为const uint16_t类型,以便在spi库中使用



所以我一直在写一些代码并完成了它,我所要做的就是通过SPI发送它。然而,我没有意识到我使用的SPI库只接受const uint_16t数据。我尝试设置一个正确数据类型的临时变量,并使用引用通过SPI发送,这很有效,但因为它是一个常量变量,所以无法更改,这是我需要它能够做到的。

以下是我的代码,任何对此的帮助都将是非常好的,作为参考,这是在C++中完成的,并使用SPI库。SPI库是Raspberry Pi Pico SDK的一部分(由于代码很长,所以只包括所需的部分(。我确实尝试过使用指针来更改常量变量,我认为你可以在C中这样做,但我无法让它在C++中工作。

如果能提供任何帮助,我将不胜感激,因为我对如何解决这个问题感到非常困惑。

谢谢,院长

class Frequency_Values{
Public:
static uint16_t position;
//constructors and special member functions here
private:
uint16_t MSB_LUT[401];
uint16_t LSB_LUT[401];
};
//----------static variable definition---------------------
unsigned short Frequency_Values::position = 0;
//---------------------------------------------------------
//-------------------get function definitions--------------
uint16_t Frequency_Values::get_MSB_LUT_Value()
{
return (Frequency_Values::MSB_LUT[position]);
}
uint16_t Frequency_Values::get_LSB_LUT_Value()
{
return (Frequency_Values::LSB_LUT[position]);
}
//-----------------------------------------------------------
Frequency_Values MSB, LSB;


int main(){
while(1){  // to show code runs multiple times
//--------------------------------------------------
const uint16_t LSB_Holder = LSB.get_LSB_LUT_Value();   // this is what I tried to get it in the correct data type
//which worked but as const the value wont change
const uint16_t MSB_Holder = MSB.get_MSB_LUT_Value();    
//--------------------------------------------------
}
spi_write16_blocking(SPI_PORT, &LSB_Holder, 1);     
spi_write16_blocking(SPI_PORT, &MSB_Holder, 1);     
}

这最终通过删除pico-sdk,spi库中的const说明符得到了修复。

最新更新