Shuffling variables


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    It is possible to use variables to name ports then change variable values to change ports. copied from my program --

    http://www.harbornet.com/sunflower/PCB.pbp


    ' The PORTL and PORTH aliases are used for the PIN numbers (0-15)

    ' PORTL is 0-7 and PORTH is 8-15.

    ' Look in the .bas and .bal files for the chip you are using.

    ' If it's a 18F4620 then open c:\pbp\18F4620.bas


    ' PORTL VAR PORTB

    ' PORTH VAR PORTD

    ' TRISL VAR TRISB

    ' TRISH VAR TRISD

    '
    ' PORTB AND PORTD can now be referenced by variables.

    ' http://www.picbasic.co.uk/forum/showthread.php?t=14493&

    '

    The default, I believe, is PORTB AND PORTC. The above changes in .bas and .bal changed that to ports B and D. The link explains in more detail

  2. #2
    Join Date
    Feb 2011
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    if azMotor then

    azMotor_A = m1_ina '14 PORTD.6 ' Digital I/O PORT D.0 - D.7 NUMBERED 8 - 15

    azMotor_B = m1_inb '13 PORTD.5

    else

    azMotor_A = m1_inb ' Software will reverse wires if motor or sensor installed backwards. 0 to 7 = PORTB.0 to PORTB.7

    azMotor_B = m1_ina ' Configured in Install subroutine. Used in Set_Motors HBridge subroutine. 8 to 15 = PORTD.0 to PORTD.7

    endif

    low azmotor_a ' Brake azimuth motor

    low azmotor_b

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    sunflower that's very interesting , but I suspect it may fail if attempted on latx registers which can still leave you exposed to rmw issues . has anybody tried it on latx ?
    I could be wrong but most pbp "functions" find the tris reg by using a fixed offset from the port reg

  4. #4
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    Quote Originally Posted by rsocor01 View Post
    Well, you are having great difficulty because you cannot reassign ports like that in the program. Ports are only assigned to variables at the beginning of the program.
    Yes I know this. I was having difficulty because I was trying to think of a way to do it in a manner that the pic would understand. Our brains can easily process this and substitute that values, but with the pic, it's not that easy.

    Quote Originally Posted by richard View Post
    looks like a dead end to me . try this way
    code is for a 12f1822 so uses porta and porta.3 is input only . but you get the idea

    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 29/11/2014                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :   pic12f1822                                                *
    '****************************************************************
    #CONFIG
    cfg1 = _FOSC_INTOSC
    cfg1&= _WDTE_ON
    cfg1&= _PWRTE_OFF
    cfg1&= _MCLRE_ON
    cfg1&= _CP_OFF
    cfg1&= _CPD_OFF
    cfg1&= _BOREN_ON
    cfg1&= _CLKOUTEN_OFF
    cfg1&= _IESO_ON
    cfg1&= _FCMEN_ON
      __CONFIG _CONFIG1, cfg1
    cfg2 = _WRT_OFF
    cfg2&= _PLLEN_OFF
    cfg2&= _STVREN_ON
    cfg2&= _BORV_19
    cfg2&= _LVP_OFF
      __CONFIG _CONFIG2, cfg2
    #ENDCONFIG
    
       osccon=$6a    '4 mhz
       anselA=0  
       trisa=000
       
      a  var  byte
      b  var  byte
      c  var  byte
      d  var  byte
      pin_mode var byte
      x var byte
      
      a=0
      b=1
      c=2
      d=4
      pin_mode=0
     
     action:
      for x = 1 to 5
      lata.0[a] = 1
      lata.0[b] = 1
      lata.0[c]= 0
      lata.0[d]= 0
      pause 200
      lata.0[a] = 0
      lata.0[b] = 0
      lata.0[c]= 0
      lata.0[d]= 0
      pause 200
      next x
      for x = 1 to 5
      lata.0[a] = 0
      lata.0[b] = 0
      lata.0[c]= 1
      lata.0[d]= 1
      pause 200
      lata.0[a] = 0
      lata.0[b] = 0
      lata.0[c]= 0
      lata.0[d]= 0
      pause 200
      next x
      gosub shuffle
      goto action
      
       shuffle :
       pin_mode=pin_mode+1
      select case pin_mode
      case 1
       a=4  ;a  var  portb.3
       b=2  ;b  var  portb.2
       c=1   ;c  var  portb.1
       d=0    ;d  var  portb.0
      case 2
       a=0  ;a  var  portb.0
       b=4  ;b  var  portb.3
       c=2   ;c  var  portb.2
       d=1    ;d  var  portb.1
      case 3
       a=0  ;a  var  portb.0
       b=2  ;b  var  portb.2
       c=1   ;c  var  portb.1
       d=4    ;d  var  portb.3 
      case 4
       a=0  ;a  var  portb.0
       b=1  ;b  var  portb.1
       c=2   ;c  var  portb.2
       d=4    ;d  var  portb.3 
       pin_mode=0
      end select
      return
    That's exactly what I was envisioning in my head, but I couldn't get it out. Thank you Richard. I'll try to plug this into my code and see how it works.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    up until now I never found a use for "swap" this makes even smaller and more elegant code

    Code:
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 29/11/2014                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :   pic12f1822                                                *
    '****************************************************************
    #CONFIG
    cfg1 = _FOSC_INTOSC
    cfg1&= _WDTE_ON
    cfg1&= _PWRTE_OFF
    cfg1&= _MCLRE_ON
    cfg1&= _CP_OFF
    cfg1&= _CPD_OFF
    cfg1&= _BOREN_ON
    cfg1&= _CLKOUTEN_OFF
    cfg1&= _IESO_ON
    cfg1&= _FCMEN_ON
      __CONFIG _CONFIG1, cfg1
    cfg2 = _WRT_OFF
    cfg2&= _PLLEN_OFF
    cfg2&= _STVREN_ON
    cfg2&= _BORV_19
    cfg2&= _LVP_OFF
      __CONFIG _CONFIG2, cfg2
    #ENDCONFIG
    
       osccon=$6a    '4 mhz
       anselA=0  
       trisa=000
       
      a  var  byte
      b  var  byte
      c  var  byte
      d  var  byte
      pin_mode var bit
      x var byte
      all_off var byte
      
      pins_on var byte
      
     
      
      
      
      a=1        ;portb.0
      b=2         ;portb.1
      c=4         ;portb.2
      d=16        ;portb.4
      pin_mode=0
      
      all_off=!(a+b+c+d)
     
      lata =   lata & all_off
     action:
      pins_on=a+b
      for x = 1 to 5
      lata =   lata | pins_on
      
      
      pause 200
      lata =   lata & all_off
      
      pause 200
      next x
      pins_on=c+d
      for x = 1 to 5
       lata =   lata | pins_on
      pause 200
      lata =   lata & all_off
      pause 200
      next x
      gosub shuffle
      goto action
      
       shuffle :
       pin_mode =!pin_mode
      if pin_mode then
      swap a,d
      swap b,c
      else
      swap a ,c
      swap b ,d
      endif
      
      
      
      
      
      return

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    shuffle wrong this is better
    Code:
       shuffle :
       pin_mode =!pin_mode
      if pin_mode then
      swap a,d
      swap b,c
      else
      swap b ,d
      swap c ,d
      endif
    Last edited by richard; - 30th November 2014 at 11:26. Reason: pressed wrong button

  7. #7
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Shuffling variables

    Quote Originally Posted by richard View Post
    ...this is better...
    Better? I'd say it's AWESOME! I had thought to suggest (as your original answer) using array values or various mask patterns perhaps, but - to my humble skills - there is simply no improvement possible to "swap". Somewhere, in the back of my mind, I remember seeing it, but had forgotten... I'll remember now!

Similar Threads

  1. How about String Variables?
    By mytekcontrols in forum PBP Wish List
    Replies: 40
    Last Post: - 20th January 2015, 12:53
  2. Replies: 2
    Last Post: - 12th November 2014, 07:57
  3. IF - WHILE and variables
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th April 2009, 13:01
  4. Using Variables with Debug
    By krohtech in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th December 2007, 02:47
  5. Help with using variables
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th June 2005, 16:03

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