Arduino Library – TimerOne
Installatie van Arduino IDE libraries: Arduino info
Informatie (ENG):
The sketch is designed to be controlled by Arduiino’s serial monitor. Use the command value “f” to set the frequency.
For example:
100000f generates a 100 kHz square wave.
153600f generates a 153.6 kHz square wave (9600 baud)
1000000f generates a 1 mHz square wave.
Download TimerOne @ decibel.ni.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#include <TimerOne.h> const char SET_PERIOD_HEADER = 'p'; const char SET_FREQUENCY_HEADER = 'f'; const char SET_PULSE_WIDTH_HEADER = 'w'; const char SET_DUTY_CYCLE_HEADER = 'c'; #define pwmRegister OCR1A // the logical pin, can be set to OCR1B const int outPin = 9; // the physical pin long period = 1000; // the period in microseconds int duty = 512; // duty as a range from 0 to 1024, 512 is 50% duty cycle int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values void setup() { Serial.begin(9600); pinMode(outPin, OUTPUT); Timer1.initialize(period); // initialize timer1, 1000 microseconds Timer1.pwm(9, duty); // setup pwm on pin 9, 50% duty cycle } void loop() { processSerial(); } void processSerial() { static long val = 0; if ( Serial.available()) { char ch = Serial.read(); if(ch >= '0' && ch <= '9') // is ch a number? { val = val * 10 + ch - '0'; // yes, accumulate the value } else if(ch == SET_PERIOD_HEADER) { period = val; Serial.print("Setting period to "); Serial.println(period); Timer1.setPeriod(period); Timer1.setPwmDuty(outPin, duty); // don't change the duty cycle show(); val = 0; } else if(ch == SET_FREQUENCY_HEADER) { if(val > 0) { Serial.print("Setting frequency to "); Serial.println(val); period = 1000000 / val; Timer1.setPeriod(period); Timer1.setPwmDuty(outPin, duty); // don't change the duty cycle } show(); val = 0; } else if(ch == SET_PULSE_WIDTH_HEADER) { if( setPulseWidth(val) ) { Serial.print("Setting Pulse width to "); Serial.println(val); } else Serial.println("Pulse width too long for current period"); show(); val = 0; } else if(ch == SET_DUTY_CYCLE_HEADER) { if( val >0 && val < 100) { Serial.print("Setting Duty Cycle to "); Serial.println(val); duty = map(val,1,99, 1, ICR1); pwmRegister = duty; show(); } val = 0; } } } bool setPulseWidth(long microseconds) { bool ret = false; int prescaleValue = prescale[Timer1.clockSelectBits]; // calculate time per tick in ns long precision = (F_CPU / 128000) * prescaleValue ; period = precision * ICR1 / 1000; // period in microseconds if( microseconds < period) { duty = map(microseconds, 0,period, 0,1024); if( duty < 1) duty = 1; if(microseconds > 0 && duty < RESOLUTION) { Timer1.pwm(outPin, duty); ret = true; } } return ret; } void show() { Serial.print("The period is "); Serial.println(period); Serial.print("Duty cycle is "); // pwmRegister is ICR1A or ICR1B Serial.print( map( pwmRegister, 0,ICR1, 1,99)); Serial.println("%"); Serial.println(); } |
[#/arduino/libraries/timerone” ]