First, the program header file includes #include , which is the 14 series, because 149 is commonly used; other models can be modified by yourself. You can also include #include "data.h" and other database header files, or function variable declaration header files, which are all defined by you.
Reference address:Getting Started with MSP430: C Programming
Next is the declaration of functions and variables void Init_Sys(void), which is system initialization. System initialization is an overall concept, which generally includes the initialization of all peripheral modules. You can write the sub-functions of peripheral module initialization into Init_Sys(), or you can write the initialization of each module separately. However, for the simplicity of structure, it is best to initialize other modules (including some interrupt initialization) here after writing the system clock initialization.
void Init_Sys()
{
unsigned int i;
BCSCTL1&=~XT2OFF; //Turn on XT2 oscillatordo
{
IFG1
&= ~OFIFG; //Clear oscillator failure flagfor
(i = 0xFF; i > 0; i--); // Delay, wait for XT2 to start oscillating
}
while ((IFG1 & OFIFG) != 0); //Judge whether XT2 has started oscillatingBCSCTL2
=SELM_2+SELS; //Select MCLK and SMCLK for XT2
//The following initializes various modules, interrupts, peripherals, etc
........................................
_EINT(); //Turn on global interrupt control
}
This involves the clock issue. Usually we choose XT2 as 8M crystal oscillator, that is, the system main clock MCLK is 8M, and the CPU executes commands based on this clock; but other peripheral modules can select other clocks in the corresponding control registers, ACLK; when you have low speed requirements and a large timing time interval, you can choose ACLK, such as setting it in the timer Timea initialization.
Main program:
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; //Turn off watchdog
InitSys(); //Initialize
//Other function functions in your own task
. . .
. . . . . . . . . . . . . . . . . . . while(1);
}
After the main program, I will talk about the interrupt function. Interrupt is an indispensable part of your microcontroller task, and it can also be said to be the soul (exaggeration?).
Take an example of a timer interrupt:
//initialization
void Init_Timer_A(void)
{
TACTL = TASSEL0 + TACLR; // ACLK, clear TAR
CCTL0 = CCIE; // CCR0 interrupt enable
CCR0=32768; //timing 1s
TACTL|=MC0; //increment count mode
}
// interrupt service
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0()
{
//the task you request the interrupt to execute
}
void Init_Sys()
{
}
while ((IFG1 & OFIFG) != 0);
=SELM_2+SELS;
//The following initializes various modules, interrupts, peripherals, etc
........................................
_EINT(); //Turn on global interrupt control
}
This involves the clock issue. Usually we choose XT2 as 8M crystal oscillator, that is, the system main clock MCLK is 8M, and the CPU executes commands based on this clock; but other peripheral modules can select other clocks in the corresponding control registers, ACLK; when you have low speed requirements and a large timing time interval, you can choose ACLK, such as setting it in the timer Timea initialization.
Main program:
void main( void )
{
//Other function functions in your own task
}
After the main program, I will talk about the interrupt function. Interrupt is an indispensable part of your microcontroller task, and it can also be said to be the soul (exaggeration?).
Take an example of a timer interrupt:
//initialization
void Init_Timer_A(void)
{
//
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0()
{
//the task you request the interrupt to execute
}
Previous article:MSP430 DCO Control Register
Next article:Design of a parking space detection system based on wireless sensor network
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Variable memory depth and segmented memory of LOTO oscilloscope
- [SAMR21 new gameplay] 21. Touchio
- STM32 learning the ninth post can not turn off the PWM pin
- I have a SOT23-6 package with a silk screen model number.
- 【CH579M-R1】+PWM breathing light and serial communication experiment
- The problem of calibrating speed
- Measuring the flow control valve signal of the auto repair actuator with an automotive oscilloscope
- How is this kind of PCB wiring designed using AD? Please give me some guidance, masters!
- EEWORLD University ---- Quartus Prime Development Process (Intel Official Tutorial)
- Issues with STM32L15xCC