PIC18F Blinking LED

Discussion in 'General Electronics Chat' started by Dritech, Oct 19, 2013.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I am trying to program a PIC18f4550 in C for the first time. The first coding that is am trying is from the web site below:

    http://www.rakeshmondal.info/pic18f4550-tutorial-blinking-led#

    Unfortunately I'm not having any luck and I cannot find why this is not working :( I did trouble shoot the circuit and everything is OK.
    Can someone please guide me?

    Attached please find the coding and the breadboard circuit. (note: in the photos the LEDs are missing)

    Code ( (Unknown Language)):
    1.  
    2. #include<p18f4550.h>                                //      [[[[Include the PIC18F550 Headers]]]]]]
    3.  
    4. /* COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS*/
    5.  
    6.  
    7. #pragma config FCMEN = OFF          // OR this way
    8. #pragma config IESO = OFF
    9. #pragma config PWRT = OFF
    10. #pragma config BOR = OFF                                    
    11. #pragma config BORV = 3
    12. #pragma config VREGEN = OFF
    13. #pragma config WDT = OFF
    14. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
    15. #pragma config CPB = OFF
    16. #pragma config CPD = OFF
    17. #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    18.  
    19. void delayzz(void)
    20. {    int i, j;
    21.     for(i=0;i<5000;i++)
    22.      {
    23.     for(j=0;j<2;j++)
    24.  
    25.              {       /* Well its Just a Timer */   }}}
    26.  
    27. void main(void)
    28. {
    29.     TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.
    30.   while(1)    {
    31.                     LATBbits.LATB0 = 1;   // RB-0 to High  
    32.         LATBbits.LATB1 = 1;   // RB-1 to High
    33.  
    34.          delayzz();
    35.         LATBbits.LATB0 = 0;    // RB-0 to LOW
    36.         LATBbits.LATB1 = 0;    // RB-1 to LOW
    37.          }
    38. }
    39. /* THE END */
    40.  
     
    Last edited by a moderator: Oct 19, 2013
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Very neat wiring job.

    I think you left out one wire: do you have the top half of the circuit wired to VDD?

    After that... seems you have a PICkit to program. That is the perfect tool to run a debug mode and single step through your code to see what is actually happening.
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    OK finally it's working (after 6 hours of troubleshooting).

    I included a delay after:

    LATBbits.LATB0 = 0; // RB-0 to LOW
    LATBbits.LATB1 = 0; // RB-1 to LOW

    and the breadboard need to be changed. Every time I touch the steel base, the whole thing is freaking :confused:
     
    Last edited: Oct 19, 2013
  4. BobTPH

    Active Member

    Jun 5, 2013
    782
    114
    Yep, you need a delay while the LED is off or you will never see it as off since it will only be a few microseconds.

    Good job. Next time it will be easier.

    Bob
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the reply.
    Another quick question, why is the program giving an error when a place the delay function after the Main function ??
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    Because delayzz is called before its defined, the compiler does not know how to generate code to call the function . The compiler needs to know what parameters the function needs and what it returns if anything. You can fix this by:
    1) placing the function before its called as in your original code -OR-
    2) 'prototyping' the function by placing
    Code ( (Unknown Language)):
    1. void delayzz(void);
    before its called by main. When the compiler sees the semicolon without any function body it won't generate any code for delayzz but it WILL remember how delayzz needs to be called. Later in the code or in another file, code delayzz with the EXACT same prototype and add the code.

    When the delay is in another source file (as will happen when your programs get big) or is in a library, you'll place the prototype in a .h file and #include the .h in any .c source that calls the function.
     
    Dritech likes this.
  7. Rohit Sharma

    New Member

    Oct 19, 2013
    3
    0
    The two images attached has a external crystal oscillator. The code above for blinking led requires any external oscillator ? What if we want to use internal ?
     
  8. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    The 16F877 does not have an internal oscillator. Pick another processor and start your own thread.
     
  9. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the reply JohnInTX.

    I am using the blinking LED code only to test if the circuit and IDE installation was done correctly.

    Another quick question :)
    From where can I download a PIC18F4550 template in C? (if it is with comments and explained the better, so I can understand the process)

    Thanks in advance.
     
  10. Rohit Sharma

    New Member

    Oct 19, 2013
    3
    0
    JohnInTX When did i mentioned that i am asking about 16F877 ? :)
    I am talking about PIC18F4550..... he code posted by Dritech and the two images he attached has a crystal oscillator with the pic18f4550, it means external oscillator is used. And i supposed pic18f4550 does has an internal oscillator..
    but on website link no oscillator at all, so i got confused. http://www.rakeshmondal.info/pic18f4550-tutorial-blinking-led
     
  11. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    Oopsie! Got my threads mixed up. Sorry.

    You select the oscillator (internal/external etc.) by programming FOSC3:FOSC0 in CONFIG1H. In C its done with some sort of compiler directive such as #pragma as in the OP's code.

    For the OP, it does not look like you are specifying an oscillator type. HS or XT would be appropriate for an external crystal. It looks like the default for the FOSC bits is 0101 which would expect an EXTERNAL oscillator. That may be why its flaky.

    Also, a lot of breadboard strips have power busses that are separated in the middle. In your picture, the break would be located between where red Vdd wire and the blue Vss/capacitor ground is plugged in. If so, you would only have Vss on one of the PIC pins and the cap grounds would have to find their way through the chip. That plus not having the CONFIG bits set correctly would account for it being flaky.
     
    Last edited: Oct 21, 2013
  12. Rohit Sharma

    New Member

    Oct 19, 2013
    3
    0
    thanks for the reply .
     
Loading...