How do I write a subroutine in the 8085 architecture

Arduino subroutines




Google search on MEINE-SCHALTUNG.de:





Perpetual calendar


The subroutines are a great aid to programming. If parts of the program appear in a sketch that are repeated several times, it makes sense to combine them and outsource them. As a result, a main program gains simplicity and clarity. In addition, the memory for the program code is saved. A very big advantage of a subroutine is its reusability. Therefore, it makes sense to create solid documentation for more extensive subroutines. Subroutines are widely used in other programming languages ​​and are named differently. One encounters designations such as procedure, function, routine, subroutine, method, module, etc. Subroutines are called with a command. Basically there is the possibility of transferring various variables to a subroutine with which the subroutine can work. It is also possible to retrieve calculated values ​​from a subroutine.

Example without return:


  // ----------------------------------------------------------
// Subroutine "Diode" without return values


int LedPin [] = {10, 11, 12};
int buttonPin [] = {42, 43, 44};
int i;

void setup () {
for (i = 0; i<3; i++)="" {="">
pinMode (LedPin [i], OUTPUT);
pinMode (button pin [i], INPUT_PULLUP);
    }
  }

void loop () {
for (i = 0; i<3; i++)="" {="">
if (digitalRead (TasterPin [i]) == HIGH) {
Diode (LedPin [i], 0);
      }
    }
if (digitalRead (TasterPin [0]) == LOW) {
Diode (LedPin [0], 25);
    }
if (digitalRead (TasterPin [1]) == LOW) {
Diode (LedPin [1], 150);
    }
if (digitalRead (TasterPin [2]) == LOW) {
Diode (LedPin [2], 250);
    }
  }

void diode (int PinNr, int value) {
analogWrite (PinNr, value);
  }

  // ----------------------------------------------------------

The task of the main program is to switch on a light-emitting diode with different brightness when a switch is pressed. In two arrays we first name the PWM pins to which we want to connect the diodes (with series resistor) and the pins to which our switches are connected. In void setup () we declare the pins as outputs and inputs accordingly. In void loop () we first switch off the diodes whose switches are not activated. We then use If queries to examine whether a switch has been operated. As soon as this is the case, a diode is switched on with a specified brightness. In both cases, diode off and diode on, we call the "Diode" subroutine.
Switching the diodes on and off is done with the "Diode" subprogram. We can freely choose the name of a subroutine. The structure of "Diode" is similar to that of void setup () and void loop (). So that our subroutine knows which diode should be switched on with which brightness, we transfer two variables to the "Diode" subroutine. There are PinNr and value. It is not absolutely necessary to transfer variables to a subroutine. In this example, the subroutine writes the transferred variables in the analogWrite () instruction and executes them.

Example with return:

It is possible to run a subroutine, e.g. a mathematical calculation, and then retrieve the result. The second example has been expanded a little to illustrate this property. Now, after the first switch has been actuated, we also want to see on the serial monitor the brightness with which, expressed in%, the first LED is lit. The corresponding calculation is carried out in the "Diode" sub-program. For this we define a new integer variable "Percent". Now it says: int percent = value * 100/255;. We can then find out the result of the operation by calling "Diode". With int diode (int PinNr, int value) we declare the type of the result. "Diode" is now called with int x = diode (LedPin [0], 25);. The result (return value of the function) is hidden in the variable x defined here and can be used further.


  // ----------------------------------------------------------
// Subprogram "Diode" with return value


int LedPin [] = {10, 11, 12};
int buttonPin [] = {42, 43, 44};
int i;

void setup () {
Serial.begin (9600);
for (i = 0; i<3; i++)="" {="">
pinMode (LedPin [i], OUTPUT);
pinMode (button pin [i], INPUT_PULLUP);
    }
  }

void loop () {
for (i = 0; i<3; i++)="" {="">
if (digitalRead (TasterPin [i]) == HIGH) {
Diode (LedPin [i], 0);
      }
    }
if (digitalRead (TasterPin [0]) == LOW) {
int x = diode (LedPin [0], 25);
Serial.print ("The brightness of the first diode in% is:");
Serial.println (x);
    }
if (digitalRead (TasterPin [1]) == LOW) {
Diode (LedPin [1], 150);
    }
if (digitalRead (TasterPin [2]) == LOW) {
Diode (LedPin [2], 250);
    }
  }

int diode (int PinNr, int value) {
analogWrite (PinNr, value);
int percent = value * 100/255;
return percent;
  }

  // ----------------------------------------------------------


Other topics:




Google search on MEINE-SCHALTUNG.de: