Teach you how to write header files efficiently

Publisher:chang_riLatest update time:2014-12-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I searched for information about header files on the Internet for a long time, but found it difficult to find the right one for me. Friends who study microcontrollers know that many programs often call the same functions. If you rewrite or copy these functions every time you write a program, it is a waste of time. Now I share my learning summary and other people's experience with you. Welcome to reprint and learn.

It is best to use structured programming when writing programs, because such programs do not look so long, and are clear at a glance. You can quickly know what functions the program implements, and troubleshooting is also very simple. Write commonly used function declarations, custom types, and external variable declarations into header files, and write commonly used functions in files with the extension .c that are paired with them. It is best to write a main function in main.c. I learned 51 microcontrollers before, and now I am playing with 430 microcontrollers. Let's take 430 microcontrollers as an example. The principle of other programming software is the same. Create a new project under IAR, including three files: main.c, mydefine.c, and mydefine.h (mydefine.c and mydefine.h are a pair) (Note: it can contain multiple paired header files and C files). Post the program first, and then explain the reason in detail.


Contents of main.c:
#include "mydefine.h"
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
 
  SegInitial(); //Initialize the digital tube control pin
 
  long m = 0;
  while(1)
  {   
   disp(m); //Display the value of m
    delay(10);
   m++;
   if(m == 1000000)
     m = 0;
  }
}
 
 
Contents of mydefine.h
 
#ifndef _MYDEFINE_H
#define _MYDEFINE_H


#include "msp430x14x.h"


typedef unsigned int uint;
typedef unsigned char uchar;


void write_595(uchar dat);
void SegInitial(void);
void disp(long num);
void delays(uint x);
void delay(uint x);


#endif
 
the contents of mydefine.c
 
#include "mydefine.h"
#include "msp430x14x.h"


/*************************************
      Definition of each pin of 74hc595 running light


*********************************/
#define CLK0 P2OUT &= ~BIT4
#define CLK1 P2OUT |= BIT4
# define STB0 P2OUT &= ~BIT2
#define STB1 P2OUT |= BIT2
#define DS0 P2OUT &= ~BIT5
#define DS1 P2OUT |= BIT5
#define LEDOFF P5OUT = 0x00

 


uchar dis_num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //digital tube 0~F common anode encoding
uchar bitnum[]={0x01,0x02,0x04,0x08,0x10,0x20}; //digital tube bit selection
uchar dispbuf[6]; //digital tube display buffer


//The following is the delay function
void delay(uint x)
{
  uint a,b;
  for(a=x;a>0;a--)
    for(b=10000;b>0;b--);
}


//Control the running light, use P2.2(STB),P2.4(CLK),P2.5(DS)
void write_595(uchar dat)
{
  uint n;
  for(n = 0;n<8;n++)
  {
    if((dat&0x80) == 0x80)
      DS1;
    else
      DS0;
    dat <<= 1;
    CLK0;  
    CLK1;
  }
  STB1;
  STB0;
}


/************************************************
            Digital tube display initialization function


*****************************************/
void SegInitial(void)
{
  P5DIR = 0XFF;
  P4DIR = 0XFF;
  P5OUT = 0X00;
  P4OUT = 0X00;
}

 


/********************************************
          Digital tube anti-ghosting delay function


*****************************************/
void delays(uint x)
{
  for (;x>0;x--);
}

 


/************************************************
            Digital tube display function
            position selection P5.0~P5.5
            segment selection P4


*****************************************/
void disp(long num)
{
    uint i;
    dispbuf[0] = num%10;
    dispbuf[1] = num/10%10;
    dispbuf[2] = num/100%10;
    dispbuf[3] = num/1000%10;
    dispbuf[4] = num/10000%10;
    dispbuf[5] = num/100000%10;
   
    for(i=0;i<6;i++)
    {
      P4OUT = dis_num[dispbuf[i]];
      P5OUT = bitnum[i];
      delays(400);
      P5OUT=0X00;
    }
}
 
First, look at main.c, which contains a main function, which tells the reader the main function of the program. mydefine.h contains some function declarations. If an external variable (or function) is used, you need to write extern before the variable (or function) to indicate that it is an external variable (or function). When writing header files, you must pay attention to:
 
#ifndef XXXX
#define XXXX
        .

        .

        .

        .

        .

        .

#endif
 
XXXX is usually capitalized. The name should not be the same as the keyword. Please refer to the above program for the usual writing method. #ifndef XXXX #define XXXX ..... The purpose of #endif is that some header files have been included in other files, but you also include them in this file. If there is no such sentence, the compiler will report an error: duplicate definition! If the definition in mydefine.h is used in mydefine.c, you need to include mydefine. Including mydefine.h means that mydefine.h is replaced with the content of mydefine.h, that is, the complete content of mydefine.c is:
#include "msp430x14x.h"


typedef unsigned int uint;
typedef unsigned char uchar;


void write_595(uchar dat);
void SegInitial(void);
void disp(long num);
void delays(uint x);
void delay(uint x);
/****************************************
      Pin definition of running light 74hc595


*********************************/
#define CLK0 P2OUT &= ~BIT4
#define CLK1 P2OUT |= BIT4
# define STB0 P2OUT &= ~BIT2
#define STB1 P2OUT |= BIT2
#define DS0 P2OUT &= ~BIT5
#define DS1 P2OUT |= BIT5

 


/*************************************
      Definition of each pin of 74hc595 running light


*********************************/
#define LEDOFF P5OUT = 0x00

 


uchar dis_num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar bitnum[]={0x01,0x02,0x04 ,0x08,0x10,0x20};
uchar dispbuf[6];


//The following is the delay function
void delay(uint x)
{
  uint a,b;
  for(a=x;a>0;a--)
    for(b=10000;b>0;b--);
}


//Control the running light, use P2.2(STB),P2.4(CLK),P2.5(DS)
void write_595(uchar dat)
{
  uint n;
  for(n = 0;n<8;n++)
  {
    if((dat&0x80) == 0x80)
      DS1;
    else
      DS0;
    dat <<= 1;
    CLK0;  
    CLK1;
  }
  STB1;
  STB0;
}


/************************************************
            Digital tube display initialization function


*****************************************/
void SegInitial(void)
{
  P5DIR = 0XFF;
  P4DIR = 0XFF;
  P5OUT = 0X00;
  P4OUT = 0X00;
}

 


/********************************************
          Digital tube delay function


*****************************************/
void delays(uint x)
{
  for (;x>0;x--);
}

 


/************************************************
            Digital tube display function
            position selection P5.0~P5.5
            segment selection P4


*****************************************/
void disp(long num)
{
    uint i;
    dispbuf[0] = num%10;
    dispbuf[1] = num/10%10;
    dispbuf[2] = num/100%10;
    dispbuf[3] = num/1000%10;
    dispbuf[4] = num /10000%10;
    dispbuf[5] = num/100000%10;
   
    for(i=0;i<6;i++)
    {
      P4OUT = dis_num[dispbuf[i]];
      P5OUT = bitnum[i];
      delays(400) ;
      P5OUT=0X00;
    } 
}
 
        Next, let's talk about the function of mydefine.c. Some commonly used functions are written in it. Generally, after we write the header file, we don't need to delve into the prototype of the function. We only need to know the function of the function, that is, the function in the header file. Declaration, when compiling and linking multiple C files, it is equivalent to putting the main function in front and other functions in the back. When calling functions, you must declare these functions first, otherwise the compiler does not know what your function prototype is. These header files serve as function declarations. The so-called header files can be understood as the programs (declarations and definitions) that need to be processed before the main function.
 
        The equivalent program of this structured programming is posted below for your convenience. Everyone understands:
 
#include "msp430x14x.h"

typedef unsigned int uint;
typedef unsigned char uchar;


void write_595(uchar dat);
void SegInitial(void);
void disp(long num);
void delays(uint x);
void delay(uint x);


void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
 
  SegInitial();
 
  long m = 0;
  while(1)
  {
   
   disp(m);
    delay(10);
   m++;
   if( m == 1000000)
     m = 0;
  }


}


/*************************************
      Definition of each pin of 74hc595 running light


*********************************/
#define CLK0 P2OUT &= ~BIT4
#define CLK1 P2OUT |= BIT4
# define STB0 P2OUT &= ~BIT2
#define STB1 P2OUT |= BIT2
#define DS0 P2OUT &= ~BIT5
#define DS1 P2OUT |= BIT5


/*************************************
      Definition of each pin of 74hc595 running light


*********************************/
#define LEDOFF P5OUT = 0x00

 


uchar dis_num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar bitnum[]={0x01,0x02,0x04 ,0x08,0x10,0x20};
uchar dispbuf[6];


//The following is the delay function
void delay(uint x)
{
  uint a,b;
  for(a=x;a>0;a--)
    for(b=10000;b>0;b--);
}


//Control the running light, use P2.2(STB),P2.4(CLK),P2.5(DS)
void write_595(uchar dat)
{
  uint n;
  for(n = 0;n<8;n++)
  {
    if((dat&0x80) == 0x80)
      DS1;
    else
      DS0;
    dat <<= 1;
    CLK0;  
    CLK1;
  }
  STB1;
  STB0;
}


/************************************************
            Digital tube display initialization function


*****************************************/
void SegInitial(void)
{
  P5DIR = 0XFF;
  P4DIR = 0XFF;
  P5OUT = 0X00;
  P4OUT = 0X00;
}

 


/********************************************
          Digital tube delay function


*****************************************/
void delays(uint x)
{
  for (;x>0;x--);
}

 


/************************************************
            Digital tube display function
            position selection P5.0~P5.5
            segment selection P4


*****************************************/
void disp(long num)
{
    uint i;
    dispbuf[0] = num%10;
    dispbuf[1] = num/10%10;
    dispbuf[2] = num/100%10;
    dispbuf[3] = num/1000%10;
    dispbuf[4] = num/10000%10;
    dispbuf[5] = num/100000%10;
   
    for(i=0;i<6;i++)
    {
      P4OUT = dis_num[dispbuf[i]];
      P5OUT = bitnum[i];
      delays(400);
      P5OUT=0X00;
    }   
}
 
I hope these summaries can help you. If you have any questions, you can communicate with each other.

Reference address:Teach you how to write header files efficiently

Previous article:High-Voltage Multi-Cell Battery Stack Monitor Enables Accurate Voltage Measurement
Next article:Eight-channel ADC0808 data acquisition

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号