PDA

View Full Version : IF...THEN inside a Loop



Bill Legge
- 10th July 2009, 06:53
The following code executes correctly on a PIC16F877A at 20 MHz but not on a PIC18F8722 at 10 or 40 MHz:



clear
DEFINE OSC 40 ' Run on PIC18F8722 using HSPPL
ADCON1 = $0F ' Set PortA to digital

TRISB=0 ' Make Port B outputs
PORTB=0 ' Clear the port

X var byte

Loop:
PORTB=$0
FOR X = 0 to 3
IF X<1 THEN PORTB=$F0
IF X>1 AND X<3 THEN PORTB=$3C
IF X>2 THEN PORTB=$0F
PAUSE 1000
NEXT X
GOTO Loop


Please note:
1. With two IF...THENs inside the loop - all is OK.
2. I know that it would be easier to use
IF X=1 THEN....
IF X=2 THEN...
Etc, but this is a simplification of my real code.
3. I've tried a lot of variations but anything with multiple (>2) IF....THENs inside the loop will not work.
4. LEDs on PORTB work OK with
FOR X=0 TO 255
PORTB=X
NEXT X
5. Going nuts on this. What am I missing?

Regards Bill Legge

EarlyBird2
- 10th July 2009, 07:49
Hi Bill

I do not think you are missing anything. The only thing I can see is that you should put parenthasis around you conditions when you use the AND statement but I can not see how that or why that would work with one chip and not another. I will read the data sheet on the PIC18F8722 but I am not hopeful. Generaly what I do in these cirumstances is to find a work round solution and then later on, some times years later, all becomes clear.

Steve

Melanie
- 10th July 2009, 08:16
What happens if X=1 ?

sayzer
- 10th July 2009, 10:47
What happens if X=1 ?

Also,
IF X<1 THEN PORTB=$F0 is the same as X = 0

IF X>1 AND X<3 THEN PORTB=$3C is the same as X = 2

IF X>2 THEN PORTB=$0F is the same as X = 3 ( For loop goes to 3)

Thus,

IF X = 0 THEN PORTB=$F0
IF X = 2 THEN PORTB=$3C
IF X = 3 THEN PORTB=$0F

looks better.

Bill Legge
- 10th July 2009, 13:02
Sorry - I made a silly mistake in the code I posted. The case where X=1 shows my error.

However, the real code that was/is causing trouble was for a larger range of X and included '>=', '<='.

My poor attempt to simplify the real problem failed!

I'll tidy up the real code and post it soonest.

Thanks for your input.

Regards Bill legge

Bill Legge
- 11th July 2009, 02:36
Sorry for my earlier muddled post. I've done some more work on the problem and have pinned down my difficulty.

I am compiling using PBPL and using a graphic LCD with 4 driver chips.
The GLCD is 240 pixels wide and I need to activate the Chip Selects as the display sweeps along:

0 to 63 CS1
64 to 127 CS2
128 to 191 CS3
192 to 239 CS4

The chip selects are connected to PORTB and my code is:



clear
DEFINE OSC 40 ' Run on PIC18F8722, HSPPL
ADCON1 = $0F ' Set PortA to digital

TRISB=0 ' Make Port B outputs
PORTB=0 ' clear the port

X var byte

Loop:
PORTB=$0
for X = 0 to 239
if X<64 then PORTB=$01
if X>63 and X<128 then PORTB=$02
if X>127 and X<192 then PORTB=$04
if X>191 then PORTB=$08
pause 5
next X
goto Loop


If I compile using PBP it works properly and the chip selects turn on/off as X goes from 0 to 239.

If I compile using PBPL it does not work.

I need to use the LONG compilation. Any idea what is going wrong?

Regards Bill Legge

Darrel Taylor
- 11th July 2009, 04:07
Hi Bill,

No idea what's wrong at this point.
But I did run your program as is, no changes, on an 18F252.

Works the same with either PBPW or PBPL. :confused:

You can also do the same thing like this ...
for X = 0 to 239
PORTB = DCD (X >> 6)
pause 5
next X


Added:
So that it only affects those 4 pins, you could do this ...
PORTB = (PORTB & $F0) | DCD (X >> 6)

mackrackit
- 11th July 2009, 05:09
Silly question.
PM or MPASM ?
Or does it matter?

Bill Legge
- 11th July 2009, 05:16
I'm using MPASM because the PBP does not support the PIC18F8722

Regards Bill Legge

Bill Legge
- 11th July 2009, 05:20
Darrel T

Thanks very much for the code using DCD. It works fine with both PBP and PBPL.

I'd still like to find out why the IF...THEN won't work with PBPL?

Regards Bill Legge
In Australia

kindows
- 11th July 2009, 06:35
I am new with PICBASIC
I am trying to understand
Good luck for all.