PDA

View Full Version : Code entering endless loop



Blackhawk
- 25th November 2006, 11:12
Hi, I'm currently testing some firmware with an electronic deployment altimeter I've made. However I'm running into a problem with one of my initial loops.

Basically the altimeter is supposed to do nothing but read baseline pressure and wait for input when powered up. It waits for two inputs, arm and mach delay. Arm arms the device at which point it waits for a large acceleration before arming the deployment charges.

Mach delay is a small subroutine that adds a value onto a variable, this variable is used later to specify the number of seconds the device will ignore pressure readings for (the idea is that as the rocket passes the speed of sound the pressure sensor will get some strange readings that it should ignore).

When the delay line is pulled high the program is supposed to add 2 seconds to the delay variable and then beep twice before returning to the start loop. However in practice when I energise the delay line the device beeps twice then continues beeping twice forever. This indicates that after going through the delay loop and going back to the start loop it thinks that the delay line is still high, even when power is no longer applied.

Here is my code:



start: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B0
Peek ADRESL,B1
W6 = W0
W0 = W0 - 50
peek PortB, B2
B3 = B2 & 1
B4 = B2 & 2
if B3 = 1 then beep
if B4 = 2 then delay
goto start

delay: B5 = B5 + 2
high 4
pause 1000
low 4
pause 1000
high 4
pause 1000
low 4
B4 = 0
goto start


I'm not sure where the problem is, clearly either B2 or B4 are not being cleared when they should be or there is something wrong with my peek to portb or my masking of B2.

Anyone have any ideas?

sayzer
- 25th November 2006, 11:47
Hi Blackhawk,

When will B3 be '0'?

As long as it is '1' it will keep beeping.

To make it as you need, assign a flag variable to B3, and based on the flag status you can have your beep. This way, even if B3 is still 1, you can control your beeping as you like.


---------------------------

Blackhawk
- 25th November 2006, 11:57
The problem is not B3 and the beep routine though, the endless loop goes between the start and delay routines (controlled by B4).

mister_e
- 25th November 2006, 13:03
I'm not sure where the problem is, clearly either B2 or B4 are not being cleared when they should be or there is something wrong with my peek to portb or my masking of B2.

Well it's seems fine to me.. let's say PORTB=%11111011

PORTB & with 2 give 2
PORTB & with 1 give 1

You could still do something much simple. Something like


Start:
'
'
PEEK PORTB, B2
B2=B2 & 3
If B2=1 then Beep
If B2=2 then Beep
GoTo Start


and sorry... but we don't see any snip of 'beep ' routine, no Symbol declaration, PIC#, schematic etc etc.

It could be a simple hardware problem too. No pull-down resistor on your Buttons, no pull-up on MCLR (or not disabled).. and so On.

Give us more details.

Blackhawk
- 25th November 2006, 14:32
I haven't got a digital version of the circuit diagram, but I can tell you that there is a pull up resistor on MCLR. The input lines will eventually be toggled by another micro so right now they don't have pull down resistors, I suspected residual charge on the pins before so I tried rigging up a pull down resistor (just loosely) and it didn't appear to help, I should try it again properly to make sure however.

EDIT: for reference here is the whole program



' Porta-0 = Y acceleration $81 then $85
' Porta-1 = X acceleration $89 then $8D
' Porta-2 = Pressure $91 then $95
' Portb-0 = Arm input
' Portb-1 = Machdelay input
' Portb-2 = Apogee charge
' Portb-3 = Main charge
' Portb-4 = Buzzer

Symbol PortB = 6 'PortB is register 6
Symbol TrisB = $86 'PortB data direction is register hexadecimal 86
symbol ANSEL = $9B 'Analogue select port
Symbol ADCON0 = $1F 'Analogue control port 0
Symbol ADCON1 = $9F 'Analogue control port 1
symbol ADRESH = $1E 'Analogue result register high
Symbol ADRESL = $9E 'Analogue result register low
poke ANSEL , $F
poke ADCON0 , $80
poke ADCON1 , $80
Poke TrisB , 0
B5 = 1

start: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B0
Peek ADRESL,B1
W6 = W0
W0 = W0 - 50
peek PortB, B2
B3 = B2 & 1
B4 = B2 & 2
if B3 = 1 then beep
if B4 = 2 then delay
goto start

delay: B5 = B5 + 2
high 4
pause 1000
low 4
pause 1000
high 4
pause 1000
low 4
B4 = 0
goto start

beep: high 4
pause 5000
low 4
goto armed

armed: poke ADCON0 , $89
pause 1
poke ADCON0 , $8D
pause 5
peek ADRESH,B6
Peek ADRESL,B7
if W3 < 300 then machdelay
goto armed

machdelay: for B8 = 1 to B5
pause 5000
next B8
goto launched

launched: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B6
Peek ADRESL,B7
W6 = W6 + 10
if W3 < W6 then apogee
W6 = W3
goto launched

apogee: high 4
pause 5000
low 4
high 2
pause 5000
low 2
goto falling

falling: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B6
Peek ADRESL,B7
if W3 > W0 then main
goto falling

main: high 4
pause 5000
low 4
high 3
pause 5000
low 3
end


Actually just looking back over what I posted, I poke portB to all outputs at the start, could this be causing the problems I'm seeing?

mister_e
- 25th November 2006, 18:07
Sure if you assign those i/o to output you'll never be able to get the expected results. And depending what happen at the power-up.. results may differ

Set them to input and attach some pull-down resistor to.

Archangel
- 25th November 2006, 23:27
Hi, I'm currently testing some firmware with an electronic deployment altimeter I've made. However I'm running into a problem with one of my initial loops.

Basically the altimeter is supposed to do nothing but read baseline pressure and wait for input when powered up. It waits for two inputs, arm and mach delay. Arm arms the device at which point it waits for a large acceleration before arming the deployment charges.

Mach delay is a small subroutine that adds a value onto a variable, this variable is used later to specify the number of seconds the device will ignore pressure readings for (the idea is that as the rocket passes the speed of sound the pressure sensor will get some strange readings that it should ignore).

When the delay line is pulled high the program is supposed to add 2 seconds to the delay variable and then beep twice before returning to the start loop. However in practice when I energise the delay line the device beeps twice then continues beeping twice forever. This indicates that after going through the delay loop and going back to the start loop it thinks that the delay line is still high, even when power is no longer applied.

Here is my code:



start: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B0
Peek ADRESL,B1
W6 = W0
W0 = W0 - 50
peek PortB, B2
B3 = B2 & 1
B4 = B2 & 2
if B3 = 1 then beep
if B4 = 2 then delay
goto start

delay: B5 = B5 + 2
high 4
pause 1000
low 4
pause 1000
high 4
pause 1000
low 4
B4 = 0
goto start


I'm not sure where the problem is, clearly either B2 or B4 are not being cleared when they should be or there is something wrong with my peek to portb or my masking of B2.

Anyone have any ideas?
Hi Blackhawk,
I just have to ask what kind of model rocket travels faster than sound, has an altimiter and deployment charges? Are you making a missle?

Blackhawk
- 26th November 2006, 03:13
Clearly you've never seen high power rocketry. For example in the US:

http://www.aeroconsystems.com/Gene_Nowaczyk_Balls2006/balls2006.html

Amateur rocket reaches 93000ft and around mach 3.45.

I live in Australia so we don't have launches with as many people as the US ones, but we still fly some decently high performance rockets. The one I'm building this altimeter for probably won't break the speed of sound and will only reach around 3500-4000ft.

The next rocket I'm planning however will reach around mach 1.1 and fly to around 5Km altitude (around 16000ft).

Blackhawk
- 26th November 2006, 06:16
Ok adding pull down resistors and properly poking trisB fixed my problem. New problem is the altimeter is detecting apogee when it is just sitting there immediately after the acceleration arms it. I have coded it such that if the current reading is more than 15 units bigger than the previous reading then it triggers an apogee event. However it is immediately triggering an apogee event, a previous datalogger using the same PIC/ADC and pressure sensor only have +/- unit of 'bit jitter' when stationary, so I can't see how currently two readings at the same pressure would be 15 units different.

Here is the code in which the new problem is.



launched: poke ADCON0 , $91
pause 1
poke ADCON0 , $95
pause 5
peek ADRESH,B6
Peek ADRESL,B7
W6 = W6 + 15
if W3 > W6 then apogee
W6 = W3
goto launched


EDIT: hahahaha oops, look at my code, I was placing the lower half of the 10 bit A/D result at the top of the word and the upper half on the bottom half of the word. As such the +/- 1 unit of bit jitter would have a huge difference. Why do I always only catch these things after posting them.

EDIT2: Except now the circuit is instantly arming and delaying by a random number before thinking it's at apogee, this is really annoying me.

Archangel
- 26th November 2006, 06:42
Clearly you've never seen high power rocketry. For example in the US:

http://www.aeroconsystems.com/Gene_Nowaczyk_Balls2006/balls2006.html

. You are correct, I have never seen amature rockets on such a large scale, in my defense, having worked in a law enforcement environment in post 911 USA, I am sure you can appreciate my concern. Ultimately, what end purpose do these rockets serve, do you and the others plan to make orbital rockets, amature satelite launches, or just sub orbital flights?
JS

Blackhawk
- 26th November 2006, 07:39
Ok I finally worked out what was causing my new issues..... I had the compiler set to compile for a 16C711 instead of a 16F88, I don't think I've ever felt like hitting my head against the wall more after I saw that.


You are correct, I have never seen amature rockets on such a large scale, in my defense, having worked in a law enforcement environment in post 911 USA, I am sure you can appreciate my concern. Ultimately, what end purpose do these rockets serve, do you and the others plan to make orbital rockets, amature satelite launches, or just sub orbital flights?
JS

For most people it's just fun, a progression to bigger, noisier and cooler rockets from the little ones they started on. For me, my own personal goal is to first develop a suborbital space rocket (I would be the first amateur in Australia to break 100Km altitude) and from there I would like to start an Australian orbital launch company.

I'm currently 2 years into an aeronautical engineering degree, so I know what I'm doing when it comes to most of this stuff, and nothing trumps practical experience. There is a growing market for satellite delivery, central Australia is almost a perfect location for this sort of thing and there is a huge technology vacuum in the region.

I know how paranoid people are and it annoys me know end, I've personally been looking into the laws down here before I take a step in any direction. For my next rocket I'm going to have to buy some concentrated hydrogen peroxide (oxidiser for the liquid fuelled motor) and I can already tell it's going to be difficult as a private citizen, even while being perfectly legal.

sayzer
- 26th November 2006, 09:12
In case something happens and your rockets loses control and goes into space somehow, do not forget to put the WEB link of this forum along side of your rocket in big letters.

Who knows ?