IF...THEN inside a Loop


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136

    Default IF...THEN inside a Loop

    The following code executes correctly on a PIC16F877A at 20 MHz but not on a PIC18F8722 at 10 or 40 MHz:

    Code:
    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

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    What happens if X=1 ?

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    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.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default If...then

    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

  6. #6
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default PIC BASIC PRO LONG and IF..THEN

    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:

    Code:
    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

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.

    You can also do the same thing like this ...
    Code:
    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 ...
    Code:
    PORTB = (PORTB & $F0) | DCD (X >> 6)
    Last edited by Darrel Taylor; - 11th July 2009 at 04:12. Reason: Added:
    DT

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Silly question.
    PM or MPASM ?
    Or does it matter?
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default IF...then pbpl

    I'm using MPASM because the PBP does not support the PIC18F8722

    Regards Bill Legge

  10. #10
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default If...then pbpl

    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

  11. #11
    Join Date
    Jul 2009
    Posts
    15


    Did you find this post helpful? Yes | No

    Smile I think u can help

    I am new with PICBASIC
    I am trying to understand
    Good luck for all.
    Last edited by kindows; - 11th July 2009 at 06:38.

Similar Threads

  1. Controlsystem for compact sporting (clay shooting)
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2009, 16:48
  2. Serin to Serin2 ??
    By Gixxer in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th January 2008, 03:56
  3. Serial Relays
    By tazntex in forum General
    Replies: 3
    Last Post: - 17th May 2007, 17:42
  4. newbie Q - edge detection?
    By RMCRAVEN in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 9th October 2006, 08:20
  5. Newbie question:
    By Izone in forum mel PIC BASIC
    Replies: 2
    Last Post: - 26th February 2006, 16:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts