Getting Started with MSP430: C Programming

Publisher:chinapxfLatest update time:2015-11-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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.
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
}
 

Reference address:Getting Started with MSP430: C Programming

Previous article:MSP430 DCO Control Register
Next article:Design of a parking space detection system based on wireless sensor network

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号