Bitbanging ports on Mega2560, and timer inturupts?

I AM A NOOB TO THE ARDUINO.
I know a little about using other MCU boards, and similar, though the Arduino is a new realm for me.

This is part of:
This is my first Arduino project, just getting my feet wet. This is part of the third version of my 3D-Printer firmware, for my third 3D printer design, and the first that I am getting ready to release under a BSD license on Thingiverse. The HW is mostly printable (no screws, no bolts, no nuts, etc), and is designed to be highly extensible along the Z-Axis (that is it can be easily extended in the height of printing).

My user-name here takes from the fact I am playing with 3D printers (the 3D part), and the goal of printing an object that is 8192 mm in height on a version of the printer I am getting ready to release.

The 3D printer works well with earlier versions of my firmware (running on AVR in AVR asm, and Raspberry Pi under RISC OS), though part of the design goal is to use an Arduino for the controller, with completely new firmware.

What I want to do:
I would like to directly bitbang a few steppers attached through ULN2803's to an Arduino Mega2560, in a timer interrupt routine. Something along the lines of (do not know if the following can work, though it is an outline):

//Move the steppers as needed, called from a timer interrupt every
//  0.1 milliseconds.
StepperMv(){
  ++StpTimer;

  if (StpTimer >= XTime  && XDist){
    XDist--; XTime += XDelay;
    XDelay = (XDlyMin < XDelay) ? (XDelay - (XDelay >> 2)) : (XDlyMin);
    YStep = (XStep + XDir) & 7 & !(abs(XDir)-1);
    PORTB = (PORTB & 0xF0) | Steps[XStep];
  }

  if (StpTimer >= YTime && YDist){
    YDist--; YTime += YDelay;
    YDelay = (YDlyMin < YDelay) ? (YDelay - (YDelay >> 2)) : (YDlyMin);
    YStep = (YStep + YDir) & 7 & !(abs(YDir)-1);
    PORTB = (PORTB & 0x0F) | Steps[YStep] << 4;
  }

  if (StpTimer >= ZTime && ZDist){
    ZDist--; ZTime += ZDelay;
    ZDelay = (ZDlyMin < ZDelay) ? (ZDelay - (ZDelay >> 2)) : (ZDlyMin);
    ZStep = (ZStep + ZDir) & 7 & !(abs(ZDir)-1);
    PORTC = (PORTC & 0x0F) | Steps[ZStep];
  }

  if (StpTimer >= ETime && EDist){
    EDist--; ETime += EDelay;
    EDelay = (EDlyMin < EDelay) ? (EDelay - (EDelay >> 2)) : (EDlyMin);
    EStep = (EStep + EDir) & 7 & !(abs(EDir)-1);
    PORTC = (PORTC & 0xF0) | Steps[EStep] << 4;
  }
}

So bitbanging the ports directly.

The Problems:
1 : I can not find the Arduino way to set up a timer interrupt.
2 : I can not find how the ports of the AVR Mega2560 map to the pins of the Arduino Mega2560.

A plea
Can someone please help with this issue? At least point me in the correct direction, please?

@J-M-L
Thank you for that.

Now just looking for the information on the Arduino way of doing an interrupt handler for a timer interrupt (so i can use the timer to run a function like that every 100 microseconds).

You do not need an interrupt for this.
Use milis() and some code.
See:

.

LarryD:
You do not need an interrupt for this.
Use milis() and some code.
See:
Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

.

I do need a timer interrupt, as other code will need to be running that can take longer than many steps, and is non-predictable. I need to separately interpret G-Code, read the values of 2 thermistors, update two PWM outputs that control current to two heating elements, update a display device, scan for button press on other pins, and read from a file on an SD-Card, and do it all asynchronously.

You can read this to understand the timers concept and get started

I can not find the Arduino way to set up a timer interrupt.

Is something wrong with GOOGLE today?

Mark

I can not find the Arduino way to set up a timer interrupt.

J-M-L:
You can read this to understand the timers concept and get started

Thank you. Though that says to do it the AVR way, not the Arduino way (Arduino is Software, Libs, and HW toga
ether), and I am attempting to write this firmware for the Arduino.

Though that article at least has a link to a third party Arduino library for dong the job of timer interrupts. I am surprised that timer interrupts are not part of the core, as they are very important to the platform.

Thank you, though not quite what I am looking for.

I want to do it the Arduino way not the AVR way (I know the ATMega2590 AVR is the CPU on the board). Timer1/Timer3 libraries look like what I am looking for, they do it the Arduino way.

While it is likely that most whom use my firmware will be using AVR based Arduino boards, I still like the portable nature of the Arduino (as an extended version of Wiring) method of doing things. Hence the goal of using an Arduino for this firmware.

Thanks for the help. I think I have it now.

I am going to be adding a module test to make sure, though the stepper code should be well in place (ignore all the defines that are for other things, not yet in this sketch):

#include "TimerOne.h"

//Define the AVR port for the X and Y steppers.
#define STEPPORT0 PORTA
#define STEPDIR0  DDRA

//Define the AVR port for the Z and E steppers.
#define STEPPORT1 PORTC
#define STEPDIR1  DDRC

//Define the pins for the end stops.
#define XSTOP 50
#define YSTOP 51
#define ZSTOP 52

//Define the pin for the bed leveling switch.
#define BEDLVL 53

//Define the pins for PWM output going to the heating elements.
#define PINHOTEND 8
#define PINHOTBED 9

//Define the Arduino pins for the thermistor readings.
#define PINE_TEMP A0
#define PINB_TEMP A1

//Define the base pin for the Display pins.
#define PINDISP  14

//Some macros.
#define ABS(v) ((v < 0) ? (-v) : (v))


//Global Variables.
unsigned long StpTimer;           //Keep time for stepping.
long XDist, YDist, ZDist, EDist;  //Keep tract of how far we have to move.
long XDelay,YDelay,ZDelay,EDelay; //Current delay between steps in 0.125
                                  // millisecond increments.
long XDlyMin,YDlyMin,ZDlyMin,EDlyMin;  //Shortest delay to achieve needed speed.
long XStep,YStep,ZStep,EStep;     //Index into Steps[] for current step.
long XDir,YDir,ZDir,EDir;         //Direction of movement, 1=HalfStep forward,
                                  // 2=FullStepForward, -1=HalfStep Backard,
                                  // -2=FullStep Backward.
long XTime,YTime,ZTime,ETime;     //Time to do next step, as time is tracked in
                                  //  StpTimer.

//The output pin values for half stepping a unipolar stepper using four pins.
long Steps[7] = {0x09, 0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01};

setup(){

  // Set the Stepper motor control pins to outputs.
  STEPDIR0 = 0x0FF;
  STEPDIR1 = 0x0FF;

  Timer1.initialize();
  Timer1.attachInterrupt(StepperMv,125);
}


loop(){
}


//Move the steppers as needed, called from a timer interrupt every
//  0.1 milliseconds.
StepperMv(){
  ++StpTimer;

  if (StpTimer >= XTime  && XDist){
    XDist--; XTime += XDelay;
    XDelay = (XDlyMin < XDelay) ? (XDelay - (XDelay >> 2)) : (XDlyMin);
    YStep = (XStep + XDir) & 7 & !(ABS(XDir)-1);
    STEPPORT0 = (STEPPORT0 & 0xF0) | Steps[XStep];
  }

  if (StpTimer >= YTime && YDist){
    YDist--; YTime += YDelay;
    YDelay = (YDlyMin < YDelay) ? (YDelay - (YDelay >> 2)) : (YDlyMin);
    YStep = (YStep + YDir) & 7 & !(ABS(YDir)-1);
    STEPPORT0 = (STEPPORT0 & 0x0F) | Steps[YStep] << 4;
  }

  if (StpTimer >= ZTime && ZDist){
    ZDist--; ZTime += ZDelay;
    ZDelay = (ZDlyMin < ZDelay) ? (ZDelay - (ZDelay >> 2)) : (ZDlyMin);
    ZStep = (ZStep + ZDir) & 7 & !(ABS(ZDir)-1);
    STEPPORT1 = (STEPPORT1 & 0x0F) | Steps[ZStep];
  }

  if (StpTimer >= ETime && EDist){
    EDist--; ETime += EDelay;
    EDelay = (EDlyMin < EDelay) ? (EDelay - (EDelay >> 2)) : (EDlyMin);
    EStep = (EStep + EDir) & 7 & !(ABS(EDir)-1);
    STEPPORT1 = (STEPPORT1 & 0xF0) | Steps[EStep] << 4;
  }
}

Just need to add some code to move the steppers around, make sure everything is correct, and then it will be on to adding everything else (the rest should be easy).