View previous topic :: View next topic |
Author |
Message |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
SOLVED 18f47k40 ADC problem on D5 |
Posted: Tue Feb 15, 2022 3:16 pm |
|
|
Compiler 5.106
When using Pin D5 as analog, on some boards it read it correctly and on other reads 0 only, replaced the PIC, no difference.
If I short out D5 and D6 and changed the analog port to D6, it works on the boards that don't work on D5. Checked continuity OK.
I know this is probably a hardware question, although I don't rule out software
Here are the test program
Code: |
#include <18F47K40.h>
#include <stdint.h>
#device ADC=8
#FUSES NOEXTOSC //External Oscillator not enabled
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES PUT //Power Up Timer
#FUSES LPBOR //Low-Power Brownout reset is enabled
#FUSES BORV27 //Brownout reset at 2.7V
#FUSES NOSCANE //Scanner module is not available for use
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or
#FUSES NOPROTECT //Code protected from reads
#FUSES NOCLKOUT
#FUSES NOWDT
#use delay(internal=64MHz)
void main() {
setup_adc_ports(sAN29);
setup_adc(ADC_CLOCK_DIV_128 | ADC_TAD_MUL_4);
setup_comparator(NC_NC_NC_NC);
setup_dac(DAC_OFF);
set_adc_channel(29);
while (TRUE){
if (read_adc()) output_high(LED); //if value higher that 0 switch on LED
else output_low(LED);
delay_ms(250);
}
}
|
Regards
Last edited by alan on Thu Feb 17, 2022 7:08 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Feb 15, 2022 4:09 pm |
|
|
Ok, I don't use that PIC but
are there any internal peripherals attached to that pin that need to be disabled ?
this if (read_adc()) output_high(LED); //if value higher that 0 switch on LED
may not be correct ?
read_adc() returns a value(0...255)
but.. there's no 'condition' for the 'if'
I'd expect something like
if(read_adc()>0) output_high(LED);
maybe it's OK, but I'd like to see the assembler to understand how/why it works. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Feb 16, 2022 7:49 am |
|
|
Using the adc value as a test is perfectly OK. This is 'standard C', where
and non zero value is treated as 'TRUE'. I must admit though if I was
testing the value from the ADC, I would have a little margin (a count or
two), since many of the PIC ADC's will still return a count intermittently
when the input is shorted to 0v...
Looking at the code generated by 5.105 (haven't got 5.106, I had a couple
of issues with this version), the ADC setup and operation looks correct.
It has got the errata implemented for the ADC.
You might want to see if it changes with *OPT 0. A couple of problems
seem to have been introduced on the latest couple of versions with the
optimiser being too enthusiastic and optimising away some needed bank
switches. However this isn't obvious in the 5.105 code.
Is this a DIL PIC, or a SM version?. If the latter, I would be looking very
carefully to make sure the connection actually is being made correctly. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Feb 16, 2022 8:55 am |
|
|
re: Using the adc value as a test is perfectly OK. This is 'standard C', where
and non zero value is treated as 'TRUE'.
interesting, glad to be educated ! See you CAN teach an old dog new tricks !!
Right about the 'margin' ! There's lots of reasons why an analog pin may not be 'ZERO' when read so a margin of say 4 -5 counts would be safer. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Feb 16, 2022 11:23 am |
|
|
The silly thing is that lots of people use the non zero test, without even
realising it. For instance:
if (val & 4)
(or 2, 8 etc.), as a test |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Feb 16, 2022 11:51 am |
|
|
hmm.
to me
if(val & 4)
is a 'proper' IF statement as 'something ' is done inside the brackets to decide a 'condition.
whereas
if(val)
doesn't do anything for the 'if' condition.
though now that I'm typing this,if val was 0,val doesn't exist kinda makes sense , as I see this ,now, like if(TRUE).
some dayze code is clear as mud...... |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Feb 17, 2022 7:07 am |
|
|
Thanks for all the replies, seems to be hardware after all, were a bit enthusiastic with my gain of the op amp, and on some of them it didn't work. Just funny that is were always the same 1 on the board, there are 4 low side current amplifiers with just an op amp. Needed to go cheap and now it bites me. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Feb 17, 2022 7:16 am |
|
|
Glad you have found it. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Feb 17, 2022 7:16 am |
|
|
glad you FOUND it !!
I learned the hard way decades ago..back then,afte rspending 2 dayze on a 'bad' board,I put all chips into sockets(DIPs). It cost less to socket 100 boards than to troubleshoot which of the 20 chips had fried on one PCB.
I've always been a firm believer of paying a little more 'upfront'.Often people don't consider true repair costs(TIME).... |
|
|
|