PDA

View Full Version : Need help - 16f876 - strange results



neverenouph
- 4th October 2013, 03:09
All,
I am trying to build a simple robot for my daughter and have run into a snag. The code seems to run normal for some time then it will just stop and the hserout command will just start displaying 111111 or 222222 or 33333 over and over again. Every time I reset, it runs for a while and then displays 1 number higher, but over and over again. I am including the code below.
Of note, when this program is running, I can hear the ultrasonic ping sensor happily pinging away, but once it starts displaying the number repeatedly, everything stops, including the blinking LED and the ping sensor, only the hserout keeps repeating the number.

I am using a HS setting and disabling the WDT, Brown out, and power up timer.


define OSC 20
DEFINE HSER_BAUD 9600

TRISC = %00000000 'All C's are outputs
TRISB = %00100000 'All B's are outputs except b5
CCP1CON = %00001100 ' Mode select = PWM1
CCP2CON = %00001100 ' Mode select = PWM2

TRIGGER var PortB.4
ECHO var PortB.5
IN1 VAR PortC.3
IN2 VAR PortC.4
IN3 var PortC.5
IN4 var PortB.1

'i var byte
'duty var byte
RAWDATA var word
inINCHES var word
low IN1
low IN2
low IN3
LOW IN4
pause 1000
HSEROUT ["Starting", 10, 13]

MAIN:
HSEROUT ["GOSUB PING first entry", 10, 13]
gosub PING

IF inInches <= 9 THEN Obstacle
high IN1
high IN3
hpwm 1, 250, 2000 ' Forward left motor
hpwm 2, 250, 2000 ' Forward right motor




goto MAIN

Obstacle: ' Backwards
HSEROUT ["backing up", 10, 13]
low IN1
low IN3
while inInches < 12
high IN2
high IN4
hpwm 1, 150, 2000
hpwm 2, 150, 2000
gosub Ping
wend
low IN2
LOW IN4
return

PING:
pulsout TRIGGER, 5
PUlsin ECHO, 1, RAWDATA
pause 100
inINCHES = RAWDATA / 74
hserout ["RAWDATA ",DEC RAWDATA, 10, 13]
hserout ["inINCHES ",DEC inINCHES, 10, 13]
return

Archangel
- 4th October 2013, 03:54
Ok only thing I see

Obstacle is a subprogram and you are getting there from a then goto so return does not do what you are asking.
try IF inInches <= 9 THEN gosub Obstacle

Amoque
- 26th October 2013, 13:24
I am not so experienced as others here, but would offer what help and encouragement that I may. It is my experience that coding never fails, some outside factor is introducing change. Odd behavior is often a power issue. Is your power well suppled? Does the pwm continue during the problem? I did not see configuration; could it be watchdog, a poor ground or other exteral condition?