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)
selectcaselmotorcase1lmotval=128case2lmotval=192case3lmotval=64case4lmotval=96case5lmotval=32case6lmotval=48case7lmotval=16case8lmotval=144EndSelectportd=lmotval;change to only affect bits 4 to 7 PLEASE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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 INDIR PORTA.1 INDIR 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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
(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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
I can do it with a variable. I could just use the whole port for 2 motors
edit- sorted doh. portd=(portd and 15) +lmotval
Last edit: stan cartwright 2017-12-30
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:
From the Mega328.dat file:
So if I'm reading him right, this ought to do the trick:
At least, that's how it worked for me for the register bits I needed to set individually.
Also, from the Help:
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.
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
How does this work for only ports 0 - 3, or 4 - 7?
I don't follow, so if you can explain it, no, not a waste.
Last edit: George Alvarez 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.
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
"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?
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.
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
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.
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
(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
Stan thanks -
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:
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.
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.
Thanks for your patience with me and the explanations. Have a great New Year.
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.
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.
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.