I am programming my ATmega32A using the C language with <avr/io.h> library but I see that a lot of people use the Arduino Library that offers some functions such as digitalRead/digitalWrite and analogRead/analogWrite. I understand what they do but where is the catch? What would happen if I use them? Will my code be not that fast? And most importantly as an engineering student who learns about embedded systems, is Arduino Library used in industry or not?
ATmega programming using Arduino Library.
There's a price for everything! It is well known the digitalRead/Write is slow, the other functions are reasonable. Is it used in industry? Who knows? Once compiled, you don't see the source!
As an engineering student all learning is good learning. You can read the code for the Arduino libraries and see exactly what they do - there's no secrets.
In my industrial products, i wrote my own library of functions that i would re-use. As such, the Arduino library is no different.
Yes but I think that Arduino Library hides all the complexity and the internal working of the microcontroller and so I won't be able to go deep into them. Am I right?
You are wrong. All the code is open. I've dug into DigitalWrite() and it is slow as Kartman suggests. You can go all the way to the very bottom level of that code and any other Arduino code. The code is not always easy to find, but it IS there.
Jim
I think that Arduino Library hides all the complexity and the internal working of the microcontroller
All libraries do that. At least, all GOOD libraries. It's up to you to decide whether you'll stop there, or dig underneath.
The Arduino libraries are all Open Source, and you can look at the source code as much as you want, either as examples, or to be modified...
A lot of the Arduino libraries are C++, and you won't be able to add them directly to C programs, without modification.
Yes but I think that Arduino Library hides all the complexity and the internal working of the microcontroller and so I won't be able to go deep into them.
But this is just why Arduino was created.
It enables the beginners to start play with mproc without studying 700 pages of the datasheet.
Evryone has some limit how deep to go.
There is a large group of people who need to create a simple program time from time. They use the functions and do not care what is inside them.
Then there are students like you who want to know the details in a function.
Nothing prevents them to find it, as the functions source code is available.
But for a scientist even a simple instruction like "LDI R16, 1" hides the complexity of what exactly happens in the guts of a mproc.
Yes but I think that Arduino Library hides all the complexity and the internal working
Of course, as already noted, that is the entire point of any library!
and so I won't be able to go deep into them.
That is a complete non-sequitur!
There is absolutely nothing to stop you, alongside using a library, also delving into the "inner workings" - especially, as in the case of Arduino, when the source is available.
With Arduino - you choose if you want to use their libraries - nothing stops you from doing direct port access but given a choice, one tends to go the easy route and use the supplied libraries simply because they work. There's a zillion Arduino apps that happily tolerate the slow digitalRead/Write functions as most of the time it makes no difference to the overall operation. Of course, there are some instances where you want to control the port bits as fast as you can - and you can do this if you so desire. People 'think' they're writing in 'Arduino' language - in reality it is just C/C++ with a load of useful functions.
Here's a little anecdote: In my earlier years I used to write tight cycle counted assembler. AS my projects got bigger, I started running into problems with the code being too rigid - if I wanted to change something, I broke something else. Then I read a book called "Code Complete". In the book it mentions that most of your code rarely executes - it tends to be only specific sections where optimisation might be a benefit. This was a revelation to me - it was obvious, but until I thought about it, I'd not actually considered this. So if you're just turning a relay or led on or off, the speed of the port operation is insignificant - a relay takes in the order of 100ms to turn of or off, so what difference does it make if the port operation takes two cycles or 100? Like most things in life - there is a cost. It's up to you to understand what you are buying and whether the cost is good value or not.
I read somewhere that the engineer's job is not one of being smart but one of economics. One could build an infinitely large bridge that would not fall down in a million years and be expensive or employ an engineer to design a bridge that is adequate for the task. The engineer would apply the required science to ensure the bridge would not fail but yet not be excessive for the task thus saving money.
so I won't be able to go deep into them. Am I right?
As others have said, you can go through the open source code to see just what it does and then roll your own.
Perhaps that is not deep enough?
The first computer I owned, I programed by entering 1's and 0's on toggle switches...
"toggle switches"? Luxury!
Do "nut jobs" substitute for "toggle switches"?
Jim
The first computer I owned, I programed by entering 1's and 0's on toggle switches...
"you can go through the open source code to see just what it does and then roll your own."
Well, that is true ... and would be easy if Arduino was for "only" a given processor; as it needs to recompile if one changes, say, from 328 to 2560,
(a) it is open source -> source exists on your computer (try to find SPI.h, say; from /usr/share with a linux + distributions arduino; from where you unzipped/tarred arduino, else)
(b) source depends on conditional compiling, and is uneasy to read (human beings are not often C preprocessors)...
(b) source depends on conditional compiling, and is uneasy to read (human beings are not often C preprocessors)...
dbrion0606 wrote:
(b) source depends on conditional compiling, and is uneasy to read (human beings are not often C preprocessors)...
if you find that to be the case you probably aren't using a decent editor!
ISTR that Atmel Studio is not particularly smart at it?
ISTR that Atmel Studio is not particularly smart at it?
I thought [the Atmel Studio editor] had VassistX which is a sort of half-baked version of Intellisense
Yes - but, AFAIR, it's lame at conditional code highlighting.
I was actually envisioning something more like Eclipse
Yes - Eclipse does it quite well.
if you find that to be the case you probably aren't using a decent editor!
I more or less agree : I use less https://en.wikipedia.org/wiki/Less_(Unix) as a plain,ressource sober -I use nanopi as PC : arduino and avr-gcc can be installed from repositories- text viewer (vim/cream has everything I need to view, edit and modify; but I donot think arduino's source should be modified by me .... )