打印
[LOOK]

LOOK菜鸟timer应用例程

[复制链接]
1894|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hotpower|  楼主 | 2011-5-23 01:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 hotpower 于 2011-5-23 19:01 编辑

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

点击下载LOOK_TIMER
LOOK_TIMER.rar (648.76 KB)

1.LOOK_TIMER.H

#ifndef __LOOK_TIMER_H
#define __LOOK_TIMER_H
#include "look_config.h"
#include <look.h>
#include <instantiate>
// 任务类 task_LOOK_TIMER_t 的定义
class task_LOOK_TIMER_t : public task_t {
public:
task_LOOK_TIMER_t() __OPT_ATTR__; // 构造函数
    void on_signal(void* addr, uintptr_t signal);    // 定义on_signal()
bool send_message(int msg);
protected:
void routine();  // 任务例程
private:
timer_t timer;
int interval;
mbox_t<int> mbox;
};
// 任务类 LOOK_TIMER 的构造函数
__OPT_INLINE__ task_LOOK_TIMER_t::task_LOOK_TIMER_t()
: timer(*this)           // 初始化定时器实例,并指定触发对象。
, interval(50)    //初始化显示间隔
, mbox(0)
{
// TODO: 在此初始化 task_LOOK_TIMER_t 的类成员
}
extern instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER;
#endif // __LOOK_TIMER_H

2.LOOK_TIMER.CPP

#include "LOOK_TIMER.h"
#include "NUC1xx.h"
#include "NUC1xxM051Seriescfg.h"
class Keyboard_t : public interrupt_t {
public:
Keyboard_t() __OPT_ATTR__;
protected:
bool isr(int vector);
void dsr(int vector, uintptr_t count);
};
// Keyboard_t 构造函数
inline Keyboard_t::Keyboard_t()
{
attach(EINT0_IRQn);
attach(EINT1_IRQn);
GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14);   // 开启中断
vector_t::enable(EINT0_IRQn);
vector_t::enable(EINT1_IRQn);
}
// Keyboard_t 中断服务例程
bool Keyboard_t::isr(int vector)
{
GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs;   // 清中断 flag
return true;
}
// Keyboard_t 中断滞后服务例程
void Keyboard_t::dsr(int vector, uintptr_t count)
{
if (vector == EINT0_IRQn)//Key2
{
  task_LOOK_TIMER.send_message(50);//+50节拍
}
else if (vector == EINT1_IRQn)//Key1
{
  task_LOOK_TIMER.send_message(-50);//-50节拍
}
}
Keyboard_t Key;         // 创建Key对象
// 任务类 task_LOOK_TIMER_t 的例程
void task_LOOK_TIMER_t::routine()
{
// TODO: 在此编写 task_LOOK_TIMER_t 例程的内容
timer.start(interval);       //定时器
while (true) {
  // TODO: 在此编写 task_LOOK_TIMER_t 例程的内容
  int msg = mbox.get();     // 等待邮箱消息
  if (msg != 0)
  {
   if ((msg + interval) > 0) {//间隔为0非法,不响
    interval += msg;
    GPIOBs.DMASK.Bits.Pin10 = 0;
    msg = (msg > 0) ? 1 : 2;//+ 响1声,- 响2声
    for (int i = 0; i < (msg * 2); i ++){
       GPIOBs.DOUT.Bits.Pin10 ^= 1;
             delay(LOOK_TICKS_PER_SEC / 20);
    }
    GPIOBs.DOUT.Bits.Pin10 = 0;//关闭BEEP
   }
  }
}
}
void task_LOOK_TIMER_t:n_signal(void* addr, uintptr_t signal)
{
static int count = 0;
if (addr == &timer) {
  timer.do_start(interval);       // 重置定时器
  // 处理定时器事务
  GPIOAs.DMASK.Regs = ~0b111100;
  GPIOAs.DOUT.Regs = (count << 2) ^ 0b111100;
  count ++;
}
else {
  task_t:n_signal(addr, signal);
}
}
bool task_LOOK_TIMER_t::send_message(int msg)
{
return mbox.do_tryput(msg);

}
#ifdef LOOK_SCHEDULING_PRIORITY
instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER(0);
#else
instantiate::task<task_LOOK_TIMER_t, LOOK_STACK_SIZE> task_LOOK_TIMER;
#endif

相关帖子

沙发
john_lee| | 2011-5-23 02:03 | 只看该作者
老哥精神可嘉。

使用特权

评论回复
板凳
hotpower|  楼主 | 2011-5-23 02:06 | 只看该作者
今天必须毕业!!!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1538

主题

21697

帖子

506

粉丝