Output PIC module


Closed Thread
Results 1 to 6 of 6
  1. #1
    freelancebee's Avatar
    freelancebee Guest

    Question Output PIC module

    Hi experienced All:
    I am trying to check advantages and disadvantages
    of Three PICs Architecture.
    I appreciate any comments and thoughts how to improve
    program below that will serve output functions
    in Output PIC.
    Thank you for your time.
    Best Regards,
    Leonid
    P.C.PICBasic Pro Compiler tell: success: 995 word used
    My inner filling tell me that is possabilities to improve
    this program.

    'PIC16F84A
    'This program only example that I will test
    'in Three PICs Architecture: Input PIC,Output PIC, and Control PIC.
    'This program serve output PIC.
    'Program used combination pins PortA.0 - PortA.4 for input
    'to change state of pins PortB.0 - PortB.5 and 6,7 for output
    'Input cominations come from second PIC
    'Order
    '1. PortA.4=0 Halt program
    '2. change code PortA.0-PortA.2 and result code from 1 to 7
    'will choose program from 1 to 7
    '3. PortA.3=0 set program
    '4. PortA.4=1 preset
    '5. PortA.3=1 run choosen program
    DEFINE OSC 20
    DEFINE _CP_OFF $3FFF
    DEFINE _PWRTE_OFF $3FFF
    DEFINE _WDT_OFF $3FFB
    DEFINE _XT_OSC $3FFD
    DEFINE _HS_OSC $3FFE
    OPTION_REG = %10000000
    INTCON = %00000000
    c var byte
    d var byte
    e var byte
    b16 var BYTE
    w9 var WORD
    'set constant
    b0 con %00000000
    b1 con %00000001
    b2 con %00000010
    b3 con %00000100
    b4 con %00001000
    b5 con %00010000
    b6 con %00100000
    b7 con %00001001
    b8 con %00010010
    b9 con %00100100
    b10 con %00011011
    b11 con %00110110
    b12 con %00101101
    b13 con %00010010
    b14 con %00111111
    b15 con %00010101
    'Set Ports
    poke TRISA, 255 '31
    poke TRISB, b0
    'Initialize variable
    w9 = $2710
    Halt:
    poke PORTB, b0
    peek PORTA, b16
    IF PortA.3 = 0 Then Switch
    IF PortA.4 = 0 Then Halt
    goto Halt
    Switch:
    poke PORTB, b0
    peek PORTA, b16
    IF PortA.0 = 1 Then c = $1
    c=0
    IF PortA.1 = 1 Then d = $2
    d=0
    IF PortA.2 = 1 Then e = $4
    e=0
    Let b16=c+d+e
    IF b16=$1 Then PROG1
    IF b16=$2 Then PROG2
    IF b16=$3 Then PROG3
    IF b16=$4 Then PROG4
    IF b16=$5 Then PROG5
    IF b16=$6 Then PROG6
    IF b16=$7 Then PROG7
    goto Switch
    PROG1: '------------Program #1
    peek PORTA, b16
    IF PortA.4=0 Then Halt
    IF PortA.2=0 Then RL1
    gOTO ROR1
    Goto PROG1
    RL1: '------------Block Rotate Left 1
    poke PORTB, b6
    pulsout 6, w9
    poke PORTB, b5
    pulsout 6, w9
    poke PORTB, b4
    pulsout 6, w9
    poke PORTB, b3
    pulsout 6, w9
    poke PORTB, b2
    pulsout 6, w9
    poke PORTB, b1
    pulsout 7, w9
    call M1
    goto PROG1
    M1:
    If PORTA.0 = 1 Then P1
    w9 = w9 - 1
    If w9 < $100 Then Lm1
    goto PROG1
    P1:
    If PORTA.1 = 1 Then PROG1
    w9 = w9 + 1
    If w9 > $5000 Then Lp1
    w9 = $5000
    Goto PROG1
    Lm1:
    w9 = $100
    Goto PROG1
    Lp1:
    w9 = $5000
    Goto PROG1
    ROR1:
    poke PORTB, b1
    pulsout 6, w9
    poke PORTB, b2
    pulsout 6, w9
    poke PORTB, b3
    pulsout 6, w9
    poke PORTB, b4
    pulsout 6, w9
    poke PORTB, b5
    pulsout 6, w9
    poke PORTB, b6
    pulsout 7, w9
    call M1
    goto PROG1
    PROG2: '------------Program #2
    peek PORTA, b16
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL2
    IF PortA.2= 1 Then ROR2
    Goto PROG2
    RL2: '------------Block Rotate Left 2
    poke PORTB, b9
    pulsout 6, w9
    poke PORTB, b8
    pulsout 6, w9
    poke PORTB, b7
    pulsout 7, w9
    call M2
    goto PROG2
    M2:
    If PORTA.0 = 1 Then P2
    w9 = w9 - 1
    If w9 < $100 Then Lm2
    goto PROG2
    P2:
    If PORTA.1 = 1 Then PROG2
    w9 = w9 + 1
    If w9 > $5000 Then Lp2
    w9 = $5000
    Goto PROG2
    Lm2:
    w9 = $100
    Goto PROG2
    Lp2:
    w9 = $5000
    Goto PROG2
    ROR2: '------------Block Rotate Right 2
    poke PORTB, b7
    pulsout 6, w9
    poke PORTB, b8
    pulsout 6, w9
    poke PORTB, b9
    pulsout 7, w9
    call M2
    goto PROG2
    PROG3: '------------Program #3
    peek PORTA, b16
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL3
    IF PortA.2= 1 Then RR3
    Goto PROG3
    RL3: '------------Block Rotate Left 3
    poke PORTB, b12
    pulsout 6, w9
    poke PORTB, b9
    pulsout 6, w9
    poke PORTB, b11
    pulsout 6, w9
    poke PORTB, b8
    pulsout 6, w9
    poke PORTB, b10
    pulsout 6, w9
    poke PORTB, b7
    pulsout 7, w9
    call M3
    goto PROG3
    M3:
    If PORTA.0 = 1 Then P3
    w9 = w9 - 1
    If w9 < $100 Then Lm3
    goto PROG3
    P3:
    If PORTA.1 = 1 Then PROG3
    w9 = w9 + 1
    If w9 > $5000 Then Lp3
    w9 = $5000
    Goto PROG3
    Lm3:
    w9 = $100
    Goto PROG3
    Lp3:
    w9 = $5000
    Goto PROG3
    RR3: '------------Block Rotate Right 3
    poke PORTB, b7
    pulsout 6, w9
    poke PORTB, b10
    pulsout 6, w9
    poke PORTB, b8
    pulsout 6, w9
    poke PORTB, b11
    pulsout 6, w9
    poke PORTB, b9
    pulsout 6, w9
    poke PORTB, b12
    pulsout 7, w9
    call M3
    goto PROG3
    PROG4: '------------Program #4
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL4
    IF PortA.2= 1 Then RR4
    Goto PROG4
    RL4: '------------Block Rotate Left 4
    poke PORTB, b14
    pulsout 6, w9
    poke PORTB, b15
    pulsout 6, w9
    poke PORTB, b14
    pulsout 6, w9
    poke PORTB, b13
    pulsout 7, w9
    call M4
    goto PROG4
    M4:
    If PORTA.0 = 1 Then P4
    w9 = w9 - 1
    If w9 < $100 Then Lm4
    goto PROG4
    P4:
    If PORTA.1 = 1 Then PROG4
    w9 = w9 + 1
    If w9 > $5000 Then Lp4
    w9 = $5000
    Goto PROG4
    Lm4:
    w9 = $100
    Goto PROG4
    Lp4:
    w9 = $5000
    Goto PROG4
    RR4: '------------Block Rotate Right 4
    poke PORTB, b13
    pulsout 6, w9
    poke PORTB, 14
    pulsout 6, w9
    poke PORTB, 15
    pulsout 6, w9
    poke PORTB, b14
    pulsout 7, w9
    call M4
    goto PROG4
    PROG5: '------------Program #5
    IF PortA.4= 0 Then Halt
    GOTO RR5
    Goto PROG5
    M5:
    If PORTA.0 = 1 Then P5
    w9 = w9 - 1
    If w9 < $100 Then Lm5
    goto PROG5
    P5:
    If PORTA.1 = 1 Then PROG5
    w9 = w9 + 1
    If w9 > $5000 Then Lp5
    w9 = $5000
    Goto PROG5
    Lm5:
    w9 = $100
    Goto PROG5
    Lp5:
    w9 = $5000
    Goto PROG5
    RR5: '------------Block Rotate Right 5
    poke PORTB, b10
    pulsout 6, w9
    poke PORTB, b14
    pulsout 6, w9
    poke PORTB, b11
    pulsout 6, w9
    poke PORTB, b14
    pulsout 6, w9
    poke PORTB, b12
    pulsout 6, w9
    poke PORTB, b14
    pulsout 7, w9
    call M5
    goto PROG5
    PROG6: '------------Program #6
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL6
    IF PortA.2= 1 Then RR6
    Goto PROG6
    RL6: '------------Block Rotate Left 6
    poke PORTB, b13
    pulsout 6, w9
    poke PORTB, b15
    pulsout 7, w9
    call M1
    goto PROG6
    M6:
    If PORTA.0 = 1 Then P6
    w9 = w9 - 1
    If w9 < $100 Then Lm6
    goto PROG6
    P6:
    If PORTA.1 = 1 Then PROG6
    w9 = w9 + 1
    If w9 > $5000 Then Lp6
    w9 = $5000
    Goto PROG6
    Lm6:
    w9 = $100
    Goto PROG6
    Lp6:
    w9 = $5000
    Goto PROG6
    RR6: '------------Block Rotate Right 6
    poke PORTB, b15
    pulsout 6, w9
    poke PORTB, b13
    pulsout 7, w9
    call M6
    goto PROG6
    PROG7: '------------Program #7
    IF PortA.4= 0 Then Halt
    GOTO RL7
    Goto PROG7
    RL7: '------------Block nO RotateD 7
    poke PORTB, b0
    pulsout 6, w9
    poke PORTB, b14
    pulsout 7, w9
    call M7
    goto PROG7
    M7:
    If PORTA.0 = 1 Then P7
    w9 = w9 - 1
    If w9 < $100 Then Lm7
    goto PROG7
    P7:
    If PORTA.1 = 1 Then PROG7
    w9 = w9 + 1
    If w9 > $5000 Then Lp7
    w9 = $5000
    Goto PROG7
    Lm7:
    w9 = $100
    Goto PROG7
    Lp7:
    w9 = $5000
    Goto PROG7
    End

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    no big improvement on your revious but compile 772 word now
    Code:
    'PIC16F84A
    'This program only example that I will test
    'in Three PICs Architecture: Input PIC,Output PIC, and Control PIC.
    'This program serve output PIC.
    'Program used combination pins PortA.0 - PortA.4 for input
    'to change state of pins PortB.0 - PortB.5 and 6,7 for output
    'Input cominations come from second PIC 
    'Order
    '1. PortA.4=0 Halt program
    '2. change code PortA.0-PortA.2 and result code from 1 to 7
    'will choose program from 1 to 7
    '3. PortA.3=0 set program
    '4. PortA.4=1 preset
    '5. PortA.3=1 run choosen program 
        DEFINE OSC 20 
        DEFINE _CP_OFF $3FFF
        DEFINE _PWRTE_OFF $3FFF
        DEFINE _WDT_OFF $3FFB
        DEFINE _XT_OSC $3FFD
        DEFINE _HS_OSC $3FFE
    
        OPTION_REG = %10000000 
        INTCON = %00000000 
    
        c var byte
        d var byte
        e var byte
        b16 var BYTE 
        w9 var WORD 
        pbpin var byte
    
        'set constant
        b0 con %00000000
        b1 con %00000001
        b2 con %00000010
        b3 con %00000100
        b4 con %00001000
        b5 con %00010000
        b6 con %00100000
        b7 con %00001001
        b8 con %00010010
        b9 con %00100100
        b10 con %00011011
        b11 con %00110110
        b12 con %00101101
        b13 con %00010010
        b14 con %00111111
        b15 con %00010101
    
        'Set Ports
        TRISA= 255 '31
        TRISB= b0
    
        'Initialize variable
        w9 = $2710 
    
    Halt:
        PORTB= b0
        b16 = PORTA
        IF PortA.3 = 0 Then Switch
        IF PortA.4 = 0 Then Halt
        goto Halt 
    
    Switch:
        PORTB= b0 
        b16 = PORTA & 7
        branch b16,[switch,prog1,prog2,prog3,prog4,prog5,prog6,prog7]            
        goto Switch 
    
    PROG1: 
        '------------Program #1
        b16=PORTA
        IF PortA.4=0 Then Halt
        IF PortA.2=0 Then RL1
        gOTO ROR1
        Goto PROG1 
    
    RL1: 
        '------------Block Rotate Left 1
        PORTB=b6
        pbpin=6
        gosub sendpulses
        
        PORTB=B5
        GOSUB SENDPULSES
    
        PORTB= b4
        GOSUB SENDPULSES
    
        PORTB= b3
        GOSUB SENDPULSES
    
        PORTB= b2
        GOSUB SENDPULSES
    
        PORTB= b1
        PBPIN=7
        GOSUB SENDPULSES
    
        call M1
        goto PROG1
    
    M1:
        If PORTA.0 = 1 Then P1
        w9 = w9 - 1
        If w9 < $100 Then Lm1
        goto PROG1 
    
    P1:
        If PORTA.1 = 1 Then PROG1
        w9 = w9 + 1
        If w9 > $5000 Then Lp1
        w9 = $5000
        Goto PROG1
    
    Lm1:
        w9 = $100
        Goto PROG1 
    
    Lp1:
        w9 = $5000
        Goto PROG1
    
    ROR1:
        PORTB= b1
        PBPIN=6
        GOSUB SENDPULSES
    
        PORTB= b2
        GOSUB SENDPULSES
    
        PORTB= b3
        GOSUB SENDPULSES
    
        PORTB= b4
        GOSUB SENDPULSES
    
        PORTB= b5
        GOSUB SENDPULSES
    
        PORTB= b6
        PBPIN=7
        GOSUB SENDPULSES
    
        call M1
        goto PROG1 
    
    PROG2: 
        '------------Program #2
        b16=PORTA
        IF PortA.4= 0 Then Halt
        IF PortA.2= 0 Then RL2
        IF PortA.2= 1 Then ROR2
        Goto PROG2 
    
    RL2: 
        '------------Block Rotate Left 2
        PORTB= b9 
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b8 
        GOSUB SENDPULSES
            
        PORTB= b7
        PBPIN=7
        GOSUB SENDPULSES
        
        call M2
        goto PROG2 
    
    M2:
        If PORTA.0 = 1 Then P2
        w9 = w9 - 1
        If w9 < $100 Then Lm2
        goto PROG2 
    
    P2:
        If PORTA.1 = 1 Then PROG2
        w9 = w9 + 1
        If w9 > $5000 Then Lp2
        w9 = $5000
        Goto PROG2 
    
    Lm2:
        w9 = $100
        Goto PROG2 
    
    Lp2:
        w9 = $5000
        Goto PROG2 
    
    ROR2: 
        '------------Block Rotate Right 2
        PORTB= b7
        PBPIN=6
        GOSUB SENDPULSES
            
        PORTB= b8
        GOSUB SENDPULSES
        
        PORTB= b9
        PBPIN=7
        GOSUB SENDPULSES
        
        call M2
        goto PROG2 
    
    PROG3: 
        '------------Program #3 
        b16=PORTA
        IF PortA.4= 0 Then Halt
        IF PortA.2= 0 Then RL3
        IF PortA.2= 1 Then RR3
        Goto PROG3 
    
    RL3: 
        '------------Block Rotate Left 3
        PORTB= b12 
        PBPIN=6
        GOSUB SENDPULSES    
    
        PORTB= b9 
        GOSUB SENDPULSES
        
        PORTB= b11
        GOSUB SENDPULSES
    
        PORTB= b8
        GOSUB SENDPULSES
    
        PORTB= b10
        GOSUB SENDPULSES
    
        PORTB= b7
        PBPIN=7
        GOSUB SENDPULSES
    
        call M3
        goto PROG3 
    
    M3:
        If PORTA.0 = 1 Then P3
        w9 = w9 - 1
        If w9 < $100 Then Lm3
        goto PROG3 
    
    P3:
        If PORTA.1 = 1 Then PROG3
        w9 = w9 + 1
        If w9 > $5000 Then Lp3
        w9 = $5000
        Goto PROG3 
    
    Lm3:
        w9 = $100
        Goto PROG3 
    
    Lp3:
        w9 = $5000
        Goto PROG3 
    
    RR3: 
        '------------Block Rotate Right 3
        PORTB= b7
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b10
        GOSUB SENDPULSES
        
        PORTB= b8
        GOSUB SENDPULSES
        
        PORTB= b11
        GOSUB SENDPULSES
        
        PORTB= b9
        GOSUB SENDPULSES
        
        PORTB= b12
        PBPIN=7
        GOSUB SENDPULSES
        
        call M3
        goto PROG3 
    
    PROG4: 
        '------------Program #4
        IF PortA.4= 0 Then Halt
        IF PortA.2= 0 Then RL4
        IF PortA.2= 1 Then RR4
        Goto PROG4
    
    RL4: 
        '------------Block Rotate Left 4
        PORTB= b14 
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b15 
        GOSUB SENDPULSES
        
        PORTB= b14
        GOSUB SENDPULSES
        
        PORTB= b13
        PBPIN=7
        GOSUB SENDPULSES
        
        call M4
        goto PROG4 
    
    M4:
        If PORTA.0 = 1 Then P4
        w9 = w9 - 1
        If w9 < $100 Then Lm4
        goto PROG4 
    
    P4:
        If PORTA.1 = 1 Then PROG4
        w9 = w9 + 1
        If w9 > $5000 Then Lp4
        w9 = $5000
        Goto PROG4 
    
    Lm4:
        w9 = $100
        Goto PROG4
    
    Lp4:
        w9 = $5000
        Goto PROG4 
    
    RR4: 
        '------------Block Rotate Right 4
        PORTB= b13
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= 14
        GOSUB SENDPULSES
        
        PORTB= 15
        GOSUB SENDPULSES
        
        PORTB= b14
        PBPIN=7
        GOSUB SENDPULSES
        
        call M4
        goto PROG4 
    
    PROG5: 
        '------------Program #5
        IF PortA.4= 0 Then Halt 
        GOTO RR5
        Goto PROG5 
    
    M5:
        If PORTA.0 = 1 Then P5
        w9 = w9 - 1
        If w9 < $100 Then Lm5
        goto PROG5 
    
    P5:
        If PORTA.1 = 1 Then PROG5
        w9 = w9 + 1
        If w9 > $5000 Then Lp5
        w9 = $5000
        Goto PROG5
    
    Lm5:
        w9 = $100
        Goto PROG5
    
    Lp5:
        w9 = $5000
        Goto PROG5
    
    RR5: 
        '------------Block Rotate Right 5
        PORTB= b10
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b14
        GOSUB SENDPULSES
        
        PORTB= b11
        GOSUB SENDPULSES
        
        PORTB= b14
        GOSUB SENDPULSES
        
        PORTB= b12
        GOSUB SENDPULSES
        
        PORTB= b14
        PBPIN=7
        GOSUB SENDPULSES
        
        call M5
        goto PROG5 
    
    PROG6: 
        '------------Program #6
        IF PortA.4= 0 Then Halt
        IF PortA.2= 0 Then RL6
        IF PortA.2= 1 Then RR6
        Goto PROG6 
    
    RL6: 
        '------------Block Rotate Left 6
        PORTB= b13 
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b15 
        PBPIN=7
        GOSUB SENDPULSES
        
        call M1
        goto PROG6
    
    M6:
        If PORTA.0 = 1 Then P6
        w9 = w9 - 1
        If w9 < $100 Then Lm6
        goto PROG6 
    
    P6:
        If PORTA.1 = 1 Then PROG6
        w9 = w9 + 1
        If w9 > $5000 Then Lp6
        w9 = $5000
        Goto PROG6
    
    Lm6:
        w9 = $100
        Goto PROG6
    
    Lp6:
        w9 = $5000
        Goto PROG6
    
    RR6: 
        '------------Block Rotate Right 6
        PORTB= b15
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b13
        GOSUB SENDPULSES
        
        call M6
        goto PROG6
    
    PROG7: 
        '------------Program #7
        IF PortA.4= 0 Then Halt
        GOTO RL7 
        Goto PROG7 
    
    RL7: 
        '------------Block nO RotateD 7
        PORTB= b0 
        PBPIN=6
        GOSUB SENDPULSES
        
        PORTB= b14 
        PBPIN=7
        GOSUB SENDPULSES
        
        call M7
        goto PROG7
    
    M7:
        If PORTA.0 = 1 Then P7
        w9 = w9 - 1
        If w9 < $100 Then Lm7
        goto PROG7
    
    P7:
        If PORTA.1 = 1 Then PROG7
        w9 = w9 + 1
        If w9 > $5000 Then Lp7
        w9 = $5000
        Goto PROG7 
    
    Lm7:
        w9 = $100
        Goto PROG7
    
    Lp7:
        w9 = $5000
        Goto PROG7 
    
    SendPulses:
        pulsout pbpin, w9
        return
    as there's a lot of jump here, there elsewhere, lalaland, i think you should build a flowchart to help you to structure everything and avoid so many jumps. Well it's my own opinion.

    One thing Jump to my face ... the use Of CALL !!!! usually we use GOSUB with a RETURN.
    Quote Originally Posted by PBP manual
    CALL Label

    Execute the assembly language subroutine named Label. GOSUB is normally used to execute a PicBasic subroutine. The main difference between GOSUB and CALL is that with CALL, the existence of Label is not checked until assembly time. Using CALL, a Label in an assembly language section can be accessed that is otherwise inaccessible to PicBasic.
    don't give up!
    Last edited by mister_e; - 10th September 2005 at 00:24.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    freelancebee's Avatar
    freelancebee Guest


    Did you find this post helpful? Yes | No

    Question Output PIC module

    Hi Steve:
    Thank you very much for your time.
    I got something new in learning PICs.
    Remark:
    Purpose for this program is to cteate rotating adjustable magnitic field that can be used to drive steppers or brushless motors and by cutting sequence
    of pulsout statements does not really impove program.
    Second reason is to create thermal control unit for 6 heaters of furnace
    that are located on 1-top,2-bottom,3-lift and 4-right side, 5-back wall and 6-sliding front door.Because of temperature difference inside chamber
    sequence of pulses will help to create equal isothermal properties iside of chamber.
    Or if you like model of autonomous flying or moving vehicle it is part of right remote control unit.
    As you see It looks like universal unit.
    If I correctly understand your remark:
    pbpin var byte
    pulsout pbpin, w9
    I am able to run PWM, Pulsout, Serout, Serin, Output .... statements
    on pre-programmed numbers of output pins in one shot?!
    Second question about "!!!! usually we use GOSUB with a RETURN.".
    I have 7 program each has two submodules rotate left/right and each has
    two procedure to calculate W9 value.
    In sum about 28 Gosub and 28 Return statements. My guess is here will be real problem with wrong switching.
    In Visual basic it is not problem,In C++ and Visual Basic It is simple to encapsulate problem in modules and classes.
    What can I do in PICBasic to avoid mistakes?
    Thank you for your help again.
    Best Regards,
    Leonid

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    If I correctly understand your remark:
    pbpin var byte
    pulsout pbpin, w9
    I am able to run PWM, Pulsout, Serout, Serin, Output .... statements
    on pre-programmed numbers of output pins in one shot?!
    Yes and no. You're able to send pulse on a single pre determined PIN for a w9 duration in one shot. Doing some Sub with common called statement or block of statement,l help to reduce the final code size.
    Second question about "!!!! usually we use GOSUB with a RETURN.".
    I have 7 program each has two submodules rotate left/right and each has
    two procedure to calculate W9 value.
    In sum about 28 Gosub and 28 Return statements. My guess is here will be real problem with wrong switching.
    If you don't have much than 4 nested gosubs, it shouldn't cause any problems. If you have more than 4 nested gosubs, your program may branch somewhere else than you expect.

    You can have as much as you want of nested GOTO. As it don't use the STACK... no harm at all.

    There's no trick, no magic formula to avoid problems. As in all programming language, you should avoid too many jump here, there elsewhere and have a good program structure. A flowChart could help to figure out the whole thing. Few gosub line placed one after the other is not a too much bad pratice.
    Code:
    Gosub DoThis
    Gosub DoThat
    Gosub DoSomethingElse
    But the use of nested gosub should be done with care.
    Last edited by mister_e; - 11th September 2005 at 01:05.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    freelancebee's Avatar
    freelancebee Guest


    Did you find this post helpful? Yes | No

    Smile Output PIC module

    Hi Steve:
    Thank you very much for showing the art of programming.
    With your help I was able to reduce number of compiled words
    from 995 to 412.
    Looks like I can write another program up to 500+ compiled
    words and to add to same PIC flash memory.
    I still have one question for you:
    PortB=b6 'pin0-pin7
    Pulsout 6, w9
    Does pins 0,1,2,3,4,5 and 7 not changing their values b6.0,b6.1,b6.2,
    b6.3,b6.4,b6.5, and b6.7 during pulsout on pin 6?
    Or need to use some kind of trick to hold their value during
    pulsout on pin 6.
    Below is corrected program.
    Best Regards,
    Leonid

    'PIC16F84A
    'This program only example that I will test
    'in Three PICs Architecture: Input PIC,Output PIC, and Control PIC.
    'This program serve output PIC.
    'Program used combination pins PortA.0 - PortA.4 for input
    'to change state of pins PortB.0 - PortB.5 and 6,7 for output
    'Input cominations come from second PIC
    'Order
    '1. PortA.4=0 Halt program
    '2. change code PortA.0-PortA.2 and result code from 1 to 7
    'will choose program from 1 to 7
    '3. PortA.3=0 set program
    '4. PortA.4=1 preset
    '5. PortA.3=1 run choosen program
    DEFINE OSC 20
    DEFINE _CP_OFF $3FFF
    DEFINE _PWRTE_OFF $3FFF
    DEFINE _WDT_OFF $3FFB
    'DEFINE _XT_OSC $3FFD
    DEFINE _HS_OSC $3FFE
    OPTION_REG = %10000000
    INTCON = %00000000

    b16 var BYTE
    w9 var WORD
    'set constant
    b0 con %00000000
    b1 con %00000001
    b2 con %00000010
    b3 con %00000100
    b4 con %00001000
    b5 con %00010000
    b6 con %00100000
    b7 con %00001001
    b8 con %00010010
    b9 con %00100100
    b10 con %00011011
    b11 con %00110110
    b12 con %00101101
    b13 con %00010010
    b14 con %00111111
    b15 con %00010101
    'Set Ports
    poke TRISA, 255 '31
    poke TRISB, b0
    'Initialize variable
    w9 = $2710
    Halt:
    PORTB=b0
    b16 = PORTA
    IF PortA.3 = 0 Then Switch
    IF PortA.4 = 0 Then Halt
    goto Halt

    Switch:
    PORTB=b0
    b16 = PORTA & 7
    branch b16,[switch,prog1,prog2,prog3,prog4,prog5,prog6,prog7]
    goto Switch

    PROG1: '------------Program #1
    b16 = PORTA
    IF PortA.4=0 Then Halt
    IF PortA.2=0 Then RL1
    gOTO ROR1
    Goto PROG1
    RL1: '------------Block Rotate Left 1
    PORTB=b6
    gosub SendPulses
    PORTB=b5
    gosub SendPulses
    PORTB=b4
    gosub SendPulses
    PORTB=b3
    gosub SendPulses
    PORTB=b2
    gosub SendPulses
    PORTB=b1
    gosub SendPulses
    gosub M1
    goto PROG1

    ROR1:
    PORTB=b1
    gosub SendPulses
    PORTB=b2
    gosub SendPulses
    PORTB=b3
    gosub SendPulses
    PORTB=b4
    gosub SendPulses
    PORTB=b5
    gosub SendPulses
    PORTB=b6
    gosub SendPulses
    gosub M1
    goto PROG1
    PROG2: '------------Program #2
    peek PORTA, b16
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL2
    IF PortA.2= 1 Then ROR2
    Goto PROG2
    RL2: '------------Block Rotate Left 2
    PORTB=b9
    gosub SendPulses
    PORTB=b8
    gosub SendPulses
    PORTB=b7
    gosub SendPulses
    gosub M1
    goto PROG2

    ROR2: '------------Block Rotate Right 2
    PORTB=b7
    gosub SendPulses
    PORTB=b8
    gosub SendPulses
    PORTB=b9
    gosub SendPulses
    gosub M1
    goto PROG2
    PROG3: '------------Program #3
    peek PORTA, b16
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL3
    IF PortA.2= 1 Then RR3
    Goto PROG3
    RL3: '------------Block Rotate Left 3
    PORTB=b12
    gosub SendPulses
    PORTB=b9
    gosub SendPulses
    PORTB=b11
    gosub SendPulses
    PORTB=b8
    gosub SendPulses
    PORTB=b10
    gosub SendPulses
    PORTB=b7
    gosub SendPulses
    gosub M1
    goto PROG3

    RR3: '------------Block Rotate Right 3
    PORTB=b7
    gosub SendPulses
    PORTB=b10
    gosub SendPulses
    PORTB=b8
    gosub SendPulses
    PORTB=b11
    gosub SendPulses
    PORTB=b9
    gosub SendPulses
    PORTB=b12
    gosub SendPulses
    gosub M1
    goto PROG3
    PROG4: '------------Program #4
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL4
    IF PortA.2= 1 Then RR4
    Goto PROG4
    RL4: '------------Block Rotate Left 4
    PORTB=b14
    gosub SendPulses
    PORTB=b15
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    PORTB=b13
    gosub SendPulses
    gosub M1
    goto PROG4

    RR4: '------------Block Rotate Right 4
    PORTB=b13
    gosub SendPulses
    PORTB=14
    gosub SendPulses
    PORTB=15
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    gosub M1
    goto PROG4
    PROG5: '------------Program #5
    IF PortA.4= 0 Then Halt
    GOTO RR5
    Goto PROG5

    RR5: '------------Block Rotate Right 5
    PORTB=b10
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    PORTB=b11
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    PORTB=b12
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    gosub M1
    goto PROG5
    PROG6: '------------Program #6
    IF PortA.4= 0 Then Halt
    IF PortA.2= 0 Then RL6
    IF PortA.2= 1 Then RR6
    Goto PROG6
    RL6: '------------Block Rotate Left 6
    PORTB=b13
    gosub SendPulses
    PORTB=b15
    gosub SendPulses
    gosub M1
    goto PROG6

    RR6: '------------Block Rotate Right 6
    PORTB=b15
    gosub SendPulses
    PORTB=b13
    gosub SendPulses
    gosub M1
    goto PROG6
    PROG7: '------------Program #7
    IF PortA.4= 0 Then Halt
    GOTO RL7
    Goto PROG7
    RL7: '------------Block nO RotateD 7
    PORTB=b0
    gosub SendPulses
    PORTB=b14
    gosub SendPulses
    gosub M1
    goto PROG7

    M1:
    If porta.0=0 then
    w9=w9-1
    endif
    if porta.1=0 then
    w9=w9+1
    EndIf
    select case w9
    case is >$5000
    w9=$5000
    case is <$100
    w9=$100
    end select
    Return

    SendPulses:
    pulsout 6,w9
    Return
    End
    Last edited by freelancebee; - 12th September 2005 at 20:56.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    still have one question for you:
    PortB=b6 'pin0-pin7
    Pulsout 6, w9
    Does pins 0,1,2,3,4,5 and 7 not changing their values b6.0,b6.1,b6.2,
    b6.3,b6.4,b6.5, and b6.7 during pulsout on pin 6?
    Or need to use some kind of trick to hold their value during
    pulsout on pin 6.
    No Pulsout should affect only one pin. The other, SHOULD stay as is.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. HSERIN & Interupts (aka controlling PIC programs from a remote PC)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th June 2009, 15:46
  2. Pic getting part power from Analog Port
    By ShaneMichael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 22nd April 2009, 11:34
  3. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 22:01
  4. Replies: 8
    Last Post: - 17th September 2006, 18:46
  5. Analog Output from a PIC
    By robertmark68 in forum General
    Replies: 4
    Last Post: - 2nd August 2006, 19:06

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