Tuesday 2 December 2014

Resetting Arduino

I ran into situation where I need to reset(?) arduino.. Where adrduino need run the setup() function, to load the new variables to execute the same code.
the initial code is like this.

void setup(){
load some variables
}
void loop(){
run the code according to the varibles
}


I don't want to RESET arduino everytime. so I dig the net to reset the arduino.

I gone through this links

In the link he described it as Dirty Solution
even though i think it meeds my needs. it executes the code from starting point


moves the control to the beginning of the program using an assembly statement jump.


so i write this code to understand myself

void setup() {
Serial.begin(9600);
Serial.println("Anil");
}

void loop() {
Serial.println("Kunchala");
delay(1000);

software_reset();
}
void software_reset(){
Serial.println("Do you want to print again?");
Serial.println("enter y or n");
while(!Serial.available()){
}
char in_char = Serial.read();
if (in_char == 'y'){
//if receivd character is y then Reset the arduino
asm volatile("jmp 0");
}
else{
//else call the function to repeat the same procedure
software_reset();
}
}

the result will look like this..
Capture


Ok... I got what I need But..
What is the meaning of the line
asm volatile("jmp 0");

as previously said it restarts the arduino from starting...,(memory location 0x00)
we can have the answer...

asm volatile is typically used for embedding assembly snippets inside C routines


so it is assembly instruction.

what is the meaning of jmp instruction

The JMP instruction provides a label name where the flow of control is transferred immediately. The syntax of the JMP instruction is:

JMP label


from WIKIPEDIA


the JMP instruction performs an unconditional jump. Such an instruction transfers the flow of execution by changing the instruction pointer register.



this line transfers the execution control to the starting memory location and has nothing to do with any other arduino registers. so registers does not go to default state(INPUT) what actual RESET (by pressing the RESET button).

No comments:

Post a Comment