[MCU] ARM LPC2103定时器中断方式寄存器设置

[复制链接]
 楼主| 舒斯特尔 发表于 2015-8-30 08:48 | 显示全部楼层 |阅读模式
定时器查询方式定时器初始化:

1、设置定时器分频数,为(x+1)分频
2、匹配通道X中断并复位TxTC
3、比较值(1S定时值)
4、启动并复位TxTC

如:
  1. T1PR = 99;                             // 设置定时器0分频为100分频,得110592Hz

  2.        T1MCR = 0x03;                        // 匹配通道0匹配中断并复位T0TC

  3.        T1MR0 = 110592/2;                 // 比较值(1S定时值)

  4.        T1TCR = 0x03;                        // 启动并复位T0TC

  5.        T1TCR = 0x01;



 楼主| 舒斯特尔 发表于 2015-8-30 08:49 | 显示全部楼层
研究了好长一段时间,LPC210X的定时器,查询方式定时很简单如上面,但中断方式要操作好多寄存器,太麻烦,一直是一头雾水。好不容易理出了思路,现将一段例程粘贴备忘。
  1. #include <intrinsics.h>
  2. #include <stdio.h>
  3. #include <iolpc2103.h>
  4. // OSC [Hz]
  5. #define FOSC            11059200UL
  6. // Core clk [Hz]
  7. #define FCCLK           FOSC
  8. // Per clk [Hz]
  9. #define PCCLK           (FOSC/4)
  10. // Timer tick per second
  11. #define TICK_PER_SEC    (4UL)
  12. #define TIM_PER_S(Val)  (PCCLK/Val)
  13. #define MAX_TICK_PER    TIM_PER_S(20)
  14. #define MIN_TICK_PER    TIM_PER_S(5)
  15. // Timer Delta period [ms]
  16. #define DELTA_PER       (50UL)
  17. #define TIM_DPER        ((PCCLK*DELTA_PER)/1000UL)
  18. #define LED_MASK        1<<18
  19. /*************************************************************************
  20. * 函数名称:irq_handler
  21. * 入口参数:无
  22. * 返回参数:无
  23. * 描    述:IRQ handler
  24. *************************************************************************/
  25. #pragma vector=IRQV
  26. __irq __arm void irq_handler (void)
  27. {
  28. void (*interrupt_function)();
  29. unsigned int vector;
  30.   vector = VICVectAddr;     //获得中断向量
  31.   interrupt_function = (void(*)())vector;
  32.   if(interrupt_function != NULL)
  33.   {
  34.     interrupt_function();  //调用中断指向的函数
  35.   }
  36.   else
  37.   {
  38.     VICVectAddr = 0;      //清除在VIC中的中断
  39.   }
  40. }
  41. /*************************************************************************
  42. * 函数名称: Timer0Handler
  43. * 入口参数: 无
  44. * 返回参数: 无
  45. * 说    明: Timer 0 handler  
  46. *************************************************************************/
  47. void Timer0Handler (void)
  48. {
  49.   // clear interrupt flag
  50.   T0IR_bit.MR0INT = 1;
  51.   // Change patern
  52.   if ((IOSET & LED_MASK) == 0)
  53.     IOSET = LED_MASK;      //关闭LED
  54.   else
  55.     IOCLR = LED_MASK;
  56.   //pNextPattern = pNextPattern->pNextPattern;        //调整当前的链表
  57.   VICVectAddr = 0;
  58. }
  59. /*************************************************************************
  60. * 函数名称: VicInit
  61. * 入口参数: 无
  62. * 返回参数: 无
  63. * 说    明: Init VIC module
  64. *************************************************************************/
  65. void VicInit (void)
  66. {
  67.   // Assign all interrupt chanels to IRQ
  68.   VICIntSelect  =  0;
  69.   // Diasable all interrupts
  70.   VICIntEnClear = 0xFFFFFFFF;
  71.   // Clear all software interrutps
  72.   VICSoftIntClear = 0xFFFFFFFF;
  73.   // VIC registers can be accessed in User or privileged mode
  74.   VICProtection = 0;
  75.   // Clear interrupt
  76.   VICVectAddr = 0;
  77.   // Clear address of the Interrupt Service routine (ISR) for non-vectored IRQs.
  78.   VICDefVectAddr = 0;
  79.   // Clear address of the Interrupt Service routine (ISR) for vectored IRQs.
  80.   VICVectAddr0  = VICVectAddr1  = VICVectAddr2  = VICVectAddr3  =\
  81.   VICVectAddr4  = VICVectAddr5  = VICVectAddr6  = VICVectAddr7  =\
  82.   VICVectAddr8  = VICVectAddr9  = VICVectAddr10 = VICVectAddr11 =\
  83.   VICVectAddr12 = VICVectAddr13 = VICVectAddr14 = VICVectAddr15 = 0;
  84.   // Disable all vectored IRQ slots
  85.   VICVectCntl0  = VICVectCntl1  = VICVectCntl2  = VICVectCntl3  =\
  86.   VICVectCntl4  = VICVectCntl5  = VICVectCntl6  = VICVectCntl7  =\
  87.   VICVectCntl8  = VICVectCntl9  = VICVectCntl10 = VICVectCntl11 =\
  88.   VICVectCntl12 = VICVectCntl13 = VICVectCntl14 = VICVectCntl15 = 0;
  89. }
  90. /*************************************************************************
  91. * 函数名称: Init_timer0
  92. * 入口参数: 无
  93. * 返回参数: 无
  94. * 说    明: Init tiner0
  95. *************************************************************************/
  96. void Init_timer0(void)
  97. {
  98. /*
  99.   // Init timer
  100.   // Reset and stop timer0
  101.   T0TCR = 2;
  102.   // Set timer counters mode - clock by PCLK
  103.   T0CTCR = 0;
  104.   // Set timer prescaler
  105.   T0PR  = 0;
  106.   // Set timer period
  107.   T0MR0 = PCCLK/TICK_PER_SEC;
  108.   // Set mack action - interrupt by MACH0 enable, reset counter
  109.   T0MCR = 3;
  110.   // No external action
  111.   T0EMR = 0;
  112. */
  113.   T0TCR = 2;
  114.   T0CTCR = 0;
  115.   T0PR = 0;
  116.   T0MR0 = PCCLK/TICK_PER_SEC;
  117.   T0MCR = 3;
  118.   T0EMR = 0;

  119.   // Assign to IRQ
  120.   VICIntSelect_bit.TIMER0 = 0;
  121.   // Set interrupt slots
  122.   VICVectAddr0 = (unsigned int) Timer0Handler;
  123.   VICVectCntl0_bit.NUMBER = VIC_TIMER0;
  124.   VICVectCntl0_bit.ENABLED = 1;
  125.   // Timer 0 interrupt enable
  126.   VICIntEnable_bit.TIMER0 = 1;
  127.   // Enable timer0
  128.   T0TCR = 1;
  129. }
  130. /*************************************************************************
  131. * 函数名称: Init_Gpio
  132. * 入口参数: 无
  133. * 返回参数: 无
  134. * 说    明: Init GPIO
  135. *************************************************************************/
  136. void Init_Gpio(void)
  137. {
  138.     // Init GPIO
  139.   PINSEL0 = PINSEL1 = 0;
  140.   // Disable fast IO
  141.   SCS_bit.GPIO0M = 0;

  142.   // Set pins connect to LEDs as outputs
  143.   IODIR = LED_MASK;
  144.   // All LEDs off
  145.   IOCLR = LED_MASK;
  146. }
  147. /*************************************************************************
  148. * 函数名称: Init_pll
  149. * 入口参数: 无
  150. * 返回参数: 无
  151. * 说    明: Init PLL
  152. *************************************************************************/
  153. void Init_pll(void)
  154. {
  155.   // Disable PLL
  156.   PLLCON = 0;
  157.   // Write Feed
  158.   PLLFEED = 0xAA;
  159.   PLLFEED = 0x55;
  160.   // Set periphery divider /4
  161.   APBDIV_bit.APBDIV  = 0;
  162.   // Set MAM fully enable
  163.   MAMCR_bit.MODECTRL = 0;
  164.   MAMTIM_bit.CYCLES  = 3;
  165.   MAMCR_bit.MODECTRL = 2;
  166. }
  167. /*************************************************************************
  168. * 函数名称: main
  169. * 入口参数: 无
  170. * 返回参数: 无
  171. * 描    述: main
  172. *************************************************************************/
  173. void main(void)
  174. {
  175.   Init_pll();
  176.   // Memory map init flash memory is maped on 0 address
  177. #ifdef FLASH
  178.   MEMMAP_bit.MAP = 1;
  179. #else
  180.   MEMMAP_bit.MAP = 2;
  181. #endif
  182.   __disable_interrupt();
  183.   VicInit();
  184.   Init_Gpio();
  185.   Init_timer0();
  186.   __enable_interrupt();
  187.   while(1)
  188.   {};
  189. }
跟屁虫 发表于 2015-8-30 10:28 | 显示全部楼层
定时器中断方式寄存器设置,定时器用的还是很多的
西门扫雪 发表于 2015-8-30 15:33 | 显示全部楼层
定时器做好了也不简单呢
史迪威将军 发表于 2015-8-31 20:57 | 显示全部楼层
匹配通道X中断并复位TxTC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

25

主题

277

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

25

主题

277

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部