Hi Everybody,
I'm a noob in arduino and this is my first post.
I've searched a lot in this forum and in google, but I can't find a solution to my problem.
In this project, I'm trying to measure the number of pulses in a 1 MHz square wave.
So, for that, I created a square wave in the digital port 7 and read the same wave in the digital port 13.
volatile unsigned int counter = 0 ;
const int z = 13;
void setup() {
Serial.begin(115200);
delay(100);
int32_t mask_PWM_pin = digitalPinToBitMask(7);
REG_PMC_PCER1 = 1<<4; // activate clock for PWM controller
REG_PIOC_PDR |= mask_PWM_pin; // activate peripheral functions for pin (disables all PIO functionality)
REG_PIOC_ABSR |= mask_PWM_pin; // choose peripheral option B
REG_PWM_CLK = 0; // choose clock rate, 0 -> full MCLK as reference 84MHz
REG_PWM_CMR6 = 0<<9; // select clock and polarity for PWM channel (pin7) -> (CPOL = 0)
REG_PWM_CPRD6 = 80; // initialize PWM period -> T = value/84MHz (value: up to 16bit), value=80 -> 1.05MHz
REG_PWM_CDTY6 = 40; // initialize duty cycle, REG_PWM_CPRD6 / value = duty cycle, for 80/40 = 50%
REG_PWM_ENA = 1<<6; // enable PWM on PWM channel (pin 7 = PWML6)
delay(10);
pinMode(z, INPUT);
attachInterrupt(z, zrising, FALLING);
}
void loop() {
Serial.println(counter);
delay(10);
}
void zrising() {
counter++;
}
The wave has the right frequency but the number of pulse is incorrect.
Lowering the frequency until 100KHz, the digital pin 13 has a correct count.
So my questions are:
1 - What's the maximum sampling frequency when I use the attachInterrupt?
2 - Someone of you have another solution with/without interrupts?
Thanks ![]()