你好,希望我把这个放在正确的地方!我正在研究一个将MCP74A/D转换器与树莓PI 3模型B接口的项目。实际上,我现在有一些代码运行起来,运行得很好,读取输入0——除了我不完全理解它,以及我将如何修改代码以从其他用途读取。有能力的渠道!让我开始分享我的代码-我不认为这是重要的,如果你不熟悉图书馆在使用中,它是一个相当通用的SPI
通信库。我已经通过将PyPIO树莓PI库与许多Python脚本的不同用法一起使用,例如Char WrreEdCMD(3)={ 1, 128, 0 };char Read数据(3);int速度=300000;int SPi句柄;int ReADVAL;SpHORADER=SPIOPEN(0,速度,0);而(1){ SPIXFER(SPIHA)NDLE,(CHAR*)和RealEdMCMD,(CHAR*)和Read数据,SIZEOF(Read OpDATA);ReADVAL=((Read OpthDATA(1)和3;lt;lt;8))Read数据(2);PrimTf(“值:%i n”,ReaDAL);}如我所说,这个代码似乎工作得很好。我已经做了一些实验,看看速度相对于速度值实际上是在增加和减少,我得到了我所期望的值。下面是我的问题;我在MCP34中写的那些三字节真的是什么?我在数据表(特别是表5-1)上看了很久,我觉得我将发送一个单行字节的二进制数为1×1 dk 0的0(或0x18,假设dk是0),这将表示起始位的1,单端A/D的1,为0的A/D。不必担心,因为我使用MCP324,然后两个零点来请求A/D的信道0,但是,我看到三个字节- 1/128 / 0,这是0 0 0 0 0 0 0 0 1 0,{1×2 }。我一定是误读了数据表!我尝试这样做,发送一个字节24,而不是三,但我真的没有得到任何东西,我希望-胡言乱语,我相信。如果MCP34是一个12位分辨率的A/D转换器,为什么我只得到10位(0?1023)?我可以看到,只有11,最后一个比特是一个符号位或什么,但在这种情况下,我本来希望得到的值范围从0到2047,而不是我现在得到的是0 - 1023。好了,就是这样。希望我已经解释得很好,而且在正确的地方。如果我能做出任何澄清,请告诉我。提前感谢任何建议或指导任何人可能有!恭敬地,Marshall
以上来自于百度翻译
以下为原文
Hello,
Hopefully, I am pos
ting this in the right place!
I am working on a project that interfaces an MCP3204 A/D converter to a Raspberry Pi 3 Model B. I actually now have some code up and running that is working quite well, reading input 0 - except that I don't fully understand it and how I would go about modifying the code to read from the other available channels!
Let me start off by sharing my code - I don't think it matters if you're not familiar with the library in use, it's a fairly generic SPI communications library. I've managed to put this code together by looking at various uses of the pigpio Raspberry Pi library along with a number of python scripts, etc.
char write_cmd[3] = {1, 128, 0};
char read_data[3];
int speed = 300000;
int spiHandle;
int readVal;
spiHandle = spiOpen(0, speed, 0);
while (1)
{
spiXfer(spiHandle, (char*)&write_cmd, (char*)&read_data, sizeof(read_data));
readVal = ((read_data[1]&3)<<8) | read_data[2];
printf("Value: %in",readVal);
}
As I said, this code seems to work quite well. I've done some experimentation to see that the speed is actually increasing and decreasing with respect to the speed value, and I am getting the values that I expect.
Here are my questions;
- What do those three bytes I write into the MCP3204 really do? I've looked long and hard at the data sheet (specifically Table 5-1) and it seems to me that I would be sending in something more along the lines of a single byte with binary 1 1 dk 0 0 (or 0x18, assuming the dk was 0) which would denote a 1 for the start bit, a 1 for single ended A/D, a 0 for the don't care since I'm using the MCP3204, and then two zeros to request channel 0 of the A/D. But instead, I see three bytes - 1/128/0, which is 0 0 0 0 0 0 0 1 | 1 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0. I must be misreading the data sheet! I tried doing this differently, sending in a single byte 24 as opposed to the three, but I don't really get anything back I'd expect - gibberish, I believe.
- If the MCP3204 is a 12 bit resolution A/D converter, why am I only getting 10 bits (0-1023)? I could see maybe only getting 11, with the last bit being a sign bit or something, but in that case, I would have expected to get values ranging from 0 - 2047, instead of what I'm getting now which is 0 - 1023.
Ok, that's about it. Hopefully I've explained myself well enough, and in the right place. If there are any clarifications I can make, please let me know. Thanks in advance for any advice or guidance anyone might have!
Respectfully,
Marshall