[CC2530 Introduction Course-04] The principle and application of CC2530 timer/counter

[CC2530 Introduction Tutorial-06] CC2530's ADC working principle and application

[CC2530 Introduction Tutorial-05] The principle and application of the serial interface of CC2530

[CC2530 Introduction Course-04] The principle and application of CC2530 timer/counter

[CC2530 Introduction Course-03] CC2530 Interrupt System and External Interrupt Application

[CC2530 Introduction Tutorial-02] CC2530's general I/O port input and output control

[CC2530 Getting Started Tutorial-01] CC2530 Microcontroller Development Getting Started Basics


1. The basic principle of timing/technical device https://www.cnblogs.com/ALittleBee/p/7078252.html

        Timer/counter is a peripheral that can count the internal clock signal or external input signal, and when the count value reaches the set requirement , it will put forward an interrupt processing request to the CPU to realize the timing or counting function.

        The most basic working principle of the timer/counter is to count . Regardless of whether it is a timer or a counter, it is essentially a counter , which can be counted up by 1 (minus 1). Every time a count signal appears, the counter will automatically increase by 1 (automatically subtract 1). When the count value changes from 0 to the maximum (Or from the maximum value to 0) When overflows, the timer/counter will raise an interrupt request to the CPU.

 

Second, CC2530 timer/counter

        CC2530 has 5 timers/counters in total , among which timer 1 is a 16-bit timer, which is the most complete timer/counter in CC2530 and should be selected first in the application. There are three working modes of Timer 1:

        <1>  Free running mode : The counter starts from 0x0000 and increases by 1 on each active clock edge. When the counter reaches 0xFFFF, it overflows, and the counter reloads 0x0000 and starts a new round of increment counting. The counting period of this mode is a fixed value of 0xFFFF. When the final count value of 0xFFFF is reached, the flag bits T1IF and OVFIF are set.

        <2>  Modulo mode : The counter starts from 0x0000 and increases by 1 at each active clock edge. When the counter reaches the value stored in the T1CC0 register, it overflows, and the counter will start a new round of increment counting from 0x0000. The counting cycle of the modulo mode can be Set by the user.

        <3>  Counting up/down counting mode : The counter repeatedly starts from 0x0000, counts up to the final count value saved by TICC0, and then counts down back to 0x0000. When the final count value is reached, the flag bits T1IF and OVFIF are set.

 

3. CC2530's timer/counter interrupt system

        There are three situations in which the timer can generate an interrupt request:

        <1> The counter reaches the final count value (overflow or return to zero).

        <2> Enter the capture event.

        <3> Output comparison event (used in modulo mode).

Special attention should be paid to         using the modulus mode . It is necessary to turn on the output comparison mode of channel 0 , otherwise, after the value of the counter reaches T1CC0, an overflow interrupt will not be generated.

 

4. Training project: apply timer 1's analog mode to realize 1 second timing

[1] Set the maximum count value of timer 1

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

        Maximum count value = timing duration/timer count period.

        In this training, the system clock is 16MHz, the frequency division factor is 128, the timing is 0.1 seconds, and the maximum count value is:

        

[2] Timer initialization function design

        <1> Write the maximum count value of timer 1 to T1CC0.

        <2> Enable the output compare mode of timer 1 channel 0 through the T1CCTL0 register.

        <3> Set the relevant interrupt control bit of Timer 1.

        <4> Set the frequency division coefficient and working mode and start the timer.

[3] Timer interrupt service function design

       <1> Clear the interrupt flag bit of T1STAT.

       <2> Accumulate the global variable count.

       <3> The count is divisible by 10, that is, the timing of 1 second has arrived.

       <4> The count is reset to zero when the timer is 10 seconds.

[4] Source code of training project

#include "ioCC2530.h"

#define  LED5   P1_3
#define  LED6   P1_4
/*===============定时器1初始化函数==================*/
void Init_Timer1()
{
  T1CC0L = 0xd4;        //设置最大计数值的低8位
  T1CC0H = 0x30;        //设置最大计数值的高8位
  T1CCTL0 |= 0x04;      //开启通道0的输出比较模式
  T1IE = 1;             //使能定时器1中断
  T1OVFIM = 1;          //使能定时器1溢出中断
  EA = 1;               //使能总中断
  T1CTL = 0x0e;         //分频系数是128,模模式
}

unsigned char count = 0;  
/*================定时器1服务函数====================*/
#pragma vector = T1_VECTOR
__interrupt void Timer1_Sevice()
{
  T1STAT &= ~0x01;      //清除定时器1通道0中断标志
  count++;
  if(count%10 == 0)     //定时1秒到
  {
    LED5 = ~LED5;
  }
  if(count == 100)      //定时10秒到
  {
    LED6 = ~LED6;
    count = 0;
  }
}
/*=================端口初始化函数====================*/
void Init_Port()
{
  P1SEL &= ~0x18;  //将P1_3和P1_4设置为通用I/O端口
  P1DIR |= 0x18;   //将P1_3和P1_4的端口设置为输出
  LED5 = 0;
  LED6 = 0;
} 
/*===================主函数=========================*/
void main()
{
  Init_Port();
  Init_Timer1();
  while(1);
}

 

Guess you like

Origin blog.csdn.net/weixin_44643510/article/details/114440536