ATmega programming using Arduino Library.

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. Jun 17, 2017 - 03:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vamoirid wrote:
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.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vamoirid wrote:
so I won't be able to go deep into them. Am I right?
How deep do you want to go?

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...

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

https://scholarlykitchen.sspnet.org/wp-content/uploads/2014/03/the-monty-python-team-try-001.jpg?w=300

 

"toggle switches"? Luxury!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do "nut jobs" substitute for "toggle switches"?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

frog_jr wrote:
The first computer I owned, I programed by entering 1's and 0's on toggle switches...

http://dilbert.com/strip/1992-09-08

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"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)...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

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!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:

(b) source depends on conditional compiling, and is uneasy to read (human beings are not often C preprocessors)...

 

clawson wrote:
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?

 

frown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
ISTR that Atmel Studio is not particularly smart at it?
Not sure why you would choose that particular editor but if you did I thought it had VassistX which is a sort of half-baked version of Intellisense. I was actually envisioning something more like Eclipse to be used if you want to study source code "intelligently".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
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.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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 ....   )

 

Last Edited: Mon. Jun 19, 2017 - 02:02 PM