Hello,
I am trying to implement a timing-critical application with an Arduino Nano Every.
I want to detect an external pulse and output several pulses at fixed intervals after this input pulse. I have connected my input signal (5V, 1.2 µs pulse at ~ 100 kHz) to pin D11 (PE0) (with pull-down resistor 4.7 kOhm to GND) and output my programmed signal via PIN D17 (PD0).
I have relatively little experience, I mainly used the youtube-tutorials by Anas Kuzechie and the AVR Instruction Set Manual to get started with assembly programming.
For the most part the code does what it's supposed to, but I can't get the time delay between my input pulse and the output under control (the absolute value doesn't matter, but I need it to be reasonably constant). Here are a few oscilloscope images to clarify.
This is my code:
ino-File:
//-------------------------
// C Code for trigger pulse manipulation
//-------------------------
extern "C"
{
void start();
void trigger();
}
//----------------------------------------------------
void setup()
{
start();
}
//----------------------------------------------------
void loop()
{
trigger();
digitalWrite(LED_BUILTIN, HIGH); //turn on LED if this section is reached
delay(10000);
}
S-File:
;---------------
; Assembly Code
;---------------
#include "avr/io.h"
;------------------------
.global start
.global trigger
;------------------------
start:
LDI R20, 0x01 ;load "00000001" into register 20
OUT VPORTD_DIR, R20 ;set PORTD0 to output
LDI R20, 0xFE ;load "11111110" into register 20
OUT VPORTE_DIR, R20 ;set PORTE0 to input, 1-7 to output
LDI R21, 0x00 ;load "00000000" into register 21
LDI R22, 0x00 ;load "00000000" into register 22
RET ;return to setup() function
;---------------------------------------------------------------------------
trigger: ;5 cycles if true 10 cycles if false
LDI R20, 0x01; 1 cycle
LDI R22, 0x00; 1 cycle
IN R21, VPORTE_IN ;Read Vport E and store to R21 1 cycle
CPI R21, 0x01 ;Compare R21 to "00000001" 1 cycle
BREQ pulse ;if input is high -> go to pulse 1 cycle if true, 2 cycles if false
OUT VPORTD_OUT, R22 ;output of bit pattern "00000000" to all d-pins 1 cycle
NOP; 1 cycle
RJMP trigger ; else -> trigger loop 2 cycles
RET ;return to loop() function ; Should not be reached
;---------------------------------------------------------------------------
pulse: ;15 cycles
OUT VPORTD_OUT, R20 ;output of bit pattern "00000001" to all d-pins OUTPUT ON 1 cycle
NOP; 1 cycle
OUT VPORTD_OUT, R22 ;output of bit pattern "00000000" to all d-pins OUTPUT OFF 1 cycle
NOP; 1 cycle
OUT VPORTD_OUT, R20 ;OUTPUT ON 1 cycle
NOP; 1 cycle
OUT VPORTD_OUT, R22 ;OUTPUT OFF 1 cycle
NOP; 1 cycle
OUT VPORTD_OUT, R20 ;OUTPUT ON 1 cycle
NOP; 1 cycle
OUT VPORTD_OUT, R22 ;OUTPUT OFF 1 cycle
NOP; 1 cycle
NOP; 1 cycle
RJMP trigger ; 2 cycles
RET ;return to loop() function ; Should not be reached
;--------------------------------------------------------------------------------------------------------------------------------------------
Do you notice anything that could explain this jitter? Is there something i'm missing that I need to do to compile the code correctly? I simply upload it via the Arduino IDE.
Or is it simply not possible to achieve single-clock accuracy with the Nano Every?
I would like to use NOP-Statements to adjust my timings (pulse widths and intervals) for the application but with this amount of jitter it is kind of pointless. The pulse durations of 125 ns match to clock cycles at 16 MHz so I don't think i'm too far off...
I am grateful for any advice!
Many thanks in advance!
Andy







