zoukankan      html  css  js  c++  java
  • 嵌入式成长轨迹41 【Zigbee项目】【CC2430基础实验】【T2的使用】

    CC2430/CC2431 包括四个定时器:一个一般的 16 位(Timer 1) 和两个8 位(Timer3,4)定时器,支持典型的定时/ 计数功能,例如测量时间间隔,对外部事件计数,产生周期性中断请求,输入捕捉、比较输出和PWM 功能。一个 16 位MAC定时器(Timer 2),用以为IEEE802.15.4 的CSMA-CA 算法提供定时以及为IEEE802.15.4 的MAC层提供定时。

    三个一般定时器与普通的8051 定时器相差不大,本实验使用 MAC定时器(Timer2) 。
    MAC定时器主要用于为802.15.4 的CSMA-CA 算法提供定时/ 计数和802.15.4 的MAC层的普通定时。
    如果MAC定时器与睡眼定时器一起使用,当系统进入低功耗模块时,而MAC定时器将提供定时功能。当系统进入和退出低功耗模式之间,使用睡眼定时器设置周期。
    以下是MAC定时器的主要特征:
        16位定时/ 计数器提供的符码/ 帧周期为:16μs/320μs 。
        可变周期可精确到31.25ns。
        8 位计时比较功能。
        20位溢出计数。
        20位溢出计数比较功能。
        帧首定界符捕捉功能。
        定时器启动/ 停止同步于外部32.768MHz 时钟以及由睡眼定时器提供定时。
        比较和溢出产生中断。
        具有DMA功能。
    当MAC定时器停止时,它将自动复位并进入空闲模式。当T2CNF.RUN设置为’1’时,MAC定时器将启动,它将进入定时器运行模式,此时MAC定时器要么立即工作要么同步于32.768MHz时钟。
    可通过向T2CNF.RUN写入’0’来停止正在运行的MAC定时器。此时定时器将进入空闲模式,停止的定时器要么立即停止工作要么同步于32.768MHz 时钟。
    MAC定时器不仅只用于定时,与普通的定时器一个样,它也是一个 16 位的计数器。
    MAC定时器使用的寄存器(详细寄存器配置请查看随书光盘中的CC2430/CC2431 芯片资料)
    包括如下:
        T2CNF–定时器2 配置
        T2HD–定时器2 计数高位
        T2LD– 定时器2 计数低位
        T2CMP– 定时器2 比较值
        T2OF2–定时器2 溢出计数2
        T2OF1–定时器2 溢出计数1
        T2OF0–定时器2 溢出计数0
        T2CAPHPH–定时器2 捕捉高位
        T2CAPLPL–定时器2 捕捉低位
        T2PEROF2–定时器2 溢出/比较计数2
        T2PEROF1–定时器2 溢出/比较计数1
        T2PEROF0–定时器2 溢出/比较计数


    【分析】
    1、:
    void Initial(void)
    {
            LED_ENALBLE(); // 启用LED
      //用T2来做实验
            SET_TIMER2_OF_INT();                   // 开溢出中断
            SET_TIMER2_CAP_COUNTER(0X00ff);
    }
    函数功能是启用LED ,使用LED 可控,开T2比较中断
     
    2、
    重要的宏定义
    开启溢出中断
    #define SET_TIMER2_CAP_INT()      \
      do{                             \
        EA = 1;                       \
        T2IE = 1;                     \
        T2PEROF2 |= 0x40;             \
    }while(0)
     
    EA, 0xAF  Global Interrupt Enable
    T2IE,  0xBA     Timer 2 Interrupt Enable
    T2PEROF2 0x40:0100 0000 下标6设1为开溢出中断

    3、
    设定溢出周期
    #define SET_TIMER2_OF_COUNTER(val) SET_WORD(T2CAPLPL,T2CAPHPH,val)
    功能:将无符号整形数val的高8 位写入 T2CAPLPL ,低8 位写入 T2CAPHPH。

    4、
    启动T2
    #define TIMER2_RUN()  T2CNF|=0X01
    T2CNF(T2配置寄存器)
    设置 0X01 为启动T2(RUN)

    5、
    停止T2
    #define TIMER2_STOP() do{T2CNF&=0XFE;}while(0)
    停止时最后一位设置为0,那其他设置为0或1都无所谓了


    void InitClock(void)
    {
      CLKCON = 0X38;
      //上面将TICKSPD设为111 定时器计数时钟源 0.25M
      while(!(SLEEP&0X40));
    }

    CLKCON (0xC6) - Clock control 
    0X38:0011 1000
    7  OSC32K 32 kHz clock oscillator select:
      0 – 32 kHz crystal oscillator
      1 – 32 kHz RC oscillator
    6  OSC  1  R/W  Main clock oscillator select. This setting will only take effect  when the selected oscillator is powered up and stable. If the selected oscillator is not powered up, then writing this bit will power it up.
      0 – 32 MHz crystal oscillator
      1 – 16 MHz HF RC oscillator
    5:3  TICKSPD[2:0]   Timer ticks output setting, can not be higher than system clock setting given by OSC bit setting
      000 – 32 MHz ticks
      001 – 16 MHz ticks
      010 – 8 MHz ticks
      011 – 4 MHz ticks
      100 – 2 MHz ticks
      101 – 1 MHz ticks
      110 – 0.5 MHz ticks
      111 – 0.25 MHz ticks
    2:0   Reserved. Always set to 000.

    SLEEP (0xBE) - Sleep mode control
    0X40:0100 0000
    bit7 -  Unused
    bit6  XOSC_STB  XOSC stable status:
     0 – XOSC is not powered up or not yet stable
     1 – XOSC is powered up and stable
    bit5  HFRC_STB  RCOSC stable status:
     0 – HF RCOSC is not powered up or not yet stable
     1 – HF RCOSC is powered up and stable
    bit4:3  RST[1:0]   Status bit indicating the cause of the last reset. If there are multiple resets, the register will only contain the last event.
     00 – Power-on reset
     01 – External reset 
     10 – Watchdog timer reset
    bit2  OSC_PD  XOSC and HF RCOSC power down setting. The bit shall be cleared if the OSC bit is toggled. Also, if there is a calibration in progress and the CPU attempts to set the bit the module shall update the bit only at the end of calibration:
     0 – Both oscillators powered up
     1 – Oscillator not selected by OSC bit powered down
    bit1:0  MODE[1:0]   Sleep mode setting:
     00 – Power mode 0
     01 – Power mode 1
     10 – Power mode 2
     11 – Power mode 3

     1 //emot.h
     2 
     3 #ifndef EMOT_H
     4 #define EMOT_H
     5 
     6 #include <ioCC2430.h>
     7 
     8 #define uint unsigned int
     9 #define uchar unsigned char
    10 
    11 /*********************************************************************
    12 //常用赋值宏
    13 *********************************************************************/
    14 #define BYTE unsigned char
    15 #define WORD unsigned int
    16 
    17 #define UPPER_BYTE(a) ((BYTE) (((WORD)(a)) >> 8))
    18 #define HIBYTE(a)     UPPER_BYTE(a)
    19 
    20 #define LOWER_BYTE(a) ((BYTE) ( (WORD)(a))      )
    21 #define LOBYTE(a)     LOWER_BYTE(a)
    22 
    23 #define SET_WORD(regH, regL, word) \
    24    do{                             \
    25       (regH) = UPPER_BYTE((word)); \
    26       (regL) = LOWER_BYTE((word)); \
    27    }while(0)
    28 /*****************************************
    29 //初始化T2的配置程序
    30 *****************************************/
    31 #define TIMER2_CMP_HIGH_BYTE()  do{T2CNF &= ~0X08;}while(0)
    32 
    33 #define TIMER2_CMP_LOW_BYTE()  do{T2CNF|=0x08;}while(0)
    34 
    35 #define TIMER2_RUN()  T2CNF|=0X01
    36 
    37 #define TIMER2_STOP() do{T2CNF&=0XFE;}while(0)
    38 
    39 #define SET_TIMER2_COUNTER(val) SET_WORD(T2TLD,T2THD,val)
    40 
    41 //清中断标志
    42 #define CLEAR_TIMER2_INT_FLAG() \
    43   do{                             \
    44     T2CNF &= ~0XC0;               \
    45     T2IF = 0;                     \
    46     }while(0)
    47 
    48 //设T2比较值
    49 #define SET_T2_CMP_COUNTER(val)   \
    50   do{                             \
    51     T2CMP = 0x00;                 \
    52     T2CMP |= (char)val;           \
    53     }while(0)
    54 
    55 //开比较中断
    56 #define SET_TIMER2_CMP_INT()      \
    57   do{                             \
    58     EA = 1;                       \
    59     T2IE = 1;                     \
    60     T2PEROF2 |= 0x80;             \
    61     }while(0)
    62 
    63 //溢出中断 -
    64 #define SET_TIMER2_CAP_INT()      \
    65   do{                             \
    66     EA = 1;                       \
    67     T2IE = 1;                     \
    68     T2PEROF2 |= 0x40;             \
    69     }while(0)
    70 
    71 //设定溢出值 -
    72 #define SET_TIMER2_CAP_COUNTER(val) SET_WORD(T2CAPLPL,T2CAPHPH,val)
    73 
    74 /*********************************************************************
    75 *以上是T2的部分驱动
    76 */
    77 
    78 
    79 /*********************************************************************
    80 //启用LED
    81 *********************************************************************/
    82 #define LED_ENALBLE()           \
    83   do{                           \
    84     P1SEL &= ~0X03;             \
    85     P1DIR = 0x03;             \
    86     P1 |= 0X03;                 \
    87     }while(0)
    88 
    89 #define led1 P1_0
    90 #define led2 P1_1
    91 /*********************************************************************
    92 *使用模块上的LED灯
    93 */
    94 
    95 #endif //EMOT_H
     1 //main.c
     2 #include <emot.h>
     3 
     4 uint counter=0;                //统计溢出次数
     5 uchar TempFlag;                //用来标志是否要闪烁
     6 
     7 void Delay(uint n);
     8 
     9 /****************************
    10 //初始化程序
    11 ***************************/
    12 void Initial(void)
    13 {
    14         LED_ENALBLE();
    15 
    16     //用T2来做实验
    17         SET_TIMER2_CAP_INT();                   //开比较中断
    18      //   SET_TIMER2_CMP_INT();
    19         //SET_T2_CMP_COUNTER(1000);
    20     //    TIMER2_CMP_HIGH_BYTE();
    21        // SET_TIMER2_CAP_COUNTER(4000);
    22       SET_TIMER2_CAP_COUNTER(1000);
    23 
    24 }
    25 
    26 
    27 void InitClock(void)
    28 {
    29   CLKCON = 0X38;
    30   //TICKSPD = 111 定时器计数时钟源 0.25M
    31   while(!(SLEEP&0X40));
    32 }
    33 /***************************
    34 //主函数
    35 ***************************/
    36 void main()
    37 {
    38         InitClock();
    39     Initial();      //调用初始化函数
    40     led1 = 0;     //点这红色LED
    41     led2 = 1;
    42 
    43         TIMER2_RUN();
    44     while(1)        //等待中断
    45     {
    46                   if(TempFlag)
    47                   {
    48             led1 = led2;
    49             led2 = !led2;
    50                         TempFlag = 0;
    51                         //Delay(40000);
    52                       //  TIMER2_STOP();
    53           }
    54     }
    55 }
    56 
    57 
    58 
    59 #pragma vector = T2_VECTOR
    60  __interrupt void T2_ISR(void)
    61  {
    62    if(T2CNF&0x40){
    63      CLEAR_TIMER2_INT_FLAG();        //清T2中断标志
    64         if(counter<200)counter++;    //200次中断LED闪烁一轮
    65         else
    66         {
    67           counter = 0;                  //计数清零
    68           TempFlag = 1;                   //改变闪烁标志
    69         }
    70    }
    71  }
    72 
    73 
    74 /****************************
    75 //普通延时程序
    76 ***************************/
    77 void Delay(uint n)
    78 {
    79     uint i;
    80     for(i=0;i<n;i++);
    81     for(i=0;i<n;i++);
    82     for(i=0;i<n;i++);
    83     for(i=0;i<n;i++);
    84     for(i=0;i<n;i++);
    85 }
  • 相关阅读:
    计算机网络 3.* 数据通信技术基础 .1
    计算机网络3.2&3.3(第二节介质&第三节多路复用)
    python mooc 3维可视化<第一周第二&三单元>
    python mooc 3维可视化<第一周第一单元>
    conda 下配置环境
    第二周<岭回归>
    第二周<线性回归>
    第二周<导学/分类>
    iOS UILabel详解
    开始iOS 7中自动布局教程(一)
  • 原文地址:https://www.cnblogs.com/zeedmood/p/2666899.html
Copyright © 2011-2022 走看看