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.

ez430-rf2500 reading the ports using enabled pull up resistors mode

Hi everyone

I am trying to read buttons from "ez430-rf2500" using pull up mode, however I could not read the button correctly, all the time it shows that the button is pressed but it is not pressed. can anyone help me? the program is shown below... I highlighted the main lines.

#define I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE
#include "bsp.h"
#include "mrfi.h"
#include "nwk_types.h"
#include "nwk_api.h"
#include "bsp_leds.h"
#include "bsp_buttons.h"
#include "vlo_rand.h"

#ifndef APP_AUTO_ACK
#error ERROR: Must define the macro APP_AUTO_ACK for this application.
#endif

void toggleLED(uint8_t);

static void linkTo(void);

static linkID_t sLinkID1 = 0;

#define SPIN_ABOUT_A_SECOND   NWK_DELAY(1000)
#define SPIN_ABOUT_A_QUARTER_SECOND   NWK_DELAY(250)

/* How many times to try a Tx and miss an acknowledge before doing a scan */
#define MISSES_IN_A_ROW  2

void createRandomAddress(void);

volatile int * tempOffset = (int *)0x10F4; // Temperature offset set at production
char * Flash_Addr = (char *)0x10F0;        // Initialize radio address location

__interrupt void ADC10_ISR(void);
__interrupt void Timer_A (void);

/* work loop semaphores */
static volatile uint8_t sSelfMeasureSem = 0;

void main (void)
{
      addr_t lAddr;

 //WDTCTL = WDTHOLD | WDTPW;
  P2SEL &=0x1F;
  P2DIR &= 0xE0;
  //P2REN|=0x1F;
  P2OUT|=0x1F;  
 
 
 
  P4SEL&=0x38;
  P4DIR &= 0xC7;
  //P4REN|=0x38;
  P4OUT|=0x38;
 
   

 
  BSP_Init();
 
 
 

  if(Flash_Addr[0] == 0xFF && Flash_Addr[1] == 0xFF &&
     Flash_Addr[2] == 0xFF && Flash_Addr[3] == 0xFF )
  {
    createRandomAddress(); // set Random device address at initial startup
  }
  lAddr.addr[0] = Flash_Addr[0];
  lAddr.addr[1] = Flash_Addr[1];
  lAddr.addr[2] = Flash_Addr[2];
  lAddr.addr[3] = Flash_Addr[3];
  SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);

  /* Keep trying to join (a side effect of successful initialization) until
   * successful. Toggle LEDS to indicate that joining has not occurred.
   */
  while (SMPL_SUCCESS != SMPL_Init(0))
  {
    toggleLED(1);
    toggleLED(2);
    SPIN_ABOUT_A_SECOND;
  }

  /* LEDs on solid to indicate successful join. */
  if (!BSP_LED2_IS_ON())
  {
    toggleLED(2);
  }
  if (!BSP_LED1_IS_ON())
  {
    toggleLED(1);
  }

  BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
  TACCTL0 = CCIE;                           // TACCR0 interrupt enabled
  TACCR0 = 12000;                           // ~ 1 sec
  TACTL = TASSEL_1 + MC_1;                  // ACLK, upmode  
 
  /* Unconditional link to AP which is listening due to successful join. */
  linkTo();

  while (1) ;
}

static void linkTo()
{
    /*
In order to send the data we need to use the foll      
      
      
      
     */
    uint8_t poi=0;//pointer
    uint8_t pp=0;// check state
    
  uint8_t     msg[1];
  uint8_t     misses, done;
 int results[5];      
      uint8_t      noAck;
      smplStatus_t rc;
        
    
while(1)
{

    if (!(P2IN & 0x00))
      {
          poi=0;
          pp=1;
          
      }
      else if (!(P2IN & 0x01))
      {
          poi=1;
          pp=1;
      }
      
      
      if (pp)
      {
 


  /* Keep trying to link... */
  while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
  {
    toggleLED(1);
    toggleLED(2);
    SPIN_ABOUT_A_SECOND;
  }

  /* Turn off LEDs. */
  if (BSP_LED2_IS_ON())
  {
    toggleLED(2);
  }
  if (BSP_LED1_IS_ON())
  {
    toggleLED(1);
  }

  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);

 
/* orginl PROGRAMM
  while (1)
  {
 
    __bis_SR_register(LPM3_bits+GIE);  // LPM3 with interrupts enabled    

    if (sSelfMeasureSem) {
      volatile long temp;
      int degC, volt;
      int results[5];      
      uint8_t      noAck;
      smplStatus_t rc;*/
    
      

      /* get radio ready...awakens in idle state */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);

        results[0]=0x5A;
      results[1]=0x5B;
      results[2]=0x46;
      results[3]=0x47;
      results[4]=0x28;
    
      msg[0]=results[poi];
 
      done = 0;
      while (!done)
      {
        noAck = 0;

        /* Try sending message MISSES_IN_A_ROW times looking for ack */
        for (misses=0; misses < MISSES_IN_A_ROW; ++misses)
        {
          if (SMPL_SUCCESS == (rc=SMPL_SendOpt(sLinkID1, msg, sizeof(msg), SMPL_TXOPTION_ACKREQ)))
          {
            /* Message acked. We're done. Toggle LED 1 to indicate ack received. */
            toggleLED(1);  // Toggle On LED1
            __delay_cycles(2000);
            toggleLED(1);
            break;
          }
          if (SMPL_NO_ACK == rc)
          {
            /* Count ack failures. Could also fail becuase of CCA and
             * we don't want to scan in this case.
             */
            noAck++;
          }
        }
        if (MISSES_IN_A_ROW == noAck)
        {
          /* Message not acked. Toggle LED 2. */
          toggleLED(2);  // Turn On LED2
          __delay_cycles(2000);
          toggleLED(2);
#ifdef FREQUENCY_AGILITY
          /* Assume we're on the wrong channel so look for channel by
           * using the Ping to initiate a scan when it gets no reply. With
           * a successful ping try sending the message again. Otherwise,
           * for any error we get we will wait until the next button
           * press to try again.
           */
          if (SMPL_SUCCESS != SMPL_Ping(sLinkID1))
          {
            done = 1;
          }
#else
          done = 1;
#endif  /* FREQUENCY_AGILITY */
        }
        else
        {
          /* Got the ack or we don't care. We're done. */
          done = 1;
        }
      }

      /* radio back to sleep */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);
    }
    
 // } this for the while(1)
 
 // }
  pp=0;
}
}


void toggleLED(uint8_t which)
{
  if (1 == which)
  {
    BSP_TOGGLE_LED1();
  }
  else if (2 == which)
  {
    BSP_TOGGLE_LED2();
  }
  return;
}

void createRandomAddress()
{
  unsigned int rand, rand2;
  do
  {
    rand = TI_getRandomIntegerFromVLO();    // first byte can not be 0x00 of 0xFF
  }
  while( (rand & 0xFF00)==0xFF00 || (rand & 0xFF00)==0x0000 );
  rand2 = TI_getRandomIntegerFromVLO();
 
  BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1MHz
  DCOCTL = CALDCO_1MHZ;
  FCTL2 = FWKEY + FSSEL0 + FN1;             // MCLK/3 for Flash Timing Generator
  FCTL3 = FWKEY + LOCKA;                    // Clear LOCK & LOCKA bits
  FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation
 
  Flash_Addr[0]=(rand>>8) & 0xFF;
  Flash_Addr[1]=rand & 0xFF;
  Flash_Addr[2]=(rand2>>8) & 0xFF;
  Flash_Addr[3]=rand2 & 0xFF;
 
  FCTL1 = FWKEY;                            // Clear WRT bit
  FCTL3 = FWKEY + LOCKA + LOCK;             // Set LOCK & LOCKA bit
}

/*------------------------------------------------------------------------------
* ADC10 interrupt service routine
------------------------------------------------------------------------------*/
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}

/*------------------------------------------------------------------------------
* Timer A0 interrupt service routine
------------------------------------------------------------------------------*/
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
  sSelfMeasureSem = 1;  
  __bic_SR_register_on_exit(LPM3_bits);        // Clear LPM3 bit from 0(SR)
}

  • Ahmed Maan said:
      P2SEL &=0x1F;
      P2DIR &= 0xE0;
      //P2REN|=0x1F;
      P2OUT|=0x1F;

    Hmmm, apparently you don't correctly understand how bit operations work.

    P2SEL &= 0x1f clears bit 5..7 of P2SEL, switching P2.5 to P2.7 to GPIO mode, leaving P2.0 to P2.4 in whatever state they are. Which is 0 by default anyway.
    P2DIR &=0xE0 clears bit 1..4 of P2DIR, switching P2.0 to P2.4 to input mode, leaving P2.5 to P2.7 in whatever direction they are. Which is input mode by default anyway.
    So the first two lines don't do anything. Luckily, because if they would be needed, you'd require more than these to handle the bits they don't touch.

    The commented lin would enable itnernal resistors on P2.0 to P2.4, but doesn't since it is commented out.

    The last line would switch the internal resistors of P2.0 to P2.4 - if they were enabled - to pull-up direction.

    So all you need to prepare P2.0 to P2.4 for pushbutton operation with internal pullups would be the last two lines.

    Ahmed Maan said:
        if (!(P2IN & 0x00))
          {
              poi=0;
              pp=1;
              
          }
          else if (!(P2IN & 0x01))
          {
              poi=1;
              pp=1;
          }

    P2IN&0x00 always gives 0. "&" operator returns all bits that are set in both operands. Since the second operand doesn't have any bit set, the result is always zero.

    You didn't write on which port pin you have your pushbutton. So I can't tell you how the correct check would look like.

  • Thank you, you are right.

    I was trying to configure the port, I thought I wrote the condition of if statement in the right way. however, the mistake is, I wanted the bit number 0 so I did and with zero, then I found that mistake, because I need to make AND operation with one.

    P2SEL &=0x0;
      P2DIR &= 0xE0;
      P2REN|=0x1F;
      P2OUT|=0x1F;  
     
      P4SEL&=0x0;
      P4DIR &= 0xC7;
      P4REN|=0x38;
      P4OUT|=0x38;

    I solved the problem and everything is working correctly

    thank you

**Attention** This is a public forum