please correct my simple code!!

Thread Starter

tuanvoi

Joined Oct 31, 2008
56
#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.
 

mik3

Joined Feb 4, 2008
4,843
I think this

TRISD = 0x10111111;

should be

TRISD = 0b10111111;


Where is the oscillator configuration, interrupts, etc?
 

Thread Starter

tuanvoi

Joined Oct 31, 2008
56
I think this

TRISD = 0x10111111;

should be

TRISD = 0b10111111;


Where is the oscillator configuration, interrupts, etc?
Thanks
But TRISD = 0x10111111 ---> for MPLAB C compiler
And TRISD = 0b10111111 ---> for CCS C compiler
So I think it should be fine on my configurations.
Any other problem that could be occur? Thanks
 

thatoneguy

Joined Feb 19, 2009
6,359
But it delays 5 to 8 seconds which supposed to be turned off when I input 0V in.
How often is this bit of code run, after optimization? Step through it the assembly listing generated at compile time.

To be thorough: Is there anything else connected to RC1 or RD6? Are RC1/RD6 used/referred to at all anywhere else in the program?

Do you have any long delays that may be called in between calls of this function?


Is it turning off on processor reset due to watchdog timer expiring?

Since there is no debouncing code, try rewriting it to a more compact form that does the same thing in one line, with no conditionals:

Rich (BB code):
#include<p18f4550.h>
void main ()
{
TRISC = 0xFF;//configure all pins of port C to be input
TRISD = 0b10111111; //configure pin6 of portD to be output

   PORTDbits.RD6 = PORTCbits.RC1 ;
}
--ETA: From Microchip C18 Manual, Page 6
0bnnnn A binary number where n is a binary digit
0b00100, 0b10
0xnnnn A hexadecimal number where n is a hexadecimal digit 0xFFFF, 0x007A
0x10111111 = 0b10000000100010001000100010001 = 281479271743489

It might be using extended math to calculate that, then write it to the port, which could take a while for a 50 bit number (promoted to 64 bit by compiler)
 
Last edited:

Thread Starter

tuanvoi

Joined Oct 31, 2008
56
How often is this bit of code run, after optimization? Step through it the assembly listing generated at compile time.

To be thorough: Is there anything else connected to RC1 or RD6? Are RC1/RD6 used/referred to at all anywhere else in the program?

Do you have any long delays that may be called in between calls of this function?


Is it turning off on processor reset due to watchdog timer expiring?

Since there is no debouncing code, try rewriting it to a more compact form that does the same thing in one line, with no conditionals:

Rich (BB code):
#include<p18f4550.h>
void main ()
{
TRISC = 0xFF;//configure all pins of port C to be input
TRISD = 0b10111111; //configure pin6 of portD to be output
 
   PORTDbits.RD6 = PORTCbits.RC1 ;
}
--ETA: From Microchip C18 Manual, Page 6
The code that I posted is a single code, nothing else involved in these pins. I don't know what's wrong!
Somebody said that it would be better if I use a switch and pullup or pulldown resister to do such inputs like I've mentioned. Please correct me, thanks
 

AlexR

Joined Jan 16, 2008
732
You need an infinite loop somewhere in the main() function. At present the program runs through main() once, gets to the end and then hangs. Probably the watchdog timer reset your chip every 5 or so seconds and the process repeats.

What you should be doing is something like this;
Rich (BB code):
#include<p18f4550.h>
void main ()
{
 TRISC = 0xFF;//configure all pins of port C to be input
 TRISD = 0xBF; //configure pin6 of portD to be output
 while(1)
  {
    if(PORTCbits.RC1 == 1)
    PORTDbits.RD6 = 1;
    else
   PORTDbits.RD6 = 0;
  }
}
 
Last edited:

AlexR

Joined Jan 16, 2008
732
Rich (BB code):
 TRISD = 0x10111111; //configure pin6 of portD to be output
This line is also wrong (as others have pointed out). The 0x prefix indicates a hex number in all versions of C, so the line should read;
Rich (BB code):
 TRISD = 0xBF; //configure pin6 of portD to be output
or if you prefer to use binary then;
Rich (BB code):
 TRISD = 0b10111111; //configure pin6 of portD to be output
Again the 0b prefix indicates a binary digit an every version of C. This along with the 0x prefix is part of the ISO "C" standard is is not negotiable.
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
You need an infinite loop somewhere in the main() function. At present the program runs through main() once, gets to the end and then hangs. Probably the watchdog timer reset your chip every 5 or so seconds and the process repeats.

What you should be doing is something like this;
Rich (BB code):
#include<p18f4550.h>
void main ()
{
 TRISC = 0xFF;//configure all pins of port C to be input
 TRISD = 0xBF; //configure pin6 of portD to be output
 while(1)
  {
    if(PORTCbits.RC1 == 1)
    PORTDbits.RD6 = 1;
    else
   PORTDbits.RD6 = 0;
  }
}
Since the port does eventually turn off, I think this routine is wrapped inside a larger loop, so I didn't ask on that. Good point though, I 'assumed'.

Does the single line version of the test work in your version of C? It follows standard C protocol (same data types). I am not sure what the optimizers turn that if/else into, but I have a pretty good idea of what the simple assignment would turn out like.

Let me know if you are aware of a trap with the assignment method. I've used it OK in BoostC in the past.

To Original Poster:

Change the 0x1011111 to 0b1011... rebuild, and see if it works, humor me.
Otherwise:
Post the assembly listing created by those lines (set TRISC, TRISD, read, set output)

We don't have Anything to work with having only the information given so far.
 
Last edited:

AlexR

Joined Jan 16, 2008
732
Since the port does eventually turn off, I think this routine is wrapped inside a larger loop, so I didn't ask on that. Good point though, I 'assumed'.
Normally I would agree that without seeing the full program it's difficult to know what is going on but in this case the function in question is main() and there is not a infinite loop in sight either in the main function itself or though possible calls to other functions.
 

Thread Starter

tuanvoi

Joined Oct 31, 2008
56
Hello,
The code below is fixed, but still I have some problems:

#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
 

AlexR

Joined Jan 16, 2008
732
You are dealing with digital ports and they interpret input as either a zero or a one, there can be nothing else in between. The output port cannot go into a tristate condition.
If you want an open circuit to be a zero use a pull down resistor, if you want it to be a one, use a pull up resistor.
 
Top