• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Picbasic program loops continuously after END command

Status
Not open for further replies.

Ian Walters

New Member
Hi

I'm trying to write a simple program using a 16F628 chip to animate a model. I'm very new to this and totally inexperienced and have only accquired enough knowledge to complete my needs..

The problem I have is that the program runs perfectly but then runs again and keeps on running continuously despite my placing an "END" command.

I read on another forum that the Watchdog Timer may need disabling to prevent this happening? If so, can anyone tell me the command to make this happen?

This is the program:

AllDigital

TRISA = %11111111
TRISB = %00100000

Symbol motor = PORTB.0
Symbol schip = PORTB.1
Symbol voice = PORTB.2
Symbol beams = PORTB.3
Symbol yellow1 = PORTB.4
Symbol switch = PORTB.5



main:
While PORTB.5 = 0
Wend

If PORTB.5 = 1 Then
Goto start
Else
Goto main
Endif

start:
High yellow1
WaitMs 500
Low yellow1
High schip
High motor
WaitMs 1600
High voice
WaitMs 3000
Low voice
WaitMs 1500
High voice
WaitMs 2800
Low voice
WaitMs 600
High beams
WaitMs 1600
Low beams
WaitMs 1000
Low schip
Low motor
WaitMs 30000
Goto main
End


I would be extremely grateful for any help on this but please speak slowly and use little words!

Thanks in anticipation
 

JLNY

Active Member
I'm not 100% sure since I don't know BASIC, but I don't see where the code would ever reach the End statement? It seems to continually loop back to the start or to main. Is there a specific point in the code where you want the program to end? under what conditions will it be reached? I'm more of a C programmer, so maybe I'm the one missing something here.

You may also wish to check the switch on B.5 to be sure that it is going low if it is continually looping back to the start without being pressed.

Also, yes, the Watchdog timer will need to be disabled. typically your programming enviroment (e.g. MPLab, etc.) will have settings somewhere for setting the "configuration bits" which will allow you to set the Watchdog timer to be disabled.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
As far as I'm aware it's up to YOU to do something to stop the program running continually, the END isn't actually a program instruction, just a commend for the compiler so it knows it's reached the end of the program and there's no need to look for more.

Assuming you want it to run just once, then stick an endless loop just before the END - such as:

Ever
GOTO Ever
END
 

Daniel Wood

Member
It may be best disabling the Watchdog timer (WDT) as some chips will have this feature on by default. The chip will reset periodically if not cleared.
I've not used basic in quite a while, the commands may be WDTE = 0 to turn it off, or CLRWDT to clear the WDT.

If turning it off doesn't work, do as Nigel says and put an empty loop in before the end of the program, but clear the WDT every-time as well.
The code below should *fingers crossed* work...

Code:
AllDigital

TRISA = %11111111
TRISB = %00100000

Symbol motor = PORTB.0
Symbol schip = PORTB.1
Symbol voice = PORTB.2
Symbol beams = PORTB.3
Symbol yellow1 = PORTB.4
Symbol switch = PORTB.5

' Disable WDT
WDTE = 0

main:
   While PORTB.5 = 0
   Wend

   If PORTB.5 = 1 Then
      Goto start
   Else
      Goto main
   Endif

start:
   High yellow1
   WaitMs 500
   Low yellow1
   High schip
   High motor
   WaitMs 1600
   High voice
   WaitMs 3000
   Low voice
   WaitMs 1500
   High voice
   WaitMs 2800
   Low voice
   WaitMs 600
   High beams
   WaitMs 1600
   Low beams
   WaitMs 1000
   Low schip
   Low motor
   WaitMs 30000
   Goto ever
   End

ever:
   CLRWDT      ' Clear WDT
   Goto ever
   End
 

DerStrom8

Super Moderator
Most Helpful Member
I agree with the others, you must put an infinite loop at the end of your code if you don't want it to repeat. This is common practice for many micros in many languages.
 

be80be

Well-Known Member
None of it's going to do what you want.
Top down code runs top down.
You want a main loop that sits and waits on the change of PORTB.5

Some thing like this
Code:
while 1 
     If PORTB.5 = 0 
          do this // just here so the next statement runs 
     elseIf PORTB.5 = 1 
          do this  // this could point to start 
    ENDIF 
wend
 

Ian Walters

New Member
Hi Gents

Thank you all for taking the time to help me out, I really appreciate it. I am now working on the advice you have given me, checking the physical environment and writing the loop into the bottom of the program as you have suggested.

If anybody needs help making a model please shout!

Best regards
 

Wp100

Well-Known Member
Am no expert on micros and programming languages , though Basic may seem appealing for its initial simple coding I think you may find it limiting long term.

As I have seen first hand, the catch some folk often find themselves in is that the more they use Basic they become frustrated with its limitations and the harder they seem to be able or want to try other languages that offer so much more.

Would suggest you look at something like the Arduino which needs no additional programmers, just a board like an Uno or better still one of the many uk stocked £5 clone Uno boards and Arduinos totally free C++ programming and IDEs which are so well supported and has masses of ready made hardware.

Example clone. one of many
http://www.ebay.co.uk/itm/ARDUINO-B...308353?hash=item43f8f05341:g:wz8AAOSw9k5XP96d
http://www.ebay.co.uk/itm/ARDUINO-B...308353?hash=item43f8f05341:g:wz8AAOSw9k5XP96d
Arduino Home and Forum
https://www.arduino.cc/
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Just a side note!!!

Eveyone has given sound advice, but I don't think your thread topic is helping..
"Picbasic program loops continuously after END command" Is missleading..

Your code is in a continual loop because you have a "goto main" at the end of your code..
If you need the code to run once only cahge to this
Code:
AllDigital

TRISA = %11111111
TRISB = %00100000

Symbol motor = PORTB.0
Symbol schip = PORTB.1
Symbol voice = PORTB.2
Symbol beams = PORTB.3
Symbol yellow1 = PORTB.4
Symbol switch = PORTB.5



main:
   While PORTB.5 = 0
   Wend

   If PORTB.5 = 1 Then
       Goto start
   Else
       Goto main
   Endif

start:
   High yellow1
   WaitMs 500
   Low yellow1
   High schip
   High motor
   WaitMs 1600
   High voice
   WaitMs 3000
   Low voice
   WaitMs 1500
   High voice
   WaitMs 2800
   Low voice
   WaitMs 600
   High beams
   WaitMs 1600
   Low beams
   WaitMs 1000
   Low schip
   Low motor
   WaitMs 30000
fin:
   Goto fin
End
 

atferrari

Well-Known Member
Not of help to you immediately but little after I started to program things, I learnt the value of using a flow diagram prior coding. With a simple one, your mistake would have not occurred.

BTW, verify in the user manual what END means for your compiler. Not what you currently believe. It gives nasty surprises when INCLUDE files are used.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top