PDA

View Full Version : Problem with WHILE - WEND



Russ Kincaid
- 26th May 2006, 12:47
I attached my program. Can someone tell me what is wrong with the highlited statement, and how can I accomplish the action?

mister_e
- 26th May 2006, 12:54
Use IF THEN instead


IF (Footsw=1) OR (Ready=1) Then Start

OR


While (FootSw=1) OR (Ready=1)
LOW RENCA
LOW EJECT
WEND

sayzer
- 26th May 2006, 12:56
First:
Goto should not be used with "While" statement. Use "IF" instead.
ex:

IF Footsw=1 or Ready=1 then Goto Start

Second:
Your statement should have been as below.

While Footsw=1 or Ready=1
Goto start 'cant'be on the same line with While
Wend

Russ Kincaid
- 26th May 2006, 13:01
Thanks for the replies. I had tried IF - THEN but did not do that right either!

mister_e
- 26th May 2006, 13:04
1. Parenthesis are important in most case
2. I would never jump out of a WHILE statement using a GOTO. Using a flag or Validate the Conditions to get out of the WHILE/WEND loop is, IMHO, a better practice.

Could be also..


Start:
LOW RENCA
LOW EJECT
While (FootSw=1) OR (Ready=1) : WEND

OR


Start:
LOW RENCA
LOW EJECT
SpinHere: If (FootSw=1) OR (Ready=1) then SpinHere

You may notice that one method generate less code space than the other.

Russ Kincaid
- 26th May 2006, 13:41
Here is my problem: I tried the spinhere loop, it works but the following statements are a loop. I want the program to stay in the loop but as soon as either footsw or ready goes high, the program jumps back to the spinhere loop. I don't want it to do that. How do I do it?

mister_e
- 26th May 2006, 13:48
SpinHere:
LOW RENCA
LOW Eject
While (FootSw=0) OR (Ready=0)
'
' Do your loop here
'
wend
goto Spinhere

Russ Kincaid
- 26th May 2006, 15:01
I have not explained my problem properly: I want a momentary switch closure (footsw) to cause the program to jump to a loop (freq:). I don't want it to jump out of the loop when the switch opens.

Acetronics2
- 26th May 2006, 15:09
Hi, Steve

Keep cool ... keep cool ...

Alain

Hi,Russ

Could you explain EXACTLY what you want to do ...

I mean : the switch closure lead to jump into the loop, OK ... but what happens if switch released ??? stay in the loop ??? go where ??? do you test your switch continuously after jumping off the loop ??? or just once at the beginning of your program ???

We're not at school ... no one will copy your project !!!!

Problem written is half the solution ....

Alain

Russ Kincaid
- 26th May 2006, 15:50
I want a momentary switch closure to jump to a loop and stay there, regardless of further action by the switch. Perhaps an interrupt is what I need? I don't exactly know how to do that, but will experiment. I prefer to use portb.5 for interrupt input, the data sheet says the RBIF flag will be set regardless of the state of the INTCON enable bit?

Russ Kincaid
- 26th May 2006, 16:08
I tried this interrupt. READY is always low; when FOOTSW goes low, the freq loop runs but only as long as FOOTSW is low. When FOOTSW goes high, the clock stops; it will start again when FOOTSW goes low again. I don't understand this.

'device: 16F627A master clear is on pin#4 (RA5)
TRISA = %00010000 ' port A all outputs, except MCLR is input
TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT
option_REG = %01111111 'enable weak pullups on port B
FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21

START:
WHILE READY = 0
LOW RENCA : LOW EJECT
on interrupt goto poo

poo:
T = 100 '1mS AT 4 mHZ

freq:

PULSOUT RENCA, T 'frequency should be 500 Hz (2mS period)
PAUSEus T*7

GOTO freq
WEND
end

Acetronics2
- 26th May 2006, 16:48
Try ( to understand ) this ...

'device: 16F627A master clear is on pin#4 (RA5)
TRISA = %00010000 ' port A all outputs, except MCLR is input
TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT
option_REG = %01111111 'enable weak pullups on port B
FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21
on interrupt goto poo


START:
LOW RENCA : LOW EJECT

WHILE READY = 0

'zzzzzzzzzzzzzzzzzzzz ... waiting !!!
WEND

GOTO what to do if not ready ???


DISABLE INTERRUPT
poo:
T = 1 '1mS AT 4 mHZ

freq:

TOGGLE RENCA
PAUSE T 'frequency should be 500 Hz (2mS period)

GOTO freq

RESUME
ENABLE INTERRUPT

end

NOTE : RESUME and ENABLE INTERRUPT are not compulsory here as program enters an endless loop ...

Alain

Acetronics2
- 26th May 2006, 17:36
and now we're here :


'device: 16F627A master clear is on pin#4 (RA5)
TRISA = %00010000 ' port A all outputs, except MCLR is input
PORTB = 0
TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT
option_REG = %01111111 'enable weak pullups on port B

INTCON = %10001000 ' Define the interrupt origin ....YESSSS

FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21

on interrupt goto poo


START:
LOW RENCA : LOW EJECT

WHILE READY = 0

'zzzzzzzzzzzzzzzzzzzz ... waiting !!!
WEND

GOTO Start 'GOTO what to do if not ready ???




DISABLE INTERRUPT

poo:

IF NOT FOOTSW THEN false 'we are looking for a Portb.5 interrupt ... not PortB.4 or else

T = 1000 '1mS AT 4 mHZ ...........Note: 1000 for MPSIM, 1 for real

freq:

TOGGLE RENCA
PAUSE T 'frequency should be 500 Hz (2mS period)

GOTO freq

false:

RESUME
ENABLE INTERRUPT

end

Russ Kincaid
- 26th May 2006, 19:44
I really appreciate your help, I hope you are not tired of my dumb questions!
I tried your program; the clock starts when power is applied but nothing happens when footsw is low. When footsw goes high, the clock stops and reset will not start it. I have to turn power off and on or if I put footsw low, the clock will start again, as long as footsw is low. I put the nap statement in to see if it was seeing that part of the program; it isn't.

I don't understand the WHILE - WEND, what is that doing?

TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT
option_REG = %01111111 'enable weak pullups on port B
FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21
on interrupt goto poo
START:
LOW RENCA : LOW EJECT

WHILE READY = 0
WEND
GOTO START
DISABLE INTERRUPT
poo:
IF NOT FOOTSW THEN FALSE
nap 6
T = 100

freq:

toggle renca 'frequency should be 500 Hz (2mS period)
PAUSEus T*7 ' 1 mS at 4 mHz

GOTO freq
false:
RESUME
ENABLE INTERRUPT

end

Russ Kincaid
- 26th May 2006, 20:22
I had a thought and used this code:
IF footsw = 0 THEN LOW portb.5
IF footsw =1 THEN poo

I figgured that since portb.5 was changed to an output, it could not generate an interupt; but the clock does not run so nothing happens.

I think I will take a break and go do something physical.

Acetronics2
- 27th May 2006, 09:48
I really appreciate your help, I hope you are not tired of my dumb questions!
I tried your program; the clock starts when power is applied but nothing happens when footsw is low.

> YOU must define what the pic does if ready is low !!! I Wrote something the program to run ... ( GOTO start ) ... so program just loops !!!

When footsw goes high, the clock stops and reset will not start it.

> No reason the clock stops ... except ...

> mmmhhh, I do not see PORTA settings anymore ...and is Reset tied to Vcc ???
> Got it ... we must add CMCON = 7 at the program top !!! to turn comparators OFF

I have to turn power off and on or if I put footsw low, the clock will start again, as long as footsw is low. I put the nap statement in to see if it was seeing that part of the program; it isn't.

I don't understand the WHILE - WEND, what is that doing?

> As long as READY = 0 does the following lines ... here, as there is nothing to do, it just verfies READY state ... and also allows interrupts !!!



TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT
option_REG = %01111111 'enable weak pullups on port B
FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21
on interrupt goto poo
START:
LOW RENCA : LOW EJECT

WHILE READY = 0
WEND
GOTO START
DISABLE INTERRUPT
poo:
IF NOT FOOTSW THEN FALSE
nap 6
T = 100

freq:

toggle renca 'frequency should be 500 Hz (2mS period)
PAUSEus T*7 ' 1 mS at 4 mHz

GOTO freq
false:
RESUME
ENABLE INTERRUPT

end

> Pauseus 700 won't give 500 Hz ...

Alain

Acetronics2
- 28th May 2006, 09:41
Hi, Russ

For those who follow the thread ...

Here is the MPLAB Project.

The .BAS file is dirctly usable ... the .Hex too !!!

Alain

Russ Kincaid
- 28th May 2006, 20:07
I copied your program, my hex file is different: it starts with 020000002C28AA but your hex file starts with 020000040000FA and then has 020000002C28AA. The last two lines of the hex files agree, but everything in between is different. I double-checked and did not make an error in copying the program. Do you suppose my assembler is corrupted?

Russ

Russ Kincaid
- 29th May 2006, 03:04
I compared the .ASM files: If it is true that lines starting with semicolon are comments, and if lines starting at the far left edge are wraparound from the previous comment, then there are no differences in the files. Otherwise, there are significant differences. So, if the .ASM files are the same, why would the .HEX files be different?

Acetronics2
- 29th May 2006, 11:17
Hi,Russ

May be Osc is Xtal for me ...

The first line gives config word ... in fact the HEX files decoding is quite simple ...

Alain

Russ Kincaid
- 29th May 2006, 13:24
I programmed my PIC using your .HEX file. Even tho my .HEX file is different, the operation is the same: Oscillator starts with power up, RENCA is low, nothing happens when FOOTSW goes low, but oscillator stops when RENCA goes high. This is my circuit:

Acetronics2
- 29th May 2006, 16:56
Hi, Russ

Having a look to your scheme, I was at the end able to discover you work with inverted I/Os ...

so ... that gives

'device: 16F627A master clear is on pin#4 (RA5)


PORTA = 0
CMCON = 7 'PortA inputs to be DIGITAL
TRISA = %00010000 ' port A all outputs, except MCLR is input

PORTB = %11100000
TRISB = %00111111 'PORT B ALL INPUT, EXCEPT 6 & 7 OUTPUT

option_REG = %01111111 'enable weak pullups on port B
INTCON = %10001000 'Enable Interrupts on PORTB.4-7


FOOTSW var portb.5 'Foot switch OR CCREADY must be high
READY var portb.4 'at start

'FOOTSW var portb.4 '************ On Alan's testboard **************
'READY var portb.3 ' ************ " **************

T var word
J VAR WORD


RENCA var portb.7 'FREQUENCY OUTPUT TO J1 PIN 13
EJECT var portb.6 '100 mS PULSE TO J1 PIN 21

on interrupt goto poo


START:
High RENCA : High EJECT

WHILE READY = 0

'zzzzzzzzzzzzzzzzzzzz ... waiting !!!
WEND

GOTO Start 'GOTO what to do if not ready ???




DISABLE INTERRUPT

poo:

IF FOOTSW THEN false 'we are looking for a Portb.5 interrupt ... not PortB.4 or else

T = 1' 000 '1mS AT 4 mHZ ...........Note: 1000 for MPSIM or testboard , 1 for real


freq: ' Note : I Use a blinking Led on my testboard ....

TOGGLE RENCA
PAUSE T 'frequency should be 500 Hz (2mS period)

GOTO freq

false:

RESUME
ENABLE INTERRUPT

end



> Renca and Eject have to be initialised to "1" ( not zero ...)

" IF FOOTSW THEN false 'we are looking for a Portb.5 interrupt ... not PortB.4 or else "

> The test must work ( jump around 500 Hz generating ) if START is on the "1" State ... not Zero.


Try to understand what the program does, instead of Copiying it rudely as is ...

I know it is not so easy ... but try.

Alain

Russ Kincaid
- 31st May 2006, 02:05
I have learned a lot, working on this problem, but the problem was that I had all my configuration fuses enabled (except protection). Thanks to ?wumpus (I forget his handle), I disabled low voltage programming and all is well now.