Simple LED program

Discussion in 'The Projects Forum' started by josephgebran, Jun 10, 2011.

  1. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    Hi, there is this small easy programme :
    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. #include <stdlib.h>
    3. #define _XTAL_FREQ 8000000
    4.  
    5.  
    6. __CONFIG(0x3CC4);    // config word 1
    7. __CONFIG(0x3eff);    // config word 2 (in sequence)
    8.  
    9. void init()
    10. {
    11. OSCCON=0b01110000;   //frequency = 8M
    12. ANSELH=0;            //Disable PortB Analog Input
    13. C1ON=0;              //turn off
    14. C2ON=0;              //comparators
    15. TRISB=0b00000001;    //RA0,
    16. PORTC=0b00000000;
    17. TRISC=0b00000000;    //RD0,
    18. PORTD=0b00000000;
    19. TRISD=0b00000000;    //RD0,
    20. }
    21.  
    22. void main()
    23. {
    24.     init();
    25. while(1)
    26. { if (RB0==1)
    27. {RC1=1;
    28. __delay_ms(500);
    29. RD1=1;
    30. __delay_ms(1000);
    31. RC1=0;
    32. RD1=0;
    33. __delay_ms(500);
    34. }}
    35.  
    36. }             // End main program
    On ISIS simulation, it is working normally, but in real, when i turn the switch on RB0 on, only the LED on RC1 turns on, and never turns off again, and LED on RD1 does nothing, i have checked the connections several times...what's wrong??
     
  2. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    May be you have any initialization problem, try this and tell us if it works or not

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include <stdlib.h>
    4. #define _XTAL_FREQ 8000000
    5.  
    6.  
    7. __CONFIG(0x3CC4);    // config word 1
    8. __CONFIG(0x3eff);    // config word 2 (in sequence)
    9.  
    10. void init()
    11. {
    12. OSCCON=0b01110000;   //frequency = 8M
    13. ANSELH=0;            //Disable PortB Analog Input
    14. C1ON=0;              //turn off
    15. C2ON=0;              //comparators
    16. TRISB=0b00000001;    //RA0,
    17. PORTC=0b00000000;
    18. TRISC=0b00000000;    //RD0,
    19. PORTD=0b00000000;
    20. TRISD=0b00000000;    //RD0,
    21. }
    22.  
    23. void main()
    24. {
    25. int i = 0;
    26. init();
    27. while(1)
    28. {  
    29.     PORTD = ~PORTD;
    30.     PORTC = ~PORTC;
    31.     for(i=0;i<10;i++)
    32.     {
    33.     __delay_ms(100);
    34.     }
    35. }
    36. }
    37.  
    Connect the LEDs to PORTC and PORTD and see if they toggles or not.Also post,which mcu you are using and the compiler,I guess Hi-tech and the circuit diagram.

    Good Luck
     
  3. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    Thanks for replying, i tried ur code although i didn't understand what PORTC =~ PORTC means, i tried it on ISIS it worked, but in real, both LEDS turn ON at the same time but never turn off again ! it's ugly :mad: what do u think might be the problem????
     
  4. josephgebran

    Thread Starter New Member

    Jun 5, 2011
    10
    0
    ah i got it PortC =~ PortC makes all the pins of PORTC change their state, and another thing, it's PIC16F887 and PICKIT 2 and HITEC
     
  5. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Post the schematic, it will help us to help you..... many times people connect their mcu improperly in respect to their configuration settings.
     
Loading...