please correct my simple code!!

Discussion in 'Embedded Systems and Microcontrollers' started by tuanvoi, Mar 10, 2009.

  1. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    #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.
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    I think this

    TRISD = 0x10111111;

    should be

    TRISD = 0b10111111;


    Where is the oscillator configuration, interrupts, etc?
     
  3. russ_hensel

    Well-Known Member

    Jan 11, 2009
    818
    47
    What does
    "And when I take 5V out from pinC1" mean?


    And do you have a pull down resistor on PORTCbits.RC1 == 1
     
  4. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    Sorry for any misleading.
    I meant input 0V into pinC1.
    Thanks
     
  5. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    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
     
  6. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Is that all the code?

    Where are the fuses and other setups?
     
  7. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    As mentioned, the code works fine, but it delays to turn off when I input 0V. Thanks
     
  8. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    So it does not work fine!!!
     
  9. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    But it delays 5 to 8 seconds which supposed to be turned off when I input 0V in.
     
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include<p18f4550.h>
    3. void main ()
    4. {
    5. TRISC = 0xFF;//configure all pins of port C to be input
    6. TRISD = 0b10111111; //configure pin6 of portD to be output
    7.  
    8.    PORTDbits.RD6 = PORTCbits.RC1 ;
    9. }
    10.  
    11.  
    12.  
    --ETA: From Microchip C18 Manual, Page 6
    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: Mar 10, 2009
  11. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    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
     
  12. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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;
    Code ( (Unknown Language)):
    1. #include<p18f4550.h>
    2. void main ()
    3. {
    4.  TRISC = 0xFF;//configure all pins of port C to be input
    5.  TRISD = 0xBF; //configure pin6 of portD to be output
    6.  while(1)
    7.   {
    8.     if(PORTCbits.RC1 == 1)
    9.     PORTDbits.RD6 = 1;
    10.     else
    11.    PORTDbits.RD6 = 0;
    12.   }
    13. }
    14.  
     
    Last edited: Mar 11, 2009
  13. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Code ( (Unknown Language)):
    1.  
    2.  TRISD = 0x10111111; //configure pin6 of portD to be output
    3.  
    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;
    Code ( (Unknown Language)):
    1.  
    2.  TRISD = 0xBF; //configure pin6 of portD to be output
    3.  
    or if you prefer to use binary then;
    Code ( (Unknown Language)):
    1.  
    2.  TRISD = 0b10111111; //configure pin6 of portD to be output
    3.  
    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: Mar 11, 2009
  14. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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: Mar 11, 2009
  15. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  16. tuanvoi

    Thread Starter Active Member

    Oct 31, 2008
    56
    0
    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
     
  17. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
Loading...