Microchip
直播中

曹珉

7年用户 203经验值
私信 关注
[问答]

MCP3204的C代码如何修改

你好,希望我把这个放在正确的地方!我正在研究一个将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 posting 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

回帖(3)

h1654155275.5748

2019-1-30 13:31:58
再次问好,经过更多的研究和拨弄,包括回到我引用的一些Python代码,我注意到下面的代码行;ADC= SPI.XFE2([ 1,(8 +传感器)& lt;4],0])相同的三个字符数组,1个在第一个位置,第三个位置为0,和公式(8 +传感器号左移四个位置),恰好等于通道0的128,在中间阵列点。所以,我尝试了下面的三个值代替128个-以及144, 160和176,和…成功。我现在可以从所有四个渠道获得数据。所有的好……除了,我仍然不明白为什么这些值的工作,我很愿意!如果有人能解释一下这个配置是如何工作的,我会非常感激的。而且,我仍然不明白为什么我只看到10位而不是12位(或者11位有符号位)-任何信息都将非常感谢!谢谢,Marshall

以上来自于百度翻译


      以下为原文

    Hello again,
 
After a bit more research and poking around, including going back to some of the python code I've been using for reference, I noticed the following line of code;
 
adc = spi.xfer2([1,(8+Sensor)<<4,0])
 
Same three char array, 1 in the first position, a zero in the third position, and a formula (8 + sensor number shifted left four positions) that happens to equate to 128 for channel 0, in the middle array spot. So, I tried the following three values out in place of 128 --> 144, 160 and 176, and...success. I am now able to get data from all four channels.
 
All good...except, I still don't really understand why these values work, and I would very much like to! If anyone can shed some light on exactly how this configuration really works, I would greatly appreciate it. Also, I still don't get why I only see 10 bits instead of 12 (or maybe 11 with a sign bit) - any info would be greatly appreciated!
 
 
Thanks,
Marshall
举报

王焕树

2019-1-30 13:44:28
在CS变低之后,MCPPT4等待直到它在开始计时之前看到高数据位。这是“开始”位,然后在4位控制数据中计时,然后再拨出12位ADC数据。因此,第一个字节是0x01,它是70位,后面是一个位。第二个字节的前四位是控制字。这就是为什么需要把左4位移位。第二个字节和整个第三个字节都是“不关心”。你的参考电压是多少,你测量的电压范围是多少?

以上来自于百度翻译


      以下为原文

    After CS goes low, the MCP3204 waits until it sees a high data bit before it starts clocking. That's the "Start" bit.
It then clocks in 4 bits of control data, then clocks out 12 bits of ADC data.
So, the first byte sent is 0x01, which is seven zero bits followed by a one bit.
The top four bits of the second byte sent are the control word.
That's why you need to shift it left 4 bits.
The lower 4 bits of that second byte, and the entire third byte are "don't care".
 
What is your reference voltage, and what is the range of voltages you are measuring?
 
 
 
举报

h1654155275.5748

2019-1-30 14:04:00
好的,我想我现在得到了数据的格式-很高兴知道,很高兴我能把我没有得到的东西拿出来。至于参考/范围电压,现在一切都在3.3V系统-我的参考电压(VREF,引脚13)连接到PI 3.3V电源和我的地面参考(AGND,引脚12)连接到数字/系统接地。我认为我的A/D通道的输入电压稍微高一些,可能高达5V左右,但我不能确定我的头部。0V绝对是基准线,没有什么会低于这个值。这是电压差(3.5V参考最大输入电压)为什么我看到最大的结果值为1023?马歇尔

以上来自于百度翻译


      以下为原文

    Ok, I think I get the format of the data now - good to know, glad I can take that off my list of things I don't get!
 
As far as the reference/range voltages, right now everything is on a 3.3V system - my reference voltage (Vref, pin 13) is connected to the Pi 3.3V supply and my ground reference (Agnd, pin 12) is connected to the digital/system ground. I think my input voltage to the A/D channels is going slightly higher though, possibly up to around 5V, but I'm not certain off the top of my head. 0V is definitely the baseline, nothing will go below that.
 
Is this difference in voltage (3.3V reference to 5V maximum input voltage) why I'm seeing a maximum resultant value of 1,023?
 
Marshall
举报

更多回帖

发帖
×
20
完善资料,
赚取积分