# Driving Multiple LED's - Problem

Status
Not open for further replies.

#### SpecialAgentBob

##### New Member
Hello everyone!

Glad to see such a supportive site for electronics.

I have a quick, most likely extremely simple question that I'm having a difficult time. Currently, I'm programming a circuit to drive a "sequence" of LEDs (with two designed to be on at one time), however, when I apply power to both LEDs, the second one to go high in the code will turn off the first one.

The code seems to work if I simply turn the entire PORTC on, such as with PORTC = 0xff. However, sending individual commands with RB6, RB7, and RB3 will only allow for one LED to be powered at once.

For example:

RC6 = 1; // LED 1
RC7 = 1; // LED 2

The code above will only make RC7 go high.

I apologize in advanced if this subject has been touched on, or if it is stated directly in the datasheet. I have tried searching, with no luck so far.

Appreciate the help in advanced.
Michael

#### gaspode42

##### Member
SpecialAgentBob,

Which pic are you using - and what is the exact code that you are suing to set the pins?
Have a look on your data-sheet for read-modify-write. If you change a pin it will read the port, modify the bit & write the port. If you have not configured the port correctly (turn off comparators etc) the will always read beck as 0 (zero).

Last edited:

#### SpecialAgentBob

##### New Member
Thanks for such a prompt response!

Here is the code:

Code:
#include <pic.h>
#include <delay.h>

void main(void)
{

#define LED_OPEN TRISC6
#define LED_LOCKED TRISC7
#define LED_CLOSE TRISC3

#define SW_DIR TRISB7
#define SW_LOCKED TRISB6

#define IN 1
#define OUT 0

#define LED_OPEN_PIN RC6
#define LED_LOCKED_PIN RC7
#define LED_CLOSE_PIN RC3

#define SW_PIN RB7
#define SW_LOCKED_PIN RB6

while(1)	// continually
{
RBPU = 0;		// enable internal pullups on PORTB

LED_OPEN = OUT;
LED_LOCKED = OUT;
LED_CLOSE = OUT;

SW_DIR = IN;
SW_LOCKED = IN;

if(SW_PIN == 1 && SW_LOCKED_PIN == 0)
{
// Then open the door
// First illuminate lock
LED_LOCKED_PIN = 1;
DelayMs(100);

// Now open the door
LED_OPEN_PIN = 1;
DelayMs(2000);

LED_OPEN_PIN = 0;
DelayMs(100);

LED_LOCKED_PIN = 0;

while(1)
{
// Door open
if(SW_PIN == 1)
{
// Door close
// First illuminate lock
LED_LOCKED_PIN = 1;
DelayMs(100);

// Now close the door
LED_CLOSE_PIN = 1;
DelayMs(2000);

LED_CLOSE_PIN = 0;
DelayMs(100);

LED_LOCKED_PIN = 0;

break;
}
}
}
else if(SW_LOCKED_PIN == 1)
{
LED_LOCKED_PIN = 1;
}
else
{
LED_LOCKED_PIN = 0;
LED_OPEN_PIN = 0;
LED_CLOSE_PIN = 0;
}
}
}
This is being compiled in MPLAB, and programmed onto a PIC16F690. After reviewing the datasheet, I do not feel as if the comparator is causing the problem - though I don't understand much about it.

As for the read-write-modify possibility, I never knew that could cause a problem! However, the delays in my code after the pins go high separate out the read-write-modify process, meaning the modify is taking place way in front of the next read, correct?

Thanks for the help!
Michael

#### gaspode42

##### Member
Have a look at the data-sheet - top of page 76 (Port C config.) ANSEL & ANSELHI need to be configured or ports will read 0. CMCON also needs to be set.

Try adding to start of code:

CMCON0 = 7 //set digital I/O
ANSEL = 0 //

Last edited:

#### SpecialAgentBob

##### New Member
Ah! That fixed it.

Thanks for all the help - I appreciate it greatly!

Status
Not open for further replies.