|
1. Connecting the upper and lower levels In the previous article, we learned together about the implementation of simple external interrupts. Now that we have hands-on experience with interrupts, let's talk about timer interrupts in this section. CC2430 has a total of 4 timers, which can be divided into 3 categories: timer 1, timer 2, timer 3/4 (the usage of 3 and 4 is the same). Since the author is also new to the CC2430 and has basically zero practical experience in projects involving timers, I do not intend (and cannot do anything) to analyze the timer in depth. This article only provides a brief experimental exploration of the use of timer 1 in count overflow interrupts, and does not mention its input capture/output comparison/PWM functions. Timer 2 and Timer 3/4 are only simple function introductions. After the author's skills reach a certain level of heat, I will stand at the height of Zigbee's practical experience to improve this essay. 2. Timer 1Timer 1 is a 16-bit timer with timer/counter/pulse width modulation. It has 3 individually programmable input capture/output comparison channels, each of which can be used as a PWM output or as a edge time to capture the input signal (for information on what input capture/output comparison is and how to implement PWM output, readers can refer to the CC2430 Chinese manual). Timers have a very important concept:Mode of operation。 Operating modes include: Free operation mode (free-running), mold mode (modulo) and Positive Count/Countdown Mode (up-down)。 The following is an excerpt from the CC2430 Chinese manual for the introduction of the 3 modes:
Comparing the three modes, it can be seen that the overflow value of the free operation mode is 0xFFFF unchangeable; The other two modes allow for precise control of the timer's overflow value by assigning a value to T1CC0. This experiment uses this feature to trigger an interrupt every 1s by the timer through a specific T1CC0, so as to accurately control the flashing interval of the LED light to 1s. (1) Introduction to the experimentIn the modulo mode of the timer, the blinking interval of the LED light is precisely controlled by 1s, namely: bright 0.5s → dark 0.5s → bright 0.5s → dim 0.5s ...... → Light 0.5s → Dark 0.5s (i.e., the time interval from dark to light is 1s). Light/dark inversion is achieved by overflowing interrupts. (2) Program flow chart
(3) Relevant calculationsAs mentioned earlier, the status of the LED light is: bright 0.5s → dark 0.5s → bright 0.5s → dim 0.5s ...... → 0.5s light → 0.5s dark, and it needs to be implemented with overflow interrupts, so the overflow period of the timer is required to be 0.5s. To do this, the corresponding overflow value needs to be calculated (temporarily set to N). The system clock frequency is selected as 32MHz, and the clock frequency given given is 16MHz by default (both are determined by special function registers).CLKCONFor details, please refer to the CC2430 Chinese manual). For Timer 1, set its clock crossover to 128 divisions. To sum up, the list is as follows:
To find N=62500, its hexadecimal is 0xF424, that is, you need to set T1CC0H=0xF4, T1CC0L=0x24 . (4) Experimental source code and analysis/*
Experiment description: Timer Timer1 experiment, timer count overflow, LED1 flashing
*/
#include
#define led1 P1_0
#define led2 P1_1
#define led3 P1_2
#define led4 P1_3
/*系统时钟初始化
-------------------------------------------------------*/
voidxtal_init(void)
{ SLEEP &= ~0x04; //都上电
while(! (SLEEP &0x40)); //晶体振荡器开启且稳定 CLKCON &= ~0x47; Choose a 32MHz crystal oscillator SLEEP |=0x04;
}
/*LED初始化
-------------------------------------------------------*/
voidled_init(void)
{ P1SEL =0x00; P1 is the normal I/O port P1DIR |=0x0F; P1.0 P1.1 P1.2 P1.3 output
led1 =1; //关闭所有LED led2 =1; led3 =1; led4 =1;
}
/*T1初始化
-------------------------------------------------------*/
voidtimer1_init(void)
{ EA=1; //开总中断 T1IE=1; //开T1中断 OVFIM=1; //开T1溢出中断
T1CC0L=0x24; //溢出值低8位 T1CC0H=0xF4; //溢出值高8位
T1CTL =0x0e; 128 crossover; modulo mode (0x0000->T1CC0); Start running; T1IF=0; Clear the interruption sign
}
/*主函数
-------------------------------------------------------*/
voidmain(void)
{ xtal_init(); led_init(); timer1_init();
while(1); //等待溢出中断
}
/*T1终端服务子程序
-------------------------------------------------------*/
#pragma vector=T1_VECTOR __interruptvoidT1_ISR(void)
{ EA=0; //关中断
led1 = !led1; //LED灯反转
EA=1; //开中断
T1CTL &= ~0x10; //清中断标志
}
OK, compile the program and debug online, LED1 on the development board flashes as scheduled, and it feels like the flashing interval is about 1s. But this is not enough to prove the success of the experiment, if the interval can be strictly determined to be 1s, it will be perfect~ So I turned on the clock of WIN 7 (click on the time on the right side of the taskbar). While looking at the second hand, he looked at the flashing of LED1 out of the corner of his eye. The result: within two minutes, the pace of the two is basically exactly the same (this precision is tolerable~). At this point, the experiment can be said to be basically completed, hehe~ 3. Timer 2Timer 2 is also calledMAC timer, is specifically designed to support the event tracking protocol in IEEE 802.15.4 MAC. The timer has an 8-digit overflow counter that can be used to record the number of cycles that have occurred; There is a 16-bit capture register that records the exact time a frame starts to receive/send or the exact time the transmission is completed. It also contains a 16-bit output comparison register that generates various command gating signals (start accepting, start sending, etc.) to the wireless module at a specific time. 4. Timer 3/4Timer 3/4 is an 8-bit timer with timer/counter/PWM function. T3/T4 has 2 output comparison channels, each of which can be used as PWM output. 5. ConclusionThis section mainly learns the method of the count overflow interrupt of timer 1, and realizes the precise control of the LED light flashing interval of 1s. Just a few other timers are just a stroke, and I'll come back to add them later. In the next section, we will introduce the CC2430 serial port communication.
|