This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530 count the pulse of encoders

Other Parts Discussed in Thread: CC2530

Hello,

I use CC2530 to control the motors of a wheeled robot. Now I want to count the pulses of the encoders. I modified the pins of keys in the project of SampleApp.eww.  The pins of my encoders are P1.2 and P1.3. The numbers are confused. would you please help me to find the mistakes?

The following are revised and modified codes:

1.  hal_board_cfg.h

/* P1.2 */

#define PUSH1_BV          BV(2)

#define PUSH1_SBIT        P1_2

#define PUSH1_POLARITY    ACTIVE_HIGH

 

/* P1.3*/

#define PUSH7_BV          BV(3)////P1.3

#define PUSH7_SBIT        P1_3

#define PUSH7_POLARITY    ACTIVE_HIGH

 

#define HAL_PUSH_BUTTON1()        (PUSH1_POLARITY (PUSH1_SBIT))

#define HAL_PUSH_BUTTON7()        (PUSH7_POLARITY (PUSH7_SBIT))

 

2. hal_key.c

(1) #define HAL_KEY_CPU_PORT_1_IF P1IF//// P1 interrupt

(2)

 /* SW_6 is at P1.2 */

#define HAL_KEY_SW_6_PORT   P1

#define HAL_KEY_SW_6_BIT    BV(2)

#define HAL_KEY_SW_6_SEL    P1SEL

#define HAL_KEY_SW_6_DIR    P1DIR

 

/* edge interrupt */

#define HAL_KEY_SW_6_EDGEBIT  BV(1)// PICTL,BV(0)P0,BV(1)P1 0-3; BV(2)P1 4-7;

#define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE

 

/* SW_6 interrupts */

#define HAL_KEY_SW_6_IEN      IEN2  /* CPU interrupt mask register */

#define HAL_KEY_SW_6_IENBIT   BV(4) /* Mask bit for all of Port_0 P0 BV(5) P1口为BV(4)*/

#define HAL_KEY_SW_6_ICTL     P1IEN /* Port Interrupt Control register */

#define HAL_KEY_SW_6_ICTLBIT  BV(2) /* P1IEN – P1.2 enable/disable bit */

#define HAL_KEY_SW_6_PXIFG    P1IFG /* Interrupt flag at source */

 

/* SW_7 is at P1.3 */

#define HAL_KEY_SW_7_PORT   P1

#define HAL_KEY_SW_7_BIT    BV(3)//

#define HAL_KEY_SW_7_SEL    P1SEL

#define HAL_KEY_SW_7_DIR    P1DIR

 

/* edge interrupt */

#define HAL_KEY_SW_7_EDGEBIT  BV(1)//PICTL,BV(0)P0,BV(1)P1 0-3; BV(2)P1  4-7;

#define HAL_KEY_SW_7_EDGE     HAL_KEY_FALLING_EDGE

 

/* SW_7 interrupts */

#define HAL_KEY_SW_7_IEN      IEN2  /* CPU interrupt mask register */

#define HAL_KEY_SW_7_IENBIT   BV(4) /* Mask bit for all of Port_1 P1 BV(4)*/

#define HAL_KEY_SW_7_ICTL     P1IEN /* Port Interrupt Control register */

#define HAL_KEY_SW_7_ICTLBIT  BV(3) /* P1IEN - P1.3 enable/disable bit */

#define HAL_KEY_SW_7_PXIFG    P1IFG /* Interrupt flag at source */

 

(3) void HalKeyInit( void )

 

add:

  HAL_KEY_SW_7_SEL &= ~(HAL_KEY_SW_7_BIT);    /* Set pin function to GPIO */

  HAL_KEY_SW_7_DIR &= ~(HAL_KEY_SW_7_BIT);    /* Set pin direction to Input */

 

(4) void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)

 

add:

 

    PICTL &= ~(HAL_KEY_SW_7_EDGEBIT);    /* Clear the edge bit */

    /* For falling edge, the bit must be set. */

  #if (HAL_KEY_SW_7_EDGE == HAL_KEY_FALLING_EDGE)

    PICTL |= HAL_KEY_SW_7_EDGEBIT;

  #endif

 

    HAL_KEY_SW_7_ICTL |= HAL_KEY_SW_7_ICTLBIT;

    HAL_KEY_SW_7_IEN |= HAL_KEY_SW_7_IENBIT;

HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT);

 

(4) void HalKeyPoll (void)

 

  if (HAL_PUSH_BUTTON1())

  {

    keys=0;

    keys |= HAL_KEY_SW_6;

  }

 

  if (HAL_PUSH_BUTTON7())

  {

    keys=0;   

    keys |= HAL_KEY_SW_7;

  } 

 

  if (HAL_PUSH_BUTTON1() && HAL_PUSH_BUTTON7())////together

  {

    keys=0;   

    keys |= HAL_KEY_SW_6;

    keys |= HAL_KEY_SW_7;   

  }   

 

(5) void halProcessKeyInterrupt (void)

add:

 

  if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)  /* Interrupt Flag has been set */

  {

    HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT); /* Clear Interrupt Flag */

    valid = TRUE;

  }

 

(6) Port1 ISR

HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )

{

  HAL_ENTER_ISR();

   if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)

  {

    halProcessKeyInterrupt();

  }

 

  if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)

  {

    halProcessKeyInterrupt();

  }

 

  /*

    Clear the CPU interrupt flag for Port_0

    PxIFG has to be cleared before PxIF

  */

  HAL_KEY_SW_6_PXIFG = 0; 

  HAL_KEY_SW_7_PXIFG = 0;

  HAL_KEY_CPU_PORT_1_IF = 0;

 

  CLEAR_SLEEP_MODE();

  HAL_EXIT_ISR();

}

 

3. SampleApp.c

 

 

void SampleApp_HandleKeys( uint8 shift, uint8 keys )

 

add:

 

  if(keys==HAL_KEY_SW_6///////////////

  {

    count1++;

    keys=0;

  } 

  if(keys==HAL_KEY_SW_7)

  {

    count2++;

    keys=0;   

  }

  if(keys==0x60)////

  {

    count1++;

    count2++;

  }