Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question on learning ATmega328 using Elegoo kit.

Status
Not open for further replies.

Alan8947

Full Member level 4
Full Member level 4
Joined
Sep 21, 2016
Messages
215
Helped
9
Reputation
18
Reaction score
11
Trophy points
1,298
Activity points
3,746
Hi, I bought the Elegoo kid from Amazon:
https://www.amazon.com/gp/product/B08C4SK6H3/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

I want to learn programming and design hardware using ATmega328P. I know I will be using C++ to program, but at the mean time, I want to know how much I have to study ATmega328.

I designed MPU stuff long time ago, I am reading the data sheet of ATmega328, it's LOONNGG!! I also reading the assembly language. That's total of over 300 pages. My question is how far I need to study this before I start working on the kit.

I want to be a little better than kids that just follow the instruction and make some of the experiment work, I do want to eventually design using the ATmega328. So how much I need to study the ATmega328.

Thanks
 

I think I got it. The difference is "GENERAL PURPOSE REGISTER" VS "I/O REGISTER".

Some instruction work specifically on I/O register save one bit by addressing 0x20 by 0x00 by SUBTRACTING 0x20 from the PHYSICAL ADDRESS.

eg. CBI is SPECIFIED for using on I/O register, so Physical address of PORTB of 0x25 becomes 0x05 when using CBI instruction to save one bit.

Is this correct?

Thanks
 

Is this correct?
I'm not sure.
The IN istruction can address 32/64 registers only, thus 5/6 bits address.
The LDS instruction can address 65.536 registers, thus 16 bits address.

Klaus
 
I'm not sure.
The IN istruction can address 32/64 registers only, thus 5/6 bits address.
The LDS instruction can address 65.536 registers, thus 16 bits address.

Klaus
Thanks, I'll deal with those when time comes, At least I know about CBI and those.

So for PORTB address, 0x05(0x25), the REAL PHYSICAL address is inside the bracket 0x25, the 0x05 in front is for use in the instruction CBI that offest by 0x20.
PortB.jpg


Thanks
 

Hi,

I am looking at the LD instruction. I am really having a hard time reading it. This is copy from the instruction set
LD.jpg


What is "REGISTER" means? Is it the true first 32 register on physical address 0x00 to 0x1F, or I/O register that started at physical address 0x20 to 0x3F?

You can see the write up, it only said "register". It did not specify.

There are ONLY 5 "d" bits, so the instruction only can address 32 location. So LD instruction ONLY can load 32 I/O registers or ONLY the first 32 register. It cannot do BOTH.

I know they try to squeeze into as short the OPCODE as possible for speed. IT IS SO HARD TO FOLLOW AND THE WRITE UP IS NOT GOOD IN THE DATA SHEET.

I tried going to youtube, then I have a hell of a time understand their English as they speak with heavy accent. I studied PDE, electromagnetics, RF/microwave, C++ all on my own. I have not encounter so much problem understand the datasheet of explanation like this one. That I have to keep asking stupid question like this.

Thanks
 

What is "REGISTER" means?
One of the 32 working registers R0...R31. As destination, not as source.
Although it does not matter, but the physical address also is 0x0000 ... 0x001F
Source is the content of 16 bit address given by X

Please read again the beginning of my post#40.
Section 2 of the Instruction set manual describes this in detail.

I don´t think the description in the datashee is bad. It´s just that you stick unnecessarily hard on the physical addresses.
As soon as you accept
* there are 32 (working) registers
* there are sepaerate IO registers
* there is seperate SRAM space
you won. You are just confused, because AVR tells you they are accessed all via the same address lines.

****
Let´s imagine there are 200 houses in a street.
The first 32 houses are blue (working registers) absolute number 0..31´, or "red 0..31"
The next 64 houses are red (I/O registers) absolute number "32 ..95" or "blue 0..63"
the rest are yellow houses (rest of the address space) absolute number (96 ...199)

Now imagine that someone says you have to go to the 1st blue house or the 3rd blue house:
* "the 1st blue house" = "blue 0" = "absolute house number 32"
* "the 3rd blue house" = "blue 2" = "absolute housenumber 34"
and so on...
so if you hear "blue" you know there is an offset of 32.

***

And indeed you don´t know all this, since the assembler does the work for you.

And you say you know "C++".
Imagine you write:
PortD = 0x02;
Do you care what is the code that is generated? do you care wheter it uses an IO access or an SRAM access? do you care which physical address PortD is?
Usually not, jsut when there are problems. But how often did you go that deep in C++ that you had to decode down to the OPCODE?

--> just let the C++ compiler make it´s job.
--> just let the assembler make it´s job.

Klaus
 
Thanks Klaus

I guess when they said register, I question whether it's I/O register or not. So in this case, the "register" really mean "Working" register.

But in this case, it can over write the X, Y, Z register that are used in LD instruction as those contain the address of the data to be loaded to the register.

I know I am digging deeper into this. I want to use this opportunity to get familiar with the ATmega processor. Am I getting to detail?

Thanks
 

Hi Klaus

I have been reading more, please correct me if I am wrong. I am getting very nervous when reading the word "register"!!

1) Rd and Rr always refer to the working registers of physical address 0x00 to 0x1F. So the 5bits in the instruction is the physical address.

2) Seems like more complex mathematical and logical operations can ONLY perform in the working registers, NOT in I/O registers.

3) Unless specifically said "I/O register", It's ALWAYS means "WORKING REGISTERS". I got tripped in LD instruction that said "loading to the registers" to means including both working registers AND I/O registers.

THEY REALLY SHOULD SPECIFY THIS REALLY LOUD RIGHT FROM THE BEGINNING. THAT IS CAUSING ME SO MUCH PROBLEM. I have no problem understanding your blue and red houses because you are very specific. The datasheet is NOT.

thanks
 

Hi Klaus


Sorry to keep asking questions on the register. Here is the copy of the IN instruction from the Instruction Set from Microchip.
IN.jpg


You explain in post #30 very clear. But look at the example here. It said read Port B to r25. but it said reading from $16. Not $05.

It should be in r25,$05 if I understand correctly.

Thanks
 

Hi,

Register is just another word for D-FlipFlop. And one can use a DFF in many many different ways.

But in this case, it can over write the X, Y, Z register
This is true. You need to be aware that registers R26..R31 are named as X, Y, Z.
It´s not only a pitfall, it´s also an opportunity.

Rd and Rr always refer to the working registers
Yes.

2) Seems like more complex mathematical and logical
That´s the job of the working registers ...
And why would one add/subtract a number of the PortD register? There are very rare cases for this.

3) Unless specifically said "I/O register",
The nomenclature is rather clear. I can´t remember I had much trouble in understanding the AVR datasheet and instruction set. And I have programmed them a lot in Assembler.

THEY REALLY SHOULD SPECIFY THIS REALLY
Honestly ... did you ever write a that complex manual, that million users all over the world had to understand? It´s not an easy task.
Not meant offensive: Most readers seem not to have the same difficulty that you have. ;-)
I try to help you with my knowledge .. which I have from the same datasheet and instruction set manual that you have.

It should be in r25,$05 if I understand correctly.
I´ve already tried to explain this in post#36

Example with houses: In one city the grocery store might have house number 5 in the other citiy it might be 34.
If you are the post delivery man you most probably will refer to the house number.
If you want to buy food you most probably want to refer to the "grocery store".
If I want to access "PortD" then I use the name "PortD" and don´t care about the address. The assembler (delivery man) will do it´s job correctly (access the correct address).

Klaus
 
Thanks so much Klaus in explaining. If only the datasheet explain like this, I would have much easier time. I bet for people have has been in this field, seeing the evolution of MPU through the decades, they will have much easier time understanding and follow. I have not done any assembly programming since 1982 and never read into assembly language since. That was with Z80 and 8085. It's a huge jump.

Honestly, I still think in HEX, decimal numbers actually throws me off now. Now that you explain the registers, I/O registers, it's a lot easier to understand. I don't know how many times I read the first 30 to 50 pages of the data sheet. I read again last night, here is an example:
Confusion .jpg


It's in P28. For people that know about register file and I/O register, it's very easy. BUT IMAGINE someone read this the very first time. See the words RED lined. "the next 64 location, the standard I/O memory!!". That really throws me off. For people that know the AVR already, it's easy. But for someone that never seen this before. Is it memory or register? I know they are DFF, but the name, why can't they be consistent, 0x20 to 0x3F is I/O register..... Be consistent for people that read it the very first time.

These stuffs are actually very simple if I can read it clear. I have no problem reading physics, ODE, PDE, Microwave electronics books. Those are hard to understand, but very few question with they really mean in words.

After you clarify these up, I move faster going through the instruction just to see what they have and what part they work on now. Thanks

Next question to you is where can I find software to write and debug in assembly language using my Elegoo kit. I know I am wasting time on this, better to get on to C++ programming. But I am just curious and I have time. At least I want to play with LD, SD, IN, OUT, setting up interrupt, stack pointer etc.

Again, thanks for you help. BTW, are you in Europe, you answer at 2AM my time here!!

Alan
 

I have no question this time. Just want to say after I get through the registers stuff, it's moving faster. I went through like 100 pages, power up, reset, Interrupt, I/O Ports. Just scan through them quick, wait until I design the board and determine which way to go and read more. Hopefully I can start learning to do the real programming in assembly. I just need to find IDE to start doing something with my Elegoo kid.

Thanks for all the help Klaus.
 

What is the newer and improved version of ATmega328? I want to look at the datasheet to see how much in common to see how easy to get to the newer chip when ATmega328 is obsoleted.

Thanks
 

Hi,

if I was interested in the same question I´d go the microchip.com internet site. They should be most up to date.
They provide production status of each type, comparison charts, migration informations....

Klaus
 

If release not there might not be on website, but you could contact FAE in your
region, they usually are kept abreast of new product development.


Regards, Dana.
 
Thanks

Am I getting too deep into the registers, ports, all the interrupts in assembly? Should I just forget those and just start the kit and get on with C++?

From the examples I've read, they still use the register names, meaning I do have to know their functions, how to set ports to be simple digital in/out port, or Alternate functions like interrupt, clocks, serial communications and all that. That means I do have to understand all the details of interrupt, ports, watchdog, ADC, choosing clocks and all that.

Sorry if the question is too ignorant, for me, this is like chicken or egg, which comes first.

Thanks
 

Hi,

Even for C programs it's useful - if not important - to know the register names as well as the functions.
C functions may assist you for very simple tasks, but usually they are limited in flexibility.

There are millions of tutorials, example codes, descriptions....

But indeed I recommend "learning by doing".
Look for a simpke task, then try to code it.

If you want to build a house you don't (necessarily) need to know how to build a bridge ... nor do you need the detailed chemical reaction of curing concrete. But later maybe you need to go into detail ....

Klaus
 

    Alan8947

    Points: 2
    Helpful Answer Positive Rating
Hi Klaus
I have been reading and I want to confirm with you.

There are TWO Separate ADDRESS BUS:
1) SRAM ADDRESS bus that address working registers, I/O registers and SRAM that we've been talking.
2) FLaSH ADDRESS bus that is for Flash program memory. For ATmega328, it's 0x0000 to 0x3FF PHYSICAL address.

I still don't know where the EEPROM address is, are they on SRAM address space?

Thanks
 

Hi,

AVR uses Harvard architecture.
1) SRAM bus (and registers, periferals....)
2) Flash bus for code
3) EEPROM

from the datasheet:
"7.1 Overview
This section describes the different memories in the ATmega328P. The AVR ® architecture has two main memory spaces,
the data memory and the program memory space. In addition, the ATmega328P features an EEPROM memory for data
storage. All three memory spaces are linear and regular.
"

Klaus
 

Thanks Klaus

I still say the datasheet is poorly written and is for people that are familiar with modern MPU. This only got mentioned in p18 7.1. I could not find any detail going through 100pages. Thanks to this link that explain so clearly in 6 pages.

https://www.arnabkumardas.com/arduino-tutorial/avr-memory-architecture/

So the EEPROM has it's OWN address bus also?

How about the data bus? Are there 3 separate data bus also?

Thanks
 

Hi,

Harvard architecture uses separate address busses. Also separate data busses where needed.
But somehow the busses need to be controlled / combined by a central unit.

Often information is needed to properly use a device. .. In a way that depending on the information one can choose to go one way or the other.
I can´t see this here.
There is a clear code sequence how to access the EEPROM. Nowing how the address bus and data bus of the EEPROM is wired won´t change the sequence. Thus the bus structure - in my opinion - is not important for proper coding.
It´s effort, stress and time .... for no improvement.

The datasheets are huge already ... if they explain every detail, so that 99.9% of all users understand them (you know different people have different ways to learn things) then the datasheets will explode.
So the manufacturer focusses on the functional description.

Klaus
 

    Alan8947

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top