Document directory
- (1) experiment Overview
- (2) program flowchart
- (3) related computing
- (4) experiment source code and Analysis
I. Connecting
In the previous article, we learned how to implement simple external interruptions. With the practical experience of interrupt, we will discuss the timer interrupt in this section. The cc2430 has four timers, which can be divided into three categories: timer 1, Timer 2, and timer 3/4 (3 and 4 are used in the same way ).
Since the author has just come into contact with cc2430, the practical experience of projects involving timers is basically zero, so I do not intend to (and can't do anything) in-depth analysis of timers. This article only makes a simple experimental exploration on the counting overflow interrupt usage of timer 1, and does not mention its input capture/output comparison/PWM function. Timer 2 and timer 3/4 are only simple functions. After the power of the author reaches a certain level, I will come to the height of ZigBee practical experience to complete this article.
Ii. Timer 1
Timer 1 is a 16-bit timer with the timer/Counter/pulse width modulation function. It has three separate programmable input capture/output comparison channels, each of which can be used as a PWM output or to capture the edge Time of the input signal (about what is input capture/output comparison, and how to achieve PWM output, readers can read the cc2430 Chinese manual ).
The timer has a very important concept:Operation Mode.
Operation modes include: free operation mode (Free-running), Mode (Modulo) And positive/inverted count (Up-down).
The following is an introduction to the three modes from the cc2430 Chinese manual:
Comparing the three modes, we can see that the overflow value of the free running mode is 0xffff and the overflow value of the timer can be precisely controlled by assigning values to t1cc0 in the other two modes. This experiment uses this feature to enable the timer to trigger an interrupt once every 1 s through a specific t1cc0, so as to precisely control the blinking interval of the LED lamp to 1 s.
(1) experiment Overview
In the modulo mode of the timer, the flashing interval of the LED lamp is precisely controlled to 1 s, that is, 0.5 s, 0.5 s, 0.5 s, and 0.5 s ...... → bright 0.5 s → dark 0.5 s (that is, the time interval from dark to Bright is 1 s ). The bright/dark reversal is implemented through overflow interruption.
(2) program flowchart
(3) related computing
As mentioned above, the LED lights are in the following status: 0.5 s, 0.5 s, 0.5 s, and 0.5 s ...... → 0.5 s → 0.5 s, and overflow interruption is required. Therefore, the overflow cycle of the timer is 0.5 s. Therefore, the corresponding overflow value (set to N) needs to be calculated ).
The system clock frequency is set to 32 MHz, and the default clock frequency provided to the timer is 16 MHz (both of which are composed of special function registers ).ClkconFor more information, see the cc2430 Chinese manual ).
For timer 1, set the clock frequency to 128.
To sum up, the columns are as follows:
Obtain n = 62500, And the hexadecimal value is 0xf424. You must set t1cc0h = 0xf4 and t1cc0l = 0x24.
(4) experiment source code and analysis /*
Lab Description: timer timer1 experiment, Timer count overflow, led1 flashing
*/
# Include <iocc2430.h>
# Define led1 p1_0
# Define led2 p1_1
# Define led3 p1_2
# Define led4 p1_3
/* System clock Initialization
-------------------------------------------------------*/
Void xtal_init (void)
{
Sleep & = ~ 0x04; // all powered on
While (! (Sleep & 0x40); // The crystal oscillator is enabled and stable.
Clkcon & = ~ 0x47; // select a 32 MHz Crystal Oscillator
Sleep | = 0x04;
}
/* Led Initialization
-------------------------------------------------------*/
Void led_init (void)
{
P1sel = 0x00; // P1 is a common I/O port
P1dir | = 0x0f; // p1.0 p1.1 p1.2 p1.3 output
Led1 = 1; // turn off all LEDs
Led2 = 1;
Led3 = 1;
Led4 = 1;
}
/* T1 Initialization
-------------------------------------------------------*/
Void timer1_init (void)
{
Ea = 1; // total disconnection
T1ie = 1; // enable T1 interrupt
Ovfim = 1; // enable T1 overflow interruption
T1cc0l = 0x24; // The overflow value is 8 bits
T1cc0h = 0xf4; // The overflow value is 8 bits in height.
T1ctl = 0x0e; // 128 frequency division; modulo mode (0x0000-> t1cc0); start running;
T1if = 0; // clear the interrupt mark
}
/* Main Function
-------------------------------------------------------*/
Void main (void)
{
Xtal_init ();
Led_init ();
Timer1_init ();
While (1); // wait for the overflow to interrupt
}
/* T1 Terminal Service subroutine
-------------------------------------------------------*/
# Pragma vector = t1_vector
_ Interrupt void t1_isr (void)
{
Ea = 0; // Guanzhong disconnection
Led1 =! Led1; // LED Light Invert
Ea = 1; // interrupt
T1ctl & = ~ 0x10; // clear the interrupt mark
}
OK. Compile the program and debug it online. The led1 ON THE DEVELOPMENT BOARD flashes as scheduled, and the flash interval is about one second. However, this is not enough to prove the success of the experiment. If we can strictly determine the interval of 1 s, it will be perfect ~ So I opened the windows 7 clock (click the time on the right of the taskbar ). While looking at the second hand, the remaining eye of led1 blinks. The result is: within two minutes, the two steps are basically the same (This accuracy can be tolerated ~). So far, the experiment can be basically completed ~
3. Timer 2
Timer 2 is also calledMac TimerIt is specially designed to support the event tracking protocol in IEEE 802.15.4 Mac. This timer has an eight-bit overflow counter that can be used to record the number of cycles that have occurred; there is a 16-bit capture register, it is used to record the exact time of receiving/sending a frame start time or the precise time of transmission completion. It also contains a 16-bit output comparison register, it is used to generate various command selection communication numbers (start to accept, start to send, etc.) for the wireless module at a specific time ).
Iv. Timer 3/4
The timer 3/4 is an 8-bit timer with the timer/Counter/PWM function. T3/T4 has two output comparison channels, each of which can be used as PWM output.
V. Conclusion
In this section, we mainly learn the counting overflow interrupt method of timer 1, and implement precise control of the flashing interval of LED lights to 1 s. Only a few other timers have been taken, and will be supplemented later. Next, we will introduce the cc2430 serial communication.
Next section: ZigBee journey (5): several important cc2430 Basic Experiments-Serial Communication (unfinished)