本帖最后由 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
|