1
\$\begingroup\$

I'm using the pic 18F4550 with microchip v8.63 and with the C compiler C18.

I want to trigger port RB3, RB4 and RB5. when, I press on respectively S1 (switch 1), S2 switch (S2) and S3 (switch 3). on these ports (RB3, RB4 and RB5) there are leds connected to, the should burn when I press S1 or S2 or S2.

My first question is, How do I stream from my S1 and S2 and S3 to the gates (RB3, RB4, RB5).

And second how can I enable port RB6, there is a light dependent resistor connected to.

I wrote this program: but, nothing is happening. only led 1 goes on.

#include "p18cxxx.h"  
void main(void) 
{ 
    // turn off all LED latches 
    LATDbits.LATD0 = 0; 
    LATDbits.LATD1 = 0; 
    LATDbits.LATD2 = 0; 
    LATDbits.LATD3 = 0; 
    // make port d bits which drive LEDs outputs 
    TRISDbits.TRISD0 = 0; 
    TRISDbits.TRISD1 = 0; 
    TRISDbits.TRISD2 = 0; 
    TRISDbits.TRISD3 = 0; 

    if(PORTBbits.RB3 == 0) {
        LATDbits.LATD0 = 1;     
    }
    if(PORTBbits.RB4 == 0) {
        LATDbits.LATD1 = 1;
    }
    if(PORTBbits.RB5 == 0) {
        LATDbits.LATD2 = 1;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ The terminology in your question is not standard, so we have difficulty understanding your meaning. When you say "stream from switches to gates" do you mean "read inputs on ports"? \$\endgroup\$
    – Martin
    Commented Mar 28, 2011 at 8:02

2 Answers 2

5
\$\begingroup\$

Your main routine runs once only. You need a loop, probably starting after the ports are initiallised, around the if statements.

\$\endgroup\$
3
\$\begingroup\$

Several of the pins you are using also have an analogue input function, which is enabled by default on reset. For instance, RB3 will be the AN9 ADC input.

See the data sheet (page 116) for details of how to enable them for digital I/O. You need to set some bits in your C program; I use this for PORTA on the 18F4520:

ADCON1 = 0x0F;
\$\endgroup\$
2
  • \$\begingroup\$ I already read this, but everything is wrote in assembly and I write this program in C program language. it would make a good, if I have an example in C. \$\endgroup\$
    – yannick
    Commented Mar 27, 2011 at 15:08
  • \$\begingroup\$ @yannick, Leon has shown you how to set the ADCON bits using a valid line of C. \$\endgroup\$
    – Martin
    Commented Mar 28, 2011 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.