Maker Pro
Maker Pro

c programming for LED

Status
Not open for further replies.

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
For a start PBo and PB0 are different.

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

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
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
 

Sid723

Jan 28, 2010
57
Joined
Jan 28, 2010
Messages
57
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

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.
 

QuantumCheese

Apr 27, 2012
74
Joined
Apr 27, 2012
Messages
74
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 :)
 

Sid723

Jan 28, 2010
57
Joined
Jan 28, 2010
Messages
57
#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:

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
At least put a 'goto' in there at the end to send it back to the beginning.:D

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...
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
goto is frowned upon in C

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
 

QuantumCheese

Apr 27, 2012
74
Joined
Apr 27, 2012
Messages
74
At least put a 'goto' in there at the end to send it back to the beginning.:D

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
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
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

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 ;)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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.
 

Sid723

Jan 28, 2010
57
Joined
Jan 28, 2010
Messages
57
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:
 
Status
Not open for further replies.
Top