473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how to make a header file

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

✓ answered by weaknessforcats

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.

6 2112
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.