how to make Arduino AVR Board code work for Arduino ARM board

I have previously worked with Arduino Uno and got this code

#define resolution 8
#define mains 50 // 60: north america, japan; 50: most other places

#define refresh 2 * 1000000 / mains


void setup() {
  Serial.begin(115200);

  // unused pins are fairly insignificant,
  // but pulled low to reduce unknown variables
  for (int i = 2; i < 14; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }

  for (int i = 8; i < 11; i++)
    pinMode(i, INPUT);

  startTimer();
}

void loop() {
  Serial.print(time(8, B00000001), DEC);
  Serial.print(" ");
  Serial.print(time(9, B00000010), DEC);
  Serial.print(" ");
  Serial.println(time(10, B00000100), DEC);

}

long time(int pin, byte mask) {
  unsigned long count = 0, total = 0;

  while (checkTimer() < refresh) {
    // pinMode is about 6 times slower than assigning
    // DDRB directly, but that pause is important
    int PORTB;
    pinMode(pin, OUTPUT);
    PORTB = 0;
    pinMode(pin, INPUT);
    int PINB;
    while ((PINB & mask) == 0)
      count++;
    total++;
  }
  startTimer();
  return (count << resolution) / total;
}

extern volatile unsigned long timer0_overflow_count;

void startTimer() {
  int TCNT0;
  timer0_overflow_count = 0;
  TCNT0 = 0;
}

unsigned long checkTimer() {
  int TCNT0;
  return ((timer0_overflow_count << 8) + TCNT0) << 2;
}

Please it would be really nice if someone could help me figure out how to make this code work in Arduino Due

Welcome to the forum.

Sometimes it is better to start from scratch. My gut feeling says this is one of those times. The code is extremely low level and does some not so clever things.

Your question and the code let me suspect you did not write the code yourself. Am I right?

Do you know what you would like the code to do and what additional functionality you want to add? Otherwise, why would you use a more powerful processor?

If you are new to the Arduino Due, I would recommend working through a couple of examples and learn about the basics. Then start writing the new code as good as you can. If you have some specific question I will try to help.

If you share your code, I can test it on my board and provide feedback on how to improve.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.