pickit2 turn on two leds?

Discussion in 'Embedded Systems and Microcontrollers' started by chris13409, Jan 7, 2010.

  1. chris13409

    Thread Starter New Member

    Jan 7, 2010
    7
    0
    I'm just starting out using the pick2 starter kit. Lesson 1 is "Turn on an led"
    This is the code to turn led DS1 on:
    Code ( (Unknown Language)):
    1. #include <p16F690.inc>
    2.      __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    3.      org 0
    4. Start:
    5.      bsf     STATUS,RP0    ; select Register Page 1
    6.      bcf     TRISC,0          ; make IO Pin C0 an output
    7.      bcf     STATUS,RP0    ; back to Register Page 0
    8.      bsf     PORTC,0          ; turn on LED C0 (DS1)
    9.      goto    $                   ; wait here
    10.      end
    11.    
    I can turn any single led on by changing TRIC,0 and PORTC,0.
    I want to turn two or more leds on. I thought it would be as simple as this:
    Code ( (Unknown Language)):
    1. #include <p16F690.inc>
    2.      __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    3.      org 0
    4. Start:
    5.      bsf     STATUS,RP0    ; select Register Page 1
    6.      bcf     TRISC,0          ; make IO Pin C0 an output
    7.      [COLOR=red]bcf     TRISC,1          ; make IO Pin C1 an output[/COLOR]
    8.      bcf     STATUS,RP0    ; back to Register Page 0
    9.      bsf     PORTC,0          ; turn on LED C0 (DS1)
    10.      [COLOR=red]bsf     PORTC,1          ; turn on LED C1 (DS2)[/COLOR]
    11.  
    12.      goto    $                ; wait here
    13.      end
    14.    
    However, the only led that will turn on is the last one in the code. (in this case DS2) I've tried with 0,1,2,3. It is allways the last one in the code that lights.

    What is the right way to turn on two leds?
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Have a search for the read, modify, write problem. Basically it is a bad idea to do bit operations on the port. Either change the bits in a buffer and then write that to the port or do:
    movlw B'00000011'
    movwf PORTC
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This is not obvious. But then after power up/reset the bits in ANSEL/ANSELH register is set to 1. From the PIC 16F690 data sheet we have
    For PORTC we also have
    In your case all ANSEL/ANSELH bits are set, so if you use the BSF function all other bits will be reset.
    Take a look at example 4-4 in the data sheet for the PIC 16f690
    Code ( (Unknown Language)):
    1.  
    2. ;EXAMPLE 4-4: INITIALIZING PORTC
    3. BCF STATUS,RP0 ;Bank 0
    4. BCF STATUS,RP1 ;
    5. CLRF PORTC ;Init PORTC
    6. BSF STATUS,RP1 ;Bank 2
    7. CLRF ANSEL ;digital I/O
    8. BSF STATUS,RP0 ;Bank 1
    9. BCF STATUS,RP1 ;
    10. MOVLW 0Ch ;Set RC<3:2> as inputs
    11. MOVWF TRISC ;and set RC<5:4,1:0> as outputs
    12. BCF STATUS,RP0 ;Bank 0
    13.  
     
    Last edited: Jan 7, 2010
  4. chris13409

    Thread Starter New Member

    Jan 7, 2010
    7
    0
    Got it figured out.
    It was mainly ANSEL.
    I still don't see the answer in the data sheet. I guesse that it's one of those things wher you know it in the first place.
     
  5. Dalaran

    Active Member

    Dec 3, 2009
    168
    0
    I think t06afre quoted the correct text from the datasheet. It specifically says that the ANSEL bits must be cleared or you might have unexpected behaviour when executing read-modify-write commands. EVERYTHING is in the datasheet, it just usually takes more reading than the minimum.

    eg. You have no need to use the ADC but this section must be read to not have difficulties, same goes for comparator, config bits, etc,etc.
     
Loading...