Please help me understand arduino all the way down

Hey,

not sure if this is the right forum for my question, but did not know where else to put it.

This is the problem:

I usually get the arduino to do what I want it to do. However, I do not really understand it "all the way down". I mean, I know e.g. that if you let the arduino add decimal numbers, they are transformed to binary digits, which can be expressed as power being on and off, and that the arduino is able to process that, then everything goes reverse with a decimal result at the end. But I do not know how exactly the addition works. I understand that there is an adder unit, but again I do not know how it works exactly. And why does the arduino know to send the numbers to the adder unit and not the XOR unit?, etc...

What I mean to say is that I have a rough understanding of how the arduino works, but I WANT TO UNDERSTAND IT FUNDAMENTALLY AND ENTIRELY, down to the lowest level. And not limited the addition example, but for any code or interaction with the arduino. I just want to completely understand everything it does when I run any code on it.

Where would I start with this? Is this even a realistic aim? I do not have a technology background, so any materials would need to be introductory-level. I think, basically I am looking for a illustrative/explaining but profound textbook, online course or similar.

This is something that that cannot be quickly answered in these forums.

You need to start with mathematics and basic digital electronic circuitry.

One of the first things to remember is a number is just a number, the way it is presented is for human convenience.

Here is some homework for you.


How to use this forum:
https://forum.arduino.cc/index.php?topic=149014.0


Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8


Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8


Arduino cheat sheet:


Watch these:
Arduino programming syntax:

Arduino arithmetic operators:

Arduino control flow:

Arduino data types:

Jeremy Blume:

Sparkfun External Interrupts

Sparkfun Timer1 Interrupts

Powering You Projects


Understanding Destructive LC Voltage Spikes:

OR

Why MOSFET gate resistors:


Some things to read

LCD information:

OR

Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic

Language Reference:

Foundations:


How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0


Multitasking:
Part 1:

Part 2:

Part 3:


Neopixels, Adafruit

Fastled


Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all

Micro Controllers:

Useful links:
https://forum.arduino.cc/index.php?topic=384198.0

Arduino programming traps, tips and style guide:

Call for useful programming discussions
https://forum.arduino.cc/index.php?topic=383980.0

Arduino products:

Motors/MOSFETs

Switches:

A good book you might want to get:


Share tips you have come across, 500+ posts:
https://forum.arduino.cc/index.php?topic=445951.0


Debug discussion:
https://forum.arduino.cc/index.php?topic=215334.msg1575801#msg1575801

Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10


Number 'type's:

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0-255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0-65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
  • float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.

You should always select the 'data type' best suited for your variables.
ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. ex: int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. ex: char joystick;
  • if your variable is used for ASCII then you need type 'char', ex: char myText[] = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. ex: boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, ex: unsigned long currentTime = millis();
    etc.

Oh, and have fun too :slight_smile: !

Please help me understand arduino all the way down

I believe it's turtles all the way down.

...R

Learn to program in assembly language.

For that you will probably need Microchip's MPLABX or ATMEL Studio. Unfortunately I can't tell you how to connect these to AVR chips. I am familiar with using MPLABX with PICs but not AVR. PICs use a PICKIT, I think the latest version works with the AVR chips, not sure. Microchip's web site will tell you.

You could certainly expand your understanding of Arduino. larryd's list has a bunch of stuff to help you and there's much more to learn if you want to get right down to what's happening on the silicon.

There's some value to that, no knowledge is wasted, but you're talking about diving into the details of things that even most professional programmers don't necessarily know or care about. Computing is all about layers of abstraction, because if you have to worry about all the details of what's going on down to the bare metal, you would never get anything done.

Consider driving a car; how much do you really know about how it works? Very little unless you're an enthusiast. And apart from detecting when your mechanic is being economical with the truth, or stopping the car when it's obvious something really bad is happening, your lack of knowledge doesn't matter.

Same thing with Arduino; you will certainly be better off with more knowledge, but you really don't need to know about the architecture of its CPU and the transistors that make it go to be effective using it.

I'd suggest that if you start down that road, you'll quickly realize that it's a massive, unnecessary undertaking and give up. If you don't though, come back to this thread and let us know how it went.

Important terms "RISC" and "CISC" (particularly the former)

PerryBebbington:
Learn to program in assembly language.

For that you will probably need Microchip's MPLABX or ATMEL Studio. Unfortunately I can't tell you how to connect these to AVR chips. I am familiar with using MPLABX with PICs but not AVR. PICs use a PICKIT, I think the latest version works with the AVR chips, not sure. Microchip's web site will tell you.

And if you REALLY, REALLY want to go all the way down, the assembled assembly language instructions still have to go through a decoder in the microprocessor to get the primitive instructions to execute the assembly instruction.

Paul

Franzaforta:
But I do not know how exactly the addition works. I understand that there is an adder unit, but again I do not know how it works exactly.

Binary numbers Scroll down to binary arithmetic.

Man has come first in this world and then the machine (the computer or the Microcontroller or the Microprocessor) has come.

Human being has made computer to do job for him quickly, round the clock, and in the hostile environment.

If I ask you how much is the result of this operation: 5 + 7? You will immediately answer: 12.

How have you done this operation?
Impossible! you cannot answer -- this is the mystery!!! How have you learnt getting warm without putting your hands into the fire wood?

We think that the human being has biologically inherited an adder circuit; but, there is no proof of it. Accordingly, we try to mimic it (the adder) through trials-and errors using electronic circuits. At some point of time, the circuit works and gives the correct result. This is the very beginning of rudimentary Artificial Intelligence (AI).

The Arduino UNO has a microcontroller chip of type ATmega328P. It has been trained to:
(1) take command from the user (+, -, x, /) via Keypad;
(2) take data from the user via Keypad;
(3) modify data in a way the user wants;
(4) gives result (the modified data) to the user via display device.

You may ask the question -- how have we trained it? If I give you the answer, you will ask me another question. There will be no end of it; but, we need to come to an end.

So, the morale of the story is:
Play with objects/examples that work. You will soon discover why the object is working in this particular way. Having done practices this way, one day you will acquire the ability of making objects that work in much better way.

This video about the Fetch Execute cycle seems to give a good overview of life at the bottom.

I haven't watched all of this video but IMHO Tom Scott's videos are interesting.

...R

PS ... "turtles all the way down" is from Terry Pratchett's DiscWorld (hope I have the quote correct)

Deep down in the processor lives the ALU.

Datasheet for a 74181 ALU. It shows all the gates, truth table, etc. It's not the Arduino ALU (as far as I know) but the principles would be similar.

Not quite what you asked but if you want to see the origins of computing then I recommend a day at Bletchley Park.

And if you want to see the origin of "programming" Google "Ada Lovelace".

Paul

Fetch-decode-execution mechanism for adding two 16-bit numbers (memory referenced and immediate) by 8086 architecture:
fetch=execute.png

fetch=execute.png

"Where would I start with this? Is this even a realistic aim?"

I agree with others that you might want to get an intro level book on assembly language programming. This should get you down to the hardware level so you can see what is going on.

https://www.amazon.com/Assembly-Language-Programming-Books/b?ie=UTF8&node=3954

zoomkat:
"Where would I start with this? Is this even a realistic aim?"

2.5.B2 Moving an Immediate Data into a Memory Location
Engineers engage hell of efforts to consolidate their understandings (acquired using suitable Learning Kit: MicroTalk-8086) within pictures which they later on disclose in the public domain.

Instruction:       mov BYTE PTR ds:[bx+10h], 45h

Coding: 
01000 (0000:1000) - mov BYTE PTR ds:[bx+10h], 45h : C6 47 10 45

Meaning: The data for the destination memory location comes from a memory location (immediate addressing mode), which just follows the location (s) that holds the opcode bytes (instruction bytes).

Assume that ds = 0000h, bx=3000h and then 45h would be stored at location 03010h

Fetch-Execution: Shown below in Fig-2.45.


Figure-2.45: Explaining Fetch-Execution Mechanism of ‘mov BYTE PTR ds:[bx+10h], 45h’ Instruction

1. The PC (Program Counter, U1) asserts the address 01000h on the address bus.

2. The CM (Control Matrix, U4) sends the RD/-signal to the memory chip.

3. The opcode C6 (content of CSM memory location 01000h) enters into the opcode decoder. The opcode is interpreted and the CM understands that current instruction has an opcode of two bytes long.

4. The CM increments the PC, which is now 01001h. The address is put on the ABUS.

5. The CM generates the next RD/-signal.

6. The opcode 47 (content of the memory location 01001h) enters into opcode decoder. The opcodes ‘C6 47’ are jointly decoded and the CM extracts the following information:

i. The current instruction is 4-byte long. Therefore, two more read operations must be carried out to bring the remaining two byes (10 45) information from the memory.
ii. Addressing Mode is determined as ‘Immediate’, which means that the last information byte (45) is the data for the destination.

7. The CM increments the PC, which is now 01002h. The address is put on the ABUS.

8. The CM generates the RD/-signal.

9. The data 10h (8-bit displacement) enters into the DAR from where it enters into TRA8 of U7.

10. The CM increments the PC, which is now 01003h. The address is put on the ABUS.

11. The CM generates the RD/-signal.

12. The data byte 45h comes from the CSM and then enters into the TRB8 of U7.

13. The content of ds, bx and TRA8 are added together to get the destination memory address as: 03010h. The resultant address is asserted on the ABUS.

14. The contents of TRB8 (45) is placed on the DBUS.

15. The CM sends data write signal to the memory chip. The data byte 45h enters into the DSM location: 03010h.

The above-mentioned 15 steps are required to accomplish the task defined by the instruction:

mov    BYTE PTR ds:[bx+10h], 45h. The instruction execution cycle consists of:

i. Four read operations (1-3, 4-6, 7-9 and 10-12).
ii. One write operation, which occurs in the background.

GolamMostafa:
Fetch-Execution: Shown below in Fig-2.45.

What microprocessor does the diagram refer to and what book is is taken from?

...R

Robin2:
What microprocessor does the diagram refer to and what book is is taken from?

It is Intel 16-bit 8086 Microprocessor.

I have attached a pdf copy of Ch-2 of the text book to which the quoted diagram belongs to.

ch2(P55-P164)final.pdf (1.48 MB)

GolamMostafa:
It is Intel 16-bit 8086 Microprocessor.

I have attached a pdf copy of Ch-2 of the text book to which the quoted diagram belongs to.

Thanks. A link to the complete book (if that is possible) would probably be more useful for the OP. Or the name of the book and the author in case he wishes to buy it.

...R

Robin2:
Thanks. A link to the complete book (if that is possible) would probably be more useful for the OP. Or the name of the book and the author in case he wishes to buy it.

Here is the Title of the 542 page Text Book on 8086 Microprocessor containing 8 Chapters --
Ch-1: Fundamental Concepts,
Ch-2: Architecture,
**Ch-3:**Instruction Set,
Ch-4: IO Controller Programming,
Ch-5: Interrupt Structure,
Ch-6: Data Conversion Algorithms,
Ch-7: System Design, and
Ch-8: 8051 Microcontroller.

Interested readers may place requests via PM to get free pdf copy of the desired chapter (s). Unfortunately, the pdf version of the whole book is not available in the net due to Publisher's restriction.