[LOOK] LOOK菜鸟timer应用例程

[复制链接]
 楼主| hotpower 发表于 2011-5-23 01:49 | 显示全部楼层 |阅读模式
本帖最后由 hotpower 于 2011-5-23 19:01 编辑

本例程是在Lee老师和M0菜地等网友的指导和监督下在线实时完成的。

点击下载LOOK_TIMER


1.LOOK_TIMER.H

  1. #ifndef __LOOK_TIMER_H
  2. #define __LOOK_TIMER_H
  3. #include "look_config.h"
  4. #include <look.h>
  5. #include <instantiate>
  6. // 任务类 task_LOOK_TIMER_t 的定义
  7. class task_LOOK_TIMER_t : public task_t {
  8. public:
  9. task_LOOK_TIMER_t() __OPT_ATTR__; // 构造函数
  10.     void on_signal(void* addr, uintptr_t signal);    // 定义on_signal()
  11. bool send_message(int msg);
  12. protected:
  13. void routine();  // 任务例程
  14. private:
  15. timer_t timer;
  16. int interval;
  17. mbox_t<int> mbox;
  18. };
  19. // 任务类 LOOK_TIMER 的构造函数
  20. __OPT_INLINE__ task_LOOK_TIMER_t::task_LOOK_TIMER_t()
  21. : timer(*this)           // 初始化定时器实例,并指定触发对象。
  22. , interval(50)    //初始化显示间隔
  23. , mbox(0)
  24. {
  25. // TODO: 在此初始化 task_LOOK_TIMER_t 的类成员
  26. }
  27. extern instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER;
  28. #endif // __LOOK_TIMER_H

2.LOOK_TIMER.CPP

  1. #include "LOOK_TIMER.h"
  2. #include "NUC1xx.h"
  3. #include "NUC1xxM051Seriescfg.h"
  4. class Keyboard_t : public interrupt_t {
  5. public:
  6. Keyboard_t() __OPT_ATTR__;
  7. protected:
  8. bool isr(int vector);
  9. void dsr(int vector, uintptr_t count);
  10. };
  11. // Keyboard_t 构造函数
  12. inline Keyboard_t::Keyboard_t()
  13. {
  14. attach(EINT0_IRQn);
  15. attach(EINT1_IRQn);
  16. GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14);   // 开启中断
  17. vector_t::enable(EINT0_IRQn);
  18. vector_t::enable(EINT1_IRQn);
  19. }
  20. // Keyboard_t 中断服务例程
  21. bool Keyboard_t::isr(int vector)
  22. {
  23. GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs;   // 清中断 flag
  24. return true;
  25. }
  26. // Keyboard_t 中断滞后服务例程
  27. void Keyboard_t::dsr(int vector, uintptr_t count)
  28. {
  29. if (vector == EINT0_IRQn)//Key2
  30. {
  31.   task_LOOK_TIMER.send_message(50);//+50节拍
  32. }
  33. else if (vector == EINT1_IRQn)//Key1
  34. {
  35.   task_LOOK_TIMER.send_message(-50);//-50节拍
  36. }
  37. }
  38. Keyboard_t Key;         // 创建Key对象
  39. // 任务类 task_LOOK_TIMER_t 的例程
  40. void task_LOOK_TIMER_t::routine()
  41. {
  42. // TODO: 在此编写 task_LOOK_TIMER_t 例程的内容
  43. timer.start(interval);       //定时器
  44. while (true) {
  45.   // TODO: 在此编写 task_LOOK_TIMER_t 例程的内容
  46.   int msg = mbox.get();     // 等待邮箱消息
  47.   if (msg != 0)
  48.   {
  49.    if ((msg + interval) > 0) {//间隔为0非法,不响
  50.     interval += msg;
  51.     GPIOBs.DMASK.Bits.Pin10 = 0;
  52.     msg = (msg > 0) ? 1 : 2;//+ 响1声,- 响2声
  53.     for (int i = 0; i < (msg * 2); i ++){
  54.        GPIOBs.DOUT.Bits.Pin10 ^= 1;
  55.              delay(LOOK_TICKS_PER_SEC / 20);
  56.     }
  57.     GPIOBs.DOUT.Bits.Pin10 = 0;//关闭BEEP
  58.    }
  59.   }
  60. }
  61. }
  62. void task_LOOK_TIMER_t:n_signal(void* addr, uintptr_t signal)
  63. {
  64. static int count = 0;
  65. if (addr == &timer) {
  66.   timer.do_start(interval);       // 重置定时器
  67.   // 处理定时器事务
  68.   GPIOAs.DMASK.Regs = ~0b111100;
  69.   GPIOAs.DOUT.Regs = (count << 2) ^ 0b111100;
  70.   count ++;
  71. }
  72. else {
  73.   task_t:n_signal(addr, signal);
  74. }
  75. }
  76. bool task_LOOK_TIMER_t::send_message(int msg)
  77. {
  78. return mbox.do_tryput(msg);

  79. }
  80. #ifdef LOOK_SCHEDULING_PRIORITY
  81. instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER(0);
  82. #else
  83. instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER;
  84. #endif

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
john_lee 发表于 2011-5-23 02:03 | 显示全部楼层
老哥精神可嘉。
 楼主| hotpower 发表于 2011-5-23 02:06 | 显示全部楼层
今天必须毕业!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

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