Code does not behave as expected

sterretje edit: topic split from Arduino Nano compatible uploaded succesfully but doesn't work

I have this problem too, I have a simple sketch that is supposed to toggle a pin a couple of times then loop forever to test execution speed but it won't run. Makes me want to revert to a standard C compiler so I have wysiwig and know what's running. This is mine:

void setup()
{
  // put your setup code here, to run once:
  pinMode(PB5, OUTPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
uint32_t Input_A = 1235;
uint32_t Input_B = 9876;
uint32_t Output;

  digitalWrite(PB5, HIGH);
  Output = Input_A * Input_B;
  digitalWrite(PB5, LOW);
  Output = Input_A / Input_B;
  digitalWrite(PB5, HIGH);

  while(1);
}

The idea is that there should be no ints, no fluff, just the code I put there.

@etmax1 A port bit cannot be written to that way.

@etmax1

You might have a similar problem but I doubt it's the same problem as in the topic that you hijacked; hence your question was given its own topic.

Please do not hijack topics.

What, exactly then, does it do? What does it not do? What are you expecting it to do?

Because when I run your code, I/O pin 5 (PB5 has the value 5) starts LOW, goes HIGH, then LOW again and finally stays HIGH. Exactly as I'd expect.

So what were you expecting?

Too fast too see.

void setup()
{
  Serial.begin(115200);
  pinMode(7, OUTPUT);
}

void loop() 
{
  uint32_t Input_A = 1235;
  uint32_t Input_B = 9876;
  uint32_t Output;

  Serial.println(Input_A * Input_B);  // 12196860
  Serial.println(Input_A / Input_B);  // 0  

  digitalWrite(7, HIGH);
  delay(777);
  Output = Input_A * Input_B;
  digitalWrite(7, LOW);
  delay(777);
  Output = Input_A / Input_B;
  digitalWrite(7, HIGH);

  while(1);
}

a7

1 Like

Whats more, since Output isn't used anywhere it gets optimized away and those steps probably get left out of the compiled code.

But -- "PB5" is 'D13'. The PB (PC, PD) number is the bit in the Port assignment (see Port Manipulation)
https://docs.arduino.cc/retired/hacking/software/PortManipulation/

I invite you to try the following on a classic Nano or Uno R3:

void setup() {
   Serial.begin(115200);
   Serial.println(PB5);
}

void loop() {
}
1 Like

It prints out 5.
If the OP thinks he's supposed to see blinking on PB5, which is D13 (the on-board LED), using his sketch then that will not happen.

That was exactly @van_der_decken's point :wink:

I thought that I was the invitee.

My apologies, not intentional.

I'm sorry, I'm not expecting to see on the LED (D13) anything significant beyond my pin toggles, but actually I do, when it powers up the LED blinks roughly the same as it did before I downloaded my sketch.

I did this same test program on an Infineon XMC and got (from memory) ~80us high and low which is the time to do the calculation plus the time to change the state of the I/O pin. On this it's like my script hasn't been downloaded as the LED blinks in the 50ms to 500ms range but there are no sub 1ms pulses I would have expected and after it gets to the while(1) I expect it to not be able to do anything else yet the LED occasionally flashes again as if I don't have the script loaded. Using serial is no good to me because the latency in the serial routines are not documented so I would need to set up timers to measure the time internally and I would not know what "fluff" there was in all that.

On the "optimised out" suggestion, in C I would use volatile so I will see if that's the case in Arduino as well. On an AVR using bare metal programming an I/O line set/reset should execute in 1 clock cycle so be negligible.

So anyway, the basic idea is set a pin, do something, clear the pin and measure the width on a scope to know run time for the given clock speed. In my example I was wanting to measure integer divide and multiply. in the same program.

void loop() 
{
  // put your main code here, to run repeatedly:
uint32_t Input_A = 1235;
uint32_t Input_B = 9876;
uint32_t Output;

  bitSet(PORTB,5);
  Output = Input_A * Input_B;
  bitClear(PORTB,5);
  Output = Input_A / Input_B;
  bitSet(PORTB,5);

  while(1);
}

I tested this again with volatile set for the output variable and it behaved the same, the LED gets pulsed by some internal startup functions (maybe in th BL) and then seems to do nothing further?

I would have expected it to do the 3 BL pulses then remain high until something else drives it which should be nothing because I left it high.

Why not use another pin for scope output, say, pin 8 (PORTB,0)?

Where are you setting I/O pin 13 to be an output?

And where is setup?

Argh: apologies and please ignore. Note to self: never post after just waking up!

1 Like

digitalWrite(13,HIGH); would turn on the internal pullup, LED would probably be too dim to see on a Nano.

I'll give that a try, but it doesn't explain why an output pin when set and cleared doesn't do so in my code or why while(1); doesn't stop all IO activity.

While(1) puts the processor in an endless loop, the I/Os will remain in their last state. Did you set pin 13 as output?
pinMode(13,OUTPUT);