本帖最后由 学长 于 2013-8-27 16:07 编辑
原帖:
http://www.geek-workshop.com/thread-4409-1-2.html
本脉冲产生器撰写时, 因为未参考原作者写的文章 How to write libraries for the Arduino? , 翻译地址在最底下
以至於使用了 学长 所习惯比较不严谨的命名方式。
今天再上传一个完全以文章内所建议的方式,将全部命名修改。另建文章以示区别。
并感谢 群内、站内各位高手,如下
星空, 看海的日子, 夜, Boku , 小清新, 咖啡冻, darkorigin 等高手,
协助测试, 并提供宝贵建议。
故修正後, 重新上传
PulseGenFree.h 设计不变, 变数名称重写。
- /*
- PulseGenFree.h - Library for Pulse Generator Free
- Created by Andy Huang, May,6,2013.
- Tradition Chinese Forum [url]http://bbs.game7777.net[/url]
- Tradition Chinese wiki [url]http://www.game7777.net[/url]
- Special Thanks: 星空, 看海的日子, 夜, Boku , 小清新, 咖啡冻, darkorigin
-
-
- 特性:
- 1. 不停止 cpu 运行, 不使用 delay() delaymicroseconds() 。
- 2. 脉冲计数。(Not implement yet, infact you can do it very simple.)
- 3. 指定脉冲 on off 时间, us 微秒。
- 4. 可同时使用多个引脚进行不同脉冲。
-
- 使用方式:
- 1. 建构时指定 引脚 on时间 off 时间
- 2. 每个循环调用 generator()
- 3. 暂时不需要脉冲时, 停止调用 generator() 即可
- 4. 重新需要输出脉冲时,必须先调用 init() 一次。时间及计数归零。
- 注意事项:
- 1. 暂停後重新启动脉冲, 如果未先呼叫 init() , 第一次所产生的脉冲时长最多可能达到71.5分钟。
- */
- #ifndef PulseGenFree_h
- #define PulseGenFree_h
- #include "Arduino.h"
- class PulseGenFree{
- public:
- /* 建立 pulse
- /* @param pin 第几引脚
- /* @param onInterval 脉冲 ON 的时间 微秒 us
- /* @param offInterval 脉冲 OFF 的时间 微秒 us
- */
- PulseGenFree(int pin,unsigned int onInterval,unsigned int offInterval);
-
- /* 重新初始化
- /* @param onInterval 脉冲 ON 的时间 微秒 us
- /* @param offInterval 脉冲 OFF 的时间 微秒 us
- */
- void init(unsigned int onInterval,unsigned int offInterval);
-
- /* 执行
- */
- void generator();
-
-
- private:
- int _pin; //指定引脚
- unsigned long pulseOnInterval; //on时间
- unsigned long pulseOffInterval; //off时间
-
- unsigned long oldTime; //比对用
- unsigned long newTime; //比对用
-
- bool pulseState; //脉冲状态
- };
- #endif
复制代码 设计不变, 一样简单易用- #include <PulseGenFree.h>
- PulseGenFree pulseFree(7,5000,5000); //七號引腳 產生 5000us on, 5000us off 脈衝
- void setup() {
- }
- void loop() {
- pulseFree.generator();
- }
复制代码 本次修正 .cpp 已知问题, 并优化部份代码。
|