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.

[SOLVED] How to use SPM instruction with ATmega328

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 am still reading the datasheet of ATmega328. I am stuck on page erase, page write of program memory(flash) using instruction SPM. I want to find ASSEMBLY CODES performing each of those below.
SPM wr.jpg


I just want to see the assembly codes how to perform on the memory shown in (i) to (iv).

I know C code can do it transparent to all these. I just want to know how to use SPM instruction.

Thanks.

Alan
 

Hi,

Problem is when someone later on come in, accidentally add some lines that change the Z, the program will be screwed because it only subtract the page size. Loading the original address to Z will avoid all the potential problem.
If someone "accidentally" add some code that change the Z .... no matter what you do from this point on, writing the page will be corrupt, the softwarer won´t work anymore. It does not matter how good you correct the Z-Pointer after that.

And "who" can be the someone that alters "your" source code and compiles it and programs it on your chip?

But no one stops you from writing your code in your own style. No one demands you to suptract. It´s an example.

Klaus
 

Hi

I think I got the feel of SPM and RWW, NRWW etc. I don't want to get too deep as I am not planning to write in assembly. I just want to dig deeper than other people for the hell of it.

I am on the last cpt of interest which is Parallel and Serial programming. I think the Parallel programming is usually not being used, most I heard from people is they use SPI or USB to program.

My question is do I need to read the serial programming before I really move onto my Elegoo kit to learn and play with the ATmega328?

I am glad this reading is coming to an end.

Thanks
 

Hi,
I am on the last cpt of interest which is Parallel and Serial programming. I think the Parallel programming is usually not being used, most I heard from people is they use SPI or USB to program.
SPI:
indeed the programming interface is called ISP (in system programming interface) but it often (not always) the same lines as the SPI interface, and it has the protocol (HW) as the SPI interface (full duplex, synchronous, MSB first, 8 bit)
The preferred programming method.

USB:
I never did update an ATMEL via it's USB interface. I'm not even sure if this is possible.
Some programmer hardware uses USB, but this USB has nothing to do with AVR_USB at all.

Parallel:
Non in system. Thus you need to disassemble the microcontroller from your PCB to program it. Not useful method.
It may be used for fast bulk programming before assembling ... or special programming features (like resetting the "disable_ISP_fuse")

Klaus
 

    Alan8947

    Points: 2
    Helpful Answer Positive Rating
Thanks Klaus

I just read through the ISP quickly and get on with the Elegoo kit. It is USB interface, but it has another IC that connects to the USB connector. So it likely talk to USB and translate to ISP to program the ATmega328.

I am excited to move onto the real thing. I hope the time I spent on reading the datasheet will help me.

Thanks
 

Since the ATMEGA328p has no USB ...
you need to read the Elegoo kit documentation on this regard.

If the Elegoo Kit is Arduino compatible, then there is a good chance that the ATMEGA comes with bootloader and it neither gets programmed with ISP.nor with parallel (nor USB) but "self programming" via UART communication = firmware (bootloader) solution.

Still it could be possible to design a "USB to SPI (ISP) programmer" to put the code on the ATMEGA.

Klaus
 
Thanks Klaus

I since packed up all the datasheets and started slowly on the Elegoo kit. I've gone through 2 programs on blinking LED and RGB LED dimming and all.

I studied C++, so it's not hard at all. But my question is my grand daughter never have any programming experience. This does not seem to be easy to pick up for her. There are all the #define, function like Digitalwrite(port#, mode), for loops etc. This is not something anyone can pick up unless they have at least a class of programming.

I thought they advertised that anyone can learn, I bought it with intention to teach her. This is only the 2nd program and I feel that's a lot for someone with no programming experience. I want to see your opinion.

As for me, it is interesting. I definitely going to get the USB-SPI programmer. The board comes with the kit is Arduino UNO R3, it has the ICSP connector( 6pin with SPI signals). I can use the USP-SPI programmer to program(with some limitations).

Any suggestion which USB-SPI programmer I should buy? There's so many, don't know where to start.

Thanks
 

Hi,

from the previous posts - with all respect - it seems you hear only what you want to hear.

So I write it most short and clear:
For an Arduino Nano you don't need a (hardware) programmer..
Again: You don't need one.

But I guess you still insist to buy one. In this case buy an AVRISP MKII or any other USB to ISB prgrammer. Nothing expensive.

Klaus
 

Hi,

from the previous posts - with all respect - it seems you hear only what you want to hear.

So I write it most short and clear:
For an Arduino Nano you don't need a (hardware) programmer..
Again: You don't need one.

But I guess you still insist to buy one. In this case buy an AVRISP MKII or any other USB to ISB prgrammer. Nothing expensive.

Klaus
I don't recall I ever ask nor even know what and how to program the ATmega328. I was concentrated on the datasheet. This is the first time I even know about USB to SPI programmer.

I don't recall I ever talk to you about programmer, and you ever said I don't need programmer. Are you assuming that I can connect UNO R3 directly to USB? That I don't need to buy an USB-SPI programmer? This is NOT what I am asking. I already know that as I did 2 programs already.

I am thinking ahead when I design using ATmega328, it's not going to be using the UNO board, it's going to be a custom pcb that I have to program on my own through the SPI port. How can I program without a USB-SPI programmer? I am only thinking ahead to play with the UNO board with USB-SPI programmer to get use to doing that. I know I can easily program the UNO by directly connect USB cable from laptop to the UNO. I want to play it safe to make sure I can program the UNO with the programmer first to make sure I know what I am doing.


Actually my post is not even asking about the programmer, it's more the opinion whether this kit is too much to teach my granddaughter as she never learn programming before. People told me it's easy for people to learn even they never learn programming. I look at only 2 program and I just cannot see how anyone without learning programming before can easily play with this kit.

Thanks
 
Last edited:

Hi,

To your post, step by step:
I don't recall I ever ask nor even know what and how to program the ATmega328.
In post#22, #23, #24, #25 .. we both did discuss about how to program the AVR.


I was concentrated on the datasheet. This is the first time I even know about USB to SPI programmer.
I don't think the datasheet talks about an USB-to-SPI programmer at all.
But you did in posts#24 ff


I am thinking ahead when I design using ATmega328, it's not going to be using the UNO board, it's going to be a custom pcb that I have to program on my own through the SPI port
Yes, thinking. The problem with thinking is, that we don't see this. We only see what your are communicating.
* The thread topic is about "SPM command" --> it has nothing to do with ISP, nor with AVR_USB (datasheet), nor with parallel programming, SPM is how a bootloader programs the chip.
* Then you say you focus on the datasheet. --> but still talk about USB stuff that is not included in the datasheet.
* Then you talk about the "Elegoo kit" --> I don't know this. So I learned from you that it includes an Arduino Uno. It's not a plain ATMEGA32p, but it comes with additional hardware (USB-to-serial IC) and with a bootloader software already burnt into the AVR. Since I am not familiar with all this I recommended you to read the Elegoo documentation.
* Then you talk that your aim is to teach your grand daughter using the Elegoo kit --> for this (if you read it's documentation or my post#25) you don't need additional programmer hardware, nor do you need to design your own board.

So we (the forum members) continously have to guess what you currently focus on
* plain datasheet reading
* Elegoo kit
* teaching your grand daughter
* designing your own AVR hardware

Are you assuming that I can connect UNO R3 directly to USB? That I don't need to buy an USB-SPI programmer? This is NOT what I am asking. I already know that as I did 2 programs already.
Until now - against our recommendations - you communicated only about "theory, datasheets, you want to learn everything before you start using the hardware". So when you wrote you "did 2 programs already" I assumed (and still am not sure) you did it the same way: theory only, without using the real hardware. You also never mentioned what IDE, what compiler .... you used for your "2 programs".

How can I program without a USB-SPI programmer?
(new information: your custom AVR design)
I assume you have the Elegoo kit, with the Arduino Uno. If so, you can use this Arduino to program your AVR on your custom made design.

I am only thinking ahead to play with the UNO board with USB-SPI programmer to get use to doing that.
I'm not sure you understand:
* There is no "USB-SPI" programmer on the Arduino Uno
* you don't need an "USB-SPI" to program the Arduino Uno

Actually my post is not even asking about the programmer
I use the pdf serch engine quite heavily. So when looking for your questions, I search for a "?". In post#26 there is only one question mark ... for the question which programmer to buy.


it's more the opinion whether this kit is too much to teach my granddaughter
The key to success in my eyes is to make her curious about electronics. Let her ask questions, let her fing her own way, let her have fun.
Be flexible: Adjust on her, don't force her to adjust on your way.
And honestly (not meant offending) from all your discussions here in this form, I miss this flexibility.

Thus my recommendation: Just let her play around. Maybe the Elegoo kit comes with a good tutorial, or show her how to find online tutorials, let her play around, let her make mistakes, let her make her own good experience.

Klaus
 

I looked at AVRISP mkII, there are all different brands that look the same. Any particular brand to buy?
--- Updated ---

Hi,

To your post, step by step:

In post#22, #23, #24, #25 .. we both did discuss about how to program the AVR.



I don't think the datasheet talks about an USB-to-SPI programmer at all.
But you did in posts#24 ff



Yes, thinking. The problem with thinking is, that we don't see this. We only see what your are communicating.
* The thread topic is about "SPM command" --> it has nothing to do with ISP, nor with AVR_USB (datasheet), nor with parallel programming, SPM is how a bootloader programs the chip.
* Then you say you focus on the datasheet. --> but still talk about USB stuff that is not included in the datasheet.
* Then you talk about the "Elegoo kit" --> I don't know this. So I learned from you that it includes an Arduino Uno. It's not a plain ATMEGA32p, but it comes with additional hardware (USB-to-serial IC) and with a bootloader software already burnt into the AVR. Since I am not familiar with all this I recommended you to read the Elegoo documentation.
* Then you talk that your aim is to teach your grand daughter using the Elegoo kit --> for this (if you read it's documentation or my post#25) you don't need additional programmer hardware, nor do you need to design your own board.

So we (the forum members) continously have to guess what you currently focus on
* plain datasheet reading
* Elegoo kit
* teaching your grand daughter
* designing your own AVR hardware


Until now - against our recommendations - you communicated only about "theory, datasheets, you want to learn everything before you start using the hardware". So when you wrote you "did 2 programs already" I assumed (and still am not sure) you did it the same way: theory only, without using the real hardware. You also never mentioned what IDE, what compiler .... you used for your "2 programs".

(new information: your custom AVR design)
I assume you have the Elegoo kit, with the Arduino Uno. If so, you can use this Arduino to program your AVR on your custom made design.


I'm not sure you understand:
* There is no "USB-SPI" programmer on the Arduino Uno
* you don't need an "USB-SPI" to program the Arduino Uno


I use the pdf serch engine quite heavily. So when looking for your questions, I search for a "?". In post#26 there is only one question mark ... for the question which programmer to buy.



The key to success in my eyes is to make her curious about electronics. Let her ask questions, let her fing her own way, let her have fun.
Be flexible: Adjust on her, don't force her to adjust on your way.
And honestly (not meant offending) from all your discussions here in this form, I miss this flexibility.

Thus my recommendation: Just let her play around. Maybe the Elegoo kit comes with a good tutorial, or show her how to find online tutorials, let her play around, let her make mistakes, let her make her own good experience.

Klaus
Hi Klaus

Sorry about the miss communication. I stopped reading the datasheet. Now it's the next phase, all hands on. The kit use Arduino IDE 2.21, so it's very standard. I spent some time getting familiar with the IDE, library and commands.

I actually plug in LEDs, using UNO to breadboard, run the programs and changing the blinking time and all. The program even use PWM to slowly dime one LED and brighten the other one.

Maybe it's a mistake to continue on this thread. That I should start a new thread.

I feel reading the data sheet does give me more insight, like I know when they write to light up those LEDs, it's on PortD, also the function controlling the bit can be defined as output, input, input with pullup. I can understand what they are doing because I read the datasheet and know how to define the port pin.

I did spent a few months studying and programming C++, so I have no problem understanding so far. I just afraid it is too difficult for someone that never do any programming before, all the definition, constants, functions.

Maybe I should just end this thread and if I have question, I'll start a new thread to avoid confusion.

Thanks
 
Last edited:

Hi

Thanks for this information.

Maybe it's a mistake to continue on this thread. That I should start a new thread.
I agree in this, since the current discussion does not fit the headline of this thread anymore.

Mind: A forum discussion is not meant as "two people discuss with each other", but it should be a public discussion with several people involved and it is meant that other members (not necessarily involved in the discussion) gain from the informations. So, to find the informations .. the discussion topic should match the headline.

Klaus
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top