查看: 989|回复: 0

[经验] LPC1788--TIMER匹配中断设置--寄存器操作

[复制链接]

该用户从未签到

发表于 2021-1-18 14:04:03 | 显示全部楼层 |阅读模式
分享到:
简单记录LPC1788定时器匹配中断设置--通过寄存器直接操作

  1. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "timer_lpc1788.h"</font>
  2. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#include "uart_lpc1788.h"</font>
  3. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void TIMER0_IRQHandler(void)    //TIMER0中断函数</font>
  4. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</font>
  5. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    if(((LPC_TIM0->IR) &(1<<0))==(1<<0))  //匹配中断发生</font>
  6. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   {</font>
  7. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">       UARTSendStr("CLOS\r\n");</font>
  8. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   }</font>
  9. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->IR |=(1<<0);//清除匹配中断标志位</font>
  10. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</font>

  11. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void TIMER0_Init(uint32_t clk, uint32_t howtime)</font>
  12. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</font>
  13. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_SC->PCONP|=(1<<1);//打开TIMER0外设时钟</font>
  14. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  15. </font>
  16. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->CTCR &=(~0x03);//计数控制寄存器</font>
  17. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->CTCR |=0x00;//定时器模式</font>
  18. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//----PC=PR-----TC++</font>
  19. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->TC =0;//定时计数器</font>
  20. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->PR =0;//预分频寄存器</font>
  21. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->PC =0;//预分频计数器</font>
  22. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  23. </font>
  24. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->TCR |=(1<<1);  //定时器控制寄存器--复位定时器</font>
  25. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->TCR &=~(1<<1);   //清除复位</font>
  26. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  27. </font>
  28. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->PR =(clk/1000000-1);//预分频寄存器--系统外设时钟/1000000=1us需要的PC值</font>
  29. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  30. </font>
  31. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->IR=0xFFFFFFFF;   //清除中断</font>
  32. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//--------------------</font>
  33. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->MR0=howtime;//匹配寄存器---多少个1us将匹配</font>
  34. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->MCR &=~(0x07);  //匹配控制寄存器--禁止匹配-复位-停止产生的中断</font>
  35. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->MCR |=(1<<0); //允许匹配中断</font>
  36. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->MCR |=(1<<1); //允许复位中断--匹配时TC复位-重新计数</font>
  37. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  38. </font>
  39. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));  //设置定时器中断优先级</font>
  40. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  41. </font>
  42. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   NVIC_EnableIRQ(TIMER0_IRQn);   //定时器中断使能</font>
  43. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  44. </font>
  45. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">
  46. </font>
  47. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   LPC_TIM0->TCR |=(1<<0);;  //启动定时器</font>
  48. <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</font>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条



手机版|小黑屋|与非网

GMT+8, 2024-11-23 18:52 , Processed in 0.128090 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.