TMS320F28335 CPU Timer

  1. 定时器系统原理介绍 
    TMS320F28335的CPU Time有三个,分别为Timer0,Timer1,Timer2,其中Timer2是为操作系统DSP/BIOS保留的,当未移植操作系统时,可用来做普通的定时器。这三个定时器的中断信号分别为TINT0, TINT1, TINT2,分别对应于中断向量INT1,INT13,INT14。图4-2为定时器的结构框图,图中TIMH:TIM为计数寄存器,PRDH:PRD为周期寄存器,形如AH格式:A的形式表示一个32位的寄存器,是由两个16位的寄存器构成,AH是高16位,A是低16位。 
    这里写图片描述

CPU定时器的计数复位时,计数寄存器TIMH:TIM加载周期寄存器PRDH:PRD所设定的值,经历一个计数器时钟后,TIMH:TIM内的值就减1,一直减到0,这时产生定时器周期中断事件,并重新装载PRDH:PRD所设定的值,重新开始计数。至于每隔多少时间,计数寄存器TIMH:TIM的值才会减1则由预分频寄存器TPRH:TPR来决定。 
TPRH和TPR这两个寄存器由两部分组成,高8位为定时器预分频计数器PSC,低8位是定时器分频TDDR。也即是说,TPRH是由PSCH和TDDRH构成,而TDDR由PSC和TDDR构成。且其工作的原理与51系列单片机定时器计数器类似,复位时,PSCH:PSC加载TDDRH:TDDR所设定的值,然后经过一个CPU时钟,PSCH:PSC的值减1,当PSCH:PSC的值减到0时,会再次装载TDDRH:TDDR所设定的值,并且产生一个计数器时钟,TIMH:TIM减1。CPU定时器0、1、2配置和控制寄存器如表4-1所示 
这里写图片描述

定时器定时时间的公式计算 
以上寄存器测值在配置函数ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)中设置。形参Timer为第几位定时器,Freq为定时频率,Period为计时周期。 
假若Freq为15,Period为1000000,则时间t = 1*15*1000000/150M = 0.1s (系统时钟频率为150M)。不过这个算式的成立是有条件的,这个条件就是以下两条语句:

Timer->RegsAddr->TPR.all = 0

Timer->RegsAddr->TPRH.all = 0

上文曾提及,定时器的计数时钟是有预分频寄存器TPRH:TPR决定的。CpuTimerxRegs.TPR.all = 0, CpuTimerxRegs.TPRH.all = 0这两句话决定了1个时钟源周期为定时器的时钟周期(即一个SYSCLKOUT,TIMH:TIM减1),若CpuTimerxRegs.TPR.all = y,CpuTimerxRegs.TPRH.all = 0,则计y+1个时钟周期为定时器的时钟周期。X表示0,1,2中的任意值(任意定时器)。

因此,真正的定时时间为:time =TPRH:TPR/SYSCLKOUT*Freq*Peroid。只有当TPRH:TPR=0时,SYSCLKOUT直接作为TIMH:TIM的时钟周期,time =Freq*Peroid/SYSCLKOUT。另外,在定时器配置函数中,

Timer->RegsAddr->TCR.bit.TSS = 1 表示停止定时器 ;

Timer->RegsAddr->TCR.bit.TRB = 1 表示重装定时器;

Timer->RegsAddr->TCR.bit.FREE = 1 表示定时器自由运行;

Timer->RegsAddr->TCR.bit.TIE = 1 表示使能定时器中断。

如果要利用定时器产生一定周期的时间中断,别忘了在主函数中设置响应的中断向量即可。

TI官方头文件DSP2833x_CpuTimers.h程序如下:

// TI File $Revision: /main/4 $
// Checkin $Date: March 20, 2007   15:33:42 $
//###########################################################################
//
// FILE:    DSP2833x_CpuTimers.h
//
// TITLE:   DSP2833x CPU 32-bit 定时器寄存器定义.
//
// NOTES:   CpuTimer1 and CpuTimer2 are reserved for use with DSP BIOS and
//          other realtime operating systems.
//
//          Do not use these two timers in your application if you ever plan
//          on integrating DSP-BIOS or another realtime OS.
//
//          For this reason, comment out the code to manipulate these two timers
//          if using DSP-BIOS or another realtime OS.
//
//###########################################################################
// $TI Release: DSP2833x/DSP2823x C/C++ Header Files V1.31 $
// $Release Date: August 4, 2009 $
//###########################################################################

#ifndef DSP2833x_CPU_TIMERS_H
#define DSP2833x_CPU_TIMERS_H

#ifdef __cplusplus
extern "C" {
#endif

//---------------------------------------------------------------------------
// CPU 定时器寄存器位定义:
//
//
// TCR: 定时器控制寄存器定义:
struct  TCR_BITS {          // bits  描述
   Uint16    rsvd1:4;       // 3:0   保留
   Uint16    TSS:1;         // 4     定时器启动/停止
   Uint16    TRB:1;         // 5     定时器重载
   Uint16    rsvd2:4;       // 9:6   保留
   Uint16    SOFT:1;        // 10    运行模式
   Uint16    FREE:1;        // 11    运行模式
   Uint16    rsvd3:2;       // 12:13 保留
   Uint16    TIE:1;         // 14    中断使能
   Uint16    TIF:1;         // 15    中断标志位
};
//定时器控制寄存器
union TCR_REG {
   Uint16           all;
   struct TCR_BITS  bit;    //定时器控制寄存器定义
};

// TPR: 预分频低电平寄存器定义:
struct  TPR_BITS {        // bits  描述
   Uint16     TDDR:8;     // 7:0   定时计数寄存器
   Uint16     PSC:8;      // 15:8  预分频计数器低位
};
//预分频低电平寄存器
union TPR_REG {
   Uint16           all;
   struct TPR_BITS  bit;    //预分频低电平寄存器定义
};

// TPRH: 预分频高电平寄存器定义:
struct  TPRH_BITS {         // bits  描述
   Uint16     TDDRH:8;      // 7:0   定时计数器寄存器
   Uint16     PSCH:8;       // 15:8  预分频计数器高位
};
//预分频寄存器高电平寄存器
union TPRH_REG {
   Uint16           all;
   struct TPRH_BITS bit;    //预分频高电平寄存器定义
};

// TIM, TIMH: 计数器寄存器定义:
struct TIM_REG {
   Uint16  LSW;             //低位
   Uint16  MSW;             //高位
};
//计数器寄存器
union TIM_GROUP {
   Uint32          all;
   struct TIM_REG  half;    //计数器寄存器定义
};

// PRD, PRDH: 周期寄存器定义:
struct PRD_REG {
   Uint16  LSW;             //低位
   Uint16  MSW;             //高位
};
//周期寄存器
union PRD_GROUP {
   Uint32          all;
   struct PRD_REG  half;    //周期寄存器定义
};

//---------------------------------------------------------------------------
// CPU 定时器寄存器文件:
//
struct CPUTIMER_REGS {
   union TIM_GROUP TIM;   // 计数器寄存器
   union PRD_GROUP PRD;   // 周期寄存器
   union TCR_REG   TCR;   // 定时器控制寄存器
   Uint16          rsvd1; // 保留
   union TPR_REG   TPR;   // 预分频低电平寄存器
   union TPRH_REG  TPRH;  // 预分频高电平寄存器
};

//---------------------------------------------------------------------------
// CPU 定时器支持的变量:
//
struct CPUTIMER_VARS {
   volatile struct  CPUTIMER_REGS  *RegsAddr;   //定时器结构体
   Uint32    InterruptCount;                    //中断计数器
   float   CPUFreqInMHz;                        //频率
   float   PeriodInUSec;                        //周期
};

//---------------------------------------------------------------------------
// 函数原型和外部定义:
//
extern void InitCpuTimers(void);                //初始化定时器
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period);
//定时器配置函数
extern volatile struct CPUTIMER_REGS CpuTimer0Regs;
extern struct CPUTIMER_VARS CpuTimer0;

//CpuTimer 1和CpuTimer2预留给DSP BIOS和其他操作系统 如果使用DSP BIOS或其他操作系统注释掉CpuTimer1 CpuTimer2
extern volatile struct CPUTIMER_REGS CpuTimer1Regs;
extern volatile struct CPUTIMER_REGS CpuTimer2Regs;

extern struct CPUTIMER_VARS CpuTimer1;
extern struct CPUTIMER_VARS CpuTimer2;

//---------------------------------------------------------------------------
// 实用定时器操作:
//
// 启动定时器0:
#define StartCpuTimer0()   CpuTimer0Regs.TCR.bit.TSS = 0

// 停止定时器0:
#define StopCpuTimer0()   CpuTimer0Regs.TCR.bit.TSS = 1

// 重载定时器周期值:
#define ReloadCpuTimer0() CpuTimer0Regs.TCR.bit.TRB = 1

// 读取定时器0计数器寄存器的值:
#define ReadCpuTimer0Counter() CpuTimer0Regs.TIM.all

// 读取定时器0周期寄存器的值:
#define ReadCpuTimer0Period() CpuTimer0Regs.PRD.all

// CpuTimer 1 and CpuTimer2 are reserved for DSP BIOS & other RTOS
// Do not use these two timers if you ever plan on integrating
// DSP-BIOS or another realtime OS.
//
// For this reason, comment out the code to manipulate these two timers
// if using DSP-BIOS or another realtime OS.

// 启动定时器1、2:
#define StartCpuTimer1()   CpuTimer1Regs.TCR.bit.TSS = 0
#define StartCpuTimer2()   CpuTimer2Regs.TCR.bit.TSS = 0


// 停止定时器1、2:
#define StopCpuTimer1()   CpuTimer1Regs.TCR.bit.TSS = 1
#define StopCpuTimer2()   CpuTimer2Regs.TCR.bit.TSS = 1

// 重载定时器周期值1、2:
#define ReloadCpuTimer1() CpuTimer1Regs.TCR.bit.TRB = 1
#define ReloadCpuTimer2() CpuTimer2Regs.TCR.bit.TRB = 1

// 读取定时器1、2计数器寄存器的值:
#define ReadCpuTimer1Counter() CpuTimer1Regs.TIM.all
#define ReadCpuTimer2Counter() CpuTimer2Regs.TIM.all

// 读取定时器1、2周期寄存器的值:
#define ReadCpuTimer1Period() CpuTimer1Regs.PRD.all
#define ReadCpuTimer2Period() CpuTimer2Regs.PRD.all


#ifdef __cplusplus
}
#endif /* extern "C" */

#endif  // end of DSP2833x_CPU_TIMERS_H definition


//===========================================================================
// End of file.
//===========================================================================

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

其中的实例程序如下:

// TI File $Revision: /main/4 $
// Checkin $Date: July 9, 2009   10:51:59 $
//###########################################################################
//
// FILE:    DSP2833x_CpuTimers.c
//
// TITLE:   CPU 32-bit Timers 初始化 & 支持功能.
//
// NOTES:   CpuTimer2 is reserved for use with DSP BIOS and
//          other realtime operating systems.
//
//          Do not use these this timer in your application if you ever plan
//          on integrating DSP-BIOS or another realtime OS.
//
//###########################################################################
// $TI Release: DSP2833x/DSP2823x C/C++ Header Files V1.31 $
// $Release Date: August 4, 2009 $
//###########################################################################

#include "DSP2833x_Device.h"     // Headerfile Include File
#include "DSP2833x_Examples.h"   // Examples Include File

struct CPUTIMER_VARS CpuTimer0;

// 当使用DSP BIOS和其他操作系统,CPU定时器2的代码注释掉
struct CPUTIMER_VARS CpuTimer1;
struct CPUTIMER_VARS CpuTimer2;

//---------------------------------------------------------------------------
// InitCpuTimers:
//---------------------------------------------------------------------------
// 改函数将三个CPU的定时器初始化到一个已知状态.
//
void InitCpuTimers(void)    //初始化定时器寄存器
{
    // CPU 定时器 0
    // 寄存器地址指针和各自计时器初始化:
    CpuTimer0.RegsAddr = &CpuTimer0Regs;
    // 定时器周期寄存器最大值初始化:
    CpuTimer0Regs.PRD.all  = 0xFFFFFFFF;
    // 预分频寄存器初始化为1 (SYSCLKOUT):
    CpuTimer0Regs.TPR.all  = 0;
    CpuTimer0Regs.TPRH.all = 0;
    // 确保定时器0停止:
    CpuTimer0Regs.TCR.bit.TSS = 1;
    // 重加载计数器周期值:
    CpuTimer0Regs.TCR.bit.TRB = 1;
    // 复位中断计时器:
    CpuTimer0.InterruptCount = 0;


// CpuTimer2 is reserved for DSP BIOS & other RTOS
// Do not use this timer if you ever plan on integrating
// DSP-BIOS or another realtime OS.

    // 寄存器地址指针和各自计时器初始化:
    CpuTimer1.RegsAddr = &CpuTimer1Regs;
    CpuTimer2.RegsAddr = &CpuTimer2Regs;
    // 定时器1、2周期最大值初始化:
    CpuTimer1Regs.PRD.all  = 0xFFFFFFFF;
    CpuTimer2Regs.PRD.all  = 0xFFFFFFFF;
    // 确保定时器停止:
    CpuTimer1Regs.TCR.bit.TSS = 1;
    CpuTimer2Regs.TCR.bit.TSS = 1;
    // 重加载计数器周期值:
    CpuTimer1Regs.TCR.bit.TRB = 1;
    CpuTimer2Regs.TCR.bit.TRB = 1;
    // 复位中断计时器:
    CpuTimer1.InterruptCount = 0;
    CpuTimer2.InterruptCount = 0;

}

//---------------------------------------------------------------------------
// 配置CpuTimer:
//---------------------------------------------------------------------------
// 这个函数初始化,选择定时器,周期参数指定“频率”和“时间”参数.
//  频率为MHz,时间为us级,定时器配置后保存在停止状态
//
void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
{
    Uint32  temp;

    // 定时器周期参数初始化:
    Timer->CPUFreqInMHz = Freq;            //频率
    Timer->PeriodInUSec = Period;          //周期
    temp = (long) (Freq * Period);
    Timer->RegsAddr->PRD.all = temp;       //周期寄存器设定值为timer

    // 预分频寄存器初始化为1 (SYSCLKOUT):
    Timer->RegsAddr->TPR.all  = 0;
    Timer->RegsAddr->TPRH.all  = 0;

    // 定时器控制寄存器初始化:
    Timer->RegsAddr->TCR.bit.TSS = 1;      // 1 = 停止定时器, 0 = 启动/重启定时器
    Timer->RegsAddr->TCR.bit.TRB = 1;      // 1 = 重载计时器
    Timer->RegsAddr->TCR.bit.SOFT = 1;     // 定时器自由运行
    Timer->RegsAddr->TCR.bit.FREE = 1;     // 定时器自由运行
    Timer->RegsAddr->TCR.bit.TIE = 1;      // 0 = 禁止/ 1 = 定时器中断使能

    // 复位中断计数器:
    Timer->InterruptCount = 0;
}

//===========================================================================
// End of file.
//===========================================================================
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值