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.

Alan8947

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
 
Hi,

I can´t find this information in my datasheet.
Please post a link.

Klaus
Hi KlausST

Thanks for the reply. Here is the site.
http://www.rjhcoding.com/avr-asm-sreg.php



The Negative Flag (N)​

The Negative Flag is set whenever bit 7 of the previous operation is set (i.e. the result is negative)
ldi r16,1 ; load r16 with 0x01
sbi r16,2 ; negative flag set (result = -1 or 255)


Thanks
 
Hi,

your link does not point to a datasheet - as you wrote before. This is second hand information.
Better rely on first hand information directly from the manufacturer.

Klaus
 
Hi,
2) From SBI r16,2 instruction, it means set bit 2 of r16. If r16 has original value of 0x01, setting bit 2 gives you 0x05 only. Bit 7 is NOT set. Why N is set?
I guess I have the answer to this.

I assume what he wanted to do is:
* store 1 into R16
* subtract 2 from R16
.. so the result becomes -1 --> negative.

But he used the wrong instruction.
--> SBI is: set bit in register
while he wanted to use
--> SUBI: Subtract immediate (he missed the "U")

****

One remark to the description to the SUBI instruction in the "instruction set" manual.
There it sates:
This instruction is
working on Register R16 to R31 and is very well suited for operations on the X, Y, and Z-pointers.
X, Y and Z are considered to be 16 bit registers. But the SUBI instruction does only work for 8 bits.
So indeed SUBI works on X, Y, Z only if the math does not cross the 8 bit (=byte) border.
--> If you want a true 16 operation on X, Y, Z then use the SBIW instruction. It is made for 16 bit wide registers.

The SBIW instruction works on register pairs:
* X: R26, R27
* Y: R28, R29
* Z: R30, R31
* but also on register pair R24, R25!

Klaus
 
Thanks KlausST

I just started learning, I don't want to say the article is wrong. Just want to double check.

Thanks

Alan
 
Hi,

I have no problem to say it's wrong.

Another person may say: it makes no sense,
the next: it's not correct.

The result is the same..


Klaus
 
Hi,

I have no problem to say it's wrong.

Another person may say: it makes no sense,
the next: it's not correct.

The result is the same..


Klaus
Thanks, I just started on this, what do I know.

I am just slowly reading through the instruction set just for the hell of it. I just feel like at least know a little more about the processor first. Don't know whether it will help me, but I am sure it won't hurt.

I want to at least to be able to write a few little programs in assembly. I am sure I have a lot more questions I need your help.

Thanks
 
Hi,

The forum will help you.

Regarding reliable informations, I can only repeat: stick to the original manufacturer documents.
They are the most reliable.

And in case my word contradicts a manufacturer information, then better trust the manufacturer.

All second hand information is based on the manufacturer informations. Simulators, assemblers, compilers, tutorials, videos, comparison charts, ....
So in case there is a mistake in the original instruction set manual, then most probably the assembler software (or compiler software) becomes erroneous, too - in first place. Then many people use this assembler/compiler .. and at least the users will detect the mistake. So the assemblers and compilers become corrected and they also (as well as other people) wirr report the mistake back to the manufacturer. And the manufacturer will update the documents.
But there is a good chance that many other documents, internet pages ... don´t get corrected.

For sure there will be mistakes even in original documents, but usually the manufacturer is interested in correcting them.

Klaus
 
Last edited:
I have quick questions.
1) What is $FF00 and $00 means? What is the meaning of "$"?
$numbers.jpg


2) Above is the example of two different LDS, 1st one is TWO 16bit instruction(16bit opcode and 16opend of address). 2nd one is only ONE 16bit opcode with 7bits address in it. So it's the number of digit after $ that distinguish the two LDS? ( $xxxx for the 1st that reach 64K space, $xx for the smaller space?)

Thanks
 
Hi,

Did you read the instructionset manual.
I can only repeat that it reads SRAM at location $xxxxx and writes the contents into according register.

1) is the address of the SRAM space
2) "$" in this case just tells that the following value is a HEX value.

It also states" This instruction is not available in all devices. Refer to the device specific instruction set summary."
Did you do?

So I wonder what exactly us unclear

Klaus
 
Hi,

Did you read the instructionset manual.
I can only repeat that it reads SRAM at location $xxxxx and writes the contents into according register.

1) is the address of the SRAM space
2) "$" in this case just tells that the following value is a HEX value.

It also states" This instruction is not available in all devices. Refer to the device specific instruction set summary."
Did you do?

So I wonder what exactly us unclear

Klaus
Mainly "$". I tried to search on line. I know symbols changed since I worked with assemble language. In my days Hex is like 0FFH, 7AH. binary is 1010B etc. Now Hex is 0xFF, 0x7A etc. binary is 0b1010 etc.

I just not sure what is say $75, $F0 what they are. You confirm it's HEX, How come it's not 0x75 and 0xF0? Is it just for the assemble coding that they use "$"?

I just attach the 2nd question while I am posting, it's not that important, I think I can figure those out.

Thanks
 
Hi,

7 bit "LDS" vs 16 bit LDS:
Here you are wrong. Only one type LDS exists on a microcontroller.
Thus they tell you to read the microcontroller datasheet for you to find out which type of LDS is available on your AVR.

In other words: There are different type of AVRs,
* some use the 7 bit LDS,
* some use the 16 bit LDS.

*****
Why they use "$" as well as "0x" ... I don't know.
If they have a reason for this, I'd expect them to explain this in the instruction set manual. Did not find anything.

Klaus
 
Last edited:
Hi,

7 bit "LDS" vs 16 bit LDS:
Here you are wrong. Only one type LDS exists on a microcontroller.
Thus they tell you to read the microcontroller datasheet for you to find out which type of LDS is available on your AVR.

In other words: There are different type of AVRs,
* some use the 7 bit LDS,
* some use the 16 bit LDS.

*****
Why they use "$" as well as "0x" ... I don't know.
If they have a reason for this, I'd expect them to explain this in the instruction set manual. Did not find anything.

Klaus
Thanks, I did not realize that. I only concentrate on ATmega328. It's a lot of new things for me, I am jumping back and fore between data sheet and codes. I find it's hard to get the feel just reading data sheet as it gives a lot of program examples also, so I decided to read the different instructions first.

I am really going back and fore.

Thanks
 
Hi, I have two simple questions, See below:
PortB.jpg

1) I circled in RED the address of PORTB 0x05. Is this the physical address of Register5 in the first 32 register in Data Memory Map of 0x00-0x1F?
Meaning DDRB is actually Register4 and PINB is actually Register3. in the memory mapping?

2) I circled in BLUE that the address is adding 0x20 to the first address in RED. I read something that this is the address of one particular version of ATmega.
But I really don't quite get it. What is that address?


I know I am jumping around, I just feel the datasheet is really jumping around. I can't just follow the data sheet and learn. Right now, I decide to learn the PORTS first, how to program them, then I got to say interrupt, program counter etc. I want to concentrate on on topic at a time. I want to finish the Data memory map first.

Thanks
 
Hi,

there is the whole 16 bit address area. Not all AVRs use all 65536 locations, but for compatibiliy reasons they designed common instructions to access this space. Like STS, LDS ...
Now you can´t generate an 16 bit instruction which contains 16 bits of address, thus these STS and LDS instructions are 16 bit but need another 16 bit "parameter". For the instruction to be performed it needs 2 x 16 bit access to the program memory (16 bit instruction + 16 bit address). Even with pipelining these instructions need 2 clock cycles to be performed.

But the AVR designers wanted a faster access to the more often used periferal registers.
So they designed fast instructions where the address is included in the 16 bits (of the instruction). Obviously this can´t work with the whole 16 bit address space. So they needed a workaround.
Their solution was to use a limited address space (32/64 locations) of 5/6 bits for the "fast access".
* If I´m not mistaken the space 0x0000 to 0x001F are used for the working registers R0...R31. The periferals register start at 0x0020.
* from 0x0020 to 0x003F there are the most important periferal registers for port access, timers and interrupts, which rather often need single bit manipulations.
* from 0x0040 to 0x005F there are the less, but still rather important periferal registers

* and above 0x0060 there are even less important registers....

The fast instructions work just on the very important registers (marked in blue). And to squeeze out the last bit, they decided to "virtually shift the address down by 0x0020".
* There are fast instructions (SBI, CBI, SBIC, SBIS...) for just the registers at 0x0020...0x003F (shifted down to 0x00 to 0x1F) - using only 5 bits of address parameter (5 bits address included in the 16 bit instruction. See the instruction documentation)
* There are other fast instructions (IN, OUT, LD, ST...) for just the registers at 0x0020...0x005F (shifted down to 0x00 to 0x3F) - using only 6 bits of address parameter. (6 bits address included in the 16 bit instruction. See the instruction documentation)

With this workaround they managed the instructions to be performed within one clock cycle (no need to load the extra 16 bit address), meaning twice the speed compared to the 16 bit address instructions.

I agree it looks weird, but indeed a rather clever solution to boost performance.

Klaus
 
Hi Klaus

I don't follow this in your reply:
* There are fast instructions (SBI, CBI, SBIC, SBIS...) for just the registers at 0x0020...0x003F (shifted down to 0x00 to 0x1F) - using only 5 bits of address parameter (5 bits address included in the 16 bit instruction. See the instruction documentation)

I looked up CBI, I don't see the address shift. Can you give me an article explain the address shifting?

Thanks
 
Hi,

This is the example from the "Instructionset manual":

cbi $12,7 ; Clear bit 7 in Port D
(Mind that this example is a general AVR example. The address of PORTD might be different on different AVRs)

On an ATMega328P PortD has the address 0x2B but for CBI instruction you need to use the (downshifted) address 0x0B.

0x2B needs 6 bits.
0x0B needs 4 bits. But the range is 0x00 to 0x1F and thus is 5 bits of address. You see the "AAAAA" = 5 address bits in the OPCode of CBI.

Klaus
 
Thanks Klaus

But how come the instruction CBI specified it's for the lower 32 register as show, You can see the range is 0 to 31.
CBI instruction.jpg


This is where I was confused yesterday.

Thanks
 
Hi,

I don't know where your confusion is. (I already tried to explain this. And why. And example)
What's the hurdle to pass?

0x00 ... 0x1F (5 bits of address "AAAAA" in the opcode) of "lower 32 I/O register" which usually occupy the address space of 0x0020 .... 0x003F in the 16 bit address space.

Klaus
 
I thought the first 32 register occupy the lowest address space from 0x00 to 0x1F physically, not 0x20 to 0x3F. See the data map in p28 of the data sheet
Data memory.jpg


I am really confused. I thought PORTB is in 0x05 according to the copy in post #33 above. That was in p100 in the Data sheet. I have not read anything about ADDING 0x20 to the physical address.

I know you did say for I/O registers, it's in 0x20 to 0x3F. Where does that say so in the data sheet?

Thanks
--- Updated ---

Hi,

I don't know where your confusion is. (I already tried to explain this. And why. And example)
What's the hurdle to pass?

0x00 ... 0x1F (5 bits of address "AAAAA" in the opcode) of "lower 32 I/O register" which usually occupy the address space of 0x0020 .... 0x003F in the 16 bit address space.

Klaus
I read this again and again, 0x00 to 0x1F is 5BITS, to use in 16BIT address, it would be 0x0000 to 0x001F. That should not change at all. How does that become 0x0020 to 0x003F?

I am really missing it.

Thanks
 
Last edited:
Hi,
I thought the first 32 register
Read carefully.
You have to distinguish between "registers" amd "IO registers".
* you are right, registers start at 0x0000
* but the said "IO registers" start at 0x0020

I thought PORTB is in 0x05
As you can see in post#33:
PortB is
* 0x0025 physical address in the 16 bit address space (address value shown in brackets)
* 0x05 as (5/6 bit) address as parameter within the instruction OPCODE (address value shown without brackets)

I have not read anything about ADDING 0x20
example PortB:
0x05 vs 0x0025 --> 0x05 + 0x20 = 0x25 = 0x0025
You may use different terms for the same:
* add 0x20
* offset of 0x20
* different address mapping

Where does that say so in the data sheet?
It is written in the "instruction set manual"
Also in the datasheet. Use the PDF search "0x20 must be added" --> 7 occurances

to use in 16BIT address, it would be 0x0000 to 0x001F. That should not change at all. How does that become 0x0020 to 0x003F?
As explained in post#34.

In one case the 5/6 bits are within the 16 bit OPCODE
In the other case is uses the 16 bit OPCODE and additionally the 16 bit address.

***********
Example:
Read the PORTD (ATMEGA328P) into R16:

1)
IN R16, 0x0B // Read contents of PORTD into working register R16
16 bit OPCODE:
1011 0AAd dddd AAAA (general)

where
ddddd = R16 = 16 = 0b10000
AAAAAA = 0x0B = 0b01011

making the instruction opcode to
1011 0011 0000 1011


2)
LDS R16, 0x002B // again Read contents of PORTD into working register R16
32-bit Opcode:
1001 000d dddd 0000
kkkk kkkk kkkk kkkk (general)

where
ddddd = R16 = 16 = 0b10000
kkkk kkkk kkkk kkkk = 0x002B = 0b0000 0000 0010 1011

making the instruction opcode to
1001 0001 0000 0000

0000 0000 0010 1011

--> both do exactly the same. Again: they do exactly the same.
but
1) is a fast 16 bit instruction, only available for physical address 0x0020 ... 0x003F (aka address parameter 0x00 ... 0x1F)
2) is a slow 32 bit instruction available for the whole 16 bit address space

Klaus
 
Last edited:

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top