Need help reading pin on pic16f690 ;)

Thread Starter

johnnyinwa

Joined Jun 24, 2013
61
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!!
 

Attachments

ErnieM

Joined Apr 24, 2011
8,377
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
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.
 

Thread Starter

johnnyinwa

Joined Jun 24, 2013
61
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!!
 

ErnieM

Joined Apr 24, 2011
8,377
Well, the first, and most relevant book, should be the datasheet.
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.
 
Top