Introduction to Msp430 timer and its basic applications[Copy link]
There are 5 types of timers in the Msp430 microcontroller. Watchdog Timer (WDT), Basic Timer1, 8-bit Timer/Counter, Timer_A and Timer_B. However, these modules are not functions available in all msp430 models. 1. Watchdog Timer (WDT) Those who have studied electronics may know that the main function of the watchdog is to restart the controlled system when a program fails. In the msp430, it is a 16-bit timer with two modes: watchdog and timer. 2. Basic Timer1 The basic timer is a module in the msp430x3xx and msp430F4xx series devices, which usually provides low-frequency control signals to other peripherals. It can be only two 8-bit timers or a 16-bit timer. 3. 8-bit Timer/Counter As its name indicates, it is an 8-bit timer, mainly used to support serial communication or data exchange, pulse counting or accumulation, and timer use. 4. 16-bit Timer A and B Timer A is available in all msp430 series microcontrollers, while Timer B appears in devices such as msp430f13x/14x and msp430f43x/44x. The basic structure is the same as Timer A. Since I am first familiar with and apply Timer A, I will mainly talk about my understanding and application of Timer A here. Timer A is a 16-bit timer with 4 working modes and optional clock source. It generally has 3 configurable input comparison/capture registers and 8 output modes. It is easy to implement PWM waves through 8 output modes. The hardware circuit of timer A can be roughly divided into two types of functional modules: 1. Counter TAR Counter TAR is the main body. It is a timer that can be turned on and off. If it is turned on, it will keep counting in a loop. There will only be one overflow interrupt, that is, when the count changes from 0xffff to 0, an interrupt TAIFG will be generated. 2. Compare/capture register CCRX How to realize the timing function? This depends on three compare/capture registers (hereinafter represented by CCRx). When the count value of counter TAR is equal to CCRx (this is the meaning of the comparison in capture/compare: compare whether TAR is equal to CCRx), the CCRx unit will generate an interrupt. According to the interrupt, the corresponding timing time can be obtained. In this way, we can get three timing times through timer A. 1: Program example I will first give one of my applications, and then use the program to explain the basic usage of timer A. The program is as follows: /**************************************************** * Timer initialization************************************************/ void init_TimerA ( void ) { CCTL0 = CCIE; //1: Enable comparator 0 interrupt CCR0 = 32768; // 2: Select the timing time as 1S second timing: because ACLK and UpMode are selected, the time for each increase of TAR is 1/32768s, and the total increase is 32768 times, so it is 1s CCTL1 = CCIE; // Enable comparator 1 interrupt CCR1 = 100; // 3.66mS display delay TACTL = TASSEL_1 + MC_1; // 3: Select the clock source and counting mode. The clock source is ACLK and the counting mode is up-counting mode LPM3; // Enter low power consumption 3 } /******************************************************** * Timer 0 interrupt****************************************************/ #pragma vector = TIMERA0_VECTOR __interrupt void Timer_A0(void) { //User code TACCR0 } /******************************************************** * Timer interrupt **********************************************************/ #pragma vector = TIMERA1_VECTOR __interrupt void Timer_A1 ( void ) { switch( TAIV ) { case 2: //User code break; // TACCR1 case 4://User code break; // TACCR2 case 10://User code break; //TAIFG } //Whether to exit low power mode according to need LPM3_EXIT; // Exit low power } 2: Program analysis 1): Look at the timer initialization module in the program. 1: CCTL0 = CCIE; CCTLx is the control register of the corresponding compare/capture register, which can set the compare/capture register. This statement means: the interrupt enable of CCR0 is turned on, and an interrupt is generated when the counter TAR counts to CCR0. 2: CCR0 = 32768; CCRx is the value of the corresponding comparator. It is a 1S second timing: because ACLK and UpMode are selected, the time for each increase of TAR is 1/32768s, and it increases 32768 times in total, so it is a 1s timing. 3: TACTL = TASSEL_1 + MC_1; TACTL is the control register of the counter. TASSEL_x is the selection of the clock source. 0-TACLK, use the external pin signal as input 1-ACLK, auxiliary clock 2-MCLK, system main clock 3-INCLK, external input clock MC_x is the working mode selection bit for selecting TAR. When setting the mode of TACTL, the timer is also turned on. To stop it, just assign MC_0 to TACTL. 0——Stop mode, used for timer pause 1——Increase count mode, the counter counts to CCR0, then clears the count 2——Continuous count mode, the counter counts to 0xffff, then clears the count 3——Increase/decrease count mode, counts to CCR0, then counts to 0 So this counter works in UP mode, and the clock source is ACLK. 2): Look at the timer interrupt module in the program. In UP or UP/DOWN mode, the maximum count value of the counter TAR can be changed by changing the value of CCR0, that is, when the counter counts to the value of CCR0, it will automatically clear the counter. Because CCR0 is special, the interrupt vector of CCRO0 is different from the interrupt vector shared by CCR1, CCR2, and TA. The interrupt of CCR0 is very easy to add your own interrupt corresponding program in the function. However, CCR1 and CCR2 share an interrupt function, so it is necessary to identify which interrupt it is, which can be identified through a register TAIV, 2-compare/capture register 1 interrupt 4-compare/capture register 2 interrupt 10-timer overflow 0-no interrupt Three: Notes It should be noted that the values of CCR1 and CCR2 should be less than CCR0 in the up-counting mode, otherwise CCR1 and CCR2 will not generate interrupts. And after each interruption of CCR1 and CCR2, they must be reassigned to ensure that the interrupt interval of CCR1 and CCR2 remains unchanged. Similar statements are as follows: CCR1 += 100; if (CCR1 >= 32768) CCR1 -= 32768; The above is the most basic application of timer A, and it will be explained in detail in combination with actual applications in the future.