[CC2530 intensive training 04] timer interval timing to achieve N-touch

[Title requirements]

2018 national vocational college skills competition "Internet of things technology application" national competition (higher vocational group) about the difficulty of the development of perception layer increased abruptly, three topics were completed under Zigbee protocol stack. The first question examined the key click, double click and triple click. In order to let you better grasp the complex processing ideas of keys, this paper, through a specific training case, describes the basic idea of realizing N-touch by interval timing.

On the black Zigbee module of the new world international competition equipment, or XMF09B and XMF09C made by little bee, click the key SW1 to switch the switch state of D5 lamp; double click the key SW1 to switch the switch state of D6 lamp; triple click the key SW1 to switch the switch state of D3 lamp; and quadruple click the key SW1 to switch the switch state of D4 lamp.

Press SW1 -------- P1 ﹣ 2

D5 light ------------ P1 ﹣ 3 (high level on)

D6 light ------------ P1 ﹐ 4 (high level on)

D3 lamp ------------ p1_ (high level on)

D4 lamp ------------ P1 ﹣ 1 (high level on)

[implementation ideas]

Each key press defines a life cycle. If it is 0.5 seconds, the final state of the key will be determined at the end of the life cycle. If a new key is pressed in the life cycle of a key, the life cycle will be recalculated, which is double-click. In the double-click life cycle, if a new key is pressed, the life cycle will be recalculated. This is the triple hit. If there is no new key press in the whole life cycle, the final key state is triple hit. And so on.

[implementation code]

#include "ioCC2530.h"

#define D3  P1_0
#define D4  P1_1
#define D5  P1_3
#define D6  P1_4
#define SW1 P1_2
#define SW2 P0_1

unsigned char count_t = 0;
unsigned char K_Num = 0;

/*=======================Simple delay function========================*/
void Delay(unsigned int t)
{
  while(t--);
}

/*======================Port initialization function========================*/
void Init_Port()
{
  P1SEL &= ~0x1b;     //P1 ﹣ 0, P1 ﹣ 1, P1 ﹣ 3, and P1 ﹣ 4 are common I/O ports
  P1DIR |= 0x1b;      //P1? 0, P1? 1, P1? 3 and P1? 4 port output
  
  P1SEL &= ~0x04;     //P1 ﹣ 2 as general I/O port
  P1DIR &= ~0x04;     //P1? 2 port input
  P1INP &= ~0x04;     //P1 ﹣ 2 set to pull up / pull down mode
  P2INP &= ~0x40;     //P1 ﹤ 2 is set to pull up
}

/*=======================Timer 1 initialization========================*/
void Init_Timer1()
{
  T1CC0L = 0xd4;      
  T1CC0H = 0x30;        //16MHz clock, 128 minutes, timing 0.1s
  T1CCTL0 |= 0x04;      //Turn on the output comparison mode of channel 0
  T1IE = 1;
  EA = 1;
  T1CTL = 0x0e;         //The division coefficient is 128, mode mode
}

/*====================Timer 1 service function========================*/
#pragma vector = T1_VECTOR
__interrupt void Timer1_int()
{
  T1STAT &= ~0x20;    //Clear overflow interrupt flag bit of timer 1
  if(K_Num != 0 && SW1 != 0)      
  {
    count_t++;        //Timer 1 overflows once plus 1, overflowing period is 0.1S
  }
}

/*====================Key scan processing function========================*/
void Scan_Keys()
{
  if(SW1 == 0)
  {
    Delay(100);         //Jitter removal
    if(SW1 == 0)
    {
      while(SW1 == 0);  //Wait for the key to release
      count_t = 0;      //Every time a key is pressed, the timing starts again
      K_Num++;          //Change key status
      if(K_Num > 4)     //Four combos above four combos
      {
        K_Num = 4;
      }
    }
  }
  if(count_t > 5)       //Key interval to
  {    
    switch(K_Num)
    {
      case 1:           //Key click
        D5 = ~D5;
      break;
      case 2:           //Key double click
        D6 = ~D6;
      break;
      case 3:           //Three consecutive keystrokes
        D3 = ~D3;
      break;
      case 4:           //Four consecutive keystrokes
        D4 = ~D4;
      break;
    }
    K_Num = 0;     //Press the key once after processing, and the status will be cleared
    count_t = 0;   //Zero timer
  }
}

/*=========================Main function=============================*/
void main()
{
  Init_Port();
  Init_Timer1();
  D3 = 0;
  D4 = 0;
  D5 = 0;
  D6 = 0;
  while(1)
  {
    Scan_Keys();
  }
}

[Guangdong Vocational and technical college, Ou Haoyuan, little bee teacher, ohy3683@qq.com]

Posted on Wed, 04 Dec 2019 03:49:41 -0800 by foo