| I. Carrying on from the previous article In the previous section, we learned together about the implementation of simple external interrupts. After having practical experience with interrupts, let's discuss timer interrupts in this section.CC2430 has 4 timers, which can be categorized into 3 types: Timer1, Timer2, Timer3/4 (the usage of 3 and 4 is the same). Since the author is also just touching CC2430, the project involving timer experience is basically zero, so I do not intend to (and can not help) in-depth analysis of the timer. This article is only on the timer 1 count overflow interrupt usage to do a simple experimental exploration, for its input capture/output comparison/PWM function is omitted. Timer 2 and Timer 3/4 are also only briefly introduced. Waiting for the author's power to reach a certain level of fire, then stand in the height of the Zigbee combat experience to improve this essay. Timer 1 Timer1 is a 16-bit timer with timer/counter/pulse width modulation functions. It has three individually programmable input capture/output comparison channels, each of which can be used as a PWM output or used to capture the edge time of the input signal (for more information on what input capture/output comparison is and how to implement PWM output, readers can refer to the CC2430 Chinese manual). The timer has a very important concept: operation mode. Operation modes include: free-running, modulo and up-down. The following is the introduction of the three modes taken from the CC2430 Chinese manual:
Comparing the three modes, it can be seen that: the overflow value of the free-running mode is 0xFFFF which is not changeable; while the other two modes can be accurately controlled by assigning a value to T1CC0 in order to precisely control the overflow value of the timer. This experiment precisely utilizes this feature to make the timer trigger an interrupt every 1s through the specific T1CC0, so as to precisely control the LED blinking interval of 1s. (1) Introduction to the experiment In the modulo mode of the timer, the flashing interval of the LED is precisely controlled to be 1s, i.e.: light 0.5s → dark 0.5s → light 0.5s → dark 0.5s ...... → light 0.5s → dark 0.5s (i.e. the momentary interval from dark to light is 1s). The reversal of light/dark is realized by overflow interrupt. (2) Program flow chart (3) Related calculations As mentioned before, the state of the LED is: bright 0.5s → dark 0.5s → bright 0.5s → dark 0.5s ...... → light 0.5s → dark 0.5s, and need to be realized with the overflow interrupt, so the overflow period of the timer is required to be 0.5s. To this end, the corresponding overflow value needs to be calculated (temporarily set to N). The system clock frequency is selected as 32MHz, and the clock frequency provided to the timer is 16MHz by default (both of them are configured by the special function register CLKCON, which can be found in the CC2430 Chinese manual). For timer 1, set its clock frequency to 128 divisions. To summarize, the following formula can be listed:
Find out N = 62500, its hexadecimal is 0xF424, that is, you need to set T1CC0H = 0xF4, T1CC0L = 0x24 that is . (4)实验源码及剖析/*
实验说明:定时器Timer1实验,定时器计数溢出,LED1闪烁
*/
#include
#define led1 P1_0
#define led2 P1_ 1 #define led3 P1_2 #define led4 P1_3 & nbsp;
/* system clock initialization -------------------------------------------------------*/ void xtal_init( void ) { SLEEP &= ~0x04;& nbsp; // both power up while (! (SLEEP & 0x40 )); //crystal oscillator on and stable CLKCON &= ~0x47; & nbsp; //select 32MHz crystal oscillator SLEEP |= 0x04; }
/*LED initialization -------------------------------------------------------*/ void led_init( void ) { & nbsp; P1SEL = 0x00; //P1 is a normal I/O port P1DIR |= 0x0F; & nbsp; //P1.0 P1.1 P1.2 P1.3 output led1 = 1; & nbsp; //Turn off all LEDs led2 = 1; led3 = 1; led4 = 1; }
/*T1 initialization -------------------------------------------------------*/ void timer1_init( void ) { EA=1; // turn on general interrupt & nbsp; T1IE=1; // turn on T1 interrupt OVFIM=1; // turn on T1 overflow interrupt T1CC0L=0x24; //overflow value low 8 bits T1CC0H=0xF4; //overflow value high 8 bits & nbsp; T1CTL = 0x0e; //128 divisions; modulo mode (0x0000->T1CC0); start running; T1IF=0; & nbsp; // clear interrupt flag }
/* main function -------------------------------------------------------*/ void main( void ) { xtal_init(); led_init(); timer1_init();
while ( 1 ); & nbsp; //wait for overflow interrupt }
/*T1 terminal service subroutine -------------------------------------------------------*/ #pragma vector=T1_VECTOR __interrupt void T1_ISR( void ) { EA=0; //off interrupt led1 = ! led1; //LEDs reversed
EA=1; //On interrupt   ; T1CTL &= ~0x10; // clear interrupt flag } OK, compile the program and online debugging, the development board on the LED1 as expected to blink up, feel the blinking interval of about 1s. But this is not enough to prove the success of the experiment, if you can rigorously determine the interval of 1s would be perfect ~ so I opened the WIN 7 clock (click on the right side of the taskbar time can be). While looking at the second hand, while out of the corner of my eye to see the flashing LED1. The result is: in two minutes, the pace of the two are basically identical (this precision can be tolerated ~). At this point, the experiment can be said to be basically complete, hehehe~ Third, Timer 2 Timer2, also known as the MAC timer, is specially designed to support the event tracking protocol in IEEE 802.15.4 MAC. The timer has an 8-bit overflow counter that can be used to record the number of cycles that have occurred; has a 16-bit capture register that can be used to record the exact time that a frame start delimiter is received/transmitted or the exact time that a transmission is completed; and contains a 16-bit output comparison register that can be used to generate a variety of command-selective communication signals to the wireless module at a specific time (start receiving, start transmitting, etc.). Timer 3/4 Timer 3/4 is an 8-bit timer with timer/counter/PWM functions. t3/t4 has two output comparison channels, each of which can be used as a PWM output. V. Conclusion In this section, we have learned the method of counting the overflow interrupt of timer 1, which realizes the precise control of the LED blinking interval of 1 s. We will just skim over the other timers, and then go back to add them later. In the next section, we will introduce about CC2430 serial port communication. |