MCP3201 12-Bit ADC Component

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Everyone

Following my previous post (but in a wrong version forum) I would again like to raise my request to know if it is possible to use MCP3201 ADC as a component in FC8 same like AD7680, if yes may i request to know how, in one of the previous post Ben mentioned about using SPI Component (or SPI CAL), but unfortunately i did not understand.

Thank you.

Abhi

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by Benj »

Hi Abhi,

I'll have a go at creating a component but here is an example using the SPI master component.
MCP3201_Demo.fcfx
(8.99 KiB) Downloaded 164 times

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hi! Ben

Many thanks to revert back, i download the chart and check how it works on the real hardware and keep you posted.

Also thank you in advance for making a component for MCP3201, i hope it is not a big trouble or a time consuming process.

Abhi

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by Benj »

Hi Abhi,

Let me know how you get on with the example code, if that works ok then I'll use that for the component. There is a shift that i'm slightly unsure of so would be nice to confirm if the range is correct or /2.

e.g. 0-2047 instead of 0-4095.

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Ben

As said i tried to run the chart, i added one OLED display to see if there is any change receiving, but i notice once the "GetADC" Macro is called then the chip halts no change after that, nothing, everything freeze. I also checked with one LED in the main loop, it flash well if i disable the "GetADC" Macro, if i again enable that Macro, nothing happen.

I am using PIC18F26K22 chip at 64MHz (16MHz + PLL), OLED display also runs well if i disable the "GetADC" Macro.

I hope i gave you correct data :) , nothing went further so could not read ADC

Abhi

EDIT:

Ben kindly excuse my mistake i did not change the port, forgot in excitement, there is some change in the display, and also the LED is blinking, i better check all the things correct and then update you. :D

Abhi

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by Benj »

Thanks Abhi,

What about this instead?
MCP3201_Demo.fcfx
(9.39 KiB) Downloaded 174 times

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hi! Ben

Below are the observations for your knowledge, i have attached the chart, kindly correct if i am doing something wrong, honestly everything is puzzling me, i was expecting to read 4096 or 2048 value, do you think i have fetched the value correctly in the chart, i tried running the PIC chip on 3.3V and 5.0V

-At 4mA reading is 58328 approximately
-At 20mA reading is 61411 approximately
-The Vref to the MCP3201 is 2.048V and VCC 3.3V, on board for the moment

I hope this feedback is of some use, kindly excuse if not helpful.

Ben i also noticed one more thing when i switch off the power and switch it on again the display for the first time reads as 4061, 4097, 4068 etc, for current 20mA, then the value reads as 61405, 61408 ... and if i keep the current to 4mA then the value at power on reads as 979, 971, 990 and then reads as 58311, 58318, 58327

Abhi
Attachments
MCP3201_Demo_Ben.fcfx
(13.32 KiB) Downloaded 139 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by Benj »

Hi Abhi,

In your calculation that converts from a float to a string try changing the code to this.

ADC_Global_ST = FloatToString$ (ADC_Global_Real,1)
ADC_Global_ST = ADC_Global_ST + " "

This will add some spaces to the end of the string and avoid leaving values on the GLCD at the end of the number from previous printed values. Let me know if this helps at all?

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Ben

As per your instructions i added the Calculation box below the existing

ADC_Global_ST = FloatToString$ (ADC_Global_Real,1)
I
ADC_Global_ST = ADC_Global_ST + " "

but without success, the display shows the values like before, i also tried the existing AD7680 ADC, no doubt the components are different but :?:
any ways is there something wrong i am doing which you could anticipate, or do you want me to provide some more information.

Abhi

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Ben
good morning

Wishing all a very safe and healthy new year.

Any news on this component or can take some more time. Thank you.

Abhi

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by chipfryer27 »

Hi Abhi

I have a couple of those chips arriving this week and I've been following your posts with interest.

I'll let you know how I get on.

Regards

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Chipfryer
good morning

Many many thanks for your interest, i am eagerly waiting for your post once you take trials, all my best wishes.

Abhi

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Ben
good morning

Any update on creating the component for MCP3201 or any other equivalent ADC, then i dont need to use PIC18F26K42 chip as every thing works well for me on the K22 chip, i am planning to use K42 only for 12bit ADC.

Abhi

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by chipfryer27 »

Hi Abhi

I got a couple of those chips the other day and I have mine working fine.

I didn't use Ben's example code as I wanted to try it for myself first and see my results before looking at Ben's code (which would most likely work better than anything I could create).

Looking at the data sheet it informs that it sends the result in over two bytes, and gives an idea on how to manage this. Armed with that and Flowcode I set about trying to get it all working. Note that at home I don't have calibrated TE so near enough is good enough for my tests <s>

Personal preference whilst trying something "new" is not to try and get too clever straight away, especially in calculations (especially as I am not that clever). So to that extent I created an array of two to hold the incoming bytes, assigned a variable to each byte, then assigned new variables for each calculation / manipulation before giving a final answer. That enables me to see values at each stage to potentially assist if not going to plan.

Initially it didn't go quite to plan but I was getting values just not what I was expecting, well hoping for. I display the two incoming bytes, then the total before looping around to start the process again. From maually calculating the total from the two displayed incoming bytes my manual calculations equalled the displayed total so no issue in the calculations, it is the raw data at fault (i.e. I'm not reading the chip correctly).

I had a potentiometer set to give me 2.50v into the chip so I was expecting ~2048 as my result. However I was getting half of whatever I inputed (with 5v = ~2048).

At ~2.50 I got 7 / 225 (Byte 1 and 2) which indicated something wrong in reading.

Looking at the SPI component I saw that my clock was set to Idle high. Changing it to idle low solved my problem and I now get 15/253 for my bytes with a total of 2046. Close enough for me for an input of ~2.50

I will clean up my flowcode and post it for you later. It may not be very efficient or use best practise and I know I can combine calculations into one, but it will serve to show each step I took to get it working.

Personally I'm happy at this as my need is to read a value as close to the source as posible before sending the value and this chip meets my needs perfectly.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by chipfryer27 »

Hi

I've downloaded both Ben's example but I think his calculation has a flaw (I question that statement myself even as I type).

I see he uses two different ways to capture the data than I do. I simply used GetByteArray but more than one way to skin a cat.

Feeding my "raw data" into his calculation I get a reading of 3938 instead of 2046 using my empirical data.

We have a 12 bit reading sent over two bytes with first byte containing two unknowns, a null bit then bits 11-7 and the second byte containing bits 6-0 and then repeating B1 on the LSB of the byte.

Why I think Ben's calculation is wrong is from the following taken from the datasheet.

"Typical procedure would then call for the lower-order byte of data to be shifted right by one bit to remove the extra B1 bit. The B7 bit is then transferred from the high-order byte to the lower-order byte, and then the higher-order byte is shifted one bit to the right as well. Easier manipulation of the converted data can be obtained by using this method."

Ben's calculation doesn't shift the LSB of Byte 1 to the HSB of Byte 2, nor does it deal with any of the potential unknown bits.

In my example code, as mentioned earlier, I took a very simplistic approach assigning a variable (A-G) to each stage of the calculations. This would help me (hopefully) if anything went wrong. Once I had the code working I compacted the calculation using variables X-Z.

I'm quite sure someone cleverer than I will make it even more efficient but for my needs at the moment using the MCP3201, then X/Y/Z are all I need <s>

Hope this helps,
Regards
Attachments
MCP_Example.fcfx
(17.48 KiB) Downloaded 100 times

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! Chipfryer (CF)
good morning

First of all million thanks for your time and efforts, i am obliged to receive your post/information/detailed study and the example chart.

To be honest with you (i don't mind saying that on the forum) i am an amateur to understand the explanation you gave about the chart and MCP3201 and i really feel very sorry that i am not able to reciprocate well/properly about the arguments you mentioned, but i will try to read again the datasheet and your explanation to understand, may be i will need some more time.

I have download your chart and will try to run the same with hardware i have got for the moment this weekend, i shall keep you posted.

Once again humbly thank you for all your efforts, i appreciate that.

Abhi

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by chipfryer27 »

Hi Abhi

In my example I'm connected to a pic16F1939 but it should work on pretty much any as long as a few parameters are met.

I'm using the Master SPI component connected to Channel 1, with the following connections:

MOSI = Port C5
MISO = Port C4
CLock = Port C3
CS/SS = Port C2
(I didn't connect MOSI as the ADC chip has no need to receive data from the uC).

Set the Prescale to suit
Clock Phase = Trailing Edge
Clock Polarity = Idle Low
Sample Point = Middle

A bit confusing if your not looking at the diagrams, but basically when initialised (CS Low) during the first two clock cycles the ADC will sample the voltage on it's input, then it will output a Low "null bit". The next 12 bits are the result of the conversion sent MSB first. If the chip is still enabled and clocked after the 12 bits have been sent the ADC then sends the result LSB first (note that the sequence is ...B1 - B0 - B1..., the B0 bit isn't repeated). Once this has been sent, if we keep clocking with CS Low it will just send out zero's continuously.

So how do we make sense of that?

We are looking for our data to be in eight bit format but clearly the output from the ADC isn't. In the following ?= unknown, N= null, Bx = Bit and if we look at the first sixteen bits received we would have

? - ? - N - B11 - B10 - B9 - B8 - B7 - B6 - B5 - B4 - B3 - B2 - B1 - B0 - B1 (If we didn't stop here, just add in B2 - B11 then 0's until you do stop <s>).

If we perhaps used GetByteArray we would have received two bytes (all going well)

So the first byte we receive would contain:-
? - ? - N - B11 - B10 - B9 - B8 - B7

Our second byte would contain:-

B6 - B5 - B4 - B3 - B2 - B1 - B0 - B1

We now need to combine them in a calculation to get the reading we require. We need to combine the two bytes, get rid of the LSB (the extra B1) and we may wish to strip out the first three as well (two unknowns and the null byte) but that is down to you. If everything is working perfectly you don't need to.

First we shift the first byte left by 8 places and get

? - ? - N - B11 - B10 - B9 - B8 - B7 - x - x - x - x - x - x - x - x

If we now add the second byte we get

? - ? - N - B11 - B10 - B9 - B8 - B7 - B6 - B5 - B4 - B3 - B2 - B1 - B0 - B1

If we shift Right by one place we get

0 - ? - ? - N - B11 - B10 - B9 - B8 - B7 - B6 - B5 - B4 - B3 - B2 - B1 - B0

This in theory is now your ADC output. However if you want to strip away the two potentially unknown bytes etc a simple AND calculation will do so. I've decided I only want bits B11 - B0 so using AND

0 - ? - ? - N - B11 - B10 - B9 - B8 - B7 - B6 - B5 - B4 - B3 - B2 - B1 - B0
0 - 0 - 0 - 0 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1

Gives
0 - 0 - 0 - 0 - B11 - B10 - B9 - B8 - B7 - B6 - B5 - B4 - B3 - B2 - B1 - B0

In my initial test I made things very, very simplistic by breaking everything down into individual calculations / manipulations and assigning a new variable at each stage. This was just to help me follow where I was probably going wrong <s> and is quite messy to say the least. Once my "calculation" was correct I simplified it.

For example an array called MCP[] populated by GetByteArray

X = ((MCP[0]<<8)+MCP[1])>>1 Shift higher byte left by 8 places, add lower byte, then shift total one place to right
Y = X AND 0b0000111111111111 Simple AND instruction to strip off first four bytes (shown in binary)

X gives you the ADC result
Y strips off the Unknowns and null

I hope this is somewhat clearer to follow than my previous posts <s>

Regards

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! CF
Good morning

I must say the above explanation is brilliant, I am understanding things which were difficult to understand in the past.

In my opinion I am going to try a different SPI component to test how much did I understand and if I can do on my own, if there is some difficulty I will bother you.

Thank you again.

Abhi

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by chipfryer27 »

Hi Abhi

You are very welcome. Glad to have been of some help.

Regards

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: MCP3201 12-Bit ADC Component

Post by AbhijitR »

Hello! CF
good afternoon

Finally i could run your chart successfully with the MCP3201, cheers to you my friend.

Again many many thanks for your efforts.

Abhi

Post Reply