TimerOne库的源代码
时间: 2023-08-10 17:03:09 浏览: 146
以下是TimerOne库的源代码,可用于Arduino开发板:
```
/*
TimerOne.h - Version: Latest
A better Timer Library for Arduino.
Created by Jesse Tane, April 13, 2010.
Released into the public domain.
*/
#ifndef TimerOne_h_
#define TimerOne_h_
#include <inttypes.h>
// 16MHz clock = 62.5ns tick
// 65535 * 62.5ns = 4.096ms
#define RESOLUTION 4 // 4 ms tick, minimum delay
// This is the number of timer clock ticks per microsecond
#define clockCyclesPerMicrosecond() (F_CPU / 1000000L)
#define clockCyclesToMicroseconds(a) ((a) / clockCyclesPerMicrosecond())
#define microsecondsToClockCycles(a) ((a) * clockCyclesPerMicrosecond())
class TimerOne
{
public:
void initialize(long microseconds=1000000);
void attachInterrupt(void (*isr)());
void detachInterrupt();
void start();
void stop();
void restart();
void resume();
void setPeriod(long microseconds);
inline void setPwmDuty(char pin, int duty) __attribute__((always_inline));
inline unsigned long getPeriod() __attribute__((always_inline));
inline unsigned long getPwmDuty(char pin) __attribute__((always_inline));
private:
static unsigned long _timer_period;
static void (*_userFunc)();
};
extern TimerOne Timer1;
#endif
```
该库提供了一系列函数,可以方便地设置定时器,附加、分离中断,以及设置PWM占空比等。在使用前需要将该库添加到Arduino IDE的库目录中,并在代码中引用。
阅读全文