|
| 1 | +// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include <Arduino.h> |
| 6 | + |
| 7 | +#define IROUT 9 |
| 8 | +#define IRIN 2 |
| 9 | + |
| 10 | +volatile unsigned long pulseCount = 0; |
| 11 | + |
| 12 | +#if defined(ARDUINO_RASPBERRY_PI_PICO) |
| 13 | +#include "hardware/pwm.h" |
| 14 | +void countPulse() { |
| 15 | + pulseCount++; |
| 16 | +} |
| 17 | +#else |
| 18 | +void countPulse() { |
| 19 | + pulseCount++; |
| 20 | +} |
| 21 | +#endif |
| 22 | + |
| 23 | +void setup() { |
| 24 | + Serial.begin(115200); |
| 25 | + pinMode(IRIN, INPUT); // Assuming the input signal is connected to pin 2 |
| 26 | + |
| 27 | + attachInterrupt(digitalPinToInterrupt(IRIN), countPulse, RISING); |
| 28 | + pinMode(IROUT, OUTPUT); |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +bool testFreq(uint32_t freq) { |
| 33 | + uint32_t temp = 0; |
| 34 | + |
| 35 | + Serial.print(freq); Serial.println(" Hz"); |
| 36 | + setFrequency(freq); // Set the initial frequency to the specified value |
| 37 | + pulseCount = 0; |
| 38 | + delay(100); |
| 39 | + temp = pulseCount; |
| 40 | + Serial.print("\tCounted "); Serial.print(temp); |
| 41 | + Serial.println(" pulses"); |
| 42 | + if ((temp > (freq / 10) + (freq / 100)) || (temp < (freq / 10) - (freq / 100))) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + Serial.println("----------------------"); |
| 50 | + if (!testFreq(30000)) return; |
| 51 | + if (!testFreq(40000)) return; |
| 52 | + if (!testFreq(50000)) return; |
| 53 | + if (!testFreq(60000)) return; |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +void setFrequency(unsigned long frequency) { |
| 58 | +#if defined(__AVR__) |
| 59 | + unsigned long ocrValue; |
| 60 | + byte csBits = 0; |
| 61 | + |
| 62 | + // Disable interrupts |
| 63 | + noInterrupts(); |
| 64 | + |
| 65 | + // Reset Timer1 Control Registers |
| 66 | + TCCR1A = 0; |
| 67 | + TCCR1B = 0; |
| 68 | + TCNT1 = 0; // Reset counter |
| 69 | + |
| 70 | + // Set Timer1 to CTC mode (Clear Timer on Compare Match) |
| 71 | + TCCR1B |= (1 << WGM12); |
| 72 | + |
| 73 | + // Determine best prescaler and OCR1A value for the desired frequency |
| 74 | + if (frequency > 4000) { // Can use no prescaler if frequency is high enough |
| 75 | + csBits = (1 << CS10); // No prescaler |
| 76 | + ocrValue = 16000000 / (2 * frequency) - 1; |
| 77 | + } else if (frequency > 500) { |
| 78 | + csBits = (1 << CS11); // Prescaler 8 |
| 79 | + ocrValue = 2000000 / (2 * frequency) - 1; |
| 80 | + } else if (frequency > 60) { |
| 81 | + csBits = (1 << CS11) | (1 << CS10); // Prescaler 64 |
| 82 | + ocrValue = 250000 / (2 * frequency) - 1; |
| 83 | + } else { |
| 84 | + csBits = (1 << CS12); // Prescaler 256 |
| 85 | + ocrValue = 62500 / (2 * frequency) - 1; |
| 86 | + } |
| 87 | + |
| 88 | + // Handle boundary conditions for OCR1A |
| 89 | + if (ocrValue > 65535) ocrValue = 65535; // Cap at maximum for 16-bit timer |
| 90 | + if (ocrValue < 1) ocrValue = 1; // Ensure OCR1A is at least 1 |
| 91 | + |
| 92 | + OCR1A = ocrValue; |
| 93 | + TCCR1B |= csBits; // Set the prescaler |
| 94 | + TCCR1A |= (1 << COM1A0); // Toggle OC1A on Compare Match |
| 95 | + |
| 96 | + // Re-enable interrupts |
| 97 | + interrupts(); |
| 98 | + |
| 99 | +#elif defined(ARDUINO_RASPBERRY_PI_PICO) |
| 100 | + // Set PWM frequency for the RP2040 |
| 101 | + gpio_set_function(IROUT, GPIO_FUNC_PWM); |
| 102 | + uint slice_num = pwm_gpio_to_slice_num(IROUT); |
| 103 | + pwm_set_wrap(slice_num, 125000000 / frequency); |
| 104 | + pwm_set_chan_level(slice_num, PWM_CHAN_A, 125000000 / (2 * frequency)); |
| 105 | + pwm_set_enabled(slice_num, true); |
| 106 | + |
| 107 | +#endif |
| 108 | +} |
0 commit comments