ASM HELP: setting/clearing output

Discussion in 'The Projects Forum' started by tigerapna, Mar 29, 2012.

  1. tigerapna

    Thread Starter Member

    Dec 14, 2011
    73
    1
    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?

    Code ( (Unknown Language)):
    1.  
    2. #include <p16F690.inc>
    3.     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    4.     cblock 0x20
    5. CounterA                   ; Define two file registers for the
    6. CounterB                  ; delay loop
    7. CounterC
    8. CounterD
    9. ALARM
    10.      endc
    11.      org 0
    12.  
    13. INIT:
    14.         BSF         STATUS,RP0
    15.         CLRF        TRISC
    16.         MOVLW       0XFF
    17.         MOVWF       TRISB
    18.         BCF         STATUS,RP0
    19. CHECKDHW:
    20.         BTFSC       PORTB,7         ;IS SWITCH OPEN
    21.         GOTO        PRIORITYOFF
    22.         GOTO        PRIORITYONLY
    23. PRIORITYOFF:
    24.         BCF         PORTC,0
    25.         GOTO      CHECKDHW
    26. PRIORITYONLY:
    27.         BSF         PORTC,0
    28.         BSF         PORTC,1
    29.         GOTO      CHECKDHW
    30.  
    31.         END
    32.  
     
  2. R!f@@

    AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I dunno what u are really asking but what is the switch input.

    Is it a push button switch ?
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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"
     
  4. tigerapna

    Thread Starter Member

    Dec 14, 2011
    73
    1
    (writing down note on desK) :p

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

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  6. tigerapna

    Thread Starter Member

    Dec 14, 2011
    73
    1
    so should I used a different PORT for output that I want to remain on? Instead of using two outputs on PORTC?
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  8. tigerapna

    Thread Starter Member

    Dec 14, 2011
    73
    1
    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.
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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 ;)
     
    tigerapna likes this.
  10. tigerapna

    Thread Starter Member

    Dec 14, 2011
    73
    1
    yes sir!! :D
     
Loading...