pickit2 turn on two leds?

Thread Starter

chris13409

Joined Jan 7, 2010
7
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:
Rich (BB code):
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
Start:
     bsf     STATUS,RP0    ; select Register Page 1
     bcf     TRISC,0          ; make IO Pin C0 an output
     bcf     STATUS,RP0    ; back to Register Page 0
     bsf     PORTC,0          ; turn on LED C0 (DS1)
     goto    $                   ; wait here
     end
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:
Rich (BB code):
#include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
     org 0
Start:
     bsf     STATUS,RP0    ; select Register Page 1
     bcf     TRISC,0          ; make IO Pin C0 an output
     bcf     TRISC,1          ; make IO Pin C1 an output
     bcf     STATUS,RP0    ; back to Register Page 0
     bsf     PORTC,0          ; turn on LED C0 (DS1)
     bsf     PORTC,1          ; turn on LED C1 (DS2)

     goto    $                ; wait here
     end
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?
 

Markd77

Joined Sep 7, 2009
2,806
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
 

t06afre

Joined May 11, 2009
5,934
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
The ANSEL and ANSELH registers are used to disable
the input buffers of I/O pins, which allow analog voltages
to be applied to those pins without causing excessive
current. Setting the ANSx bit of a corresponding pin will
cause all digital reads of that pin to return ‘0’ and also
permit analog functions of that pin to operate correctly.
The state of the ANSx bit has no effect on the digital
output function of its corresponding pin. A pin with the
TRISx bit clear and ANSx bit set will operate as a digital
output, together with the analog input function of that
pin. Pins with the ANSx bit set always read ‘0’, which
can cause unexpected behavior when executing read
or write operations on the port due to the
read-modify-write sequence of all such operations.
For PORTC we also have
All write operations are read-modify-write operations.
Therefore, a write to a port implies that the port pins are
read, this value is modified and then written to the PORT
data latch.
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
Rich (BB code):
;EXAMPLE 4-4: INITIALIZING PORTC
BCF STATUS,RP0 ;Bank 0
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 0Ch ;Set RC<3:2> as inputs
MOVWF TRISC ;and set RC<5:4,1:0> as outputs
BCF STATUS,RP0 ;Bank 0
 
Last edited:

Thread Starter

chris13409

Joined Jan 7, 2010
7
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.
 

Dalaran

Joined Dec 3, 2009
168
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.
 
Top