Internet of Things|Interrupt method for timer counter development|Timer interrupt processing function|Complete test code|Bluetooth 4.0 BLE basics of the Internet of Things-study notes (6)

11 Interrupt method for timer counter development

The LED control circuit is the same as the previous section:
insert image description here
The T3 timer (8 bits) of CC2530 needs to understand the T3GJL, T3CCTLO, T3CCO, T3CCTL1, T3CC registers. As shown in the table below:
insert image description here
Configure T3 according to the contents of the table register. Since the timer T3 is 8 bits, the configuration is slightly different.
T3CTL = 0x08 ;
enable overflow interrupt
T3IE = 1;
enable total interrupt and T3 interrupt
TRCTL = OxE0;
//128 frequency division, 128/16000000*N=0.5S, N=62500
T3CTL &=~Ox03;
automatic reload 00 one >Oxff 62500/255=245 (times)
T3CTL = Ox10;
!Start
EA= 1;
then turn on the total interrupt
Note:
(1) T3CTL &= ~0X03; This statement is understood as: 0X03=0000 0011, then~ 0X03=1111 1100; so after executing T3CTL &= ~0X03, other bits of T3CTL remain unchanged, and D0 and D1 become 0.
insert image description here

(2) Since it is divided by 128, it takes time t=128/16000000 to count once. If we want to get a delay of 0.5s, then 128/16000000*N=0.5S, we have to count N=65200 times. And we set Timer3 to automatically reload 0x00—0xFF, that is, generate an overflow interrupt counting 256 times, and now we want it to count 65200 times, so we need to accumulate 254 overflow interrupts to make LED1 reverse.
| is a bitwise operation, and the operation object is 1; & is also a bitwise operation, but the operation object is 0, and the ~ inversion operation.
insert image description here
insert image description here
insert image description here

Implementation code:

void T3init(void)
{
  T3CTL |= 0x08;    //0x08=1000 或操作1,仅操作第3位,不影响其他位使溢出中断可以,不操作其他位
  T3IE= 1;          //开总中断
  T3CTL |= 0xE0;   //0xE0=1110 0000 或操作1,仅操作7:5位,128分频 128/16000000*N=0.5s,N=62500
  T3CTL &= ~0x03;  //0x03=0000 0011,取反1111 1100,Free running, repeatedly count from Ox00 to OxFF

  //自动从0x00到0xff(255)重装,62500/255=245次,&操作0,1:0位置0,其他位不变
  T3CTL != 0x10;   //启动 0x10=0001 0000 或操作1,仅改变第4位,启动
  EA= 1;           //再开启一开总中断
}

Timer interrupt handler function:

Template:
#pragma vector = T3_VECTOR // Format: #pragma vector = interrupt vector followed by interrupt handler
__interrupt void T3_ISR(void)
{

}
This case:

#pragma vector = T3_VECTOR    //格式:#pragma vector = 中断向量,紧接着是中断处理程序
  __interrupt void T3_ISR(void)
 {
    
    
  IRCON = 0x00;//来中断就清0,防止误操作,方便下一次判断
  if(count ++ > 245) //0.5s时间
  {
    
    
    count = 0; //每次进入就计数清零
    LED1 = ~LED1;
  }
  /*Delay(10);            //去除抖动
  LED1=~LED1;             //改变LED1状态
  P0IFG = 0;             //清中断标志
  P0IF = 0;             //清中断标志
  */
 }

Main program body:

/*************************
*程序入口函数
*************************/
void main(void)
{
  ledinit();
  T3init();
  while(1)
  {
    //空循环即可
  };
};

Complete test code:

#include <ioCC2540.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define LED1 P1_0 //
uint count; //定时器计数
void ledinit(void)
{
   P1DIR |= 0x01;//输出模式
   LED1 =0;//上电默认输出0,即关灯
}
void T3init(void)
{
  T3CTL |= 0x08;    //0x08=1000 或操作1,仅操作第3位,不影响其他位使溢出中断可以,不操作其他位
  T3IE= 1;          //开总中断
  T3CTL |= 0xE0;   //0xE0=1110 0000 或操作1,仅操作7:5位,128分频 128/16000000*N=0.5s,N=62500
  T3CTL &= ~0x03;   //0x03=0000 0011,取反1111 1100,Free running, repeatedly count from Ox00 to OxFF
  //自动从0x00到0xff(255)重装,62500/255=245次,&操作0,1:0位置0,其他位不变
  T3CTL != 0x10;   //启动 0x10=0001 0000 或操作1,仅改变第4位,启动
  EA= 1;           //再开启一开总中断
}
/****************************
      定时器中断处理函数
*****************************/
#pragma vector = T3_VECTOR    //格式:#pragma vector = 中断向量,紧接着是中断处理程序
  __interrupt void T3_ISR(void)
 {
  IRCON = 0x00;//来中断就清0,防止误操作,方便下一次判断
  if(count ++ > 245) //0.5s时间
  {
    count = 0; //每次进入就计数清零
    LED1 = ~LED1;
  }
  /*Delay(10);            //去除抖动
  LED1=~LED1;             //改变LED1状态
  P0IFG = 0;             //清中断标志
  P0IF = 0;             //清中断标志
  */
 }
/*************************
*程序入口函数
*************************/
void main(void)
{
  ledinit();
  T3init();
  while(1)
  {
    //空循环即可
  };
};

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/130640937