Menu

can I identify e.g. PORTB.0 with a variable?

Help
2007-08-05
2013-05-30
  • Hank Fletcher

    Hank Fletcher - 2007-08-05

    I was wondering if I can identify any given port using a variable like this:

    for MyPort = 0 to 7
    SET PORTB.MyPort ON
    next MyPort

     
    • Hugh Considine

      Hugh Considine - 2007-08-05

      No, that will not work. This is due to the internal design of the PIC.

      If you're using a fairly new version of GCBASIC (any version since May, I think), you can use these macros:

      macro SetBit(inport, inbit)
      bittemp = inbit + 1

      decfsz bittemp,F
      bsf inport,0
      decfsz bittemp,F
      bsf inport,1
      decfsz bittemp,F
      bsf inport,2
      decfsz bittemp,F
      bsf inport,3
      decfsz bittemp,F
      bsf inport,4
      decfsz bittemp,F
      bsf inport,5
      decfsz bittemp,F
      bsf inport,6
      decfsz bittemp,F
      bsf inport,7
      End macro

      macro ClearBit(inport, inbit)
      bittemp = inbit + 1

      decfsz bittemp,F
      bcf inport,0
      decfsz bittemp,F
      bcf inport,1
      decfsz bittemp,F
      bcf inport,2
      decfsz bittemp,F
      bcf inport,3
      decfsz bittemp,F
      bcf inport,4
      decfsz bittemp,F
      bcf inport,5
      decfsz bittemp,F
      bcf inport,6
      decfsz bittemp,F
      bcf inport,7
      End macro

      Then you can use the code like this:
      for MyPort = 0 to 7
      SetBit (PORTB, MyPort)
      next

      I've not tried either of those macros, but they should work.

      Unfortunately the program generated will be quite inefficient when downloaded and run on the PIC, both in terms of clock cycles and program words. There isn't any way to avoid this with the PIC (or AVR) architecture.

      There may be a more efficient way to work around this - is there any particular algorithm that you're trying to program?

       
    • Nobody/Anonymous

      Thanks for the help! I'm having a try at building a robot that can play a recorder (the musical instrument, like a flute but simpler).  As you can see in the code below, I've used the FingerPort array to identify the various port pins.  If it was possible and program space wasn't an issue, I would have used FOR...NEXT as I suggested in my original post.  I'm trying to be mindful of efficiency, though, since program space is an issue, especially if I'm planning on using it to store the array values also described below.  Please let me know if you have any ideas on how I might make this most efficient, while still keeping it relatively easy to re-program with different tunes!

      #chip 16F88, 8
      #config OSC = INTRC_IO, MCLR_ON

      dim FingerPort(8)  'sets how many fingers to operate
                         'and might end up being more

      #define FingerPort(1) PORTB.1  
      #define FingerPort(2) PORTB.2
      #define FingerPort(3) PORTB.3
      #define FingerPort(4) PORTB.4
      #define FingerPort(5) PORTB.5
      #define FingerPort(6) PORTB.6
      #define FingerPort(7) PORTB.7
      #define FingerPort(8) PORTA.0

      DIR PORTA OUT    
      DIR PORTB OUT    

      DIM Fingering(80)
      DIM Wind(80
      DIM Duration(80)

      'not shown here are the values for each element of the Fingering, Wind, and Duration arrays, but eventually it will look like:
      'first note
      Fingering(1) = 125
      Wind(1) = 27
      Duration(1) = 127

      'second note
      Fingering(2) = 14
      Wind(2) = 27
      Duration(2) = 63
      'etc, for as many notes as there are in the music, up to 80

      FOR Note = 1 to 80

      for FingeringBit = 1 to 8     
      ROTATE Fingering(Note) RIGHT SIMPLE
      IF Fingering(Note) < 128 THEN SET FingerPort(FingeringBit) OFF
      IF Fingering(Note) > 127 THEN SET FingerPort(FingeringBit) ON
      next FingeringBIt

      HPWM 1, 16, Wind(Note)  'controls airflow through recorder

      WAIT Duration ms    'controls how long each note is played
                              'although more code will be added here in
                              'order to make the most of a 0-255 range

      NEXT Note

       
    • Hank Fletcher

      Hank Fletcher - 2007-08-05

      Thanks for the help! I'm having a try at building a robot that can play a recorder (the musical instrument, like a flute but simpler).  As you can see in the code below, I've used the FingerPort array to identify the various port pins.  If it was possible and program space wasn't an issue, I would have used FOR...NEXT as I suggested in my original post.  I'm trying to be mindful of efficiency, though, since program space is an issue, especially if I'm planning on using it to store the array values also described below.  Please let me know if you have any ideas on how I might make this most efficient, while still keeping it relatively easy to re-program with different tunes!

      #chip 16F88, 8
      #config OSC = INTRC_IO, MCLR_ON

      dim FingerPort(8)  'sets how many fingers to operate
                         'and might end up being more

      #define FingerPort(1) PORTB.1  
      #define FingerPort(2) PORTB.2
      #define FingerPort(3) PORTB.3
      #define FingerPort(4) PORTB.4
      #define FingerPort(5) PORTB.5
      #define FingerPort(6) PORTB.6
      #define FingerPort(7) PORTB.7
      #define FingerPort(8) PORTA.0

      DIR PORTA OUT    
      DIR PORTB OUT    

      DIM Fingering(80)
      DIM Wind(80
      DIM Duration(80)

      'not shown here are the values for each element of the Fingering, Wind, and Duration arrays, but eventually it will look like:
      'first note
      Fingering(1) = 125
      Wind(1) = 27
      Duration(1) = 127

      'second note
      Fingering(2) = 14
      Wind(2) = 27
      Duration(2) = 63
      'etc, for as many notes as there are in the music, up to 80

      FOR Note = 1 to 80

      for FingeringBit = 1 to 8     
      ROTATE Fingering(Note) RIGHT SIMPLE
      IF Fingering(Note) < 128 THEN SET FingerPort(FingeringBit) OFF
      IF Fingering(Note) > 127 THEN SET FingerPort(FingeringBit) ON
      next FingeringBIt

      HPWM 1, 16, Wind(Note)  'controls airflow through recorder

      WAIT Duration ms    'controls how long each note is played
                              'although more code will be added here in
                              'order to make the most of a 0-255 range

      NEXT Note

       
    • Hank Fletcher

      Hank Fletcher - 2007-08-05

      Sorry, I hit "post comment" too quickly.  That last post describing the recorder robot program was by me also.  Thanks,
      Hank Fletcher.

       
    • kent_twt4

      kent_twt4 - 2007-08-05

      A good way to look at Port bits is to view them on a 8 LED display.  This might be especially useful for your project.  This could be an over-simplification but try just equating your FingerPort to your Fingering() number.  Hopefully you could swap back PortB.O into the fold to accomplish this.

      An IN and Out Burger flash sequence:

      Dir PortD out

      Main:

      PortD = 0
      Portbit = 1

      For Bit = 1 to 8
      PortD = PortD + PortBit
      wait 25 10ms
      Portbit = PortBit*2
      Next

      goto Main

       
    • kent_twt4

      kent_twt4 - 2007-08-05

      Upon further inspection if all the arrays can be defined as constants, you could gain back a bunch program memory. Approimately 400 words per array variable(80).  For instance:

      #define FingeringA = 125 ;These are nonsensical random numbers
      #define FingeringB = 14
      #define FingeringC = 225
      #define FingeringD = 84
      #define FingeringE = 7
      #define FingeringF = 99
      #define FingeringG = 175
      #define FingeringAf = 48
      .....
      .....
      etc.

       
    • Nobody/Anonymous

      By the way, "DIM Wind(80 " should be DIM Wind(80).
      -Omar

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.