Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

please correct my simple code!!

Status
Not open for further replies.

tuanvoi

New Member
#include<p18f4550.h>

void main ()
{

TRISC = 0xFF;//configure all pins of port C to be input

TRISD = 0x10111111; //configure pin6 of portD to be output

if(PORTCbits.RC1 == 1)
{
PORTDbits.RD6 = 1;

}

else

{

PORTDbits.RD6 = 0;

}

}

The code runs correctly. The problem is: when I input 5V into the pinC1, the pinD6 LED is on. And when I take 5V out from pinC1, the pinD6 LED continues to light for about 5 to 8 seconds(ideally it should turn off immediately). Could you please help me figure out the problem.

p/s I'm using PIC18F4550.
 
First PLEASE use the # sign to place
Code:
tags around you code like this.

Code:
#include<p18f4550.h>
void main ()
{
  TRISC = 0xFF;//configure all pins of port C to be input
  TRISD = 0x10111111; //configure pin6 of portD to be output
  if(PORTCbits.RC1 == 1)
  {
    PORTDbits.RD6 = 1;
  }
  else
  {
    PORTDbits.RD6 = 0;
  }
}

The problem is: when I input 5V into the pinC1, the pinD6 LED is on. And when I take 5V out from pinC1,
It is not enough to take the 5V away. The input pin needs to be grounded for it to read zero. Without 5V or GND the pin floats.

The most common way to do this is to use a switch and a pullup (or pulldown) resistor.

3v0
 
First PLEASE use the # sign to place
Code:
tags around you code like this.

Code:
#include<p18f4550.h>
void main ()
{
  TRISC = 0xFF;//configure all pins of port C to be input
  TRISD = 0x10111111; //configure pin6 of portD to be output
  if(PORTCbits.RC1 == 1)
  {
    PORTDbits.RD6 = 1;
  }
  else
  {
    PORTDbits.RD6 = 0;
  }
}

It is not enough to take the 5V away. The input pin needs to be grounded for it to read zero. Without 5V or GND the pin floats.

The most common way to do this is to use a switch and a pullup (or pulldown) resistor.

3v0

Thanks for your advice 3v0! I'll try it!
 
Last edited:
Hello,
The code below is fixed, but still I have some problems:
Code:
#include <p18f4550.h>
#pragma config WDT=OFF,LVP=OFF,FOSC=HSPLL_HS,PLLDIV=5,CPUDIV=OSC2 _PLL3 //run with external oscillator
void main(void)
{
ADCON0bits.ADON=0;
ADCON1 = 0x07;
//CMCON = 0x07;
TRISA = 0x00;
TRISC = 0x00;
TRISB = 0xFF;

while(1)
{
if(PORTBbits.RB0 == 1) 
PORTAbits.RA0 = 1; 

else
PORTAbits.RA0 = 0;
} 

}

The problem is when there is no input on PORTB pin 0, the output PORTA pin0 still show 5V. If I input 0V (ground from the PIC board), the output show exactly 0V as expected and as if I input 5V(Vdd from the PIC board), the output shows exactly 5V. The question is why, when there is not input (not 0V or 5V), the output still shows me 5V? Thank you!
Tom
 
Last edited:
Sorry where should I put? Between RA0 of PIC18 #1 and RB0 of PIC18 #2? What values should they be? Thank 3v0!
Tom

You have an output pin from one PIC coneected to the input pin of the second.

As long as the driving pin is not tristated (using the asociated TRIS register) you do not need a pullup resistor.
You have only provided the code for the PIC that is reading the signal. So I have no idea if the sending PIC is tristating the line.

If your code tristates the driving pin then you need a pullup. Try a 4.7K resistor between the input pin and +5.

When the sending PIC tristates the output pin, the resistor will pull the line to +5.
If you would rather have the line go to gnd tie it to gnd instead of +5. Then it would be a pull down resistor.

With only half the code and no real schematic I can only guess.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top