View previous topic :: View next topic |
Author |
Message |
Guest
|
PIC Wizard: Function Generation, and "Opening Brace&quo |
Posted: Tue Sep 22, 2009 7:23 pm |
|
|
Hello everyone,
That title isn't quite as descriptive as I'd hoped to squeeze in, but here's what I'm asking:
In the PIC Wizard, there's a section in General -> Options Tab.
Function Generation, "Opening brace on the following line" and "opening brace on the same line."
Now, sure I know what that means.
Code: |
if ((thing1+thing2) == seuss)
{
}
|
vs
Code: |
if ((thing1+thing2) == seuss) {
}
|
Question is: Does this add something to the wizard-generated code? Does it change the compiler behavior? Or is it merely a rhetorical question?
Thus far, I haven't found any difference in either the generated files, or in the compiler behavior.
I just want to be sure; I'm modifying some code, and the previous user followed the same-line method. I prefer the next-line format. I just want to be sure it's not going to be causing any problems.
(Coding is being done for a PIC18F4520 chip.) |
|
|
Jeff7
Joined: 22 Sep 2009 Posts: 13
|
|
Posted: Tue Sep 22, 2009 7:24 pm |
|
|
Huh, I didn't notice that I wasn't automatically logged in after activating the account. Oops.
(OP above is mine. ) |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Sep 22, 2009 8:30 pm |
|
|
Typically no.
if (expr) {
}
and
if (expr)
{
}
are functionally the same.
The braces on separate lines I believe is a more proper etiquette. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 22, 2009 8:49 pm |
|
|
Quote: |
I just want to be sure; I'm modifying some code, and the previous user
followed the same-line method. I prefer the next-line format. I just want
to be sure it's not going to be causing any problems.
|
See this Wikipedia article on Indent Style (and on placement of braces):
http://en.wikipedia.org/wiki/C_bracing_style |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Sep 22, 2009 9:51 pm |
|
|
I tend to use the 1TBS variant.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Jeff7
Joined: 22 Sep 2009 Posts: 13
|
|
Posted: Tue Sep 22, 2009 10:12 pm |
|
|
bkamen wrote: | Typically no.
if (expr) {
}
and
if (expr)
{
}
are functionally the same.
The braces on separate lines I believe is a more proper etiquette. |
Ok, I figured as much.
I just wanted to be sure that the CCS compiler wasn't doing something weird if I opted to use one style over another. Most of the code now has the opening brace on the following line; I find it easier to follow that way.
My program restarts itself from time to time; I have a suspicion that it's a stack overflow problem, as changing STVREN to NOSTVREN seems to stop the resets. That of course doesn't seem like a good thing to have disabled, so I'm looking elsewhere to figure out what the heck is causing this.
My watchdog timer is enabled, but it's set to 2304ms. None of my WHILE/FOR loops should take more than 500ms; any that could take longer (waiting for or retrieving data from a software UART, for example) have a timed condition to break them out if they take too long.
I'm kind of easing my way into PIC programming with C, so some of this is a bit new to me. I'm trying to play around with some arrays of anywhere from 70-200 integers, as local variables. Perhaps that isn't the best approach, especially after reading something like this: "Due to their transient nature, they are stored on the stack."
Always more things to learn. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Sep 22, 2009 10:18 pm |
|
|
Programmers I know that work on OS's like Windows or Linux never seem to consider the finer points of memory handling.
You'll find that most microcontroller programmers count bytes and look at the list file to see where C turns into cruddy ASM code (I do).
Microcontrollers are a whole different land. I find it fun.
I'm actually an EE that's is compelled to program firmware for the gizmos I design.
-Ben
p.s. this is a fun one. In your travels, try doing:
Code: |
int8 x;
x = 0;
x ^= 1;
|
and
Code: |
short (or int1) x;
x=0;
if (x) {
x = 0;
} else {
x = 1;
}
|
and see which one executes faster vs which one takes up more instructions. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Jeff7
Joined: 22 Sep 2009 Posts: 13
|
|
Posted: Tue Sep 22, 2009 10:30 pm |
|
|
bkamen - you're closer to the field than me, I think.
I'm a mechanical engineer by degree; this job has me doing some mechanical design, some light electronic design, and a fair amount of programming. Not that I'm complaining, it definitely keeps things interesting.
And yes, I'd imagine an OS, where you've got a few juicy sticks of 1GB+, and several megabytes of cache to work with, things can be very different than if you've got only 1.5kB of RAM to work with.
I haven't done anything in assembly code yet, besides looking at it and thinking (don't take this the wrong way), "People actually understand this stuff. Wow." Then I go sit in a corner rocking back and forth while sucking my thumb, until I think it's safe to go back near the computer. ;)
(Honestly, I'm not sure which of those two described methods would be quicker or more efficient. A 1-bit integer sounds appealing, but then it could be a tricksy sort of question too. Those if/else statements are concerning, something in the back of my mind tells me that such things take quite a few instructions. The other seems like it'd be a simple bit-shift-a-ma-jig operation. I'm kind of leaning toward the int8 section being the leaner code.) |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Sep 22, 2009 10:50 pm |
|
|
Depends...
Check it out.
Code: |
.................... unsigned int8 x;
.................... unsigned short y, z;
....................
....................
.................... x = 0;
0D80A: CLRF x
.................... y = 0;
0D80C: BCF y
.................... z = 0;
0D80E: BCF z
....................
.................... x ^= 1;
0D810: MOVLW 01
0D812: XORWF x,F
....................
.................... if (y) {
0D814: BTFSS x89.0
0D816: BRA D81C
.................... y = 0;
0D818: BCF y
.................... } else {
0D81A: BRA D81E
.................... y = 1;
0D81C: BSF y
.................... }
....................
.................... z ^= 1;
0D81E: MOVLW 00
0D820: BTFSC z
0D822: MOVLW 01
0D824: XORLW 01
0D826: BCF x89.1
0D828: BTFSC WREG.0
0D82A: BSF z
|
PIC's have some very nice instructions for byte-wide XOR... but not bit-wise XOR.
It's easy to build a structure of bits and then want to operate on one of them.. maybe just flipping a bit.
But to do it with bit ^= 1; is 7 instruction and must execute every time vs the 5 instructions with the IF statement that actually only executes some of them depending on the outcome of the testing of bit y.
Subtle stuff.. I convey this to one of the smartest programmers I know who works in Java and he just shakes his head that we hardware guys think of this stuff.. Hehe...
Anyway - welcome to the forum. ;)
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|