Is it possible to set more than one pin high or low at a time? I would to have several pins change state at the same time. Can I somehow force a value (maybe binary) to a group of pins (like a port)?
Can I somehow force a value (maybe binary) to a group of pins (like a port)?
Yes. Have you searched for Direct Port Manipulation? http://www.arduino.cc/en/Reference/PortManipulation
Define "at the same time".
Are we talking nanoseconds, microseconds, milliseconds?
Paul, your very reasonable comment reminds me that the Arduino site doesn't seem to have any links where one can download the Atmel datasheets and they are surely an essential reference for anyone working with an Arduno.
...R
PaulS:
Yes. Have you searched for Direct Port Manipulation? http://www.arduino.cc/en/Reference/PortManipulation
Robin2:
Paul, your very reasonable comment reminds me that the Arduino site doesn't seem to have any links where one can download the Atmel datasheets and they are surely an essential reference for anyone working with an Arduno....R
Maybe not where you looked, but check the product pages. For example, there is a link to the datasheet for a ATmega328 on the UNO page, in the first sentence of the Overview section. http://arduino.cc/en/Main/ArduinoBoardUno
reminds me that the Arduino site doesn't seem to have any links where one can download the Atmel datasheets and they are surely an essential reference for anyone working with an Arduno
No, they're surely not - you're missing the whole point of the Arduino.
AWOL:
reminds me that the Arduino site doesn't seem to have any links where one can download the Atmel datasheets and they are surely an essential reference for anyone working with an Arduno
No, they're surely not - you're missing the whole point of the Arduino.
You have a good point about the point of the Arduino. But for this thread, isn't doing low-level hardware bashing like direct port manipulation also out of the scope of the point of the Arduino? (Sorry for the extremely poor grammar.)
Sembazuru:
You have a good point about the point of the Arduino. But for this thread, isn't doing low-level hardware bashing like direct port manipulation also out of the scope of the point of the Arduino? (Sorry for the extremely poor grammar.)
Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"
Hi,
here we go for me a getting a roasting.
I was looking at this today, I know I missed something and I did start with the tutorial on bit manipulation, but for the life of me I could not work out how to set the individual bits I wanted safely, and as normal I went all round the houses and ended up doing two operations. As an aside saved a lot of memory as is well documented.
#define PB4 4
#define PB5 5
PORTC |= _BV(PB4); //set bit 4
PORTC &= ~(_BV(PB5)); //clear bit 5
I have missed a point and would also love the answer, seeing the wood for the trees is often a problem.
Cheers
Ian
Without taking account of the full capabilities of the Atmel chips the Arduino is just a toy. Any serious application is likely to benefit from understanding and using registers and ports. And if you see it as an educational tool an understanding of the full capabilities is even more important to equip the student with the ability to transfer her/his knowledge to other microcontrollers.
AWOL:
No, they're surely not - you're missing the whole point of the Arduino.
And SEMBARZURU - the fact that I missed the link that you referenced proves the point I am trying to make. There should be a heading somewhere "Download Atmel Datasheets". And an exhortation to read them would also be a good idea.
...R
There should be a heading somewhere "Download Atmel Datasheets".
There is; it's on the Atmel website, where it belongs.
And an exhortation to read them would also be a good idea.
Again, we're just going to have to agree to disagree. The core audience will mostly be incapable (or unwilling) of getting past the TLAs on the the front page, at least early on. By the time they realise they can (or need to) do this stuff, they'll have found the Atmel site.
Without taking account of the full capabilities of the Atmel chips the Arduino is just a toy.
From the home page:
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.
codlink:
Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"
No, why would we do that? However the question is not well defined:
I would to have several pins change state at the same time. Can I somehow force a value (maybe binary) to a group of pins (like a port)?
And I don't believe I have got a response to my initial response.
"At the same time" is not very precise. You can't measure two electrons passing through a slit "at the same time". Plus "group of pins" is not very precise. You can in fact affect a number of pins, if they happen to be on the same port, almost simultaneously.
This forum is not limited to beginners, so please don't treat us like that. If you ask a technical question you will get a technical answer.
Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"
The flip-side of this is that most dangerous of beasts - the user who knows a little, but not enough.
The one who sees a particular parallel interface and imagines all the pins have to change value at the same instant and goes to great lengths and trouble to structure his code and hardware around this supposition, missing the significance of the line labelled "CLK".
Yes, I've had that question asked of me - it took a long time to get the details, and allay their fears, which is why I always try to get the bottom of what the person wants to do, not how they think it ought to be done.
[quote author=Nick Gammon link=topic=159754.msg1203247#msg1203247 date=1366108348]
codlink:
Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"
No, why would we do that? However the question is not well defined:[/quote]
That question was aimed at Robin2. He seems to think since this forum is for beginners. So for questions that's based on more complex operations, he was implying we shouldn't answer them.
This forum is not limited to beginners, so please don't treat us like that. If you ask a technical question you will get a technical answer.
Agree 110%.
AWOL:
Yes, I've had that question asked of me - it took a long time to get the details, and allay their fears, which is why I always try to get the bottom of what the person wants to do, not how they think it ought to be done.
I understand. It's funny reading a post and a member asking for more details about the project, but the OP doesn't want to divulge more information because he/she thinks that they have a million dollar idea. I am not saying they don't, but 99% of the time, someone has already thought of it first and realized that it was a dream.
codlink:
Sembazuru:
You have a good point about the point of the Arduino. But for this thread, isn't doing low-level hardware bashing like direct port manipulation also out of the scope of the point of the Arduino? (Sorry for the extremely poor grammar.)Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"
Naw. I was just following AWOL's tangent. (Poorly at that... Sorry I shouldn't try to make comments like that when fighting a nasty headache. Makes it hard to think cogently.)
Robin2:
Without taking account of the full capabilities of the Atmel chips the Arduino is just a toy. Any serious application is likely to benefit from understanding and using registers and ports. And if you see it as an educational tool an understanding of the full capabilities is even more important to equip the student with the ability to transfer her/his knowledge to other microcontrollers.AWOL:
No, they're surely not - you're missing the whole point of the Arduino.And SEMBARZURU - the fact that I missed the link that you referenced proves the point I am trying to make. There should be a heading somewhere "Download Atmel Datasheets". And an exhortation to read them would also be a good idea.
...R
@Robin2 (BTW, you a he or she? I strive to use the correct gender pronoun but I know both guys and gals with the name Robin...)
Sorry if I sounded brusque, I was trying to be helpful. I do agree that on the help pages for some of the more advanced techniques (like direct port manipulation) should include references to more hardware documentation. (To it's credit, that page does reference the pin-mapping diagrams several times.) And also some explicit warnings that coding too close the the bare metal may break cross-compatibility between Arduino models. (Not that there isn't already that issue with differing pin assignments between models. I remember a recent project I did where I was having issues and decided to just use the pin13 LED for some simple troubleshooting. I couldn't understand why the LED seemed to be about half-bright almost all the time until it dawned on me that the UNO uses pin13 for SPI, and I was using the SD library... I felt silly after that...)
That said, the port manipulation page is really well written, especially compared to some of the pages that have nearly no useful information. For example the page for pointers http://arduino.cc/en/Reference/Pointer Not even a vetted external reference for using pointers in C/C++...
But, there is probably better forum threads to discuss these points in detail...
@All
But... back to the OP. Yes, port manipulation is the fastest way to change multiple pins nearly simultaneously, provided they are all on the same port. But it is rarely necessary to actually change multiple pins nearly simultaneously. An evaluation of the intended purpose is needed to decide if that advanced technique is really necessary (as AWOL pointed out). Does the hardware really need it? Are you attempting to do it to learn something? (This is a valid reason IMHO). Does it actually make your programming easier? (For example, if you already have a parallel byte in a variable, it actually might be simpler to write the byte directly to the register rather than shift through the byte sending digitalWrites based on individual bit states.)
I'm leaving all the quotes in here for clarity as I don't want to offend anyone.
I was surprised to see the comment aimed at me as I had not connected it with me when it was originally made.
Just to clarify ... I have NEVER had it in mind that the forum should not answer complex questions. As far as possible all questions should be answered.
All I have in mind is that newcomers should be aware from the outset that more detailed information is available, where they can get it and that they will probably get a lot of useful information from it.
...R
codlink:
[quote author=Nick Gammon link=topic=159754.msg1203247#msg1203247 date=1366108348]
codlink:
Are you going to tell someone that is asking a question, "No, I can't tell you because this is a beginner forum?"No, why would we do that? However the question is not well defined:
That question was aimed at Robin2. He seems to think since this forum is for beginners. So for questions that's based on more complex operations, he was implying we shouldn't answer them.
This forum is not limited to beginners, so please don't treat us like that. If you ask a technical question you will get a technical answer.
Agree 110%.
AWOL:
Yes, I've had that question asked of me - it took a long time to get the details, and allay their fears, which is why I always try to get the bottom of what the person wants to do, not how they think it ought to be done.
I understand. It's funny reading a post and a member asking for more details about the project, but the OP doesn't want to divulge more information because he/she thinks that they have a million dollar idea. I am not saying they don't, but 99% of the time, someone has already thought of it first and realized that it was a dream.
[/quote]
Hi,
Hope I have not double posted, due to strange windows behavior.
To the original poster, as I said yesterday I was also looking at something I believe to be similar, in that I wanted to change the state of two bits at the same time, but ensure none of the other bits of the same port are affected by the operation. I had resorted to doing two operations, which in my case is ok I am just changing the state of two leds, which should not be on together and therefore the timing is good enough. However not wanting to leave a problem unsolved, I came up with this, if I start with the bits in a known state by doing the below in setup().
PORTC |= _BV(PB4); //set bit 4
PORTC &= ~(_BV(PB5)); //clear bit 5
It is actually easy to toggle these bits at the same time without affecting the state of the other bits, do note I said toggle as that is what I need.
PORTC ^= B00110000;
Here I believe is one of the things people are pointing out, this could get confusing and hard to track, very quickly, where as if you set and clear the bits individually, it is easier to debug.
Then I considered Nick's comments on 'at the same time' and more reading and although not a problem for my situation, it may be in yours. If you are using interrupts and the ISR is called between setting the first bit and clearing the second bit then you will have a longer than normal 'same time' before the pins are correct, hence you could use the above to make sure they both toggle 'at the same time' to get round this, however. More reading and this apparently, this may not be the best way, instead, disable interrupts before the part of your code that could be affected and then enable them afterwards. Below are the links I used to find this information.
http://www.nongnu.org/avr-libc/user-manual/group__avr__sfr.html
http://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html
That is my understanding, but if I am wrong I would be happy for someone to point out my mistakes.
All the best
Ian
ijm51000:
I was looking at this today, I know I missed something and I did start with the tutorial on bit manipulation, but for the life of me I could not work out how to set the individual bits I wanted safely, and as normal I went all round the houses and ended up doing two operations. As an aside saved a lot of memory as is well documented.
Remove these...
#define PB4 4
#define PB5 5
The correct designations are PORTC4 and PORTC5.
The correct way to atomically manipulate multiple bits on one port...
uint8_t Temp;
noInterrupts();
Temp = PORTC;
Temp |= _BV( PORTC4 );
Temp &= ~ _BV( PORTC5 );
// More bit manipulations go here
PORTC = Temp;
interrupts();
It certainly is much cleaner and easier to follow.
Thank you
Ian