Addressing multiple PORTs using one Variable


+ Reply to Thread
Results 1 to 32 of 32

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923

    Default Addressing multiple PORTs using one Variable

    Hi All,

    I need to address five ports conditionally according to the value of one variable.

    I found some posts about this subject and hoped this thread would help, but unfortunately, it doesn't.

    This a code extract of my current project where timing is crucial and I have to write down a lot of repeating lines of code since FOR/NEXT and GOTOs and GOSUBs take too much time (I'm driving five NeoPixel SW2812B strings with each 28 LEDs/Pixels).

    Code:
    ' PIC 16F690 @20MHz
    #CONFIG
        __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    #ENDCONFIG
    
    TRISC = %00000000
    
    DEFINE OSC 20
    
    PortPin VAR PORTA.0
    X       VAR BYTE
    X       = 23            ' PORTC.7 - "PORTA.0[23]"
    
    MAIN:
        PORTC.7      = 1    ' OK, obviously will work but useless - just make shure the scope works too
        'PORTA.0[23]  = 1   ' OK but useless
        'PORTA.0[X]   = 1   ' No! Makes a blink in the scope and then stays LOW
        'PORTA[X]     = 1   ' No!, nothing happens, pin stays LOW
        'PortPin[23]  = 1   ' OK but useless   
        'PortPin[X]   = 1   ' No!, nothing happens, pin stays LOW
        'PortPin.0[X] = 1   ' No!, error message "Bad variable size modifier: .0"
        '...
            ASM
                nop
                nop
                nop
            ENDASM
        PORTC.7 = 0         ' have this syntax for tests only here
            ASM
                nop
            ENDASM
            
        GOTO MAIN
    END

    How do I solve this please?
    Last edited by flotulopex; - 23rd July 2024 at 18:13.
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Code:
        PORTA.0[X]   = 1   ' No! Makes a blink in the scope and then stays LOW
    works ok in your loop for me, although it is much slower , period goes from 3uS to 13uS
    Warning I'm not a teacher

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,038


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    "Makes a blink in the scope and then stays LOW"

    It will return to low if:

    1. you force it somewhere in the program to return to low

    2. you PIC is reset for some reason (Watchdog maybe, decoupling cap missing, etc).

    Other than these, I see no way to behave like this.

    Ioannis

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    1. you force it somewhere in the program to return to low

    2. you PIC is reset for some reason (Watchdog maybe, decoupling cap missing, etc).
    All the code and fuses settings are here.

    I soldered the crystal directly on the µC's pins, a 100nF cap is there...

    although it is much slower , period goes from 3uS to 13uS
    Strange. The pulse should be 800ns (nano seconds).


    Okay, I'll give it a try with another 16F690...
    Roger

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,580


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Not sure I understand....
    Are you saying that this code works:
    Code:
    MAIN:
        PORTA.0[23]  = 1   ' OK but useless
        ASM
          nop
          nop
          nop
        ENDASM
        
        PORTC.7 = 0         ' have this syntax for tests only here
            ASM
                nop
            ENDASM
            
        GOTO MAIN
    END
    And this isn't:
    Code:
    X VAR BYTE
    X = 23
    MAIN:
        PORTA.0[X]   = 1   ' No! Makes a blink in the scope and then stays LOW
        ASM
          nop
          nop
          nop
        ENDASM
        
        PORTC.7 = 0         ' have this syntax for tests only here
        ASM
          nop
        ENDASM
            
        GOTO MAIN
    END
    The 16F690 has an ADC and comparators.
    Many of the pins are analog. I don't see you setting them to digital mode. PortC.7 is, however, not one of the analog pins but since you're trying to "index" pins with a variable I suspect PortC.7 isn't the only pin you want to use.

    /Henrik.

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Addressing multiple PORTs using one Variable

    Yes Henrik, you might be right.

    Keen to keep the code snippet very (maybe too) short here, I removed all others register settings I have set in the original code.

    I'll give it also a try...when I'm back from office.
    Roger

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    not really the results i expected, more doubt on proteus ?
    i only expected the period to change


    Name:  f1.jpg
Views: 29871
Size:  314.4 KB

    Name:  f2.jpg
Views: 29958
Size:  311.0 KB
    Warning I'm not a teacher

  8. #8
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Addressing multiple PORTs using one Variable

    So Richard,

    What is the idea now, then?

    Is there any "clean" -meaning software- workaround?

    If not, I'll have to make five small independant circuits to each drive his own NeoPixel string, five circuits controlled by a sixth one


    BTW, here is how my circuit works with 1 single NeoPixel string.
    https://youtube.com/shorts/4fZk9KT4V1M

    ...still in office now.
    Last edited by flotulopex; - 24th July 2024 at 14:35.
    Roger

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,580


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Are you displaying the same "information" on all 5 strings or calculating the "content" for each string on the fly as you go about updating them?
    Reason I ask is that 5 strings times 28 LEDs times 3 bytes per LED equals 420 bytes and the 16F690 only has 256 bytes of RAM.

  10. #10
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Addressing multiple PORTs using one Variable

    It is (going to be) a four digits 7 "segments" (each segment is made up of four NeoPixels >> 4x7= 28 NeoPixels per string) wall clock.

    The fifth segment is made of 2 or 4 four (not decided yet) NeoPixels and will serve as seconds separator/indicator.

    Yes, each string will be uptated specifically, if I can say so.

    By now, it all fits in my 16F690 nicely because I address the strings only once at the time or 1x28x3 bytes in total.
    Last edited by flotulopex; - 24th July 2024 at 14:48.
    Roger

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Can all the outputs be on the same port, preferably sequential?
    Warning I'm not a teacher

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    try this 16 led strip on RC7


    Code:
    ' PIC 16F690 @20MHz
    #CONFIG
        __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_HS_OSC &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    #ENDCONFIG
    CLEAR
    TRISC = %00000000
    
    
    DEFINE OSC 20
    pinsel  var byte 
    RED  var byte 
    GREEN  var byte
    BLUE var byte
    px      var byte 
    bt      var byte 
    pc      var byte 
    pinsel   = 128
    
    
    
    
    
    
    
    
    MAIN:
    GREEN=128
    
    
    asm
            MOVLW 16
            MOVWF _pc
    _nxRby        
            MOVF  _GREEN,W
            movwf _px
            MOVLW 8
            movwf _bt
            RLF _px,F
    _nxRbt  movf _pinsel,W
            IORWF PORTC ,F
            BTFSS STATUS,C
            GOTO XXR
            NOP
            NOP
            NOP
            
    XXR     CLRF PORTC  
            NOP
            RLF _px,F
            DECFSZ _bt,F
            GOTO _nxRbt
            MOVF  _RED,W
            movwf _px
            MOVLW 8
            movwf _bt
            RLF _px,F
    _nxGbt  movf _pinsel,W
            IORWF PORTC,F
            BTFSS STATUS,C
            GOTO XXG
            NOP
            NOP
            NOP
            
    XXG     CLRF PORTC  
            NOP
            RLF _px,F
            DECFSZ _bt,F
            GOTO _nxGbt
            MOVF  _BLUE,W
            movwf _px
            MOVLW 8
            movwf _bt
            RLF _px,F
    _nxBbt  movf _pinsel,W
            IORWF PORTC  ,F
            BTFSS STATUS,C
            GOTO XXB
            NOP
            NOP
            NOP
            
    XXB     CLRF PORTC  
            NOP
            RLF _px,F
            DECFSZ _bt,F
            GOTO _nxBbt
            DECFSZ _pc,F
            GOTO _nxRby
            
       
    endasm  
     PAUSE 100
    
    
    
    
    GOTO MAIN
    Warning I'm not a teacher

  13. #13
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Addressing multiple PORTs using one Variable

    Here's the link for the video again.



    Sorry, I'm not the YouTube expert so I hope it works now
    Roger

  14. #14
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    923


    Did you find this post helpful? Yes | No

    Default Addressing multiple PORTs using one Variable

    Quote Originally Posted by richard View Post
    try this 16 led strip on RC7
    Thank a lot Richard. I'll give it a try
    Roger

  15. #15
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    I take it that's a fail. I'm not surprised, the simulator indicated that @20mHz a pic16f690 cannot to a conditional branch quickly enough for a "LOW" pulse for a ws2812
    Warning I'm not a teacher

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,038


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Now, that is a surprise to me. This is a simple task for a simulator to calculate. It knows clock rate, instruction cycle, why does it fail to make simple calculations?

    Ioannis

  17. #17
    Join Date
    May 2013
    Location
    australia
    Posts
    2,546


    Did you find this post helpful? Yes | No

    Default Re: Addressing multiple PORTs using one Variable

    Now, that is a surprise to me. This is a simple task for a simulator to calculate. It knows clock rate, instruction cycle, why does it fail to make simple calculations?
    the simulator correctly indicated that the code will not work right from the start, the TOH is just out of spec

    the code provided was to hopefully prove the outcome one way or the other in real life.
    i assume it failed since roger never replied and then went on to ask pic18 questions
    Warning I'm not a teacher

Similar Threads

  1. Addressing ports via offset using lookup command
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 1st November 2013, 04:50
  2. How to drive 7 segment using multiple ports?
    By guess79 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 20:43
  3. multiple ports ===> one symbol?
    By PICMAN in forum General
    Replies: 4
    Last Post: - 8th March 2005, 23:24
  4. Problem reading multiple ADC ports
    By jswayze in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 4th November 2004, 17:46
  5. Reading multiple ports
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th May 2004, 18:22

Members who have read this thread : 17

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