Atom Pro getting stuck/freezing?

Hey!

I have the following code to measure distances from 3 SRF04 sensors, but it seems that once in a while the output from the terminal gets stuck? Is it just the terminal, or is my microcontroller actually freezing? Pressing reset works sometimes, and others it just advances one more time through. It seems to be random.

Any suggestions?

Thanks!

convfac con 74 ' use inches

SONAR_INIT con p16
SONAR_ECHO con p17

sonar_dist var word
output SONAR_INIT
input SONAR_ECHO

; ### Main Function ###
main
	goto getDistance
goto main
  
; ### Get Distances ###
getDistance:

	SONAR_INIT con p16
	SONAR_ECHO con p17

	low SONAR_INIT
	pulsout SONAR_INIT, 20      '10us pulse to init reading    
	RCTIME SONAR_ECHO, 1, sonar_dist ; use instead of pulsin since buggy
	RCTIME SONAR_ECHO, 0, sonar_dist

	debug [dec sonar_dist, 13]
 
	sonar_dist = sonar_dist / convfac 
	serout s_out, i9600, "Distance1: ", sdec sonar_dist, 13, 10]      ;display result in terminal
	
	;if sonar_dist < 15 then brake
	;if sonar_dist > 15 then accelerate 
;###############	
	SONAR_INIT con p18;
	SONAR_ECHO con p19;
	
	low SONAR_INIT
	pulsout SONAR_INIT, 20      '10us pulse to init reading    
	RCTIME SONAR_ECHO, 1, sonar_dist ; use instead of pulsin since buggy
	RCTIME SONAR_ECHO, 0, sonar_dist

	debug [dec sonar_dist, 13]
 
	sonar_dist = sonar_dist / convfac 
	serout s_out, i9600, "Distance2: ", sdec sonar_dist, 13, 10]      ;display result in terminal
;#############

	SONAR_INIT con p20;
	SONAR_ECHO con p21;
	
	low SONAR_INIT
	pulsout SONAR_INIT, 20      '10us pulse to init reading    
	RCTIME SONAR_ECHO, 1, sonar_dist ; use instead of pulsin since buggy
	RCTIME SONAR_ECHO, 0, sonar_dist

	debug [dec sonar_dist, 13]
 
	sonar_dist = sonar_dist / convfac 
	serout s_out, i9600, "Distance3: ", sdec sonar_dist, 13, 10]      ;display result in terminal
	
	
return

A couple of quick things I noticed.

  1. You are mixing debug statements and serout s_out… Personally I prefer to stick with the serout statements and normal compiles as debug mode can cause problems with time specific commands…

  2. You are corrupting the stack. That is right after the label Main you do a
    goto getDistance. After that you have a goto main statement. My first thought was the goto main will never happen as the first goto will always happen. Then looking at the label getDistance I see it does a return at the end. But as there was no call (gosub) it will returning to never never land.

Try changing the goto getDistance to
gosub getDistance.

Good Luck
Kurt

Also, the main reason you can’t use serout s_out commands while debugging is because the debugger code is using the s_out port to talk to the PC side debugger. You will end up corrupting the data being transmitted back to the PC. The PC side debugger will try to ignore the garbage but it won’t be perfect and can cause freezing and wierd jumps around your code. Combining this with your corruption of the stack and your program is doomed. :slight_smile: So definitely fix those two things.