Klausj
Published © GPL3+

Arduino Single Step with variable Speed

You can replace the crystal by a push-button or a NE555, but after a reset you need more than 100000 pulses before you reach setup function.

AdvancedFull instructions provided2 hours30
Arduino Single Step with variable Speed

Things used in this project

Story

Read more

Code

Invoke1

Arduino
Code for auxiliary Arduino to drive the main Arduino
/*
  Single-Step, Einzel-Schritt
  Programm, um mit fallender Flanke an Pin-12
  am Quarz-Eingang (Pin-10) eines zweiten
  ATmega328 den Program-Counter zu
  incrementieren und eine neue CPU-Instruktion
  zu lesen und auszufhren.
  controlling "singleStepPortBdefine.ino"
*/

const byte clock = 12;
const byte inSetup = 8;
const byte inLoop = 7;
const byte set = 2;
const int time = 500;
volatile boolean starting = true;

void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  // visible control:
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(inSetup, OUTPUT);
  pinMode(inLoop, OUTPUT);
  digitalWrite(inSetup, HIGH);
  // drive slave:
  pinMode(clock, OUTPUT);
  // enable ISR:
  pinMode(set, INPUT_PULLUP);
  EICRA = 2; // falling
  EIMSK = 1; // enable ISR INT0
  long count = 0;
  // execute bootloader fast:
  while (starting) {
    digitalWrite(clock, HIGH);
    digitalWrite(clock, LOW);
    count++;
  }
  Serial.print("clocks before end of setup ");
  Serial.println(count);
  digitalWrite(inSetup, LOW);
  digitalWrite(inLoop, HIGH);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(clock, HIGH);
  delay(time);
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(clock, LOW);
  delay(time);
}

ISR(INT0_vect) {
  // terminate setup
  starting = false;
}

Example code to be examined

Arduino
A few lines of assembly code to be executed step by step
/*
  Insert crystal for uploading, then feed 
  pulses to TOSC = PB6 = pin-10 of DIL-28-chip.
  The statements in the define are getting
  executed without call and return instructions
  controlled by "invoke.ino"
*/

#define LOAD_CONSTANTS \
  asm  ( \
         "ldi r16, 1 \n"   \
         "ldi r17, 2 \n"   \
         "ldi r18, 4 \n"   \
         "ldi r19, 8 \n"   \
         "ldi r20, 16 \n"  \
         "ldi r21, 32 \n"  \
       );

#define RUNNING_LIGHT   \
  asm ( \
        "out 0x05, r16 \n"  \
        "out 0x05, r17 \n"  \
        "out 0x05, r18 \n"  \
        "out 0x05, r19 \n"  \
        "out 0x05, r20 \n"  \
        "out 0x05, r21 \n"  \
      );

const byte LED3 = 7;

void setup() {
  pinMode(LED3, OUTPUT);
  digitalWrite(LED3, HIGH);
  //Serial.begin(9600);
  //Serial.println(__FILE__);
  //Serial.println("x");
  digitalWrite(LED3, LOW);
  DDRB = 0xFF;
  // force long delays:
  DDRD = 4; // pinMode(2, OUTPUT);
  DDRD = 0;
  LOAD_CONSTANTS
  // no C code from here on !
  // this is the loop:
label:
  RUNNING_LIGHT
  goto label;
}

void loop() {}

Example using PORTC

Arduino
Due to different layout it is more easy to use PORTC in the breadboard version.
/*
  Insert crystal for uploading, then feed 
  pulses to TOSC = PB6 = pin-10 of DIL-28-chip.
  The statements in the define are getting
  executed without call and return instructions
  controlled by "invoke.ino"
*/

#define LOAD_CONSTANTS \
  asm  ( \
         "ldi r16, 1 \n"   \
         "ldi r17, 2 \n"   \
         "ldi r18, 4 \n"   \
         "ldi r19, 8 \n"   \
         "ldi r20, 16 \n"  \
         "ldi r21, 32 \n"  \
       );

#define RUNNING_LIGHT   \
  asm ( \
        "out 0x05, r16 \n"  \
        "out 0x05, r17 \n"  \
        "out 0x05, r18 \n"  \
        "out 0x05, r19 \n"  \
        "out 0x05, r20 \n"  \
        "out 0x05, r21 \n"  \
      );

const byte LED3 = 7;

void setup() {
  pinMode(LED3, OUTPUT);
  digitalWrite(LED3, HIGH);
  //Serial.begin(9600);
  //Serial.println(__FILE__);
  //Serial.println("x");
  digitalWrite(LED3, LOW);
  DDRB = 0xFF;
  // force long delays:
  DDRD = 4; // pinMode(2, OUTPUT);
  DDRD = 0;
  LOAD_CONSTANTS
  // no C code from here on !
  // this is the loop:
label:
  RUNNING_LIGHT
  goto label;
}

void loop() {}

Credits

Klausj
95 projects • 8 followers

Comments