I have been studying and using MSP430 for a few months. I even saw a note on the micro-control forum, which is well recorded. Now I have reposted it to my blog for memo.
Preliminary hardware
I am only learning about the 16-bit ultra-Power Single-Chip MSP430 produced by TI. I hope to learn from other friends. If not, please advise me more.
Next, we will start our 430 journey.
There are also a lot of books to explain 430, but most of them describe the underlying hardware structure in detail. It is a bit empty and boring, in my opinion, to understand the basic features of a MCU, you must first understand the functions of each module.
1. First you need to know the memory structure of the MSP430. The typical Microprocessor has two structures: Feng. Noriman structure-Unified coding of program memory and data storage; Harvard structure-program memory and data storage; MSP430 Series Single-Chip Microcomputer belongs to the former, while commonly used MCS51 series belongs to the latter.
0-0xf special function registers; 0x10-0x1ff peripheral module registers; 0x200 -? Scales from low to high based on different models. 0x1000-0x0000f seg_b0x1080_0x10ff seg_a scales down from 0xffff for flash information storage. For example, if 149 is 60kb, 0xffff-0x1100
2. the reset signal is the starting point of MCU operation. There are two types of Reset signals: power-on reset signal por and power-on cleaning signal PUC. The por signal is only set to the reset function on power-on and RST/NMI reset pins, and the system is reset when the power is low. The PUC signal is generated by the por signal, and other events such as timed overflow of the guard dog and incorrect security key value are generated. However, no matter the reset triggered by the signal, the MSP430 will read the reset interrupt vector at address 0xffff, and the program will execute the reset interrupt vector from the address indicated by the interrupt vector. The Status After resetting is not written. For more information, see reference.
3. The system clock is the commander of a program running. Timing and interruption are also the core and central axis of the entire program. 430 has a maximum of three oscillator, dCO internal oscillator; lfxt1 external low frequency oscillator, common 32768Hz, without external load capacitor; can also be connected to high frequency 450 kHz-8 m, need to be connected to the load capacitor; xt2 is connected to high-frequency 450 kHz-8 m, and an external capacitor is added. (In experience, it is found that when xt2 is connected, you need to enable xt2 by yourself, and wait 50 US for xt2 to start vibration, and then manually clear the ofifg bit in ifg1. The operation sequence is as follows: turn on xt2-> wait for xt2 to stabilize-> switch the system clock to xt2)
430 has three clock signals: The mclk system master clock, which can be divided into 1, 2, 4, and 8 for CPU use. Other Peripheral modules can also be used when selected; smclk system sub-clock, for the peripheral module, you can select the clock signal generated by different oscillator. The aclk auxiliary clock can only be generated by lfxt1 for the peripheral module.
4. interrupt is a major feature of the 430 processor, because almost every peripheral module can be generated, 430 can enter the Low Power State when there is no task, interrupt when there is an event wake-up CPU, processing is completed again into the low power state.
The entire interrupt response process is like this. When a request is interrupted, if the CPU is active, the current command is completed first. If the request is in low power consumption, the system exits first, press the Pc value of the next instruction into the stack. If there are multiple interrupt requests, the response priority is high first. After the execution, wait for the flag reset of the interrupt request, the flag of the interrupt request of a single interrupt source is automatically reset, while the flag of multiple interruptions needs to be reset by the Software. Then, the system's total interruptions are allowed by the SR. the program will resume execution from this address when the program resets and the corresponding interrupt vector value is loaded into the PC.
Here, we should note that the interrupt allowed bit Sr. Gie and interrupt nesting issues. If you want to respond to more advanced interrupt requests when executing the interrupt program, you must set Sr. gie to the first interrupt.
In fact, the clock of other peripheral modules is executed along the core of clock and interrupt. For more information about the structure, see the 430 series manual.
C language programming started
Because the commonly used 430 programming and development is a C language, the following describes the overall structure of C language for 430 programming. It basically belongs to the framework structure, that is, the overall modular programming. In fact, this is also the basic law of hardware programming (not the rule I have defined ).
The first is the program's header file, including # include <export x14x. h>, which is a 14 series, because 149 is commonly used. You can modify other models by yourself. You can also include database header files such as # include "data. H" or function variable declaration header files, which are defined by yourself.
The following is the void init_sys (void) Declaration of the function and variable, that is, system initialization. System initialization is a general concept. In a broad sense, it includes initialization of all peripheral modules. You can write the subfunctions initialized by the peripheral modules to init_sys, you can also write the initialization of each module separately. However, the structure is concise. It is best to initialize other modules (including some interrupt initialization) after the system clock initialization.
Void init_sys ()
{
Unsigned int I;
Bcsctl1 & = ~ Xt2off; // enable the xt2 Oscillator
Do
{
Ifg1 & = ~ Ofifg; // clear the oscillator failure sign
For (I = 0xff; I> 0; I --); // latency, waiting for xt2 to start
}
While (ifg1 & ofifg )! = 0); // determines whether xt2 is vibrating.
Bcsctl2 = selm_2 + sels; // select mclk and smclk as xt2
// Initialize various modules, interruptions, and peripheral devices
........................................
_ Eint (); // enable global interrupt control
}
The clock is involved here. Generally, we select xt2 as the 8 m crystal oscillator, that is, the system's main clock mclk is 8 m, and the CPU command is executed based on this clock; however, other peripheral modules can select other clocks in the corresponding control registers, aclk. When you have low speed requirements and large timing intervals, you can select aclk, for example, set in timer timea initialization.
Main Program:
Void main (void)
{
Wdtctl = wdtpw + wdthold; // disable the Watchdog
Initsys (); // Initialization
// Other functions in your task
.....................
While (1 );
}
After the main program, I want to talk about the interrupt function. Interrupt is an indispensable part of your single-chip microcomputer task. It can also be said to be the soul (exaggerated ).
/*************************************** ********************************
Interrupt functions, which can be written in order of priority
**************************************** *******************************/
For example, scheduled interruption:
// Initialization
Void init_timer_a (void)
{
Tactl = tassel0 + taclr; // aclk, clear tar
Cctl0 = CCIE; // ccr0 interrupt enabling
Ccr0 = 32768; // scheduled for 1 s
Tactl | = mc0; // Add count mode
}
// Interrupt service
# Pragma vector = "timera0" _ Vector
_ Interrupt void timera0 ()
{
// The task you want to interrupt.
}
Of course, there are other timing, and a variety of interruptions, the number of Interrupt vectors of each series of chips is also different.
After learning and understanding the above knowledge, we recommend you read the application principles of common MSP430 modules on the micro-control forum.