Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

ADC Pi Plus channel switch timeout

2984 Views - Created 02/02/2016

02/02/2016

Posted by:
johnm

johnm Avatar

Hi. I have an rpi2 with (several) ADC Pi Plus boards on top. I have read through the 'manual' at ABE_ADCPi.py and ported it to Java using pi4j. After some tries I got it working, but apparantly I have to use a timeout between channel switching and reading new values. Weird, because in the original perl sample there is no waiting between reading the different channels demo-readvoltage.py . If I run the sample (and remove the 0.5sec sleep) it almost instantly refreshes all the channels without any problem.
This is what I do (pseudo) :

write(1001 0000); //channel 1, continuous mode, PGA1, 12bit resolutionread(); //Reading 4 bytes, loop untill highest bit of 4th byte is '1', then use first 2 bytes as the 'value'write(1011 0000); //channel 2, continuous mode, PGA1, 12bit resolutionread(); //Reading 4 bytes, loop untill highest bit of 4th byte is '1'


=>last read would still return the value from channel 1 allthought we switched to channel 2


write(1001 0000); //channel 1, continuous mode, PGA1, 12bit resolutionread(); //Reading 4 bytes, loop untill highest bit of 4th byte is '1'write(1011 0000); //channel 2, continuous mode, PGA1, 12bit resolutionwait(100msec)read(); //Reading 4 bytes, loop untill highest bit of 4th byte is '1'


=>last read now returns value of channel2

So, am I doing something wrong or is there a way of reading the status after a channel switch to see when it is ready? Imho waiting an arbitrary amount of msecs feels a bit fishy, also in the original perl sample there seems to be no need for it.
Secondly, what is bit 7 doing in the config? In the perl script it seems to be initialized on '1' (the initial config is 0x9C) and stays on that value. Only when switching to single shot it is set to 1 (which has no use, since it is already 1?) but then after writing it is set to 0 again.
Thanks!

03/02/2016

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hi, I think I can see where you are going wrong with your code.

Bit 7 of the config byte, which is the last byte returned, is the Ready Bit which the ADC uses to show when a sample is ready to be used. To initiate a new read on the ADC you have to set the Ready Bit to 1 in the config and write that to the ADC. You then have to keep reading from the ADC until the ready bit returns as 0. When it is 0 that means the last set of bytes you read from the ADC contains the new voltage value. Page 17 of the MCP3424 datasheet describes how the ready bit works in more detail.

If you look at the python library from lines 141 to 154 you can see that there is a while loop which reads the bytes from the ADC and then checks the status of bit 7 in the config byte. The while loop only breaks when bit 7 is returned as 0.

Hopefully, if you add this while loop into your java code it will return the correct values.

03/02/2016

Posted by:
johnm

johnm Avatar

Thanks for the answer. I didn't provide that info, but I was already doing this. But after taking a closer look it might be that I made a mistake there (I'm checking for the status bit to become '1' instead of '0'). Let me check this first and I'll post back later.
Just for the record (I don't have the board here atm): is the laste byte returned a copy of the config byte ? So is this reflecting the config the ADC is actually using at that time?

03/02/2016

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

The last byte returned is the config byte that you write to the ADC. The only bit in the config byte that should change is the ready bit at bit 7. The position of this byte will change depending on the bit rate. In 18-bit mode it will be the fourth byte, while in the other three bit-rates it is the third byte.

Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.