CC2530 bare metal programming series notes 1--Timer Timer1 mode programming

Publisher:MengyunLatest update time:2019-09-04 Source: eefocusKeywords:cc2530 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timer/counter is one of the important resources of single-chip microcomputer, and the timer/counter of CC2530 has more working modes compared with the ordinary 51 series single-chip microcomputer. According to the study manual, CC2530 timer/counter has three working modes, namely free mode, modular mode and up/down counting mode. No matter which mode, the timer can be used in two ways: query and interrupt. This topic describes the programming method of CC2530 timer/counter in modular mode.


First, we use the query method to use the Timer1 timer. When using the Timer timer, we must first initialize the timer. The code is as follows:


void INIT_Timer1()

  T1CTL = 0x00; //1 division, stop running

  T1CTL = 0x0e; //128 frequency division mode

  T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode

  T1CC0L = 0x24;

  T1CC0H = 0xF4;

  IRCON &= ~0x02;

}


It is easy to see that when using Timer1, we need to use five registers: T1CTL, T1CCTL0, T1CC0L, T1CC0H and IRCON. T1CTL is a control register, which is used to ① set the divider division value such as f/8 or f/128, where f is the mark frequency. ② Select the three modes of the timer. The two registers T1CC0L and T1CC0H store the final value of the count (when the count reaches this value, the interrupt flag is set, or an interrupt is generated). IRCON is the interrupt flag. The reason why the code is written

IRCON &= ~0x02;

This is to clear the interrupt flag corresponding to Timer1 first.

T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode


So what is the significance of the assignment operation to T1CCTL0 in this line of code?


The modulo mode needs to enable the output compare mode of channel 0, otherwise the counter will only generate an overflow interrupt (corresponding overflow flag) when it reaches 0XFF. That is, if the output compare mode of channel 0 is not set, the counter value will not generate an overflow interrupt (corresponding overflow flag will not be set to 1) after reaching T1CC0. This needs special attention. The code of the entire program is posted below:



#include

#define LED1 P1_0 //Define LED1 as P1.0  

 

int count = 0;

 

void INIT_LED(void)

{

  P1SEL &= ~0x01; //Set P1.0 to normal I/O function

  P1DIR |= 0x01; //Set P1.0 as output direction

  LED1 = 0; //Turn on LED1

}

 

void INIT_Timer1()

  T1CTL = 0x00; //1 division, stop running

  T1CTL = 0x0e; //128 frequency division mode

  T1CCTL0 |= 0x04; //Set timer1 channel 0 output comparison mode

  T1CC0L = 0x24;

  T1CC0H = 0xF4;

  IRCON &= ~0x02;

}

 

void main( void )

{

  INIT_LED();

  INIT_Timer1();

  LED1 = 1 ;

  //int count = 0;

  while(1)

  {

    if(IRCON &= 0x02)    

    {

      count++;

      if(count==1)

      {

         IRCON &= ~0x02 ;

         count = 0;

         LED1 = !LED1;

      }

    }

  }

}

      The reason why the program is written in the query mode is that the following code is


if(IRCON &= 0x02)    

    {

      count++;

      if(count==1)

      {

         IRCON &= ~0x02 ;

         count = 0;

         LED1 = !LED1;

      }

    }

      In the main program, the interrupt flag is constantly queried. If the interrupt flag is set, it needs to be cleared to 0 by software.

Keywords:cc2530 Reference address:CC2530 bare metal programming series notes 1--Timer Timer1 mode programming

Previous article:CC2530 bare metal programming series notes 4--use of GPIO port
Next article:51 MCU Series Knowledge 6--Interrupt System (1)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号