473,499 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to make a header file

4 New Member
hello everyone ,

here is my code

Expand|Select|Wrap|Line Numbers
  1. #include<msp430.h>
  2.  
  3. #include<stdio.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "string.h"
  10.  
  11.  
  12.  
  13. char dest[20];                        //Global Variable
  14.  
  15. void uart0_send_data()
  16. {     
  17.         char c[ ]="My name is girish";
  18.  
  19.  
  20.         int j,l;
  21.  
  22.         printf("Transmitted string:\t");
  23.         printf("%s",c);
  24.  
  25.         for(j=0;c[j]!='\0';j++)
  26.         {
  27.  
  28.         TXBUF0 = c[j];
  29.         for(l=0;l<5000;l++)
  30.         _NOP();
  31.         }
  32.  
  33.         printf("\nthe received string is:\t");
  34.         printf("%s",dest);
  35.         printf("\nexit send data\n");
  36.  
  37. }
  38.  
  39. void uart0_init()
  40. {    
  41.  
  42.  
  43.     WDTCTL=WDTPW+WDTHOLD;            //Stop Watch Dog Timer
  44.  
  45.         P3SEL |= 0x30;                //Select UART0 Transmit and Receive
  46.  
  47.         P3DIR &= ~0x20;                          //Make URXD0 as input
  48.  
  49.         P3DIR |= 0x10;                           //make UTCD0 as output
  50.  
  51.         P3OUT |= 0x10;                           
  52.         ME1 |= UTXE0 + URXE0;            //module enable 
  53.  
  54.         U0CTL = CHAR;                //Select 8 bit transceive mode
  55.  
  56.         U0TCTL = SSEL0;                //ACLK = UCLK
  57.         U0RCTL |= 0x00;
  58.         U0BR0=0x1b;                //Baud rate register
  59.         U0BR1=0x00;
  60.         U0MCTL=0x24;                //Modulation control register
  61.         U0CTL &= ~SWRST;            
  62.  
  63.     IE1 = URXIE0+UTXIE0;            //Enable receive and transmit interrupt
  64.  
  65.  
  66.     _BIS_SR(GIE);
  67.  
  68.  
  69.  
  70. }
  71.  
  72. ISR(UART0RX,uart0_rx_interrupt)
  73. {
  74.  
  75.     static int k=0;
  76.     int i;
  77.     for(i=0;i<5000;i++)
  78.     _NOP();
  79.  
  80.     printf("%c",RXBUF0);        
  81.     dest[k-1]=RXBUF0;
  82.     k++;
  83.  
  84.     IFG1 &= ~URXIFG0;
  85.     return;
  86.  
  87. }
  88.  
  89.  
  90.  
  91. PROCESS(uart_check,"uart-communication");
  92. AUTOSTART_PROCESSES(&uart_check);
  93.  
  94. PROCESS_THREAD(uart_check,ev,data)
  95. {
  96. PROCESS_BEGIN();
  97. uart0_init();
  98.  
  99. uart0_send_data();
  100. PROCESS_END();
  101. }
  102.  
  103.  
This code is used to transmit and receive the data. My only concern as of now is to create a header file for uart0_init() function. Hoping someone could help me out. thank you

regards,
pacifc
Apr 16 '14 #1
6 2118
weaknessforcats
9,208 Recognized Expert Moderator Expert
A header file is just a text file same as any other source code file.

You use header files when the code for your function is not in the file being compiled.

All the header file will contain is the function prototype of your function but no code. The code will be in a separate surce file.

If you are not using separate source files, then you don't need a header file. Just be sure the function code appears before main().
Apr 16 '14 #2
pacific
4 New Member
hi,
to create a init header file , i would need to write the init function in another file, right? what i am unsure of is to how to use or call the function in the present file.
sorry if i am not very clear but i am a beginner in programming

pacific
Apr 16 '14 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
If your function is in another file, then you can call the function from the current file by using a function "prototype".

This is just the first line of the function terminated by a semi-colon.

If this is your function in some other file:

Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int arg, int arg1)
  2. {
  3.     ..some code....
  4. }
Then the function prototype is:

Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int arg, int arg1);
  2.  
Just put this prototype at the beginning of every file that calls the function.

Most programmers will put this prototype in a text file and then #include the text file. Since this #include is at the head of the file, it gets the name "header file".

Header files save the effort of copying your prototypes all over the place. The #include lets you change the contents of the header file in one spot then propagate the change by recompiling your program.

Do not worry about connecting your call with your function code in another file. The linker will do this for you.
Apr 17 '14 #4
pacific
4 New Member
hello sir,

i have given below the initialization header file and below that i have given the main program code.


init.h
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<msp430.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "string.h"
  10.  
  11.  
  12.  
  13. void uart0_init()
  14. {
  15.     WDTCTL=WDTPW+WDTHOLD;            //Stop Watch Dog Timer
  16.  
  17.         P3SEL |= 0x30;                //Select UART0 Transmit and Receive
  18.  
  19.         P3DIR &= ~0x20;                          //Make URXD0 as input
  20.  
  21.         P3DIR |= 0x10;                           //make UTCD0 as output
  22.  
  23.         P3OUT |= 0x10;                           
  24.         ME1 |= UTXE0 + URXE0;            //module enable 
  25.  
  26.         U0CTL = CHAR;                //Select 8 bit transceive mode
  27.  
  28.         U0TCTL = SSEL0;                //ACLK = UCLK
  29.         U0RCTL |= 0x00;
  30.         U0BR0=0x1b;                //Baud rate register
  31.         U0BR1=0x00;
  32.         U0MCTL=0x24;                //Modulation control register
  33.         U0CTL &= ~SWRST;            
  34.  
  35.     IE1 = URXIE0+UTXIE0;            //Enable receive and transmit interrupt
  36.  
  37.  
  38.     _BIS_SR(GIE);
  39.  
  40. }    
  41.  
below is the main file

Expand|Select|Wrap|Line Numbers
  1. #include<msp430.h>
  2.  
  3. #include<stdio.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "init.h"
  10.  
  11.  
  12. #include "string.h"
  13.  
  14.  
  15. void main()
  16. {
  17.  
  18. void uart0_init();
  19.  
  20. }
  21.  
it is supposed to work right? i mean , the contents of the header file will be replace the function prototype "void uart0_init();" in the main program. Your comments please .

regards,
pacific
Apr 17 '14 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Not correct.

1) Do not code the function in the header file. If you do and you use the header file twice you will duplicate the code in the file and your program will never compile again.

Your header file should look like:

init.h
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<msp430.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "string.h"
  10.  
  11.  
  12.  
  13. void uart0_init();  //this is your function prototype
  14.  
2) Put your function prototype outside any function. Usually this is done at the beginning of the file:

Expand|Select|Wrap|Line Numbers
  1. #include<msp430.h>
  2.  
  3. #include<stdio.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "init.h"
  10.  
  11.  
  12. #include "string.h"
  13.  
  14.  
  15. void main()
  16. {
  17.  
  18. }
  19.  
Your function prototype is inside init.h, which is outside any function.

3) You need a third source file to contain the code for your function:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<msp430.h>
  4.  
  5. #include "contiki.h"
  6.  
  7. #include "isr_compat.h"
  8.  
  9. #include "string.h"
  10.  
  11.  
  12.  
  13. void uart0_init()
  14. {
  15.     WDTCTL=WDTPW+WDTHOLD;            //Stop Watch Dog Timer
  16.  
  17.         P3SEL |= 0x30;                //Select UART0 Transmit and Receive
  18.  
  19.         P3DIR &= ~0x20;                          //Make URXD0 as input
  20.  
  21.         P3DIR |= 0x10;                           //make UTCD0 as output
  22.  
  23.         P3OUT |= 0x10;                           
  24.         ME1 |= UTXE0 + URXE0;            //module enable 
  25.  
  26.         U0CTL = CHAR;                //Select 8 bit transceive mode
  27.  
  28.         U0TCTL = SSEL0;                //ACLK = UCLK
  29.         U0RCTL |= 0x00;
  30.         U0BR0=0x1b;                //Baud rate register
  31.         U0BR1=0x00;
  32.         U0MCTL=0x24;                //Modulation control register
  33.         U0CTL &= ~SWRST;            
  34.  
  35.     IE1 = URXIE0+UTXIE0;            //Enable receive and transmit interrupt
  36.  
  37.  
  38.     _BIS_SR(GIE);
  39.  
  40. }    
  41.  
When you make the program you need to compile both source files separately an have the linker combine the object files and connect a call in main() to the code in the other source file.

If you are using a tool like Visual Studio you would have both source files in the same project.
Apr 17 '14 #6
pacific
4 New Member
Thank you sir.
That was really helpful :)
Apr 17 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2887
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
3
2036
by: Sujan Datta | last post by:
What are the possible effects of modifying an existing header file, which includes bunch of defines, function prototypes and some struct definitions. The structure of the header file looks...
4
2588
by: s.subbarayan | last post by:
Dear all, How different is a header file from a library file in C?Under what circumstance u go for library and under what circumstance u go for header?Afaik,both have some declarations which can...
4
2826
by: Adam Clauss | last post by:
OK, lets say I have a C# Windows application. In it is a a series of namespaces, all rooted for a certain namespace A. For ex, the "using" directives would read something like: using A; using...
0
3908
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
4028
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
7
2230
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
3
1508
by: Xohaib | last post by:
hey guys i wnat to make a my own header file like i want that i make a header file exmple.h and i want that when i use exmple.h i should not need to include iostream.h and one other header files...
6
3420
by: bobby | last post by:
hi group, Does the header file size or number in include(s) effect the size of executable file? In other world if i chose a large header file and include it with my source file does it increase...
11
2895
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.