New At PIC's, need some help with code


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    SOHCKing03's Avatar
    SOHCKing03 Guest


    Did you find this post helpful? Yes | No

    Default

    Okay so then here is an updated code:

    '----------Code for the Front Left, Front Right, Back Left, and Back Right

    SYMBOL PORT_PIN = PORTB
    X VAR BYTE ' For loop & bit index pointer


    ADCON1 = 7 ' All digital

    PORTB = 0 ' Clear port pins

    TRISB = 0 ' Make them all outputs

    Main:

    FOR X = 0 TO 7
    PORT_PIN.0[X] = 1 ' Set all pins high
    PAUSE 200
    NEXT X

    GOTO Off

    Off:

    ADCON1 = 7 ' All digital

    PORTB = 0 ' Clear port pins
    PORTC = 0

    TRISB = 0 ' Make them all outputs
    TRISC = 0

    FOR X = 0 TO 7
    PORT_PIN.0[X] = 0 ' Set all pins low
    PAUSE 200
    NEXT X

    GOTO Main

    '----------Code for Both Sides

    SYMBOL PORT_PIN = PORTB
    X VAR BYTE ' For loop & bit index pointer


    ADCON1 = 7 ' All digital
    PORTA = 0
    PORTB = 0 ' Clear port pins
    PORTC = 0

    TRISB = 0 ' Make them all outputs
    TRISC = 0

    Wait:

    INPUT PORTA.0
    IF PORTA.0 = GOTO Main

    GOTO Wait

    Main:

    FOR X = 0 TO 15
    PORT_PIN.0[X] = 1 ' Set all pins high
    PAUSE 200
    NEXT X

    GOTO Off

    Off:

    ADCON1 = 7 ' All digital

    PORTB = 0 ' Clear port pins
    PORTC = 0

    TRISB = 0 ' Make them all outputs
    TRISC = 0

    FOR X = 0 TO 15
    PORT_PIN.0[X] = 0 ' Set all pins low
    PAUSE 200
    NEXT X

    GOTO Main

    -------------------------------------------------

    How does that look? For the front and back ones I only need 6 outputs but I need 16 for the sides. I hope I did that INPUT thing right. I assumed that PORT_PIN would still equal PORTB since I'm only using PORTA for the INPUT. Did I do everything correct? Also, I am using a 16F876 so all PORTB and PORTC pins can be outputs, however PORTA.6 and PORTA.7 are unimplemented so that is why I am starting with PORTB.

    Thanks again.

    -Brad

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


    Did you find this post helpful? Yes | No

    Default

    IF PORTA.0 = GOTO Main
    there's a missing value in that one, 0 or 1

    Also, as you're using PORTB and PORTC, there's no special need to use the array method. Just use
    HIGH x (from 0 to 15)=> refer to section 4.11 of your PBP manual

    something like
    Code:
    X VAR BYTE ' For loop & bit index pointer
    ADCON1 = 7 ' All digital
        '   
        ' Clear portB&C pins
    PORTB = 0 
    PORTC = 0 
        '
        ' Make them all outputs
    TRISB = 0 
    TRISC = 0
        '
        ' Set PORTA.1 as input
    TRISA.1=1
        
    Main:
         FOR X = 0 TO 7
             HIGH X ' Set all pins high
             PAUSE 200
             NEXT X
    Off:
        FOR X = 0 TO 7
            LOW X ' Set all pins low
            PAUSE 200
            NEXT X
    
        GOTO Main
    Steve

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

  3. #3
    SOHCKing03's Avatar
    SOHCKing03 Guest


    Did you find this post helpful? Yes | No

    Default

    Alright here is an updated code as of 10:12 last night

    Code:
    '---------- FRONT TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears port pins
    TRISB = 0  'sets as outputs
    
    Main:                              'turns each LED on
         FOR X = 0 TO 7
              HIGH X
              PAUSE 200
         NEXT X
    
    PAUSE 7000                     'waits for sequence to finish
    
    GOTO Off
    
    Off:                                'turns each LED off
         FOR X = 0 TO 7
              LOW X
              PAUSE 200
         NEXT X
    
    PAUSE 6000
    
    GOTO Main
    Code:
    '---------- SIDE TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears all port pins
    PORTC = 0
    TRISB = 0  'sets all as outputs
    TRISC = 0
    TRISA.0 = 1  'set as input
    
    Wait:                              'wait for high signal
         INPUT PORTA.0
         IF PORTA.0 = 1 THEN GOTO Main
    GOTO Wait
    
    Main:                               'turns each LED on
         FOR X = 0 TO 15
              HIGH X
              PAUSE 200
         NEXT X
    
    GOTO Wait_Again
    
    Wait_Again:                   'waits for low signal
         INPUT PORTA.0
         IF PORTA.0 = 0 THEN GOTO Off
    
    GOTO Wait_Again
    
    Off:                              'turns each LED off
         FOR X = 0 to 15
              LOW X
              PAUSE 200
         NEXT X
    
    GOTO Wait                     'restarts
    Code:
    '---------- BACK TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears all port pins
    TRISB = 0  'sets all as outputs
    TRISA.0 = 1  'set as input
    
    Wait:                             'wait for high signal
         INPUT PORTA.0
         IF PORTA.0 = 1 THEN GOTO Main
    GOTO Wait
    
    Main:                             'turns each LED on
         FOR X = 0 TO 7
              HIGH X
              PAUSE 200
         NEXT X
    
    GOTO Wait_Again
    
    Wait_Again:                   'waits for low signal
         INPUT PORTA.0
         IF PORTA.0 = 0 THEN GOTO Off
    
    GOTO Wait_Again
    
    Off:                              'turns each LED off
         FOR X = 0 to 7
              LOW X
              PAUSE 200
         NEXT X
    
    GOTO Wait                     'restarts

    How does that look? I went and double checked everything so I think it is near completion. Are there any mistakes or better ways to code it?

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


    Did you find this post helpful? Yes | No

    Default

    First code:
    You don't need the GOTO Off... it will jump there anyway

    Second code:
    Wait and Off are reserved word, so you need to change them
    Code:
    GOTO Wait_Again    '******* not needed
    
    Wait_Again:                   'waits for low signal
    Code:
    Wait:                              'wait for high signal
         INPUT PORTA.0 '***** this one you don't need it as it's 
                       ' already defined above
    Code:
    Wait_Again:                   'waits for low signal
         INPUT PORTA.0 '******* same thing here
    another method
    Code:
    '---------- SIDE TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears all port pins
    PORTC = 0
    TRISB = 0  'sets all as outputs
    TRISC = 0
    TRISA.0 = 1  'set as input
    
    Start:
         While PORTA.0=0 : Wend ' wait 'till PORTA.0=1
    
         FOR X = 0 TO 15
              HIGH X
              PAUSE 200
              NEXT X
    
         While PORTA.0=1 : Wend 'waits for low signal
    
         FOR X = 0 to 15
             LOW X
             PAUSE 200
             NEXT X
    
        goto start
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    ....
    Wait and Off are reserved word, so you need to change them

    Who/what reserves the "wait" Steve?

    I did not know that!
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    SOHCKing03's Avatar
    SOHCKing03 Guest


    Did you find this post helpful? Yes | No

    Default

    Okay I updated it again...

    Code:
    '---------- FRONT TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears port pins
    TRISB = 0  'sets as outputs
    
    Turn_On:                        'turns each LED on
         FOR X = 0 TO 7
              HIGH X
              PAUSE 200
         NEXT X
    
    PAUSE 7000                     'waits for sequence to finish
    
    
    Turn_Off:                       'turns each LED off
         FOR X = 0 TO 7
              LOW X
              PAUSE 200
         NEXT X
    
    PAUSE 6000
    
    GOTO Turn_On
    Code:
    '---------- SIDE TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears all port pins
    PORTC = 0
    TRISB = 0  'sets all as outputs
    TRISC = 0
    TRISA.0 = 1  'set as input
    
    Waiting:                              'wait for high signal
    
         IF PORTA.0 = 1 THEN GOTO Turn_On
    
    GOTO Waiting
    
    Turn_On:                               'turns each LED on
         FOR X = 0 TO 15
              HIGH X
              PAUSE 200
         NEXT X
    
    
    Wait_Again:                       'waits for low signal
    
         IF PORTA.0 = 0 THEN GOTO Turn_Off
    
    GOTO Wait_Again
    
    Turn_Off:                              'turns each LED off
         FOR X = 0 to 15
              LOW X
              PAUSE 200
         NEXT X
    
    GOTO Waiting                     'restarts
    Code:
    '---------- BACK TWO PICS' CODE
    X VAR BYTE
    ADCON1 = 7  'sets all as digital
    PORTB = 0  'clears all port pins
    TRISB = 0  'sets all as outputs
    TRISA.0 = 1  'set as input
    
    Waiting:                             'wait for high signal
    
         IF PORTA.0 = 1 THEN GOTO Turn_On
    
    GOTO Waiting
    
    Turn_On:                             'turns each LED on
         FOR X = 0 TO 7
              HIGH X
              PAUSE 200
         NEXT X
    
    
    Wait_Again:                   'waits for low signal
    
         IF PORTA.0 = 0 THEN GOTO Turn_Off
    
    GOTO Wait_Again
    
    TURN_Off:                              'turns each LED off
         FOR X = 0 to 7
              LOW X
              PAUSE 200
         NEXT X
    
    GOTO Waiting                    'restarts
    I changed the names to unreserved names and I took out my INPUT statements.

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


    Did you find this post helpful? Yes | No

    Default

    PAUSE 7000 'waits for sequence to finish

    This line will execute AFTER the sequence is finished.

    So you wait 7 seconds additionaly; and while waiting nothing will take place.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    Who/what reserves the "wait" Steve?

    I did not know that!
    wait is also a modifier for Hserin, Serin2, etc etc.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    "There's no problem. Only learning opportunities."


    --------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. decoding quadrature encoders
    By ice in forum mel PIC BASIC Pro
    Replies: 93
    Last Post: - 28th February 2017, 09:02
  2. Nokia COLOR LCD PicBasicPro 2.50a example code
    By skimask in forum Code Examples
    Replies: 49
    Last Post: - 28th September 2011, 01:43
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  4. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 19:51
  5. Writing code for battery operated projects
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th June 2005, 03:39

Members who have read this thread : 0

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