Indexing port pins for serout2/serin2


Closed Thread
Results 1 to 10 of 10
  1. #1

    Default Indexing port pins for serout2/serin2

    in reference to this thread:

    http://www.picbasic.co.uk/forum/showthread.php?t=13529

    This makes sense and works for turning pins on and off and works when I use it, but when I try to do something like:

    serout2 porta.0[25], 8276, ["hello"]

    25 should be portd.1 on pic16f877

    I get serial data out on port B2. It seems to always come out on B2 no matter what I stick in for x in porta.0[x]

    serout2 portd.1,8276,["hello"] works.

    I want to do serial in and out on around 16 pins but I want to use some code like this to save code space:

    Relay var Byte[4]
    x var byte
    i var byte

    Relay[1] = 14 'RB6
    Relay[2] = 20 'RC4
    Relay[3] = 26 'RD2
    main:
    ' cycle through relays
    for i = 1 to 3
    x = relay[i]
    serout2 porta.0[x], 8276, ["hello"]
    PAUSE 300
    next i



    Thanks!

  2. #2


    Did you find this post helpful? Yes | No

    Default

    I was hoping there was an easy answer to this and someone would post, "You silly you have to do it this way...." or "Yes that code should work by you forgot about some setting in the programer." Hrm, I guess I'll go back to hacking away at the keyboard on Monday.

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


    Did you find this post helpful? Yes | No

    Default

    You can use pin "Numbers".

    But the pins will need to be grouped within 2 full ports, instead of scattered like you've shown.
    Preferably PORTB and PORTC.
    0-7 is PORTB, 8-15 is PORTC, but they can be changed in the 16F877.bas file.

    Code:
    for i = 0 to 15
        serout2 i, 8276, ["hello, this is PIN# ",DEC i]
        PAUSE 300
    next i
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Using the pin "number" was the first thing I tried because I saw them do it in the PBP manual. But when I implemented it I couldn't make any sense of how the "numbers corresponded to the actual ABCDE Ports. And for some 2 different "numbers" I can't remember which ones, would correspond to data coming out on the same pin.

    Why is PORTB.O = "number" 0 ? Where is PORTA?


    This is what is in my .bas file:

    '************************************************* ***************
    '* 16F877.BAS *
    '* *
    '* By : Leonard Zerman, Jeff Schmoyer *
    '* Notice : Copyright (c) 1998 microEngineering Labs, Inc. *
    '* All Rights Reserved *
    '* Date : 10/23/98 *
    '* Version : 2.11 *
    '* Notes : *
    '************************************************* ***************

    BANK0 $0020, $007F
    BANK1 $00A0, $00EF
    BANK2 $0110, $016F
    BANK3 $0190, $01EF
    EEPROM $2100, $21FF
    LIBRARY "PBPPIC14"
    DEFINE CODE_SIZE 8

    include "PIC14EXT.BAS"

    PORTL VAR PORTB
    PORTH VAR PORTC
    TRISL VAR TRISB
    TRISH VAR TRISC

    include "PBPPIC14.RAM"

    '*-----------------------* EOF 16F877.BAS *---------------------*

    here is my .inc:

    ;************************************************* ***************
    ;* 16F877.INC *
    ;* *
    ;* By : Leonard Zerman, Jeff Schmoyer *
    ;* Notice : Copyright (c) 2003 microEngineering Labs, Inc. *
    ;* All Rights Reserved *
    ;* Date : 11/07/03 *
    ;* Version : 2.45 *
    ;* Notes : *
    ;************************************************* ***************
    NOLIST
    ifdef PM_USED
    LIST
    include 'M16F87x.INC' ; PM header
    device pic16F877, xt_osc, wdt_on, pwrt_on, lvp_off, protect_off
    XALL
    NOLIST
    else
    LIST
    LIST p = 16F877, r = dec, w = -302
    INCLUDE "P16F877.INC" ; MPASM Header
    __config _XT_OSC & _WDT_ON & _PWRTE_ON & _LVP_OFF & _CP_OFF
    NOLIST
    endif
    LIST

    Thanks for the reply!

  5. #5


    Did you find this post helpful? Yes | No

    Default

    You can use pin "Numbers".

    But the pins will need to be grouped within 2 full ports, instead of scattered like you've shown.
    Preferably PORTB and PORTC.
    0-7 is PORTB, 8-15 is PORTC, but they can be changed in the 16F877.bas file.

    Can I add ports A D and E somehow to the BAS file? I screwed aroud with trying to change the ports during runtime, but that doesn't seem to work.



    for i = 0 to 15
    serout2 i, 8276, ["hello, this is PIN# ",DEC i]
    PAUSE 300
    next i

    i = 0
    PORTL = A
    PORTH = D
    TRISL = A
    TRISH = D

    for i = 0 to 15
    serout2 i, 8276, ["hello, this is PIN# ",DEC i + 16]
    PAUSE 300
    next i

    next i

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flyingasics View Post

    Relay[1] = 14 'RB6
    Relay[2] = 20 'RC4
    Relay[3] = 26 'RD2
    main:
    ' cycle through relays
    for i = 1 to 3
    x = relay[i]
    serout2 porta.0[x], 8276, ["hello"]

    Thanks!
    Something I can't understand ...

    Porta.0[14,20 or 26] ???

    Also note relay[] can be a bit array...

    May be some " select case " will do a better job here ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Something I can't understand ...

    Porta.0[14,20 or 26] ???

    Also note relay[] can be a bit array...

    May be some " select case " will do a better job here ...

    Alain
    Porta.0[14,20 or 26] is supposed to be an undeclared array and since the PORTS are located sequentially in memory PicBasic Pro takes the value in brackets and treats it as an offset from porta.0 so

    porta.0[1] is porta.1
    porta.0[8] is portb.0

    The problem with using loops and select case is that my code will start to get really long and nasty looking.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Can someone explain why this will not work with the serout/serin functions? I had given up doing it this way and writing the program using cases for each pair of ports that I want to talk to, but it is looking like i am going to run out of flash memory on the PIC doing it this way.


    Relay var Byte[4]
    x var byte
    i var byte

    Relay[1] = 14 'RB6
    Relay[2] = 20 'RC4
    Relay[3] = 26 'RD2
    main:
    ' cycle through relays
    for i = 1 to 3
    x = relay[i]
    serout2 porta.0[x], 8276, ["hello"]
    PAUSE 300
    next i

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flyingasics View Post
    Can someone explain why this will not work with the serout/serin functions?
    serout2 porta.0[x], 8276, ["hello"]
    porta.0[x] is an Array function.
    When used with HIGH LOW SEROUT or any command other than a math formula, it reads the indicated array element (a pin) and returns the value to be used in the command.

    Since it's a BIT Array, it will only return a 0 or 1 depending on the state of the indexed PIN.
    Therefore, the serout2 command will only output on PORTB.0 or PORTB.1

    To reiterate my previous recommendation ...
    If you moved your serial outputs so they are all on two PORTs, instead of scattered across 3 or four ports ... you can do exactly what you want by using pin "Numbers", and a very small bit of code.
    DT

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Ok, so am I just out of luck using some kind of loop to reduce code and make it easier to modify the in the future if I want to use ports ABCDE? Not because I have them spread out but I just have that many serial devices I want to talk to. What is the reason why there are only 2 ports at a time that you can reference with a pin "number".

    Can I change PORTL and PORTH during runtime to other ports?

    Thanks.

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