Menu

setting ports

Help
2017-12-29
2018-01-02
  • stan cartwright

    stan cartwright - 2017-12-29

    I read the instructions but sorry can't find answer. Please..a word from the wise.
    I want to set mega328 portd 4 to 7 and not change bits 0 to3 with a var that is bits 4 to 7
    portd=lmotval ;works but changes all port d
    these don't make motor turn
    ; pind=((pind) and (lmotval+15))
    ; portd=portd and (lmotval+15)

        select case lmotor
          case 1
          lmotval=128
          case 2
          lmotval=192
          case 3
          lmotval=64
          case 4
          lmotval=96
          case 5
          lmotval=32
          case 6
          lmotval=48
          case 7
          lmotval=16
          case 8
          lmotval=144
        End Select
          portd=lmotval ;change to only affect bits 4 to 7 PLEASE
    
     
  • stan cartwright

    stan cartwright - 2017-12-30

    I can do it with a variable. I could just use the whole port for 2 motors

    pd=portd and 15:portd=pd + lmotval
    

    edit- sorted doh. portd=(portd and 15) +lmotval

     

    Last edit: stan cartwright 2017-12-30
  • George Alvarez

    George Alvarez - 2017-12-31

    Hi Stan,

    In an earlier thread, I was asking about some assembly I was trying to run, which turned out to be a macro language and not assembly, and a GCB user named Mike told me:

    The Mega328p dat file used by GCB names all the registers and Bits of registers so you can use them in your code.

    From the Mega328.dat file:

    PORTD0,PORTD,0
    PD0,PORTD,0
    PORTD1,PORTD,1
    PD1,PORTD,1
    PORTD2,PORTD,2
    PD2,PORTD,2
    PORTD3,PORTD,3
    PD3,PORTD,3
    PORTD4,PORTD,4
    PD4,PORTD,4
    PORTD5,PORTD,5
    PD5,PORTD,5
    PORTD6,PORTD,6
    PD6,PORTD,6
    PORTD7,PORTD,7
    PD7,PORTD,7
    

    So if I'm reading him right, this ought to do the trick:

    PD4 = lmotval
    PD5 = lmotval
    PD6 = lmotval
    PD7 = lmotval
    

    At least, that's how it worked for me for the register bits I needed to set individually.

    Also, from the Help:

    Dir
    Syntax:

    Dir port.bit {In | Out}               (Individual Form)
    Dir port {In | Out | DirectionByte}  (Entire Port Form)
    

    Command Availability:

    Available on all microcontrollers.

    Explanation:

    The Dir command is used to set the direction of the ports of the microcontroller chip. The individual form sets the direction of one pin at a time, whereas the entire port form will set all bits in a port.
    ...
    Example:

    'This program sets PORTA bits 0 and 1 to in, and the rest to out.
    'It also sets all of PORTB to output, except for B1.
    'Individual form is used for PORTA:
    DIR PORTA.0 IN
    DIR PORTA.1 IN
    DIR PORTA.2 OUT
    

    ...

    I realize that's not what you're looking to do, but it does reveal a way to address each bit individually, and it specifically explains the behavior you see setting the entire port.

    Good luck.

     
  • stan cartwright

    stan cartwright - 2017-12-31

    Thanks George. I think I had the motors mixed.
    I have 2 unipolar stepper motors, one is portd.4 to portd.7, the other is portb.0 to portb.3.
    So portb=(portb and 240)+rmotval just sets bits 0 to 3 and leaves bits 4 to 7 unchanged.
    Same portd=(portd and 15)+lmotval just sets bits 4 to 7 and leaves bits 0 to 3 unchanged.
    I didn't want to set individual port bits one at a time. This works. Sorry if I wasted your time.

     

    Last edit: stan cartwright 2017-12-31
  • George Alvarez

    George Alvarez - 2017-12-31

    How does this work for only ports 0 - 3, or 4 - 7?

    portb=(portb and 240)+rmotval 
    portd=(portd and 15)+lmotval
    

    I don't follow, so if you can explain it, no, not a waste.

     

    Last edit: George Alvarez 2017-12-31
  • stan cartwright

    stan cartwright - 2017-12-31

    portb and 11110000 ie 240 is top 4 bits of portb
    rmotval is made a lower 4 bit value ie 0000xxxx
    portb=portb top 4 bits + rmotval low 4 bits.
    the same for portd but using top 4 bits so lmotval is made upper 4 bit value ie xxxx0000
    ..if that makes sense. It's called bit masking or similar name.

     
  • stan cartwright

    stan cartwright - 2017-12-31

    I explained badly
    I'm using portb.0 to portb.3 so I'm changing x ie 0000xxxx and not 0000 bits.
    so portb and 240 is the 0000 bits of portb and I just need to change the xxxx bits which I do by adding.

     

    Last edit: stan cartwright 2017-12-31
  • George Alvarez

    George Alvarez - 2017-12-31

    "top 4 bits of port b" top 4 meaning bits 4 - 7? I'm thinking of this as numbers, placed from right to left.

    the way I understand what you wrote is that you actually ARE setting the entire port, it's just that you're just setting the whole thing to what you want it to look like in the end using a little math. so if rmotval= 00001001, then at the end, it's 11111001?

    Like that?

     
  • stan cartwright

    stan cartwright - 2017-12-31

    No, I mask the port so I keep the 4 bits I'm not changing then add the 4 bits I want to change.
    The point is the values I add are only those bits ie >=15 or <=16
    ie 0000xxxx to portb....and xxxx0000 to portd.
    It does work....honest...I'm not good at explaining and stepper motor phases are new to me.

     
  • George Alvarez

    George Alvarez - 2017-12-31

    That's ok, I'll interrogate. Let's just talk about portb, I get that what you're saying applies to both.

    The only way this makes sense to me is that the first half of this operation

    (portb and 240)
    

    is not addition, rather, this is a true/false exercise:

    for the 4 leftmost bits, you're doing a comparison; if both are the same, then 1, else 0
    by passing all 1's to the left four, matching bits (1's) will yield 1 and nonmatching bits (0's) will yield 0. The first 4 bits will always retain their original value if you compare them to ones, if both have to match. You don't need to know the existing value in those 4 bits in order to always get this right.


    how am I doing so far?


    the second half of this operation is addition.

    +rmotval 
    

    On the surface, this seems straightforward. But it seems to me that you can't be completely indifferent to the sum, like the way you could not know the starting value in the first operation. If you want the left 4 bits to stay static, then xxxx + rmotval MUST < 16. If you don't care how those left 4 bits end up, then at a minimum, you the ending result to be < 256. That means at some point, you either have to reset to some initial value, or you have to start subtracting too. You can't keep adding those values forever.


    Am I starting to clue in?

     

    Last edit: George Alvarez 2017-12-31
  • stan cartwright

    stan cartwright - 2017-12-31

    (portb and 240)
    is not addition, rather, this is a true/false exercise:

    George, and is a bit wise comparison, not addition.
    The ports are all dir out.
    portb and 1 = portb.0
    portb and 128 = portb.7
    portb and 240 = portb.4,5,6,7 bits which must not change
    add vallue 0 to 15 ie bits 0 to 3
    portb=result with only low 4 bits changed
    It's probably in help but couldn't find it.
    ps search for and,or,xor,not.
    portb=portb+portb assembles which is shift left as does portb=portb/2 which is shift bits right but it's not mentioned

     

    Last edit: stan cartwright 2017-12-31
    • George Alvarez

      George Alvarez - 2017-12-31

      Stan thanks -

      George, and is a bit wise comparison, not addition.
      The ports are all dir out.
      portb and 1 = portb.0
      portb and 128 = portb.7
      portb and 240 = portb.4,5,6,7 bits which must not change

      I think that's what I said, that any bit value operation and 1 will always yield the original bit value. Thinking about it a little more, I guess any bit value operation and will always yield zero.

      Moreover, you can do both of these operations blindly, without knowing what's in those 4 bits.

      As for this:

      add vallue 0 to 15 ie bits 0 to 3
      portb=result with only low 4 bits changed

      The observation I was trying to make is that if you started out with 11111101 and you add 15, you're going to run into trouble. That must mean there is more code surrounding your bit changes than just addition. It seems unavoidable, but I'm hesitant to take anything for granted.

       
  • stan cartwright

    stan cartwright - 2018-01-01

    yes 11111101 + 15 overflows.but I'm changing the 1101 bits so irrellevent. It's the 1111 bits I don't want to change.
    I think I'll go back to using a MCP23017 as finding 8 unused pins is too much bother. I did a demo and it's peasy to use.

     
  • George Alvarez

    George Alvarez - 2018-01-01

    Thanks for your patience with me and the explanations. Have a great New Year.

     
  • stan cartwright

    stan cartwright - 2018-01-01

    Happy new year to you. Is masking ports in gcb help? Explaining things is not easy sometimes and any help contributions seem welcome but try writing one. I haven't.

     
  • George Alvarez

    George Alvarez - 2018-01-02

    It isn't, at least not as an explanation. There's an example of doing it in BITWISE/FNLSR & FNLSL, but I get the impression that this assumes you already know what it is.

    That said, I started to look around, and the internet is full of explanations, so it's not really an issue except for the first few times you need it.

     
  • stan cartwright

    stan cartwright - 2018-01-02

    Logic functions are worth getting to know. Say you have buttons connected to a port, instead of testing each bit you can use a mask to and the buttons ie left and fire button pressed same time.
    Glcd graphics can benefit like using xor (see simple oscilloscope using xor)
    You also get to understand those arduino c statements :)
    Happy coding.

     

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.