Pages: 1 [2]   Go Down
Author Topic: Things you always wanted to know but never dared to ask  (Read 1915 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Full Member
***
Karma: 3
Posts: 245
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi things i always wanted to know but dare not ask...what does "OP" mean in this forum? i see people using it a lot...haha
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Original Poster - the person who started the thread.  Sort if " back to the subject" after the thread has gone a bit away from the original question
Logged

Malaysia
Offline Offline
Full Member
***
Karma: 3
Posts: 245
Stay calm and call batman!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh, i always tot it was "one person" haha
Logged

Brussels, BE
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think asking about the inner workings and neccessity of a bootloader is a relevant question that does not need to be ferreted away in Bar Sports ...

When the microchip is powered up/reset - what is it to do?

The "reset" does a few things. It zeros some hardware (not the RAM, it will contain random bytes) and sets some registers to know values, in particular the Program Counter (the address pointed to a the reset vector which is at 0000). In short, it starts the program at a defined location.

If that is your program, fine, it runs. You now want to change the program. Forgot to put in a code to allow your program to be overwritten? Your program has a fault? Damn - we're locked out.

The way that problem is solved on the Arduino, is that the program that executes is The Bootloader. It first checks if the Serial line is sending a new program. If not, it goes to the start of "your program".

"Your program" is just two functions, setup and loop. The program, which the bootloader jumps to once it is done, is the "Arduino core", which starts by initialising the defined RAM variables to the values you have given and zero if you did not (so "your RAM" does not contain random bytes), setting ports, timers and interrupts to good/known values. Then it calls setup and loop as documented and your sketch is running.

The "New program?"-test blinks LED 13 (dont know why) while it waits for some Serial input. A few bytes are exchanged so the Loader program on the pc and the Arduino match and then the program bytes are transferred and stored in your chip. When it is done, it jumps to "Your program" the same way as if no bytes had been transferred.  My experience is that if there is no USB hooked up at all, the bootloader determines quickly nothing is comming and the the main program starts quicker.

Note that the bootloader code, which also sits in the same flash as your program, is NOT overwritten. It executes on every reset, and is never changed during normal Arduino IDE programming and operation.

As the others have written, there is another solution. It is to solve the problem - how did the bootloader get in there? After the chip is manufactured all the flash bytes contain the same value (all zeros or ones, doenst matter which, useless). So there must be method whereby some bytes are set. This is handled by an internal hardware bootloader (nothing to do with the bootloader described previously). It is triggered by special pulsing Reset and the pins 11-12-13 (ie. the SPI interface), and then the bytes being loaded through the SPI with a simple protocol (if any)

So really when you hit reset
1: Circuitry inside chip set to known values
2: Is the hardware bootloaader triggered?
  Yes: 3: starts reading bytes at SPI port and stores in flash. <details missing>
No
3: chip program starts, begins at 0000->vector->Bootloader
4: Bootloader checks Serial line/USB, includes blinking the 13 LED
5: New program?
  Yes: 6: start loading bytes <details missing>
no
6: Jump to arduino core main()
7: Arduino core does more initialisation of ports and memory
8: call setup()
8b: call loop() again and again, forever

Additions and corrections welcome.
Very informative, thank you very much for having taken the time to thoroughly answer my question.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

oh, i always tot it was "one person" haha

Could also mean the original post, I think. So again, back to original topic.
Logged


Brussels, BE
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have another question that struk me recently. I the date calculation code for my clock, I have something like (yeah it's ugly, i'll change it someday) :
Code:

    if(Mois==3&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==4&&Jour>=31){
        Jour -= 30;
        Mois++;
    }
    if(Mois==5&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
with both Jour (day) and Mois (month) being integers. When I increase days, everything happens as excpected : days fall back to 1 and month is increased by oone. But when I decrease days down to 1, and then decreasing by one again, month decreases by one and days take the correct value (30 or 31)...

This is nice, but how does it happen ? Nowhere I indicated what to do if the day become negative or zero. Is it the compiler that does that or is it a "emergeant" phenomenon due to the way the microprocessor handles information ?

Thanks
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Provide more code!
Logged


Brussels, BE
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've added everything that relates to date calculation (actually more, but it's just "inputry"). By the way, if you have ideas to improve the code, i'm all listening.

Code:
 // SETTING SELECTION ////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////
  int LeftInput = digitalRead(A2);
  int RightInput = digitalRead(A3);

  if(LeftInput==LOW && RightInput==HIGH)  Set = 0; //Left couple of tubes will be available for setting
  if(RightInput==LOW && LeftInput==HIGH)  Set = 2; //Right pair of tubes will be available for setting
  if(RightInput==HIGH && LeftInput==HIGH)  Set = 1; //Middle pair of tubes will be available for setting
  
  // SETTING THE SELECTED PAIR OF TUBES ////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  int DecreaseInput = digitalRead(A4);  
  int IncreaseInput  = digitalRead(A5);

  if(DecreaseInput==0)    DecreaseButtonPressed = true;
  if(IncreaseInput==0)    IncreaseButtonPressed = true;
  
  if(DecreaseButtonPressed==true && DecreaseInput==1)
  {
    if(Set ==0){
      if(Mode==1)  ClockHourSet--;
      if(Mode==2)  ClockDaySet--;
    }
    if(Set ==1){
      if(Mode==1)  ClockMinSet--;
      if(Mode==2)  ClockMonthSet--;
    }
    if(Set == 2){
      if(Mode==1)  ClockSecSet--;
      if(Mode==2)  ClockYearSet--;
    }
    DecreaseButtonPressed = false;
  }
  if(IncreaseButtonPressed==true && IncreaseInput==1)
  {
    if(Set == 0){
      if(Mode==1)  ClockHourSet++;
      if(Mode==2)  ClockDaySet++;
    }
    if(Set == 1){
      if(Mode==1)  ClockMinSet++;
      if(Mode==2)  ClockMonthSet++;
    }
    if(Set == 2){
      if(Mode==1)  ClockSecSet++;
      if(Mode==2)  ClockYearSet++;
    }
    IncreaseButtonPressed = false;
  }

  // DATE CALCULATION /////////////////////////////////
  /////////////////////////////////////////////////////
  int Jour = days;
  int Mois = ClockMonthSet;
  int Annee = ClockYearSet;

    if(Mois==1&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==2){
      if(Annee%4==0){
        if(Jour>=30){
          Jour -= 29;
          Mois++;
        }
      }
      if(Annee%4!=0){
        if(Jour>=29){
          Jour -= 28;
          Mois++;
        }
      }
    }
    if(Mois==3&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==4&&Jour>=31){
        Jour -= 30;
        Mois++;
    }
    if(Mois==5&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==6&&Jour>=31){
        Jour -= 30;
        Mois++;
    }
    if(Mois==7&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==8&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==9&&Jour>=31){
        Jour -= 30;
        Mois++;
    }
    if(Mois==10&&Jour>=32){
        Jour -= 31;
        Mois++;
    }
    if(Mois==11&&Jour>=31){
        Jour -= 30;
        Mois++;
    }
    if(Mois==12&&Jour>=32){
        Jour -= 31;
        Mois++;
    }

    //Defining years incrementation.
    if(Mois>=13){
      Annee++;
      Mois=Mois-12;
    }
    if(Mois<=0){
      Annee--;
      Mois=Mois+12;
    }
As you can see, there's a rule for months<0 but not for days.
« Last Edit: June 04, 2013, 05:25:06 pm by Mad physicist » Logged


Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would do your "magic" if the Jour variable is defined as unsigned. ...
<thinks>
lets see, if it is a unsigned byte, then -1 is interpreted by the math as 255 - bigger than 31. so we decrease it by 30 (=225). Next time round it is still bigger and we repeat, until we have 15 left. And the month has increased by 8. Nope, that did not explain all your observation.
</thinks>
Anyhow, your Jour is a an (signed) int, so all above is irrelevant

In your code the Jour is initialized from days, but I do not see where that came from. Code missing. Nor do I see what/where the ClockDaySet variable is defined or initialized, so still more code lurking somewhere.
Logged

Brussels, BE
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"days" is just an integer that increases by one every 24 hours. Here's what come just before //DATE CALCULATION in the previous piece of code :
Code:
 // TIME CALCULATION /////////////////////////////////
  /////////////////////////////////////////////////////
  // Get time in seconds.
  unsigned long time = runTime / 1000;
  time += ClockSecSet + 60*ClockMinSet + 3600*ClockHourSet + 86400*ClockDaySet;    //time in seconds, based on offset

  // Convert time to days,hours,mins,seconds
  long days  = time / 86400;    time -= days  * 86400;
  long hours = time / 3600;   time -= hours * 3600;
  long minutes  = time / 60;    time -= minutes  * 60;
  long seconds  = time;
where "runtime" is simply millis().

important edit: Now I think of it, the code inside the clock might be different, as I carried out some modifications on the main sketch but didn't uploaded it yet (because the board is buried deep in a amorphous mass of wires, and because I'm lazy). That might be it then? I'm sorry I didn't tought about this earlier but I really thought there were some cool hidden process behind this.
« Last Edit: June 05, 2013, 03:30:35 pm by Mad physicist » Logged


Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am meekly wondering one thing ... how do some people write [code] tag in text without it starting the code pane?

(Typicall! in trying to explain what my question was, I found the solution)
Logged

Pages: 1 [2]   Go Up
Jump to: