I'm confused - P2-ES to MCP3204
twm47099
Posts: 867
in Propeller 2
I've connected an MCP3204 ADP to the P2-ES and programmed it using TAQOZ. The program (below) measures the voltage on each of the 4 channels and displays it on the terminal as "volts * 10" (e.g. 3.3v shows as 33 volts). The 4 channels and the MCP3204 are wired as shown in Chapter 4 of "Programming & Customizing the Propeller Microcontroller" book.
Channels 0 and 1 are connected to potentiometers for variable voltages. Channel 2 is connected to ground (0 volts) and channel 3 is connected to 3.3v on the P2-ES.
Everything works well, except the channel connected to 3.3v displays 15 (i.e. 1.5v). I have switched the 3.3v connection to different channels, but whatever channel it is connected to displays 15.
I have tried moving the P2-ES jumper to the LDO setting, but got the same result. If I disconnect the channel from 3.3v and connect it to 5v, it displays 4.9v as I expect. I've measured the voltage of the v0815 pin to gnd using a DVM and it shows 3.3v.
Any ideas?
Thanks
Tom
This is the code I am using:
Channels 0 and 1 are connected to potentiometers for variable voltages. Channel 2 is connected to ground (0 volts) and channel 3 is connected to 3.3v on the P2-ES.
ch# 0 31 volts ch# 1 49 volts ch# 2 0 volts ch# 3 15 volts
Everything works well, except the channel connected to 3.3v displays 15 (i.e. 1.5v). I have switched the 3.3v connection to different channels, but whatever channel it is connected to displays 15.
I have tried moving the P2-ES jumper to the LDO setting, but got the same result. If I disconnect the channel from 3.3v and connect it to 5v, it displays 4.9v as I expect. I've measured the voltage of the v0815 pin to gnd using a DVM and it shows 3.3v.
Any ideas?
Thanks
Tom
This is the code I am using:
--- ------------------------------------------------------ --- **** go3204 --- This word uses the 3204 to measure volts on 4 channels --- ------------------------------------------------------ --- define SPI pins 8 := *SCLK 9 := *MISO 10 := *MOSI 11 := *CS --- define number of command and indata bits 5 := *CMDBITS 13 := *INBITS : clockbit *SCLK HIGH *SCLK LOW ; : cmdvalue ( channel_number -- command ) %11000 OR ; : start3204 *CS HIGH *SCLK LOW *CS LOW ; --- sendcmd Send 1 startbit (high), 4 command bits, 1 don't care bit : sendcmd ( channel -- ) cmdvalue ( channel -- command ) 32 *CMDBITS - ( -- command, 32-CM ) << ( -- command shifted to MSB ) *CMDBITS 0 DO 1 rol dup 1 AND if *MOSI high else *MOSI low then clockbit loop drop *MOSI low clockbit ; --- END 3204 sendcmd --- Receive 13 bits from 3204 and drop initial bit : getdata ( -- 12bit_raw ) 0 ( will become raw ) *INBITS ( includes 1st don't care bit ) FOR 2* clockbit *MISO pin@ 1 and or NEXT ( -- 13bit_raw ) 4095 and ( -- 12bit_raw ) ; --- Calculate volts from 12bit raw : volts ( 12bit raw -- volts x 10 ) 50 * 4096 / ; --- main word : read3204 ( channel number -- volts ) start3204 sendcmd getdata volts *CS HIGH ; --- Get data from all 4 channels and display it : go3204 CRLF 4 0 do i dup ." ch# " . ." " read3204 . ." volts " CRLF loop CRLF ; --- **** Type go3204 to run program ****
Comments
Testing (without any chip)
Thanks.
I ran the code you posted and got the following:
Believe what your ADC is telling you, don't assume it is your software.
The ADC is wired up on a breadboard using 100mm long jumpers. (In the past I've used a similar setup on my Propeller Professional Development Board with joysticks & an MCP3208 and the readings were accurate. But the wiring was not as rat's nesty as my current setup).
I didn't collect enough data this time to plot ADC vs DVM so I don't know if there is a linear relation or not.
I'm going to rewire and also try a different 3204.
Any ideas as to what is causing the voltage difference as voltage is reduced?
And Peter thanks for the example of .AS"
Tom
Since you are only using one decimal place without rounding you will see 4.9V instead of 4.9. You will never get 5V because your calculation is based on 4096 as the highest value when the ADC can only read back 4095.
Have a look at this slightly modified version of your code that uses two decimal place (but no rounding) and reports 5.00 volts for maximum reading.
Ok I'll try it too. I have a bucket of old pots, lots of wire and hopefully there are some(4) 5k or 10k pots in the pile . I have a MCP3208 which is the same thing with 4 extra channels . Hopefully it still works correctly after my last experiments with it. LOL I don't have the reference book you mentioned above however, I will use the MPC3208 PDF to figure the pinouts.I will try Peter's code . I have the soldering station heating up and will start on it tonight but it's late and may have to finish it sometime tomorrow. I played with TAQOZ a little last night and it's been fun! (Thanks Peter). This will be my first experiment with the P2-ES .
Results: Plotted in EXCEL it is linear. But as I reduced the voltage more this happened:
Also linear, but it seems that between 2.57 and 2.50v the ADC = 0 and then it started high again ???
Any idea what is going on???
Tom
eg if you lose the MSB, and scale the remaining bits, that's the plot you would expect - two sawtooths, with a ~ zero at 50% scale.
+5v on the P2-ES board
That appears to work. Now I have to figure out what is wrong with my code.
Tom
The ADC input on the MCP320x needs something reasonably stiff (or ideally an op amp buffer to drive the ADC)
EDITED
I think that in the word sendcmd the last line (*MOSI low clockbit) needed to be deleted. When I did that I got good values. Channel 3 that was connected to 3.3 v showed an ADC value of 3.25 v, and I didn't get the reversal of values like I saw above.
Peter suggested that the problem was having clockbit before reading the MISO pin in the getdata definition. putting clockbit after the pin@ and adding "*MOSI low clockbit" back as the last line of the sendcmd definition works.
The cleaned up code is:
Thanks Peter and JMG
Tom
That would be good.
Sourcing power from anywhere else on the PCB is not recommended.
The circuit I referred to in the book has a 10k resistor in series with Dout (MISO) since the ADC is running on 5v.
Tom
That was the problem. clockbit comes after reading the pin.
I found an old data sheet that I had previously used with the 3208 (also shows the 3204), and the timing diagram is clearer showing that.
EDIT
I changed the code to incorporate Peter's fix in the listing approx 7 posts back..
Tom