免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3779 | 回复: 0
打印 上一主题 下一主题

28335定时器0使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
定时器0使用
(1)定时器时钟配置,由于定时器的时钟源与系统时钟SYSCLOCK同步,详细配置可见SysCtrl.c文件;
(2)定时器分频设定;
(3)定时周期设定;
其中(1)(2)(3)可概括为InitCpuTimers();
(4)定时器启动TCR寄存器TSS,定时器中断使能TCR寄存器TIE=1;概括为ConfigCpuTimer(&CpuTimer0, 150, 500000);
     设置定时器中断向量入口地址;PieVectTable.TINT0 = &cpu_timer0_isr;
(5)定时器中断;包含(a)使能定时器中断TCR寄存器(b)IER|=M_INT1;使能连接的INT1中断;
     (c)PieCtrlRegs.PIEIER1.bit.INTx7=1;使能TINT0,1组中第7中断;(d)开中断EINT;ERTM;
(6)中断响应函数中需要:PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
调试代码如下:
  1. #include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
  2. #include "DSP2833x_Examples.h" // DSP2833x Examples Include File

  3. #define LED            *(Uint16 *)0x4080
  4. Uint16 i=0;
  5. Uint16 valueLED=0;
  6. // Prototype statements for functions found within this file.
  7. interrupt void cpu_timer0_isr();
  8. interrupt void xint1_isr(void);

  9. void main(void)
  10. {
  11.    
  12. // Step 1. Initialize System Control:
  13. // PLL, WatchDog, enable Peripheral Clocks
  14. // This example function is found in the DSP2833x_SysCtrl.c file.
  15.    InitSysCtrl();

  16. // Step 2. Initalize GPIO:
  17. // This example function is found in the DSP2833x_Gpio.c file and
  18. // illustrates how to set the GPIO to it's default state.
  19. // InitGpio(); // Skipped for this example


  20. // Step 3. Clear all interrupts and initialize PIE vector table:
  21. // Disable CPU interrupts
  22.    DINT;

  23. // Initialize the PIE control registers to their default state.
  24. // The default state is all PIE interrupts disabled and flags
  25. // are cleared.
  26. // This function is found in the DSP2833x_PieCtrl.c file.
  27.    InitPieCtrl();

  28. // Disable CPU interrupts and clear all CPU interrupt flags:
  29.    IER = 0x0000;
  30.    IFR = 0x0000;

  31. // Initialize the PIE vector table with pointers to the shell Interrupt
  32. // Service Routines (ISR).
  33. // This will populate the entire table, even if the interrupt
  34. // is not used in this example. This is useful for debug purposes.
  35. // The shell ISR routines are found in DSP2833x_DefaultIsr.c.
  36. // This function is found in DSP2833x_PieVect.c.
  37.    InitPieVectTable();
  38.     InitXintf();
  39. // Interrupts that are used in this example are re-mapped to
  40. // ISR functions found within this file.
  41.    EALLOW; // This is needed to write to EALLOW protected registers
  42.    PieVectTable.TINT0 = &cpu_timer0_isr;
  43.    PieVectTable.XINT1=&xint1_isr;
  44.    EDIS; // This is needed to disable write to EALLOW protected registers

  45. // Step 4. Initialize the Device Peripheral. This function can be
  46. // found in DSP2833x_CpuTimers.c
  47.    InitCpuTimers(); // For this example, only initialize the Cpu Timers
  48. #if (CPU_FRQ_150MHZ)
  49. // Configure CPU-Timer 0 to interrupt every 500 milliseconds:
  50. // 150MHz CPU Freq, 50 millisecond Period (in uSeconds)
  51. //定时周期计算150/sysclk*500000
  52.    ConfigCpuTimer(&CpuTimer0, 150, 500000);
  53. #endif
  54. #if (CPU_FRQ_100MHZ)
  55. // Configure CPU-Timer 0 to interrupt every 500 milliseconds:
  56. // 100MHz CPU Freq, 50 millisecond Period (in uSeconds)
  57.    ConfigCpuTimer(&CpuTimer0, 100, 500000);
  58. #endif

  59. // To ensure precise timing, use write-only instructions to write to the entire register. Therefore, if any
  60. // of the configuration bits are changed in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
  61. // below settings must also be updated.
  62.     //使能定时器中断和启动定时器,对应寄存器为TCR=1和TSS=0
  63.    CpuTimer0Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0

  64. // Step 5. User specific code, enable interrupts:

  65. // Configure GPIO0 as a GPIO output pin
  66.    EALLOW;
  67.    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;
  68.    GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;
  69.    EDIS;

  70.    // Configure GPIO31 as a GPIO input pin
  71.    EALLOW;
  72.    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;//控制寄存器,控制IO工作方式
  73.    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;//方向控制寄存器
  74.    GpioCtrlRegs.GPAQSEL2.bit.GPIO31=0;//配置采样周期,判断IO管脚状态避免噪声
  75.    EDIS;

  76.    // GPIO31 is XINT1
  77.    EALLOW;//数据手册PIE&GPIO,pp101,00000对应GPIO0
  78.    GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = 11111; // Xint1 is GPIO31
  79.    EDIS;

  80.    // Enable XINT1
  81.    XIntruptRegs.XINT1CR.bit.ENABLE = 1; // Enable Xint1

  82. // Enable CPU INT1 which is connected to CPU-Timer 0:
  83.    IER |= M_INT1;

  84. // Enable TINT0 in the PIE: Group 1 interrupt 7
  85.    PieCtrlRegs.PIEIER1.bit.INTx7 = 1;

  86. // Enable global Interrupts and higher priority real-time debug events:
  87.    EINT; // Enable Global interrupt INTM
  88.    ERTM; // Enable Global realtime interrupt DBGM

  89. // Step 6. IDLE loop. Just sit and loop forever (optional):
  90.    for(;;);
  91. }


  92. interrupt void cpu_timer0_isr()
  93. {
  94.        valueLED = 3 - valueLED;
  95.     LED = valueLED;
  96.    CpuTimer0.InterruptCount++;
  97.    GpioDataRegs.GPATOGGLE.bit.GPIO0 = 1; // 取反,Toggle GPIO32 once per 500 milliseconds
  98.    // Acknowledge this interrupt to receive more interrupts from group 1
  99.    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
  100. }

  101. interrupt void xint1_isr(void)
  102. {
  103.     i++;
  104.     // Acknowledge this interrupt to get more from group 1
  105.     PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;

  106. }


  107. //===========================================================================
  108. // No more.
  109. //===========================================================================
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP