Program modification

Hello!

I am begginer in Arduino programming :slightly_frowning_face:
I would like to ask you for some advice how to rewrite program which runs on AT89051 to run on Mega 2560 Board…
Program is used in wiring tester for some cables. Program checks if it gets same result in inputs as if it was generated in output .

How to start with structures and modification? :-[

#include "io51.h"

#define O1   P3.0
#define O2   P3.1
#define O3   P3.2
#define O4   P3.3
#define O5   P3.4
#define O6   P3.5

#define LED  P3.7

#define I1   P1.5
#define I2   P1.2
#define I3   P1.6
#define I4   P1.7
#define I6   P1.4
#define I5   P1.3

struct port
 {
 unsigned char s0: 1;
 unsigned char s1: 1;
 unsigned char s2: 1;
 unsigned char s3: 1;
 unsigned char s4: 1;
 unsigned char s5: 1;
 unsigned char s6: 1;
 unsigned char s7: 1;
 };

union byte
  {
  char ch;
  struct port bits;
  };

long i;
char j;
char b;
char con;

main ()
  {
  con = 1;

  /* Led ON */
  LED = 0;
  for(i=0; i<3333; i++);
  LED = 1;

  /* Run test of wiring. */
  for(j=0; j<64; j++)
    {
    SetOut(j);
    b = ReadIn();
    if(b!=j) con = 0;
    }

  if(con==1) LED=0;
  else
  {
    while (1)
    {
      LED = 0;
      for (i=0; i<1000; i++);
      LED = 1;
      for (i=0; i<1000; i++);
    }
  }
  /* IDLE mode */
  while ( 1 )
    {
    }

  } /* end main */
/*--------------------------------------------------------------------*/
SetOut(z)
  char z;
  {
  union byte p;
  p.ch = z;
  O1 = p.bits.s0;
  O2 = p.bits.s1;
  O3 = p.bits.s2;
  O4 = p.bits.s3;
  O5 = p.bits.s4;
  O6 = p.bits.s5;
  }

/*--------------------------------------------------------------------*/
ReadIn()
  {
  union byte in;
  in.bits.s0 = I1;
  in.bits.s1 = I2;
  in.bits.s2 = I3;
  in.bits.s3 = I4;
  in.bits.s4 = I5;
  in.bits.s5 = I6;
  return in.ch;
  }
/*--------------------------------------------------------------------*/

Program is used in wiring tester for some cables. Program checks if it gets same result as if it was generated in input

This almost goes right over my head but it's more of a glancing blow. I've read this sentence about a dozen times. Each time I ALMOST understand what you're saying, but not quite.

And BTW I doubt anyone will be able to help you with this without the contents of io51.h

and PLEASE. Enclose all code within [code] and [/code] tags.

All it is doing is testing that when a bit pattern (from 0 to 63) is set on 6 output pins, the same pattern is read back on 6 input pins. If it fails, it loops forever flashing the LED on and off. All you have to do is decide which Arduino pins will be the outputs and which will be the inputs. The programming is simple and is left as an exercise for the reader.

Pete

byanco:
How to start with structures and modification? :-[

Is that a fully working program?

Do you have a schematic of the cable tester hardware circuit?

I don’t know the controller and the compiler used, but I cannot detect anything about activating internal pull-up resistors in the software, so I suppose: The cable tester hardware must provide either external pull-down or external pull-up resistors with each connection.

Otherwise the tester software might possibly not detect some defective cables when reading “floating input” pins. In that case you’d better write an improved Arduino version.

Of course is simple to rewrite the program for the Arduino. An UNO or other Atmega328 board is enough, as the program just needs 13 pins: 12 for cable testing and 1 for LED. The UNO provides 18 pins free for usage and two RX/TX pins for Serial. So an UNO will do easily.

The main test is in this loop:

/* Run test of wiring. */
  for(j=0; j<64; j++)
    {
    SetOut(j);
    b = ReadIn();
    if(b!=j) con = 0;
    }

The 6 lines are used as a 6-bit counter, counting up from 0 to 63 and setting the “bits” (meaning the outputs). After each bit-pattern is set, the bit-pattern is read from the input. If the bit-pattern is different, the test will fail (“if(b!=j) con = 0;”).

Yes this is fully working programm. Cables are wired directly on cable or on wired circuit which is tested.
Problem is that i dont know yet how to define pins in structure as it is made in this programm :-![

#include "io51.h"

#define O1   P3.0
#define O2   P3.1
#define O3   P3.2
#define O4   P3.3
#define O5   P3.4
#define O6   P3.5

#define LED  P3.7

#define I1   P1.5
#define I2   P1.2
#define I3   P1.6
#define I4   P1.7
#define I6   P1.4
#define I5   P1.3

struct port
 {
 unsigned char s0: 1;
 unsigned char s1: 1;
 unsigned char s2: 1;
 unsigned char s3: 1;
 unsigned char s4: 1;
 unsigned char s5: 1;
 unsigned char s6: 1;
 unsigned char s7: 1;
 };

union byte
  {
  char ch;
  struct port bits;
  };

byanco: Yes this is fully working programm.

On an Arduino?

No it works on AT89C4051 microcontroller.. this is reason for expanding I/O and rewriting programm for using with arduino boards.

byanco:
Yes this is fully working programm. Cables are wired directly on cable or on wired circuit which is tested.
Problem is that i dont know yet how to define pins in structure as it is made in this programm :-[

OK, I did my best to rewrite the program for Arduino.

This is my try:

// define Arduino output pins, avoid pin 0/1 which is Serial RX/TX
byte outputPins[]={2, 3, 4, 5, 6, 7}; // Output pins 2...7
byte inputPins[]={A0,A1,A2,A3,A4,A5};// Input pins A0...A5

#define LED 13  // LED on pin 13

char con;

void setup()
{
  for (int k=0;k<6;k++)
  {
    pinMode(outputPins[k], OUTPUT);
    pinMode(inputPins[k], INPUT); // or maybe INPUT_PULLUP?
  }
  pinMode(LED,OUTPUT);
  con = 1;

  /* Led ON */
  digitalWrite(LED, HIGH);

  /* Run test of wiring. */
  for(int j=0; j<64; j++)
  {
    // SetOut(j);
    for (int k=0;k<6;k++) 
    {
      digitalWrite(outputPins[k], bitRead(j,k));
    }
//    b = ReadIn();
    int b=0;  
    for (int k=0;k<6;k++)
    {
      if (digitalRead(inputPins[k])) b+= 1<<k;
    }
    if(b!=j) con = 0;
  }
  // Added programming logic for safety reasons: 
  // Finally set all pins to INPUT after testing (except LED pin)
  for (int k=0;k<6;k++)
  {
    pinMode(outputPins[k], INPUT);
  }
}

void loop()
{
  /* IDLE mode after test is finished */
  if(con==1) digitalWrite(LED,LOW);
  else
  {
    digitalWrite(LED,LOW);
    //  for (i=0; i<1000; i++); // I don't know how long this is???
    delay(250); // perhaps 250 ms?
    digitalWrite(LED,HIGH);
    //  for (i=0; i<1000; i++); // I don't know how long this is???
    delay(250); // perhaps 250 ms?
  }
}

The blink delay is just a guess (0.25s OFF and 0.25s ON cycle after a bad test).
In the original code the delay depends on how fast the program is running.
I don’t know that.

I also don’t know what is a “wired circuit which is tested”, so the correct pinMode is perhaps INPUT if the “wired circuit which is tested” has pull-down resistors to pull the lines down while testing.

One thing has been added by me, which is not contained in the original source code: After the test is finished and the controller enters its final loop (LED off or LED blinking), I set all OUTPUTs to INPUT state.

I try to explain.
If you say this is for a cable tester, the original source code is a very bad cable tester.
As a cable tester the code is OK for testing:

  • cables for being not connected
  • cables beeing wrong connected (i.e. 2nd Output to 4th Input or something like that)

But the original cable tester is very bad in testing:

  • cables providing a short circuit between cables
    In that case, some microcontroller outputs may be operated at a short circuit current which is much higher than the allowed current. So when testing a short circuit cable this may damage the microcontroller used for testing!

I think, this should never happen, that the tested device (cable) can damage the testing device (microcontroller), but unfortunately your code is able to do so. And worse than that during test: The short circuit state may continue after the test has been finished.

As a simple countermeasure I finish the test with switching all OUTPUT to INPUT, so any possible short circuit due to faulty (short-circuit) cables is shut off before entering the final loop. So perhaps those strong little AVR controllers may survive even short-circuit cables to be connected during test.

But in general, if I made a “cable tester” code, it would be much different from that code, so that in no case and with no possible fault the cable could damage the microcontroller.

Thank you for your answer! I have tested program on Arduino2560 and it works ok :grin:
Device will be used for testing not connected cables, and for testing corect wiring on some connectors. As i have tested it detects shorts between wires.

Next functionalty will be print of results which wires are wired wrong and detected shorts … but it will take some time… :wink:

byanco: Thank you for your answer! I have tested program on Arduino2560 and it works ok :grin:

That's exactly what I had expected. :grin:

byanco: As i have tested it detects shorts between wires.

Yes, of couse it will "detect shorts between wires".

As a shortened wire with a short circuit against LOW will not be able to pull the shortened output to a HIGH with the corresponding input, this will be detected.

But if you shorten OUTPUT/HIGH against OUTPUT/LOW on your Atmega microcontroller, the resulting short-circuit current between those pins is surely out of the specified range (absolute max. 40mA).

I've tried to keep times very short during measure (a few microseconds) and at the end of the cycle I set everythng to safe 'INPUT' state. But that's not how you should test cables for shortened wires.

Anyway. It works and I don't think it will damage the Atmega within a few microseconds of over-current in case of shortened wires. Over-current will only occur during testing cables with shortened wires.

In all other test cases like - cable is OK - cable wires are not connected - cable wires are connected in wrong cross-over connections everything is fine during the test.

byanco: Next functionalty will be print of results which wires are wired wrong and detected shorts ..... but it will take some time

You leave the simple things done by me, and the complicated things will be worked out by you. That sounds fine - for me! ;D