Someone help me to simplify this


Closed Thread
Results 1 to 5 of 5
  1. #1
    thrix's Avatar
    thrix Guest

    Question Someone help me to simplify this

    hi all... i manage to program my led matrix with the following code. is there any way to make it simpler?

    I VAR WORD 'DELAY FOR ALL COLUMN DISPLAY
    J VAR WORD 'DELAY FOR SHIFT LEFT NO 1 DISPLAY
    K VAR WORD 'DELAY FOR SHIFT LEFT NO 2 DISPLAY
    L VAR WORD 'DELAY FOR SHIFT LEFT NO 3 DISPLAY
    M VAR WORD 'DELAY FOR SHIFT LEFT NO 4 DISPLAY
    N VAR WORD 'DELAY FOR SHIFT LEFT NO 5 DISPLAY

    TRISA = 255
    TRISB = 0

    MAIN:
    FOR I = 0 TO 25
    GOTO LOOP

    loop:
    ' DISPLAYING AT ALL COLUMN
    PORTB = $7F ''''''''''''''''''
    LOW PORTA.4 ' END POINT '
    PAUSE 3 ' '
    HIGH PORTA.4 ''''''''''''''''''

    PORTB = $8
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3

    PORTB = $8
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2

    PORTB = $8
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1

    PORTB = $7F 'STARTING POINT
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0

    NEXT


    'SHIFT NO 1 TO LEFT
    FOR J = 0 TO 25

    PORTB = $8
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0

    PORTB = $8
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1

    PORTB = $8
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2

    PORTB = $7F 'END POINT
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3


    NEXT

    'SHIFT LEFT NO 2
    FOR K = 0 TO 25

    PORTB = $8
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0

    PORTB = $8
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1

    PORTB = $7F ' END POINT
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2

    ' --------------> ' COLUMN 3 IS HIGH

    PORTB = $7F ' STARTING POINT
    LOW PORTA.4
    PAUSE 3
    HIGH PORTA.4

    NEXT

    'SHIFT LEFT NO 3
    FOR L = 0 TO 25


    PORTB = $8
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0

    PORTB = $7F 'END POINT
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1

    PORTB = $7F ' STARTING POINT
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3

    PORTB = $8
    LOW PORTA.4
    PAUSE 3
    HIGH PORTA.4

    NEXT

    'SHIFT LEFT NO 4
    FOR M = 0 TO 25

    PORTB = $7F 'END POINT
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0

    PORTB = $7F ' STARTTING POINT
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2

    PORTB = $8
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3

    PORTB = $8
    LOW PORTA.4
    PAUSE 3
    HIGH PORTA.4

    NEXT
    'SHIFT LEFT NO 5
    FOR N = 0 TO 25

    PORTB = $7F 'STARTING POINT
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1

    PORTB = $8
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2

    PORTB = $8
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3

    PORTB = $8
    LOW PORTA.4
    PAUSE 3
    HIGH PORTA.4

    NEXT





    goto MAIN

    ' Go back to loop and blink LED forever
    End
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Thrix

    Thrix, a couple of things ok?
    1. I do not have anyway how to test this ok?


    Now. 2nd thing.... I noticed that you used the variables I-N for some For-Next Loops. Well I also noticed that none of these loops were nested. That means you can use 1 variable for *all* the loops (for this program example only that you gave).

    Now the 3rd thing.... I Noticed that you were only Toggling 5 ports on and off... Nothing more. So what I did, was put every one of your toggles into a SubRoutine. When I want to Toggle that port, I just call that subroutine. This probably cut the number of lines in your code by 1/3.

    Since I have no way of checking the program against something, I may have errored in reading your code. I also noticed a Loop: and a Goto Loop: Those seemed redundant, because at the end of your program you have Goto Main. It is also not wise to jump in the middle of a For/Next loop with a goto *outside* the for/next loop.

    Here is what i think the code may look like after the above points are implimented:



    I VAR WORD 'DELAY FOR ALL COLUMN DISPLAY

    TRISA = 255
    TRISB = 0

    MAIN:
    FOR I = 0 TO 25

    ' DISPLAYING AT ALL COLUMN
    PORTB = $7F ''''''''''''''''''
    GoSub TogPA4

    PORTB = $8
    GoSub TogPA3

    PORTB = $8
    GoSub TogPA2

    PORTB = $8
    GoSub TogPA1

    PORTB = $7F 'STARTING POINT

    GoSub TogPA0

    NEXT I


    'SHIFT NO 1 TO LEFT
    FOR I = 0 TO 25

    PORTB = $8
    GoSub TogPA0

    PORTB = $8
    GoSub TogPA1

    PORTB = $8
    GoSub TogPA2

    PORTB = $7F 'END POINT
    Gosub TogPA3

    NEXT I



    'SHIFT LEFT NO 2
    FOR I = 0 TO 25

    PORTB = $8
    gosub TogPA0

    PORTB = $8
    GoSub TogPA1

    PORTB = $7F ' END POINT
    Gosub TogPA2
    ' --------------> ' COLUMN 3 IS HIGH

    PORTB = $7F ' STARTING POINT
    GoSub TogPA4

    NEXT I

    'SHIFT LEFT NO 3
    FOR I = 0 TO 25

    PORTB = $8
    GoSub TogPA0

    PORTB = $7F 'END POINT
    GoSub TogPA1

    PORTB = $7F ' STARTING POINT
    GoSub TogPA3

    PORTB = $8
    GoSub TogPA4
    NEXT I



    'SHIFT LEFT NO 4
    FOR I = 0 TO 25

    PORTB = $7F 'END POINT
    GoSub TogPA0

    PORTB = $7F ' STARTTING POINT
    GoSub TogPA2

    PORTB = $8
    GoSub TogPA3

    PORTB = $8
    GoSub TogPA4
    NEXT I


    'SHIFT LEFT NO 5
    FOR I = 0 TO 25

    PORTB = $7F 'STARTING POINT
    GoSub TogPA1

    PORTB = $8
    GoSub TogPA2

    PORTB = $8
    GoSub TogPA3

    PORTB = $8
    GoSub TogPA4
    NEXT I


    goto MAIN


    ;subroutines!!!!!!!!!!


    TogPA4:
    LOW PORTA.4 ' END POINT '
    PAUSE 3 ' '
    HIGH PORTA.4 ''''''''''''''''''
    return

    TogPA3:
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3
    return

    TogPA2:
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2
    return

    TogPA1:
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1
    return

    TogPA0:
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0
    return


    ' Go back to loop and blink LED forever
    End

  3. #3
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Thrix,


    A couple of more things.
    1. I was able to do a For/Next loop, and shift 5 times, but I could not include the very first shift, because I did not know if you had to "Toggle" the port in that sequence.

    the First time you toggled, (the I loop) you toggled P.4, P.3, P.2, P.1,P.0, in that order. The rest of the times you toggled them exactly backwards. P.0,P.1,P.2,P.3,P.4.
    I looped the ones you toggled backwards together, and put if statments in them. I couldn't loop the very first one, because I do not know if you can toggle them the other way.

    2. I wrote the *if* statements in a different langauge. I do not know the exact structure of the if statement for what you are using. This means you will have to convert my if statement to yours. For example;

    if(counter==1)
    {
    ..........
    } else
    {
    .......
    }

    Might be converted to your if statement as:

    if (counter=1)
    Begin
    .......
    else
    .......
    endif


    3. If it doesn't matter on the very first loop whether you toggle the ports 0-4 or 4 - 0 , You can include that into my loop by adding just one if statement and making the counter go from 0 to 5, instead of 1 to 5, and get rid of another loop and simplify it even more.


    Here is a shorter , more simplified program....

    I VAR WORD 'DELAY FOR ALL COLUMN DISPLAY
    counter var word'

    TRISA = 255
    TRISB = 0

    MAIN:
    FOR I = 0 TO 25

    ' DISPLAYING AT ALL COLUMN
    PORTB = $7F ''''''''''''''''''
    GoSub TogPA4

    PORTB = $8
    GoSub TogPA3

    PORTB = $8
    GoSub TogPA2

    PORTB = $8
    GoSub TogPA1

    PORTB = $7F 'STARTING POINT

    GoSub TogPA0

    NEXT I


    'Lets shift!
    For counter=1 to 5
    For J = 0 TO 25

    if(counter==4)
    {
    PORTB = $7
    GoSub TogPA0
    }else if(counter!=5)
    {
    PORTB = $8
    GoSub TogPA0
    }

    if((counter==3) or (counter==5))
    {
    PORTB = $7
    GoSub TogPA1
    }else if(counter!=4)
    {
    PORTB = $8
    GoSub TogPA1
    }

    if((counter==2) or (counter==4))
    {
    PORTB = $7
    GoSub TogPA2
    }else if(counter!=3)
    {
    PORTB = $8
    GoSub TogPA2
    }

    if((counter==1) or (counter==3))
    {
    PORTB = $7
    GoSub TogPA3
    }else if(counter!=2)
    {
    PORTB = $8
    GoSub TogPA3
    }


    if(counter==2)
    {
    PORTB = $7
    GoSub TogPA4
    }else if(counter!=1)
    {
    PORTB = $8
    GoSub TogPA4
    }

    NEXT j
    Next counter



    goto MAIN


    ;subroutines!!!!!!!!!!


    TogPA4:
    LOW PORTA.4 ' END POINT '
    PAUSE 3 ' '
    HIGH PORTA.4 ''''''''''''''''''
    return

    TogPA3:
    LOW PORTA.3
    PAUSE 3
    HIGH PORTA.3
    return

    TogPA2:
    LOW PORTA.2
    PAUSE 3
    HIGH PORTA.2
    return

    TogPA1:
    LOW PORTA.1
    PAUSE 3
    HIGH PORTA.1
    return

    TogPA0:
    LOW PORTA.0
    PAUSE 3
    HIGH PORTA.0
    return


    ' Go back to loop and blink LED forever
    End

  4. #4
    thrix's Avatar
    thrix Guest


    Did you find this post helpful? Yes | No

    Smile thanks

    thanks a lot friend... i'm new to this pic thing but getting okay step by step .. btw thanks again

  5. #5
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Thrix,

    Thrix >>thanks a lot friend... i'm new to this pic thing but getting okay step by step .. btw thanks again<<

    You are very welcome. I am also very new to these PIC chips. I used to program the Phillips chips with assembly, and decided to try these little PIC chips for a project of mine.

    I chief programmer for a major company, so the programming part I have no problems with. Its just trying to learn the different syntax of this chip and/or the basic pro.

    Dwayne

Similar Threads

  1. Simplify This:
    By Art in forum General
    Replies: 2
    Last Post: - 29th December 2009, 20:19
  2. Recommendations - Model Train Controller
    By malc-c in forum mel PIC BASIC Pro
    Replies: 101
    Last Post: - 8th March 2007, 08:17
  3. Strangw results when using PWM command
    By malc-c in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th July 2006, 12:14

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