CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

SOLVED 18f47k40 ADC problem on D5

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alan



Joined: 12 Nov 2012
Posts: 337
Location: South Africa

View user's profile Send private message

SOLVED 18f47k40 ADC problem on D5
PostPosted: Tue Feb 15, 2022 3:16 pm     Reply with quote

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: 8525
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 15, 2022 4:09 pm     Reply with quote

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: 18072

View user's profile Send private message

PostPosted: Wed Feb 16, 2022 7:49 am     Reply with quote

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: 8525
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 16, 2022 8:55 am     Reply with quote

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: 18072

View user's profile Send private message

PostPosted: Wed Feb 16, 2022 11:23 am     Reply with quote

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: 8525
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 16, 2022 11:51 am     Reply with quote

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: 337
Location: South Africa

View user's profile Send private message

PostPosted: Thu Feb 17, 2022 7:07 am     Reply with quote

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: 18072

View user's profile Send private message

PostPosted: Thu Feb 17, 2022 7:16 am     Reply with quote

Glad you have found it. Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 8525
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 17, 2022 7:16 am     Reply with quote

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)....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group