Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Chapter one introduces timer technology and CC2530 clock source

1. Timer technology

Timer in the MCU is also a basic necessary function, very commonly used; In the program design, many places need to use the concept of time, such as: the use of timer to do some polling detection, accurate delay function, serial broken frame detection, timer send, provide heartbeat packet and so on.

The realization principle of timer in CPU is as follows:

Timer, is a kind of can count the internal clock signal or external input signal, when the count value reaches the set requirements, to the CPU interrupt processing request, so as to realize the timing or counting function of the peripheral.

The most basic working principle of timer is to count. Whether timer or counter, essentially is a counter, can be added 1 (minus 1) count, every occurrence of a count signal, the counter will automatically add 1 (automatically minus 1), when the count value from 0 to the maximum (or from the maximum into 0) overflow, timing will interrupt CPU request.

CC2530 has a total of five timers, among which timer 1 is a 16-bit timer, which belongs to the most comprehensive timer in CC2530 and can be used preferentially in application development.

Timer 1 can work in three modes according to the document:

The first mode is free-running mode: the counter runs from0x0000At first, increment at each active clock edge1, when the counter reaches0xFFFFWhen overflows, the counter reloads0x0000And start a new increment count. This mode has a fixed count period0xFFFF, when the final count value is reached0xFFFF, the flag bits T1IF and OVFIF are set. The first2The mode is modular mode: counter from0x0000To start, add at each active clock edge1When the counter reaches the value stored in the T1CC0 register, it overflows, and the counter overflows from0x0000Start a new round of incremental counting, the counting cycle of modular mode can be set by the user. The first3The mode is forward count/reverse count mode: the counter is repeated from0x0000At first, counting up to the final count saved by TICC0, and then counting back0x0000When the final count is reached, the flag bits T1IF and OVFIF are set.Copy the code

2. The clock source

CC2530 has two optional clock sources, one is internal and the other is external.

1, internal RC oscillator (32KHz, 16MHz)

2, external quartz crystal oscillator (32.768khz, 32MHz)

Generally, the external quartz crystal is used in wireless transceiver, because the external quartz crystal is more stable, not affected by the INTERNAL TEMPERATURE of the CPU.

3. Switch the clock source

This command is used to check whether the clock source switchover is successful

4. The timer is interrupted

Timer has three cases can generate interrupt request (almost all microcontrollers are such event classification):

1. The counter reaches the set value

2. Generate an input capture event

3. An output comparison event is generated

Chapter 2 Relevant registers

Chapter 3: Sample code

Timer 1 has 5 pairs of T1CCxH and T1CCxL registers, corresponding to channels 0 to 4 respectively. When using the timing function of timer 1, two registers T1CC0H and T1CC0L are used to store the high and low 8 bits of the maximum count value.

Write the following use of timer 1 example code, turn on the timer interrupt. Timing 1 seconds and 10 seconds, respectively, in the interrupt service function for judgment, complete LED lamp control.

#include <ioCC2530.h>

// Define the LED light port
#define LED1 P1_2
#define LED2 P1_3

/* Initialize hardware connection: LED1-->P1_2, LED2-->P1_3 */
void LED_Init(void)
{
    P1DIR |=0x3<<2;  // Set P1_2 and P1_3 as output modes
    LED1 = 1;
    LED2 = 1;
}

/* Delay 200 milliseconds */
void delay200ms(void)   / / error - 0.125 us
{
    unsigned char a,b,c;
    for(c=95; c>0; c--)for(b=181; b>0; b--)for(a=14; a>0; a--); }/*=============== timer 1 initialization function ==================*/
void Init_Timer1(a)
{
  T1CC0L = 0xd4;        // Set the lower 8 bits of the maximum count
  T1CC0H = 0x30;        // Set the highest 8 bits of the maximum count
  T1CCTL0 |= 0x04;      // Enable the output comparison mode of channel 0
  T1IE = 1;             // Enable the interruption of timer 1
  T1OVFIM = 1;          // Enable timer 1 overflow interrupt
  EA = 1;               // Enable total interrupt
  T1CTL = 0x0e;         // The frequency division coefficient is 128, mode mode
}


unsigned char count = 0;  
/*================ timer 1 service function ====================*/
#pragma vector = T1_VECTOR
__interrupt void Timer1_Sevice(a)
{
  T1STAT &= ~0x01;      // Clear the interrupt flag of timer 1 channel 0
  count++;
  if(count%10= =0)     // Time is 1 second{ LED1 = ! LED1; }if(count == 100)      // Timed for 10 seconds{ LED2 = ! LED2; count =0; }}/* Main function */
void main(void)
{
    LED_Init(a);// Initializes the LED control IO port
    Init_Timer1(a);while(1) {}}Copy the code