原创 CC2430/CC2431源代码:CC2430定时器中断

2009-7-13 16:53 1866 7 7 分类: 消费电子

//cd wxl 2008年1月29日
#include <ioCC2430.h>

//说明:本实验在成都无线龙提供的CC2430模块中调试通过。http://www.c51rf.com

#define led1 P1_0
#define led2 P1_1

#define uchar unsigned char
#define uint unsigned int

/*****************************************
//定义全局变量
*****************************************/
uint counter = 0;
uchar GlintFlag = 0;

/*****************************************
//函数声明cd wxl
*****************************************/
void Delay(uint n);
void Init_T4_AND_LED(void);

/*****************************************
//T3配置定义
*****************************************/
// Where _timer_ must be either 3 or 4 cd wxl
// Macro for initialising timer 3 or 4
#define TIMER34_INIT(timer)   \
   do {                       \
      T##timer##CTL   = 0x06; \
      T##timer##CCTL0 = 0x00; \
      T##timer##CC0   = 0x00; \
      T##timer##CCTL1 = 0x00; \
      T##timer##CC1   = 0x00; \
   } while (0)

//Macro for enabling overflow interrupt
#define TIMER34_ENABLE_OVERFLOW_INT(timer,val) \
   (T##timer##CTL = (val) ? T##timer##CTL | 0x08 : T##timer##CTL & ~0x08)

 

// Macro for configuring channel 1 of timer 3 or 4 for PWM mode. cd wxl
#define TIMER34_PWM_CONFIG(timer)                 \
   do{                                            \
      T##timer##CCTL1 = 0x24;                     \
      if(timer == 3){                             \
         if(PERCFG & 0x20) {                      \
            IO_FUNC_PORT_PIN(1,7,IO_FUNC_PERIPH); \
         }                                        \
         else {                                   \
            IO_FUNC_PORT_PIN(1,4,IO_FUNC_PERIPH); \
         }                                        \
      }                                           \
      else {                                      \
         if(PERCFG & 0x10) {                      \
             IO_FUNC_PORT_PIN(2,3,IO_FUNC_PERIPH);\
         }                                        \
         else {                                   \
            IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
         }                                        \
      }                                           \
   } while(0)

// Macro for setting pulse length of the timer in PWM mode cd wxl
#define TIMER34_SET_PWM_PULSE_LENGTH(timer, value) \
   do {                                            \
      T##timer##CC1 = (BYTE)value;                 \
   } while (0)


// Macro for setting timer 3 or 4 as a capture timer cd wxl
#define TIMER34_CAPTURE_TIMER(timer,edge)          \
   do{                                             \
      T##timer##CCTL1 = edge;                      \
      if(timer == 3){                              \
         if(PERCFG & 0x20) {                       \
            IO_FUNC_PORT_PIN(1,7,IO_FUNC_PERIPH); \
         }                                         \
         else {                                    \
             IO_FUNC_PORT_PIN(1,4,IO_FUNC_PERIPH); \
         }                                         \
      }                                            \
      else {                                       \
         if(PERCFG & 0x10) {                       \
            IO_FUNC_PORT_PIN(2,3,IO_FUNC_PERIPH); \
         }                                         \
        else {                                     \
           IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH);   \
        }                                          \
     }                                             \
}while(0)

//Macro for setting the clock tick for timer3 or 4 cd wxl
#define TIMER34_START(timer,val)                         \
    (T##timer##CTL = (val) ? T##timer##CTL | 0X10 : T##timer##CTL&~0X10)

#define TIMER34_SET_CLOCK_DIVIDE(timer,val)        \
do{                                             \
    T##timer##CTL &= ~0XE0;                               \
      (val==2) ? (T##timer##CTL|=0X20):                   \
      (val==4) ? (T##timer##CTL|=0x40):                   \
      (val==8) ? (T##timer##CTL|=0X60):                   \
      (val==16)? (T##timer##CTL|=0x80):                   \
      (val==32)? (T##timer##CTL|=0xa0):                   \
      (val==64) ? (T##timer##CTL|=0xc0):                  \
      (val==128) ? (T##timer##CTL|=0XE0):                 \
      (T##timer##CTL|=0X00);             /* 1 */          \
}while(0)

//Macro for setting the mode of timer3 or 4cd wxl
#define TIMER34_SET_MODE(timer,val)                \
do{                                             \
    T##timer##CTL &= ~0X03;                               \
    (val==1)?(T##timer##CTL|=0X01): /*DOWN            */ \
    (val==2)?(T##timer##CTL|=0X02): /*Modulo          */ \
    (val==3)?(T##timer##CTL|=0X03): /*UP / DOWN       */ \
    (T##timer##CTL|=0X00);           /*free runing */     \
}while(0)


/*****************************************
//T3及LED初始化
*****************************************/
void Init_T4_AND_LED(void)
{
    P1DIR = 0X03;
    led1 = 1;
    led2 = 1;

    TIMER34_INIT(4);                  //初始化T4
    TIMER34_ENABLE_OVERFLOW_INT(4,1); //开T4中断cd wxl
    EA = 1;
    T4IE = 1;

    //T4CTL |= 0XA0;                    //时钟128分频101
    TIMER34_SET_CLOCK_DIVIDE(4,128);
    TIMER34_SET_MODE(4,0);                 //自动重装00->0xffcd wxl
    T4CC0 = 0Xf0;
    TIMER34_START(4,1);                    //启动
};

/*****************************************
//主函数cd wxl
*****************************************/
void main(void)
{
    Init_T4_AND_LED();
    while(1)
    {
      if(GlintFlag == 1)led1 = !led1;                   //改变小灯的状态cd wxl
      Delay(4000);
    };
}

/*****************************************
//延时cd wxl
*****************************************/
void Delay(uint n)
{
uint ii;
for(ii=0;ii<n;ii++);
for(ii=0;ii<n;ii++);
for(ii=0;ii<n;ii++);
for(ii=0;ii<n;ii++);
for(ii=0;ii<n;ii++);
}

#pragma vector = T4_VECTOR
__interrupt void T4_ISR(void)
{
   IRCON = 0x00;    //可不清中断标志,硬件自动完成cd wxl
        //led2 = 0;                       //for test
         if(counter<1000)counter++; //10次中断LED闪烁一轮cd wxl
        else
        {
         counter = 0;                  //计数清零cd wxl
         GlintFlag = !GlintFlag;
        }
}

更多资料与技术讨论请联系:

上海无线龙通讯科技有限公司

联系人:司文铠

QQ876692208

MSN:siwenkai@hotmail.com

TEL13816689475

email:wxlsiwenkai@yahoo.com.cn

skype:sparksi01

公司网址:www.wxlsoc.com

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
7
关闭 站长推荐上一条 /3 下一条