Strange ADC behaviour

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

Moderator: Benj

Post Reply
User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Strange ADC behaviour

Post by QMESAR »

Hi Team.

I have a prototype pcb wit a 16F1938 on with Code written in V7 which was now working for 3 months in a vehicle with no issues
We then made now a new pcb with production intend however there is a problem it seems the ADC at AN3 is drawing to much current and pulling the signal level down .
The ADC on AN0,AN1 AN2 is working correctly :shock:
(1) with out the micro on the pcb the signal level is correct( loading the signal with 2mA level still correct which should be sufficient for an ADC pin)
(2) with the micro on the pcb and the AN 3 set ad digital input the signal level is also correct (there is no pcb or circuit errors)
(3) programming the micro with the exsisting code(which is running on proto 1 the signal level drops down to 0.5V from 2.10V ,the 2.1 is the correct level of the signal.(the ADC pin must be draing more that 5mA).Pin IS_IN is the output from an gain amplifier to AN3 which measure the current in a watervalve circuit
P1.jpg
P1.jpg (79.53 KiB) Viewed 11189 times
I am not sure what is casuing this it seems that the ANSEL registers is not set properly (as the AN3 can also be the Vref pin) the strange thing is that it is the same code .??

any hints idea's tot track this down will help me out :D

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: Strange ADC behaviour

Post by Benj »

Hi Peter,

The only thing I can think of is if the pin is outputting a logic 0 to the pin. The ansel sets the input mode between analogue and digital. Maybe check the tris register and see what state that is in. It should be logic 1 for input mode.

The ADC component restores the state of the tris so if it was a 0 before sampling the adc then the 0 would be restored after sampling.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Strange ADC behaviour

Post by QMESAR »

Hi Ben thank you for your reply

as you will see from the generated C code the TRISA is set at 0 's this outputs and my understanding from the datasheet is as yours
the ANSEL should be 1 to select Analog and the TRIS for the corresponding pin should be set a 1 as input ,The ANSEL A is set correctly
what would you suggets how I can try to force this in my Flowchart :)
P1.jpg
P1.jpg (52.43 KiB) Viewed 11172 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: Strange ADC behaviour

Post by Benj »

Hi Peter,

I would recommend that any pin you use for an analogue input that you initialise the tris bit to 1 instead of 0.

e.g. if pins RA0-RA3 are all analogue inputs then the init code should be.

TRISA = 0x0F;

In fact any pin that is not specifically used as an output should ideally have the tris bit set, this is the default state on reset so getting rid of the tris initialisers may simply solve the problem for you?

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Strange ADC behaviour

Post by QMESAR »

Hi Ben

I am with you on this however this iniatilization code id Flowcode generated automaticaly , Would you suggest that I just before the main loop put some C code and intialize RA0 -R3 to inputs ?
Again you are to clever for the old man today :D

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: Strange ADC behaviour

Post by Benj »

Hi Peter

Yep a c code icon before the main loop should do it :)

Let us know how you get on.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Strange ADC behaviour

Post by QMESAR »

:D :D :D :D :D :D :D
Ben you are the are best !!
Issue solved setting the the ANx to Input with the ANSEL signal spot on current mesurement spot
Ben thank you for pulling me out of this whole

I appreciate you guys always prepaired to help . :D

Lambra
Posts: 3
Joined: Mon Jan 23, 2023 10:25 am
Been thanked: 1 time
Contact:

Re: Strange ADC behaviour

Post by Lambra »

hello.. there guys and gals.. !!
okay so I have a very simple circuit... a PIC16LF88 is used.. I have an ULTRASONIC device connected to.. pin 1.. and the other pin is connected to pin 18.. so now I only want to utilize these two pins.. RA1.. and RA2... in fact A0.. is used to drive a LED... but that is hopefully on its own... so now I tried to create a loop.. that will switch on A1.. and A2 off.. then this is where it came unstuck.. I need a 12.5 usec delay.. then... A1 off.. and A2 on... then wait 12.5 usec.. then switch again... FLOWCODE cannot do this.. with PIC16LF88 at 8 mHz..!!!
so I set about to attempt a C-code loop.. where I have found that if I use TRISA =0xF9;.. the A0 port is affected.. of course... !!!
what other way.. please.. can I use the TRISA command.. so that only the A1 and A2 ports are able to be used.. and A0 stays out the system to be used by FLOWCODE icons to flash...
after the TRISA.. I used PORTA=0x04.. then like say 17 NOP instructions to cause a delay time... ( I will adjust the number of NOP instructs.. )...for the correct delay..).. then the next instruct is.. PORTA=0x02.. with NOP after that.. then again...
so.. please with C-coding.. how / what do I do.. to just have A1 and A2 in this loop.. with A0 and in fact all other A ports free...
I have to do this so that the ULTRASONIC device.. works at 40 000 Hz..
thanking anyone.. in advance.. !! Lambra..

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Strange ADC behaviour

Post by mnf »

Hi,

You need to use | (or) and & (and) to set or clear the relevant bits..

For example in C

Register |= 0b10; // Set bit 1
Register &= ~0b10; // Clear bit 1

Or more generally:

Register |= (1 << bit);
Register &= ~(1 << bit);

Where register is PORTA for example.


Martin

Post Reply