Connect with us

c programming for LED

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Apr 10, 2012.

Scroll to continue with content
  1. vead

    vead

    473
    14
    Nov 27, 2011
    i am trying to write c code for LED to turn on and off i am trying how to write program please check me
    #include<pic/io.h>
    main()
    {
    DDRA=0xFF; //port b as output
    PBo=0; //led off
    PB0=1; //led on
    }



    please tell me what i did wrong
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,397
    2,777
    Jan 21, 2010
    For a start PBo and PB0 are different.

    Secondly, this is going to happen so quick that you won't see a thing.
     
  3. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    AND: if you set Port a for output using DDRA=0xFF; then you still can't write to port B.
    So with DDRA=... goes PORTA=...
    Or DDRB=... then PortB=...

    Also you normally cannot set single port bits with an instruction like PB0=...
    See your other thread (https://www.electronicspoint.com/write-c-code-motor-t246544.html) and my post there for more details on how to set and reset single port bits.

    Harald
     
  4. Sid723

    Sid723

    57
    1
    Jan 28, 2010
    This will happen so fast, the LED will look dim.
    You should set up a timer to use as a delay, then when
    the timer gets to zero, it will call a interrupt routine.

    In the interrupt routine is where you can alternately turn
    the LED on then off when the interrupt routin is called again.

    Meanwhile, in the main routine, just have it get caught
    in a loop waiting for the interrupt to go off.

    Good luck.
     
  5. QuantumCheese

    QuantumCheese

    73
    0
    Apr 27, 2012
    1st off, as others have pointed out, it looks like you have some confusion over 0,O and o. thats quite important!

    2nd. even if that program did execute, it would only do it once and so fast you wouldn't see anything.

    You can turn single bits on & off in C (or at least in CCS C you can). something like.....
    /
    output_bit(pin_c7,1);
    for (delay=0;delay<10000;delay++);
    output_bit(pin_c7,0);
    for (delay=0;delay<10000;delay++);
    /
    in that snippit i'm turning c7 on & off but it could easily be changed to port b.
    note the delay - that way you can see something happening.

    but by far the most important bit you missed was
    /
    while(1)
    {
    // your code here
    }
    /
    without that, it's only going to do it once!

    Happy coding :)
     
  6. Sid723

    Sid723

    57
    1
    Jan 28, 2010
    #include<pic/io.h>
    main()
    {
    DDRA=0xFF; //port b as output
    PBo=0; //led off
    PB0=1; //led on
    }


    At least put a 'goto' in there at the end to send it back to the beginning.:D
     
    Last edited: Apr 30, 2012
  7. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    goto is frowned upon in C (actually by a lot of people even in BASIC), in C you will usually see the while(1) command as in the above post by QuantumCheese...

    I'm still old school (since I learned to code so long ago as a child in BASIC) that I still use goto quite often, so personally it doesn't bother me, but you will come across those who jump up and down when you dare suggest it's use...
     
  8. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    Yes, I know. That is, however, not the "GOTO"s fault. It is the programmers who recklessly use it. If you look down the chain, the assembler code or machine code a compiler generates from statements like WHILE, FOR, IF etc. breaks down to a jump instruction (mostly conditional) that is the low level equivalent of a GOTO.

    If you program in a structured way, a GOTO is as good as any other control instruction. The problem is that GOTOs make it easy to leave the well paved roads of structured programming and lead you into a jungle of less than well beaten tracks.
    Havin said this: Using WHILE, FOR etc. for controlling the flow of a program just makes it easier to keep discipline while hacking down your statemens.

    Harald
     
  9. QuantumCheese

    QuantumCheese

    73
    0
    Apr 27, 2012
    I'm sure that was a tongue-in-cheek comment!

    Goto is as CocaCola put it, frowned upon in C. It defies the structure of the language. putting something in a While(1) loop is apparently the way it's done in c (i'm not a professional coder).

    If you want to have a go at no-holes-bared coding, why not start off using assembler?
    As far as i can tell, you can program that free-style!
    That was how i got into PIC programming, i found it made you think about the actual device/pins/clock etc before moving layers of compiler away from the actual hardware
    or it could be just me!

    Wiki has a good explanation of goto's here
    http://en.wikipedia.org/wiki/Goto
     
  10. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    I agree, but as it stands if you visit C programming forums the general rule is don't use it and they will almost always scold you for using it... If you visit BASIC forums they will generally accept it's use and encourage it... As I said I myself use it quite a bit, it works but it can produce some real debugging headaches...

    I learned to code in BASIC when I was a pre-teen on an Atari 800 computer and later the Apple II computers, the pre-teen self taught sloppy coding haunts me to this day even with proper later life schooling ;)
     
  11. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,397
    2,777
    Jan 21, 2010
    Aside from languages that require jumps (assembler is probably the only one left), I would suggest that the use of GOTOs or their equivalent should never form part of your design.

    It happens that some implementation issues such as stack size or the lack of availability of a modern language on some hardware can mean that you require its use.

    However there are ways to use it "correctly", and ways that will make people scream.

    Please try not to make people scream.
     
  12. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    Thus the reason 99% of my code never is seen by anyone but myself ;) I know better but those bad habits are hard to let go of...
     
  13. QuantumCheese

    QuantumCheese

    73
    0
    Apr 27, 2012
    You'd better not look at any of my code then!

    ANSI, how DO you spell that anyway :D
     
  14. Sid723

    Sid723

    57
    1
    Jan 28, 2010
    Yes, tongue in cheek intended. You all did see the green smiley face... didn't you? :D

    Just send the flow of the program back to the beginning to repeat the turn-on then turn-off of the LED. I didn't mean to start a c programming argument.

    So, thanks for the little lecture on proper C programming etiquette guys.:cool:
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-