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.
Previous article:High-Voltage Multi-Cell Battery Stack Monitor Enables Accurate Voltage Measurement
Next article:Eight-channel ADC0808 data acquisition
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- CS5090 5V USB input charges 2 lithium batteries, with path management application, which can realize the function of "charging and discharging at the same time".
- DSP interrupt system and its application
- FPGA Simplified Design Method Classic Case 3
- MSP430G2553 interrupt processing function
- Electrostatic Shielding
- In the second week after returning to work, I received: resignation form and notice of termination of labor contract!
- "Intel SoC FPGA Learning Experience" + Three mmap Examples in Eclipse under Linux
- Learn about MSP430FR5969 remote upgrade
- Urgent Purchase
- High salary + recruiting medical device embedded (single-chip microcomputer) software engineers