quickie adc question

Discussion in 'Embedded Systems and Microcontrollers' started by Dakta, Jan 30, 2011.

  1. Dakta

    Thread Starter New Member

    Nov 22, 2010
    4
    0
    Hi everyone, got a little problem with the adc module on a pic 16f873A.

    Basically I'm trying to use 2 channels of the adc module, 0 and 1. Channel 0 works fine, however all the other channels pull the analogue signal down. (i.e 0v, even if the sensor is putting out a voltage). If I remove the pic, the voltage level is what the sensor outputs, if i plug the pic in, it is then tied down.

    Channel 0 works fine, channel 1 onwards pulls the signal down.

    I beleive the problem is a misconfiguration of the chip, as the circuit is fine (when the pic isn't connected anyway) and I have absolutely no problems whatsoever in using the first adc channel (0).

    I am using MikroC if this helps and this is my configuration:

    TRISA = 1 (all port A as input)
    ADCON0 = 11001001 (set adc on, set channel to '1')
    ADCON1 - (last four bits set to 0) - all channels set to analog.

    Now these two are the only registers that set up the adc module (ignore the adresult high and low registers), so I'm not sure what could be wrong.

    Any ideas? It's a daft problem really.

    cheers
     
  2. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    Some code would be nice to have; especially if you want assistance.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This sets only PORTA, 0 to input.
    You want to use TRISA=0xFF
     
    Dakta likes this.
  4. Dakta

    Thread Starter New Member

    Nov 22, 2010
    4
    0
    Thanks Mark you solved the problem.

    This is not my first project, I've been teaching myself for a while now, and I think this is one of those instances where working through example code does less favours to a learner than reading a book, because the line:

    TRISA = 1; I had seen, and used (without any issues as I normally work with channel 0 of the adc module) under the assumption that the compiler treat it as shorthand for setting all the bits to input.

    I knew when I made this thread that it would be a simple solution, but I couldn't see it. Thanks chaps.

    I know exactly what you mean, but the last thing I wanted to do as a first time poster was start expecting y'all to go through my code.

    I'm a slow learner but I try not to expect too much help. (key word - try).
     
  5. nigelwright7557

    Senior Member

    May 10, 2008
    487
    71
    TRIS A shoud be 255
     
  6. Dakta

    Thread Starter New Member

    Nov 22, 2010
    4
    0
    Agreed it should.

    There was just a bit of confusion over which bits were set (just the one!)
     
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    You can use the 0bxxxxxxxx format to enter a binary number directly, specifying each port, port0 is the rightmost bit.

    Using two nibbles (4 binary digits) can make a hex number easily, such as 0x0F sets the first 4 to input, and the second 4 to outputs.

    I very rarely use decimal numbers when programming microcontrollers, thinking in bits just seems to be the simplest, and hexadecimal is made for thinking in bits, without having to write all of them out. :)
     
    Dakta likes this.
  8. Dakta

    Thread Starter New Member

    Nov 22, 2010
    4
    0
    That's a nifty bit of information :D
     
Loading...