⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 CC2430相关的一些实验程序,开发环境为IAR,如果有兴趣的人是不错的资料.
💻 C
字号:
//cd wxl 2008年1月29日
#include <ioCC2430.h>

#define led1 P1_0
#define led2 P1_1

#define uchar unsigned char
#define uint unsigned int

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

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

/*****************************************
//T3配置定义
*****************************************/
// Where _timer_ must be either 3 or 4
// 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.
#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
#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
#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
#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 4
#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中断
    EA = 1;
    T4IE = 1;

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

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

/*****************************************
//延时
*****************************************/
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;			//可不清中断标志,硬件自动完成
        //led2 = 0;                       //for test
         if(counter<1000)counter++;	//10次中断LED闪烁一轮
        else
        {
         counter = 0;                  //计数清零
         GlintFlag = !GlintFlag;
        }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -