# ASM HELP: setting/clearing output

#### tigerapna

Joined Dec 14, 2011
73
I am a little puzzled by the following sampLe in my code.

If I close switch one and set PORTC,0 and PORTC,1, why does PORTC,1 also go off if when i open the switch I only clear PORTC,0?

Rich (BB code):
#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
cblock 0x20
CounterA                   ; Define two file registers for the
CounterB                  ; delay loop
CounterC
CounterD
ALARM
endc
org 0

INIT:
BSF         STATUS,RP0
CLRF        TRISC
MOVLW       0XFF
MOVWF       TRISB
BCF         STATUS,RP0
CHECKDHW:
BTFSC       PORTB,7         ;IS SWITCH OPEN
GOTO        PRIORITYOFF
GOTO        PRIORITYONLY
PRIORITYOFF:
BCF         PORTC,0
GOTO      CHECKDHW
PRIORITYONLY:
BSF         PORTC,0
BSF         PORTC,1
GOTO      CHECKDHW

END

#### R!f@@

Joined Apr 2, 2009
9,682
I dunno what u are really asking but what is the switch input.

Is it a push button switch ?

#### t06afre

Joined May 11, 2009
5,934
I have two words for you. ANSEL and ANSELH So write a BIG note with this words and put in on your desk. "By default analog functions on PICs are turned on. So I must always remember to set the ANSEL and ANSELH register correct in every program I make"

#### tigerapna

Joined Dec 14, 2011
73
I have two words for you. ANSEL and ANSELH So write a BIG note with this words and put in on your desk. "By default analog functions on PICs are turned on. So I must always remember to set the ANSEL and ANSELH register correct in every program I make"
(writing down note on desK)

So because of analog being set I cannot manipulate individual bits on PORTC?

#### t06afre

Joined May 11, 2009
5,934
PICs use something named Read-Modify-Write (or RMW). In the instruction BCF. You might think that the processor just clears the bit, which on a port output pin would clear the pin. What actually happens is the whole port (or register) is first read, THEN the bit is cleared, then the new modified value is written back to the port (or register).
If a pin is configured for analog input. Any read of that pin will read a zero, regardless of the voltage on the pin. You can still configure an analog pin as an output in the TRIS register, and drive the pin high or low by writing to it, but you will always read a zero. And as I have mention before on the PIC16F series. If a pin have any analog input function. This will be the default setting after any reset or power on cycle.

#### tigerapna

Joined Dec 14, 2011
73
PICs use something named Read-Modify-Write (or RMW). In the instruction BCF. You might think that the processor just clears the bit, which on a port output pin would clear the pin. What actually happens is the whole port (or register) is first read, THEN the bit is cleared, then the new modified value is written back to the port (or register).
If a pin is configured for analog input. Any read of that pin will read a zero, regardless of the voltage on the pin. You can still configure an analog pin as an output in the TRIS register, and drive the pin high or low by writing to it, but you will always read a zero. And as I have mention before on the PIC16F series. If a pin have any analog input function. This will be the default setting after any reset or power on cycle.
so should I used a different PORT for output that I want to remain on? Instead of using two outputs on PORTC?

#### t06afre

Joined May 11, 2009
5,934
so should I used a different PORT for output that I want to remain on? Instead of using two outputs on PORTC?
Do not be so slow. You must focus on what I am writing. BOTH RC0 AND RC1 do have analog functions. IF you are going to use them as digital I/O. You MUST set the corresponding bits in the ANSEL (or ANSELH) to zero. I gave you a perfectly good explanation why you see this behavior in post #5

#### tigerapna

Joined Dec 14, 2011
73
Do not be so slow. You must focus on what I am writing. BOTH RC0 AND RC1 do have analog functions. IF you are going to use them as digital I/O. You MUST set the corresponding bits in the ANSEL (or ANSELH) to zero. I gave you a perfectly good explanation why you see this behavior in post #5
Yes t06afre, you gave a great explanation as always.

Maybe I should have explained. You said I will always read a 0 instead of any voltage on the input PIN if I do not clear ANSEL. Oddly, without clearing ANSEL my program was working fine except for the output part which was not manipulated individual bits.

So my question was if I CLRF ANSEL will that effect my output registers.

But everything is working great now and Thank You again for answering my questions.

#### t06afre

Joined May 11, 2009
5,934
OK first take a look at table 4-3 in the datasheet. You will se that it is TWO ansel registers ANSEL and ANSELH. For PORTC bit 0 and 1 the corsponding bit are ANSELH bit 0, and 1. As I said as long as you use only digital IO. Clear BOTH ANSEL and ANSELH registers at the start of your program. Just do it