Problem with 10bit ADC on Flowcode 5

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Problem with 10bit ADC on Flowcode 5

Post by KeesB »

I am designing an application whereby (amongst others) a voltage is sampled with 10 bit accuracy, send over an optically isolated barrier, and transformed back to analog on the other side with a 10bit PWM and an active 3rd order Butterworth low pass filter.
The output voltage is fed back to the PIC through a voltage divider so that there is a small amount of amplification (5 / 4.096 to be precise).
The final application will run on a PIC16F1825 with an internal clock of 32 MHz, but I am testing with a PIC18F452 with an 8 MHz crystal and the 4x PLL enabled.

I have experienced serious problems getting the ADC to work accurately, and at first thought this was due to the high clock-frequency and/or a wrong setting, but finally found out the problem lies with Flowcode V5.
To find this out I have created two small test-programs (Test_Flowcode4 & Test_Flowcode5) which do only what I described above without the serial link. Both programs also output the ADC value on some digital outputs for monitoring purposes.
I created a test-circuit on a breadboard whereby the input is driven from a triangle oscillator with a dual opamp. This helps determining easily if the ADC is linear.
As it turned out the Flowcode V4 variant worked flawlessly from the start with the same settings as the Flowcode V5 program. I have made small movies of signals on my oscilloscope and an LED-bar connected to the digital outputs.
Images from the oscilloscope screen are included. The bottom signal is the input, and the top signal is the output. As you can see the performance of the Flowcode V5 variant is hardly acceptable.

To be absolutely safe I have used the FRC mode in both instances, and set the acquisition time to 206 instruction cycles which on 32 MHz corresponds with twice the required Tacq (=12.86us according to the 18F452 datasheet).
I have finally de-installed Flowcode V5 from my system, deleted all the files from the program files directory, and did a clean install to Flowcode V5.2 without any improvement.

Can you look into this, because this problem is hindering development.

I would rather use Flowcode V5, not only because it is more sophisticated, but it also allows me to use timers 4 and 6 which Flowcode V4 does not.
In my program I would like to use timer 2 as an interrupt source, but I conclude from the 16F1825 & 18F452 datasheets that this timer is used for the PWM-module.
On the 16F1825 it is possible to use timer 4 or 6 for this purpose but I assume that Flowcode V4 uses the default, and that timer 2 is in fact unavailable when PWM is used (am I right?).
So on Flowcode V5 I would be able to use timer 4 or 6 as my interrupt source.
Attachments
Oscilloscope-screens.jpg
Left: Flowcode 4, Right: Flowcode 5
(222.3 KiB) Downloaded 3103 times
Test_Flowcode5.fcf
Flowcode 5 test program
(11.5 KiB) Downloaded 418 times
Test_Flowcode4.fcf
Flowcode 4 test program
(9 KiB) Downloaded 397 times

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

It's even stranger than I thought.

I rewrote my main program to Flowcode 4 because Flowcode 5 can't save in Flowcode 4 format.
When the final circuit with the PIC16F1825 arrived I started testing my program. After struggling for quite a long time to get the ADC to work I followed a hunch.
I rewrote the two test programs above for the PIC16F1825. Because the limited amount of pins on this chip I couldn't output the ADC-value on an I/O-port,
so I used the UART, and monitored the output on my oscilloscope. Next I performed some comparative tests.
As it turned out 10bit ADC on the 16F1825 didn't function correctly with Flowcode 4, but with Flowcode 5 to my surprise it did. On Flowcode 4 the low byte was all zeros.
The results so far, including the earlier tests is:

18F452 Flowcode 4 Flowcode 5
8bit not tested not tested
10bit OK NOK

16F1825 Flowcode 4 Flowcode 5
8bit OK OK
10bit NOK OK

The problem looks to be like a combination of the Flowcode version and the processor type.

I rewrote my main program to Flowcode 5 again, which now works correctly.
For an other application using Flowcode 5 on a 18F452 using 10bit ADC I still need a solution to this problem however.

User avatar
Steve
Matrix Staff
Posts: 3434
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Problem with 10bit ADC on Flowcode 5

Post by Steve »

Hello KeesB,

Thanks very much for the detailed posts. I'll make sure we look into this as a priority.

Regards,
Steve.

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Thanks,

If you would like me to test anything, just let me know.

Kees.

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: Problem with 10bit ADC on Flowcode 5

Post by Benj »

Hello,

I think I have spotted the problem.

In v5 please can you click on each ADC component in turn and follow these steps.

Go into the ADC component properties and change the Conversion Speed to FOSC/8 to match your v4 setting.

It looks like the ADC settings for this device have become a bit scrambled in v5 so I am working on a way to unscramble these settings.

Let me know how you get on and sorry for the delay.

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Hello Benj,

My Flowcode5 test program on the 18F452 now works correctly with these settings. That's a relief.
There are a few things that I don't understand however:

Does FOSC/8 on Flowcode5 give me the correct TAD (1.6us minimum) on the 18F452 (or 16F1825) at 32MHz?
Was my setting FRC too fast for some reason?

And if this is a Flowcode5 problem, how does this explain the problem that I had with Flowcode4 on the 16F1825?

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Update:

The setting FOSC/8 does not work on a PIC18F4620 at 40MHz.
Can you advise me a working setting for this chip?

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: Problem with 10bit ADC on Flowcode 5

Post by Benj »

Hello,

It seems the ADC conversion rate properties have become a bit scrambled in v5. I have a plan to solve this but it requires a significant amount of effort so at the moment getting the correct setting may be more like trial and error.

Have a look at the C file for your v4 program and you will see something like this.

Code: Select all

//ADC(0): //Defines:

/**** Macro Substitutions ****
0 = Which ADC Channel
40 = Acquisition time
3 = Conversion Speed
0 = VRef+ Option
The number next to conversion speed is your v4 setting.

For v5 you will see something like this in the C code.

Code: Select all

#define ADC_11_MX_ADC_CHANNEL	0
#define ADC_11_MX_ADC_ACTIME	40
#define ADC_11_MX_ADC_CONVSP	3
If the two conversion speed parameters match then your v4 and v5 settings should be identical and the ADC should work as it did in v4. I think the v5 list goes in order starting from 0.

The device datasheet should detail what each conversion speed should be doing.

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Hello,

I have worked out the relation between the property setting and the define:
FRC -> 0
FOSC/2 -> 1
FOSC/4 -> 2
FOSC/8 -> 3
FOSC/16 -> 4
FOSC/32 -> 5
FOSC/64 -> 6

According to all datasheets that I have (16F1825, 18F452 & 18F4620) this should be:
ADCS2:ADCS0:
000 (0) FOSC/2 (select FRC)
001 (1) FOSC/8 (select FOSC/2)
010 (2) FOSC/32 (select FOSC/4)
011 (3) FRC (select FOSC/8)
100 (4) FOSC/4 (select FOSC/16)
101 (5) FOSC/16 (select FOSC/32)
110 (6) FOSC/64 (=correct)
111 (7) FRC

Between parenthesis I have added what you would need to select to get what you really want.
So for FRC you need to select FOSC/8, which is what you told me.
This doesn't solve my problem on the 18F4620 however (because FOSC/8 didn't work). I will try to investigate further, but I think that my options are limited.

electron67
Posts: 323
Joined: Sat Jan 12, 2008 2:08 am
Has thanked: 106 times
Been thanked: 48 times

Re: Problem with 10bit ADC on Flowcode 5

Post by electron67 »

The problem that you have with 18f4620, can you test with Flowcode4. I have the problem with VRef in 18f4620 and not work in Flowcode5 but I test with Flowcode4 and yes work.

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: Problem with 10bit ADC on Flowcode 5

Post by Benj »

I have made a note to look into the 18F4620 ADC with v5 tomorrow and hopefully I can get this resolved for you both.

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

@ electron67:

I have a large program for the 18F4620 (which migrated in time from the 18F887 via the 18F452 to the 18F4620) that we have used in a testfacility in our company for years. In that program I use 5 ADC-inputs with 10 bit accuracy, and I never had any problems with that.
In Flowcode 3 as well as Flowcode 4.

Recently I had to make a big change to the program and decided to use Flowcode 5 because it makes your life a lot easier.
As it turned out everything worked OK, except the ADC-inputs.

Next monday I will try FOSC/64 and see if that works.

Kees

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

I changed the conversion speed to FOSC/64 without any succes.

Then I changed the VRef option to VDD in stead of VRef+, and that did the trick.
So for the 18F4620 in addition to the conversion speed problem there is also a problem with the Vref option.
I checked the C-sources for the Flowcode4 an Flowcode5 versions, and both give VRef option 1.
It must be something in the underlying software.

Kees

electron67
Posts: 323
Joined: Sat Jan 12, 2008 2:08 am
Has thanked: 106 times
Been thanked: 48 times

Re: Problem with 10bit ADC on Flowcode 5

Post by electron67 »

Hi KeesB and Benj
I can't work the VRef; My program the half work in Flow4 a the another half work in Flow5;
In Flow5 work the timer very good and VRef not and Flow4 VRef work fine and the timer work fine in flowcode4 but in hardware not.
I do all program in Flow4, you can see the timer program in that is wrong, I select 1 second, but work for 8 minutes, the delay is a variable with calculation are minutes and hours; When I use variable in Delay work fine in Flow4 but hardware not. (PIC18F4620)

Thank you
Attachments
TIMER.fcf
PROGRAM
(18.18 KiB) Downloaded 331 times

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Hi electron67,

I have received a PM from howard123 with the attached file which he received from Ben Rowlands.
This file has solved my problem with the external Vref on the 18F4620.

Kees
Attachments
PIC_CAL_ADC.c
(85.07 KiB) Downloaded 380 times

electron67
Posts: 323
Joined: Sat Jan 12, 2008 2:08 am
Has thanked: 106 times
Been thanked: 48 times

Re: Problem with 10bit ADC on Flowcode 5

Post by electron67 »

Thank you KeesB, I tests the program

Electron67

electron67
Posts: 323
Joined: Sat Jan 12, 2008 2:08 am
Has thanked: 106 times
Been thanked: 48 times

Re: Problem with 10bit ADC on Flowcode 5

Post by electron67 »

¡ YES WORK THE PROGRAM !

Thank you

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Hello,

I have upgraded to a 18F46K22 at 64MHz, and the same problem has surfaced.
To remedy the problem I have tried to change the clock speed, but at FOSC/64 (recommended in the datasheet) I get the following line(s) in the C-source, and that is correct:

#define ADC_7_MX_ADC_CONVSP 6

Then I upgraded to Flowcode 5.4 without succes.

Next I switched to internal reference (VDD), and that did the trick.
Last time the problem was solved with a new PIC_CAL_ADC.c file, so I looked in this file in the relevant section for the 46K22 and changed the following:

if (Vref != 0)
st_bit(adcon1, PVCFG0); //assign VREF functionality

To:

if (Vref != 0)
{ //assign VREF functionality
st_bit(adcon1, PVCFG0);
cr_bit(adcon1, PVCFG1);
}

This doesn't work however.
In the mean time my program works, but I really need the external reference functionality.

I hope anyone can help.

Kees

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: Problem with 10bit ADC on Flowcode 5

Post by Benj »

Hello Kees,

Both sections of code should work ok to set the external VRef. Can I ask what voltage you have connected to the VRef+ pin? Looks like the minimum voltage that can be applied is around 2V.

KeesB
Posts: 11
Joined: Wed Sep 02, 2009 9:57 am
Been thanked: 2 times

Re: Problem with 10bit ADC on Flowcode 5

Post by KeesB »

Hello Benj,

The external reference voltage is 5.000V.

I use this reference to accurately measure 6 analog inputs.
One of them, a pressure sensor with a proportional output voltage, was supplied by the reference voltage.
As a fix I have connected this sensor to Vdd, and I now use Vdd as the reference voltage.
As a consequence only the output of this sensor is accurate, the rest is not (Vdd has an accuracy of +/-5%).

When I use the external reference all input readings become unstable.

Kees

Post Reply