Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PORTC,1 input 16f690

Status
Not open for further replies.

ibwev

Member
On a 5 volt system using PicKit2 debugger and PIC 16F690, PORTC,1 is not acknowledging an input signal from a light sensor. If I connect the light sensor to RA4, it works; however, when I connect the same sensor to RC1 with TRISC,1 set high, RC1 remains clear. If I connect RC1 directly to 5 volts, RC1 will set. My initialization code is below. Please tell me how to get RC1 to work with the light sensor.

;initialize PORTA
BCF STATUS,RP0 ;Bank 0
BCF STATUS,RP1 ;
CLRF PORTA ;Init PORTA
BSF STATUS,RP1 ;Bank 2
CLRF ANSEL ;digital I/O
BSF STATUS,RP0 ;Bank 1
BCF STATUS,RP1 ;
MOVLW 0x1C ;Set RA<4:2> as inputs
MOVWF TRISA ;and set RA<5,1:0> as outputs
BCF STATUS,RP0 ;Bank 0

;Initialize PORTC datasheet 66
BCF STATUS,RP1 ;
CLRF PORTC ;Init PORTC
BSF STATUS,RP1 ;Bank 2
CLRF ANSEL ;digital I/O
BSF STATUS,RP0 ;Bank 1
BCF STATUS,RP1 ;
MOVLW 0x2 ;Set RC<7:2,0> as output
MOVWF TRISC ;set RC<1> as input
BCF STATUS,RP0 ;Bank 0
 
Actually...RC1 is part of the comparator and the comparators are initialized to off upon power up/reset.

To the OP...you've got a bit of redundant code in there. It's much "neater" to set up the port/ansel/anselh/tris registers all in one shot rather than one at a time. You don't need a separate routine to initialize each port.

Also, the "banksel" directive makes things much easier as it allows the assembler to generate the correct bank select code rather than you having to think about which register page bits to set/clear. Here's how the init code should look -

Code:
;initialization routine

		clrf		PORTA		;initialize all ports
		clrf		PORTB
		clrf		PORTC
		banksel		ANSEL		;bank 2
		clrf		ANSEL		;all ports digital I/O
		clrf		ANSELH
		banksel		TRISA		;bank 1
		movlw		b'00011100'	;RA<4:2> as inputs
		movwf		TRISA
		movlw		b'00000000'	;RB0-RB7 outputs
		movwf		TRISB
		movlw		b'00000010'	;RC<7:2,0> as output
		movwf		TRISC		;RC1-Input
		banksel		PORTA		;bank 0

Without seeing a schematic I'm assuming that the sensor is connected between the pin and ground as the current sink. On inputs, you will ALWAYS need an external pull up resistor unless -

1) The port you're using has weak internal pull ups

2) The device driving the input pin has source/sink capability

If you look in the 16F690 datasheet you will see that only PORTA and PORTB pins have weak internal pull ups. Although they should be turned off upon power up/reset (OPTION_REG:RABPU comes up as 1 and is an active low bit), it sounds like RA4's weak internal pull up is active, which allows the sensor to work on RA4 without the external pull up resistor.

Since PORTC does not have weak internal pull ups on its pins and the light/dark sensor can only sink, you will need an external pull up resistor between RC1 and the +5V source. 10K is the standard value.

You can reference the 16F690 datasheet here -

https://www.electro-tech-online.com/custompdfs/2011/08/41262a.pdf
 
Last edited:
Thanks for your insightful responses. I do have weak internal pull ups turned on. I did not realize that it only applied to PORTA and PORTB. Prior to reading Jon's reply, I tried the light sensor on RC5 with a 200 ohm pull up resistor. It worked on RC5 but did not work on any of the other PORTC ports which were tied to a light on the PicKit2 board. Is this because the light on the PicKit2 board is pulling enough current to prevent the Pic from reading a high signal? I noticed when the light sensor was high (and the default light on the PicKit2 board was on), the voltage on RC1 was 2.7 volts when attached to the PicKit2 board and 4.5 volts when unattached.
 
This is another one of those questions that could be answered by consulting some literature, in this case being the Microchip Low Pin Count Demo Board User Guide. On reader page 41 you will find the schematic for the board.

RC0 - RC3 are tied to an LED/resistor, but also have jumpers that allow you to disconnect the LEDs from those PORTC lines. You must remove the jumper from the PORTC line that you intend to configure as an input. That's why those jumpers are there. If you don't, the LED/resistor will act as a pull down as it pulls current through the pull up (causing the pull up to drop the pin voltage) so your light/dark resistor won't even be seen by the pin.

Word of advice...start reading the literature on both the Low Pin Count Demo Board as well as the 16F690 data sheet. If you don't understand something in the literature, post up here and we will help you with it. We're always willing to help those who make the effort to read the literature that is available. One of the awesome things about Microchip is that their documentation is second to none and it is freely available from their website.
 
Last edited:
Thanks Jon for the generous help. I will try to do a better job researching answers before posting questions.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top