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;
  • \$\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 Mar 28 '11 at 8:02

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


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;
  • \$\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 Mar 27 '11 at 15:08
  • \$\begingroup\$ @yannick, Leon has shown you how to set the ADCON bits using a valid line of C. \$\endgroup\$ – Martin Mar 28 '11 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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