Strange behavior with While() loops...

Hello everyone, I'm new here, I work on project to read a frame from camera chip and I'm stuck with silly problem.
I've got to receive frame 320x240 pixels, I've looked up in the internet how other people have done it and general idea is to put instructions in two While() loops:

uint16_t Height = 240;
uint16_t Width = 320;

While(Height--){

      While(Width--){
            //receive pixel data and process it..
      }

}

When I tried to run it, program was hanging when got into them 'While' loops, well I thaught it was hanging, but after good few seconds it got out from loops eventually..
As far as I know program should get out from these two 'while' loops after 76800 iterations (320 * 240), but after adding simple counter and printing it out after getting out it prints 15663185 counts!

uint32_t Counter = 0;
uint16_t Height = 240;
uint16_t Width = 320;

While(Height--){

      While(Width--){
            Counter++;
      }

}
Serial.println(Counter);

Could someone explain what is going on here please...

DmMoraw:
Hello everyone, I'm new here, I work on project to read a frame from camera chip and I'm stuck with silly problem.
I've got to receive frame 320x240 pixels, I've looked up in the internet how other people have done it and general idea is to put instructions in two While() loops:

uint16_t Height = 240;

uint16_t Width = 320;

While(Height--){

While(Width--){
            //receive pixel data and process it..
      }

}




When I tried to run it, program was hanging when got into them 'While' loops, well I thaught it was hanging, but after good few seconds it got out from loops eventually..
As far as I know program should get out from these two 'while' loops after 76800 iterations (320 * 240), but after adding simple counter and printing it out after getting out it prints 15663185 counts!



uint32_t Counter = 0;
uint16_t Height = 240;
uint16_t Width = 320;

While(Height--){

While(Width--){
            Counter++;
      }

}
Serial.println(Counter);




Could someone explain what is going on here please...

You don't reset Width before the inner loop so it "underflows" resulting in many more counts.

:o
OMG I would never find it myself :confused:
Thank You Blackfin, you're a STAR!