Beginner Confusion about Headers!!!

Thread Starter

hairyharry

Joined Jul 21, 2009
4
Hi all, if anybody could help me with figuring out headers i would appreciate it. i get what they're all about and stuff, and i've googled a lot about them, so that's not the issue, but i didn't know how to ask what's confusing me in google so i'll need some actual human help...

i've been reading and watching a bunch of tutorials online about different microcontrollers and how to program them using C/C++ but they're all using different programming environments and different microcontrollers - but of course the basic idea is the same. as far as headers are concerned, they're all initializing different headers and using slightly different syntax while programming. so my question is how do you know what headers to call? is it part of the microcontroller documentation or the software documentation or what??? i would like to start programming on my own without without looking at a tutorial but i can't get past this. and somewhat related...how do you know the proper syntax to use?? for example i saw one tutorial that set bits by saying
Rich (BB code):
TRISIO.B2 = 1; //Sets pin B2 to output
and another
Rich (BB code):
DDRB |= 1 << PINB2; //Sets pin B2 to output
Now, I understand what both of these codes are doing but I don't understand when to use which. Are they interchangeable? Or is it the compiler or the chip (they were using different chips). i'm just not sure.

Thanks for any clarification...
 

MrChips

Joined Oct 2, 2009
30,806
What you are seeing are different programming styles. They accomplish the same thing.

Rich (BB code):
DDRB |= 1 << PINB2; //Sets pin B2 to output
In the header file, DDRB would be defined as a register address.
PINB2 would be defined as:

#define PINB2 2

The code creates a mask 00000100 and a bitwise OR operation is performed on register DDRB,

Rich (BB code):
TRISIO.B2 = 1; //Sets pin B2 to input
This instruction takes advantage of structures and unions to create the address pointing to a specific bit in order to set the bit to 1. The compiler creates the required ASM code to set or clear that bit.

You could have accomplished the same thing with:
Rich (BB code):
TRISB |= 1 << PINB2; //Sets pin B2 to input
Note that the reversal from output to input is simply an idiosyncrasy of PIC mcus.
 
Last edited:

t06afre

Joined May 11, 2009
5,934
Micros are very different from brand to brand. And the C-compilers may be more or less true to ANSI-C. But they all have their way of solving how to deal with the micros peculiarities. I recommend to first select which brand of controller you will use. Then select your compiler. Before you do that. It kind of hard to give good answers to your questions :)
 

Thread Starter

hairyharry

Joined Jul 21, 2009
4
Micros are very different from brand to brand. And the C-compilers may be more or less true to ANSI-C. But they all have their way of solving how to deal with the micros peculiarities. I recommend to first select which brand of controller you will use. Then select your compiler. Before you do that. It kind of hard to give good answers to your questions :)
ahhh ok thanks! so it's compiler specific based on the micro?? so if i chose a brand and compiler then the code should be pretty much the same regardless of the specific chip i use...is that the idea?

so for the headers when one says
Rich (BB code):
#include <regx52.h>
while another says
Rich (BB code):
#include <io.h>
it's a matter of chips and compilers so i need to pick one and stick with it? but still...is there any official documentation on where i can find what headers to use...suppose tutorials didn't exist and i bought a micro and wanted to program it...where would i go to find which headers to include?
 

t06afre

Joined May 11, 2009
5,934
but what about the headers? how do i know which ones to use? is it in the mc datasheet or my environment help files? one tutorial says
The compiler user manual will tell you which header that need to be included for the different functions. That is the best general asnwer I can give you. Other headres may again be specific to which micro you use.
 

codehead

Joined Nov 28, 2011
57
for example i saw one tutorial that set bits by saying
Rich (BB code):
TRISIO.B2 = 1; //Sets pin B2 to output
and another
Rich (BB code):
DDRB |= 1 << PINB2; //Sets pin B2 to output
Now, I understand what both of these codes are doing but I don't understand when to use which. Are they interchangeable? Or is it the compiler or the chip (they were using different chips). i'm just not sure.
I'm not sure if you really understand what they are doing...if you did, that would answer your own question. And the lack of additional details (we can't see the different headers your talking about, don't know what the different chips are you're talking about...) makes it difficult to answer except to comment about generalities.

Basically, the headers are there to abstract addresses and structures, in this context (assuming it doesn't come with a library of code, and includes function declaration too).

So, the two code examples you gave could indeed be 100% identical. I can't say for sure, because you didn't tell us what TRISO, DDRB, etc. were defined as.

But I can infer that TRISO is some address (location), and the B2 is part of a bitfield struct definition; DDRB is an address, and PINB2 is a constant—I suspect it's the value 4 (100 binary, or 2^2), to identify bit 2. I suspect that the B2 bitfield identifies bit 2 also.

So, if DDRB and TRISO point to the same location and and are defined to the be same same type (size), then the two lines of code are identical and interchangeable. Either one reads the value at the location, ORs the value with the bit mask (binary 00000100, apparently), and writes it back out. The result is that bit 2 gets set (regardless of what state it was in before), and none of the other bits are touched.

In the end, you need to be able to figure out what the header is, and what you've got, in order to make the decision to use it. For instance, it you need to replace a fuel pump on a Honda, you know enough to not grab the service manual for a Toyota. But you should also know, when you go to Pep Boys to look for a manual, that you grab a Honda manual that's for the proper model and year. And if two different companies make the appropriate manual, and one tells you to remove a "threaded metal fastener with a 9mm socket", and the other to remove the "bolt with a 9mm box wrench", and the diagrams point to the same thing, that the two instructions are interchangeable. It's the same thing with the headers and code.

Make sense?
 

Thread Starter

hairyharry

Joined Jul 21, 2009
4
I'm not sure if you really understand what they are doing...
very true! :confused:
And the lack of additional details (we can't see the different headers your talking about, don't know what the different chips are you're talking about...) makes it difficult to answer except to comment about generalities.
thank you, and sorry for the generalities. I can explain further about the codes. the first one
Rich (BB code):
TRISIO.B2 = 1; //Sets pin B2 to output
a guy on youtube was programming using MikroC and didn't have any #include in his code. He was programming a 12F683 PIC Micro controller.
The other bit of code
Rich (BB code):
DDRB |= 1 << PINB2; //Sets pin B2 to output
is from another guy on youtube who is using winavr with programmers notepad to program an AVR Atmega32

so i watch both of these guys and i wonder...why does one not have to use any #include while the other uses #include <io.h>. how does the first guy know he doesn't need to include anything, and where did the second guy read that he needs to include io.h? are these better questions that clarify my confusion?

(also to set one thing straight, so is it fair to say that different compilers require different header files because in those header files the registers and pins are defined. so the difference in syntax while setting these pins B2 to output is due to the way the compilers were set up and different variables within the header files???)
 

MrChips

Joined Oct 2, 2009
30,806
In both examples you don't need header files.
You can declare your own %define statements and structures in your own code.
However, it means that you will have to consult the mcu manufacturer's datasheet to determine the correct I/O address to use.

The compiler vendor or the mcu manufacturer has saved you trouble by providing the addresses in a header file.
 

ErnieM

Joined Apr 24, 2011
8,377
a guy on youtube was programming using MikroC and didn't have any #include in his code. He was programming a 12F683 PIC Micro controller.
So no wonder you are confused. MikroC does indeed use a header file for each specific PIC you are using... but MikroC selects and includes the file for you based on the PIC chosen in your project.

For the 12F683 PIC device the file would be "...\Program Files\Mikroelektronika\mikroC PRO for PIC\Defs\P12F683.c"

(So not only does it use a secret file it compounds the felony by using not a header but another C file.)

Inside that file you will find the definitions for TRISIO and even B2 and lots of other goodies. It's a good thing to know this file as it holds many symbols you will need in your code.

Rich (BB code):
// Individual bit access constants
const char B0 = 0;
const char B1 = 1;
const char B2 = 2;
const char B3 = 3;
const char B4 = 4;
const char B5 = 5;
const char B6 = 6;
const char B7 = 7;
...

sfr unsigned short volatile TRISIO           absolute 0x0085;
 

Thread Starter

hairyharry

Joined Jul 21, 2009
4
THANK YOU ALL! This has been amazingly helpful. Looking at WinAVR I have located on my hard drive the documentation with explanations for headers included with the program. From there I can look inside the specific header files and see all the pins and data registers laid out with variables...

so i've learned that the variables are named within the header files that the compiler designer (or whoever...) set. These variable are based in name on the chip that i'm using. I think, though not positive, i could change the variables, such as DDRB to anything i want in the header file...(i won't...just saying for theoretical understanding). and if i want information on which header files to use i can look at my compiler documentation and cross reference that with my chip datasheet to see what bits are being set in the datasheet and find what header files those bits are in.

That makes sense to me and I think and hope it's correct. Thanks to everyone for the help!
 

MrChips

Joined Oct 2, 2009
30,806
Yes, you can rename or add new names to your liking.

It is a good exercise to locate and examine the header file for your specific device so that you know the correct symbols to use in your code.
 

ErnieM

Joined Apr 24, 2011
8,377
...I think, though not positive, i could change the variables, such as DDRB to anything i want in the header file...
Yes you could. Microchip did this not too long ago to make the code names a better match to the names in their data sheet.

Damn did their user forums go wild.
 

WBahn

Joined Mar 31, 2012
30,058
I think, though not positive, i could change the variables, such as DDRB to anything i want in the header file...(i won't...just saying for theoretical understanding).
Yes, you could but don't.

I don't recommend every changing someone else's header files unless you have truly made them your own. To see what I mean by this, consider the following: Your favorite microcontroller supplies a header file as part of the code you download. You make modications to that header file to rename variables or functions or whatever to your liking. A year from now you download that code base again, perhaps to get access to new functions that have been added or perhaps to get bug fixes. Well, the header file that you modified is part of the new download and it may well have undergone some changes. So now neither header file will work because one of them is incompatible with the new source code downloaded and the other is incompatible with the code that you've written that uses the modified header file. Figuring out how to craft a new modified header file can become quite a challenge.

Instead, write your own header file that makes whatever translations you like between how they did it and how you want to do it. Then include both header files. If you download an updated set of files later, you stand a very good change of it working with your existing code off the bat, you can keep writing code the way you are used to, and you can keep adding to and evolving your custom header file (or occasionally removing stuff if you discover that they way you did it isn't necessary because you've learned some feature of the language or of the supplied libraries that makes it better/easier).

I have a header file that I have used in most of my projects for the last 15 years or so named dirtyd.h (short for "dirty deeds done dirt cheap") that has a lot of macros in it ranging from simple #defines for TRUE and FALSE and some simple typedefs (some of which I have since disabled for new code because I found better stuff in the new standard libraries) to some fairly sophisticated macros and functions (which are in dirtyd.c).
 
Top