问题描述
我正在尝试将压力传感器(MS5803-14BA)与我的电路板(NUCLEO-STM32L073RZ)连接.
I'm trying to interface a pressure sensor (MS5803-14BA) with my board (NUCLEO-STM32L073RZ).
根据数据表(第3页),压力传感器需要几毫秒的时间才能准备好读取测量值.对于我的项目,我会对最高分辨率(原始数据转换大约需要10毫秒)感兴趣.
According to the datasheet (page 3), the pressure sensor requires some milliseconds before the measurement is ready to be read. For my project, I would be interested in the highest resolution that requires around 10 ms for the conversion of the raw data.
不幸的是,该压力传感器没有任何可用于查看测量准备就绪时间的中断引脚,因此,我暂时解决了在请求新数据后延迟的问题.
Unfortunately, this pressure sensor doesn't have any interrupt pin that can be exploited to see when the measurement is ready, and therefore I temporarily solved the problem putting a delay after the request of new data.
我不喜欢当前的解决方案,因为在那10毫秒内,我可以让MCU在其他地方工作(我的板上还连接了其他几个传感器),但是没有任何中断引脚,我不确定解决此问题的最佳方法是什么?
I don't like my current solution, since in those 10 ms I could put the mcu working on something else (I have several other sensors attached to my board), but without any interrupt pin, I'm not sure about what is the best way to solve this problem.
我想到了另一个解决方案:使用一个计时器,该计时器每20秒触发一次并执行以下操作:
Another solution came into my mind: Using a timer that triggers every say 20 ms and performs the following operations:
1.a Read the current value stored in the registers (discarding the first value)
1.b Ask for a new value
这样,在下一次迭代中,我只需要读取上一次迭代结束时请求的值即可.
In this way, at the next iteration I would just need to read the value requested at the end of the previous iteration.
我不喜欢我的测量值总是20毫秒.直到延迟保持20毫秒,它应该还是可以的,但是如果我需要降低速率,则解决方案的读数年龄"会增加.
What I don't like is that my measurement would be always 20 ms old. Until the delay remains 20 ms, it should be still fine, but if I need to reduce the rate, the "age" of the reading with my solution would increase.
您对如何处理还有其他想法吗?
Do you have any other idea about how to deal with this?
谢谢.
注意:如果您需要查看我当前的实施情况,请告诉我.
Note: Please let me know if you would need to see my current implementation.
推荐答案
首先,感谢您的建议.我试图分析您提出的每个可能的解决方案.
First of all thank you for your suggestions. I tried to analyze every single possible solution you proposed.
Peter提出的解决方案似乎非常有趣,但是我不得不说,在浏览了数次数据表之后,我认为这是不可行的.我的考虑基于以下事实.
The solution proposed by Peter seemed very interesting but I have to say that, after having gone through the datasheet several times, I don't believe that is feasible. My consideration is based on the following facts.
使用范围,我看到发送命令进行转换后立即收到确认.参见以下有关温度转换的图像:
Using a scope I see that the acknowledge is received right after sending the command for doing conversion. See following image concerning the temperature conversion:
在我看来,命令后的确认位非常清楚.之后,SDA线(黄色)变高,因此,我看不到如何利用它来检测转换准备就绪的时间.
It seems quite clear to me the acknowledge bit right after the command. After that the SDA line (yellow) goes high, therefore I don't see how it is possible that I can exploit that for detecting when the conversion is ready.
关于使用SPI时的解决方案,是的,在转换过程中SDO仍然很低,但是我不能使用它:我需要坚持使用I2C.此外,我在该SPI总线上连接了其他传感器,并且我同意Gabriel Staples所说.
Concerning the solution when using SPI, yes, the SDO remains low during the conversion, but I cannot use it: I need to stick with I2C. Furthermore, I have other sensors attached to that SPI bus and I agree with what Gabriel Staples says.
考虑之后,我选择了加布里埃尔·斯台普斯(Gabriel Staples)提出的解决方案(考虑到要读取压力值,我还需要读取并转换温度).
After my consideration I went for the solution proposed by Gabriel Staples (considering that, in order to read pressure value, I also need to read and convert temperature).
我当前的解决方案基于具有6个状态的状态机.在我的解决方案中,我区分了压力转换的等待时间和温度转换的等待时间,其想法是,如果使用不太精确的温度读数,我可以尝试查看压力读数会下降多少.
My current solution is based on a state machine with 6 states. In my solution, I distinguish between the wait time for the pressure conversion and the wait time for the temperature conversion with the idea the I could try to see how much the pressure reading degrades if I use a less precise temperature reading.
这是我当前的解决方案.在main内部调用以下函数:
Here is my current solution. The following function is called inside the main while:
void MS5803_update()
{
static uint32_t tStart; // us; start time
switch (sensor_state)
{
case MS5803_REQUEST_TEMPERATURE:
{
MS5803_send_command(MS5803_CMD_ADC_CONV + TEMPERATURE + baro.resolution);
tStart = HAL_GetTick();
sensor_state = MS5803_WAIT_RAW_TEMPERATURE;
break;
}
case MS5803_WAIT_RAW_TEMPERATURE:
{
uint32_t tNow = HAL_GetTick();
if (tNow - tStart >= conversion_time)
{
sensor_state = MS5803_CONVERTING_TEMPERATURE;
}
break;
}
case MS5803_CONVERTING_TEMPERATURE:
{
MS5803_send_command(MS5803_CMD_ADC_READ);
uint8_t raw_value[3]; // Read 24 bit
MS5803_read_value(raw_value,3);
temperature_raw = ((uint32_t)raw_value[0] << 16) + ((uint32_t)raw_value[1] << 8) + raw_value[2];
sensor_state = MS5803_REQUEST_PRESSURE;
break;
}
case MS5803_REQUEST_PRESSURE:
{
MS5803_send_command(MS5803_CMD_ADC_CONV + PRESSURE + baro.resolution);
tStart = HAL_GetTick();
sensor_state = MS5803_WAIT_RAW_PRESSURE;
break;
}
case MS5803_WAIT_RAW_PRESSURE:
{
uint32_t tNow = HAL_GetTick();
if (tNow - tStart >= conversion_time)
{
sensor_state = MS5803_CONVERTING_PRESSURE;
}
break;
}
case MS5803_CONVERTING_PRESSURE:
{
MS5803_send_command(MS5803_CMD_ADC_READ);
uint8_t raw_value[3]; // Read 24 bit
MS5803_read_value(raw_value,3);
pressure_raw = ((uint32_t)raw_value[0] << 16) + ((uint32_t)raw_value[1] << 8) + raw_value[2];
// Now I have both temperature and pressure raw and I can convert them
MS5803_updateMeasurements();
// Reset the state machine to perform a new measurement
sensor_state = MS5803_REQUEST_TEMPERATURE;
break;
}
}
}
我不假装自己的解决方案更好.我只是发布它,以征询大家的意见.注意:我仍在努力.因此,我不能保证没有错误!
I don't pretend that my solution is better. I just post it in order to have an opinion from you guys. Note: I'm still working on it. Therefore I cannot guarantee is bug-free!
对于PeterJ_01:我可以同意这并不是严格的教学门户,但是我相信这里的每个人都会提出问题,以学习新知识或提高自我.因此,如果您认为使用ack的解决方案更好,那么如果您可以向我们展示您的想法草稿,那就太好了.对我来说,这将是新知识.
For PeterJ_01: I could agree that this is not strictly a teaching portal, but I believe that everybody around here asks questions to learn something new or to improve theirselves. Therefore, if you believe that the solution using the ack is better, it would be great if you could show us a draft of your idea. For me it would be something new to learn.
任何进一步的评论,表示赞赏.
Any further comment is appreciated.
这篇关于从没有中断引脚的传感器读取数据的最佳方法,这需要一些时间才能准备好措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!