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.

关于rtc中断的问题

/*******************************************************************************
 *
 *  MspExp430F5137RxUserExperience.c - Main User Experience Application
 *
 *  Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/
/*----------------------------------------------------------------------------
 *  MSP-EXP430F5137Rx User Epxerience Code
 *	End device, connects to Access Point MSP-EXP430F6137Rx in a star network
 *  topology.
 *  Transmits device's analog data (Vcc, Temp) as well as button presses to the
 *  AP periodically.
 *
 *  Version 1.0                       D. Dang
 *
 *  Texas Instruments, Inc.
 *  Copyright 2011 Texas Instruments Incorporated. All rights reserved.
 *
 *---------------------------------------------------------------------------- */



#include "bsp.h"
#include "mrfi.h"
#include "nwk_types.h"
#include "nwk_api.h"
#include "bsp_leds.h"
#include "bsp_buttons.h"


#include "hal_leds.h"
#include "hal_buttons.h"
#include "hal_adc.h"

#define MESSAGE_BUTTON_PRESSED      0xFF
#define MESSAGE_HEART_BEAT          0xEE


#define USER_EXPERIENCE_FIRMWARE_MODE              0x00
#define USER_EXPERIENCE_PRODUCTION_TEST_MODE       0xFF


/*-----------------------------------------------------------------------------
 *  
 * 
 *  Packet construct:
 *  Byte 0: Message Type      [HeartBeat            or        ButtonPressed]
 *  Byte 1:   Content            Vcc                     MESSAGE_BUTTON_PRESSED 
 *  Byte 2:   Content            Temp_MSB                MESSAGE_BUTTON_PRESSED
 *  Byte 3:   Content            Temp_LSB                MESSAGE_BUTTON_PRESSED
 *  Byte 4:   PacketID MSB 
 *  Byte 5:   PacketID LSB
 *  
 
 *---------------------------------------------------------------------------- */

unsigned char FIRMWARE_MODE = 0xFF;

extern volatile unsigned char  buttonsPressed;
static void linkTo(void);



static uint16_t sTid = 0;

/* Callback handler */
static uint8_t sCB(linkID_t);

static volatile uint8_t  sPeerFrameSem = 0;
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 SetupRtc(void);
void MspExp430F5137RxUserExperience (void)
{
  addr_t lAddr;    //adress

  BSP_Init();                  //initialize
//

  HalAdcInit();
  HalAdcSetQuitFromISR( 1 );
  SetupRtc(); //SetupRtc();
      //P1DIR |= BIT0; //����P1.0�����
      P1DIR=0XFF;
      P1OUT=0;
      P2DIR=0XFF;
      P2OUT=0;
      P3DIR=0XFF;
      P3OUT=0;
      PJDIR=0XFF;
      PJOUT=0;

      P1REN |= BIT7;//����P1.3�ڲ�����������
      P1OUT |= BIT7;//��������������
      P1DIR &= ~(BIT7); // P1.3������
      P1IES |= BIT7;// P1.3��Ϊ�½����ж�
      P1IE  |= BIT7 ;// ����P1.3�ж�

      P1IFG &= ~BIT7;        //�жϱ�־λ����
       _EINT();

  // Create a unique address based off the device's wafer ID &
  // X/Y coordinates

  lAddr.addr[0] = (*(uint8_t *)0x01A0E) ^ (*(uint8_t *)0x01A0A);
  lAddr.addr[1] = (*(uint8_t *)0x01A0F) ^ (*(uint8_t *)0x01A0B);
  lAddr.addr[2] = (*(uint8_t *)0x01A10) ^ (*(uint8_t *)0x01A0C);
  lAddr.addr[3] = (*(uint8_t *)0x01A11) ^ (*(uint8_t *)0x01A0D);

  SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);   //generate an address



  /* 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(sCB))//join   //wait until smpl_init reture smpl_success,
                                           //can this change to interrupt way???
  {

    SPIN_ABOUT_A_SECOND;
  }

  /* LEDs on solid to indicate successful join. */


  /* Unconditional link to AP which is listening due to successful join. */
  linkTo();

  while (1) ;
}



static void linkTo() //linking
{
  uint8_t     msg[6];         //create a group to save message
  int         temperature;
  uint8_t      misses, done;

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



  /* sleep until button press... */
  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);

  RTCPS1CTL |= RT1PSIE;

  while (1)
  {
    /* Send a message when either button pressed */
    __bis_SR_register(LPM3_bits + GIE);
    __no_operation();

    //if (buttonsPressed & 0x80)  //   writing message
    //{
      //msg[0] = MESSAGE_BUTTON_PRESSED; //0xff
      //msg[1] = MESSAGE_BUTTON_PRESSED;
      //msg[2] = MESSAGE_BUTTON_PRESSED;
      //msg[3] = MESSAGE_BUTTON_PRESSED;
      //buttonsPressed  = 0;             //set it back to 0
    //}
    //else    // half a second heart beat
    //{
            HalAdcStartRead(); //quit lmp3 after done
            __bis_SR_register(LPM0_bits + GIE);
            HalAdcConvertTempVcc();
            msg[0] = MESSAGE_HEART_BEAT;
            msg[1] = HalAdcGetVcc();
            temperature = HalAdcGetTempDegF();
            msg[2] =  *( (unsigned char*)(&temperature) ) ;
            msg[3] = *((unsigned char*)(&temperature)+1)     ;
      //  }
    sTid++;
    msg[4] = (unsigned char) (sTid>>8);
    msg[5] = (unsigned char) (sTid & 0xFF);

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

    /* Set TID and designate which LED to toggle */
    done = 0;
    while (!done)
    {
      for (misses=0; misses < MISSES_IN_A_ROW; ++misses)
        if (SMPL_SUCCESS == SMPL_Send(sLinkID1, msg, sizeof(msg))) //send message
          break;
      if (misses == MISSES_IN_A_ROW)
      {
        /* This can only happen if we are supporting Frequency Agility and we
         * appear not to have received an acknowledge. Do a scan.
         */
        ioctlScanChan_t scan;
        freqEntry_t     freq[NWK_FREQ_TBL_SIZE];

        scan.freq = freq;
        SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SCAN, &scan);
        /* If we now know the channel (number == 1) change to it. In any case
         * try it all again. If we changed channels we should get an ack now.
         */
        if (1 == scan.numChan)
        {
          SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SET, freq);
        }
      }
      else
      {
        /* Got the ack. We're done. */
        done = 1;
      }
    }

    /* radio back to sleep */
    SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);  //special void, manage the i/o

  }
}


static uint8_t sCB(linkID_t lid)
{
  if (lid == sLinkID1)
  {
    sPeerFrameSem++;
    return 0;
  }

  return 1;
}



/**********************************************************************//**
 * @brief  Initializes the RTC module for interval mode
 *
 *
 *
 * @param  none
 *
 * @return none
 *
 *  void SetupRtc(void)
{
  RTCCTL01 = RTCMODE + RTCBCD + RTCHOLD + RTCTEV_1;



  RTCCTL01 &= ~RTCHOLD;

  RTCPS1CTL = RT1IP_7;              // Interrupt freq: .5Hz
//  RTCPS1CTL = RT1IP_6;              // Interrupt freq: 1Hz
//  RTCPS1CTL = RT1IP_5;              // Interrupt freq: 2Hz
 //RTCPS1CTL = RT1IP_4;              // Interrupt freq: 4Hz



  //RTCPS0CTL = RT0IP_7;                      // Interrupt freq: 128hz

  //RTCCTL0 |= RTCTEVIE;           // Enable interrupt
}

#pragma vector=RTC_VECTOR
__interrupt void RTC_ISR(void)
{
  switch (RTCIV)
  {
    default:
      __bic_SR_register_on_exit(LPM3_bits);
  }
}
 *************************************************************************/
void SetupRtc(void)
{
 RTCCTL01 = RTCMODE + RTCBCD + RTCHOLD + RTCTEV_1;



 RTCCTL01 &= ~RTCHOLD;

 RTCPS1CTL = RT1IP_7;              // Interrupt freq: .5Hz
//  RTCPS1CTL = RT1IP_6;              // Interrupt freq: 1Hz
//  RTCPS1CTL = RT1IP_5;              // Interrupt freq: 2Hz
//RTCPS1CTL = RT1IP_4;              // Interrupt freq: 4Hz



 //RTCPS0CTL = RT0IP_7;                      // Interrupt freq: 128hz

 RTCCTL0 |= RTCTEVIE;           // Enable interrupt
}

#pragma vector=RTC_VECTOR
__interrupt void RTC_ISR(void)
{
    P1OUT ^= BIT0;
     __bic_SR_register_on_exit(LPM3_bits);
 }


/*�жϷ������*/
#pragma vector = PORT1_VECTOR
__interrupt void PORT_1(void)
{
    __delay_cycles(1000);//����
    if((P1IN & BIT7) == 0){
        __bic_SR_register_on_exit(LPM3_bits);
    }
    P1IFG &= ~BIT7; //�жϱ�־λ����
}
 我的板子是msp-expcc430rf4, 在研究历程是发现如图这段程序, 这是个实时时钟用作定时器? 我尝试在中断程序中加入led灯反转: P1OUT ^=BIT0; 但是运行后并无反应。 请问这不是定时器吗? 我也有尝试将//RTCCTL0 |= RTCTEVIE;前的//删去 但是没用。

  • 请问您现在使用的是哪个例程?
  • 没找到你的这个例程。方便说下例程的文件名吗?或者上传下这个例程的文件

    从你现在提供的代码看,是没使能RTC的中断。

  • 已上传例程.c 谢谢
  • 已经可以了。 代码如下。
    void SetupRtc(void)
    {
    RTCCTL01 = RTCMODE + RTCBCD + RTCHOLD + RTCTEV_1;

    RTCCTL01 &= ~RTCHOLD;

    RTCPS1CTL = RT1IP_7; // Interrupt freq: .5Hz
    // RTCPS1CTL = RT1IP_6; // Interrupt freq: 1Hz
    // RTCPS1CTL = RT1IP_5; // Interrupt freq: 2Hz
    //RTCPS1CTL = RT1IP_4; // Interrupt freq: 4Hz



    //RTCPS0CTL = RT0IP_7; // Interrupt freq: 128hz

    RTCCTL0 |= RTCTEVIE; // Enable interrupt
    }

    #pragma vector=RTC_VECTOR
    __interrupt void RTC_ISR(void)
    {
    switch (RTCIV)
    {
    default:
    __bic_SR_register_on_exit(LPM3_bits);
    }
    P1OUT ^= BIT0;
    }。
  • 但是有一个疑问。 我尝试将这段代码中的enable interrupt语句 //注释掉,led灯一样可以闪。这是为何? 其他几个语句似乎都没有中断允许的。
  • 很高兴您能解决问题

    您现在使用的是5137的板子?

    我们是有一个RTC的例程的,也是反转P1.0,您可以参考一下

    /* --COPYRIGHT--,BSD_EX
     * Copyright (c) 2012, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     *******************************************************************************
     * 
     *                       MSP430 CODE EXAMPLE DISCLAIMER
     *
     * MSP430 code examples are self-contained low-level programs that typically
     * demonstrate a single peripheral function or device feature in a highly
     * concise manner. For this the code may rely on the device's power-on default
     * register values and settings such as the clock configuration and care must
     * be taken when combining code from several examples to avoid potential side
     * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
     * for an API functional library-approach to peripheral configuration.
     *
     * --/COPYRIGHT--*/
    //******************************************************************************
    //  CC430F513x Demo - RTC in Counter Mode toggles P1.0 every 1s
    //
    //  This program demonstrates RTC in counter mode configured to source from ACLK
    //  to toggle P1.0 LED every 1s.
    //
    //                CC430F5137
    //             -----------------
    //        /|\ |                 |
    //         |  |                 |
    //         ---|RST              |
    //            |                 |
    //            |             P1.0|-->LED
    //
    //   M Morales
    //   Texas Instruments Inc.
    //   April 2009
    //   Built with CCE Version: 3.2.2 and IAR Embedded Workbench Version: 4.11B
    //******************************************************************************
    
    #include <msp430.h>
    
    int main(void)
    {
      WDTCTL = WDTPW+WDTHOLD;
    
      P1OUT |= 0x01;
      P1DIR |= 0x01;
    
      // Setup RTC Timer
      RTCCTL01 = RTCTEVIE + RTCSSEL_2 + RTCTEV_0; // Counter Mode, RTC1PS, 8-bit ovf
                                                // overflow interrupt enable
      RTCPS0CTL = RT0PSDIV_2;                   // ACLK, /8, start timer
      RTCPS1CTL = RT1SSEL_2 + RT1PSDIV_3;       // out from RT0PS, /16, start timer
    
      __bis_SR_register(LPM3_bits + GIE);
    }
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=RTC_VECTOR
    __interrupt void RTC_ISR(void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(RTC_VECTOR))) RTC_ISR (void)
    #else
    #error Compiler not supported!
    #endif
    {
      switch(__even_in_range(RTCIV,16))
      {
        case 0: break;                          // No interrupts
        case 2: break;                          // RTCRDYIFG
        case 4:                                 // RTCEVIFG
          P1OUT ^= 0x01;
          break;
        case 6: break;                          // RTCAIFG
        case 8: break;                          // RT0PSIFG
        case 10: break;                         // RT1PSIFG
        case 12: break;                         // Reserved
        case 14: break;                         // Reserved
        case 16: break;                         // Reserved
        default: break;
      }
    }