Need help reading pin on pic16f690 ;)

Discussion in 'Embedded Systems and Microcontrollers' started by johnnyinwa, Aug 4, 2015.

  1. johnnyinwa

    Thread Starter Member

    Jun 24, 2013
    41
    1
    Please consult the attached code listing and schematic following this post (3 pages). I am using the XC8 compiler with the MPLABX IDE from microchip with a third party hardware tool programmer. My code compiles fine and dumps normally into my pic16f690 chip. When I stick the chip into my breadboard the three seven segments execute the "subtest function" no matter what I do with the button. I know I'm probably missing something basic here but any help you guys can give me would be much appreciated. I wrote a fairly long program for the pic18f458 with my setup and it worked fine. Thanks for your help guys!!
     
  2. Jswale

    Member

    Jun 30, 2015
    121
    6
    Have you simulated it in MPLAB SIM will RB4 high and low?
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    RB4 is on the same pin as AN10. You need to disable AN10 before you can read data out of that pin.

    That goes for all pins with analog functions: PICs wake up (quite properly too) with analog on by default.
     
    johnnyinwa and JohnInTX like this.
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Plus, analog pins read back as '0' so it will think the button is pressed all the time. Visit ANSEL and ANSELH, see sec 4.2.1 in the datasheet. Any active analog inputs need their TRIS bits set to '1' as well.

    Have fun.
     
    johnnyinwa likes this.
  5. johnnyinwa

    Thread Starter Member

    Jun 24, 2013
    41
    1
    OK guys here is the poop (I figured it out). Adding the line of code: "ANSELH=0;" after the TRIS statements allows the input pin to be configured as a digital input. Then the button will work. You must remember however that the program is written in C. JOHNINTX was right on with his post (thank you very much!!). Without the line of code mentioned above the chip will always read a zero on the pin.

    Anyway there are actually two eight bit registers that control weather a pin is analog or digital. Setting a bit to zero makes the pin digital and setting a bit to one makes it analog. There are two registers for the pic16f690 that control this process -- ANSEL (ANS0-ANS7) and ANSELH (ANS8-ANS11). Remember that four bits in ANSELH are not used. This web site rocks -- I have learned gobs more here than any book ever taught me!!
     
  6. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Well, the first, and most relevant book, should be the datasheet.
     
    JohnInTX and ErnieM like this.
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    QFT.

    Microchip has been quite kind in their data sheets concerning the ports and what registers affect them. It's always at the end of every port; this was not the case "back in the day" and it was easy to miss some importaint information. Now they try to make it more obvious if you take the time to read the data sheet.

    Always go thru that list, though as a shortcut look at the pin diagram where all the alternate pin functions are also defined. Make sure of what gets turned on or off by default when you want a some specific function on an I/O pin.
     
Loading...