What to know about avr? - Page 1
Forum section: electronics / general

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 08:28:42 am
content: Hey guys, I’m familier with arduinos and avr procesors, and want to learn more about it, but when I google about it it’s always the same beginer basic tutorials, what advanced topics (like fuses programming) I would need to know to become better at it and must know knowladge?

author: brucehoult
author_rank: Super Contributor
timestamp: December 17, 2019, 08:57:27 am
content: Have you tried diving into the data sheet? It's a relatively simple device.http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfThe only problem there maybe is although it does describe the instruction set it's a very very brief description with just one line per instruction. Ok, maybe, if you already have experience with other CPUs.

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 09:12:31 am
content: Quote from: brucehoult on December 17, 2019, 08:57:27 amThanks for the tip. Will look up to it

author: NivagSwerdna
author_rank: Super Contributor
timestamp: December 17, 2019, 09:39:37 am
content: Which rabbit hole do you want to go down?For hardware... The datasheet or GoogleFor software.... You can read the Arduino wiring source on github and there is an AVR assembly video series on YT if you haven't done assembly before and want to learn bit twiddling in asm.

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 09:50:19 am
content: Quote from: NivagSwerdna on December 17, 2019, 09:39:37 amMainly I am interested in developin priject using this simple mcu, and I want to learn as much as possible about it. After intence googling session (joke intensifies), I learned about fuses, programming options, debuging, clock selection and lockingnof chips, I’m interested in that kind id stuff, I just want to know as musch as possible.

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 17, 2019, 10:30:44 am
content: You might also consider AVRs with native USB, like ATmega32U4 (Arduino Leonardo and Pro Micro clones).They can be programmed on bare metal with open source tools: avr-gcc, avr-libc, and avrdude.The USB side opens up especially Human Interface Devices, i.e. keyboards, mice, and joysticks, that work on all machines with USB ports without installing any drivers.

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 11:09:07 am
content: Quote from: Nominal Animal on December 17, 2019, 10:30:44 amI do own leonardo, and used it before as a joystick, thank tou for offer. But as I almost never need HID, I’m leaning to simple atmega16, atmega32 or smth like that

author: westfw
author_rank: Super Contributor
timestamp: December 17, 2019, 11:18:18 am
content: The datasheet is the big thing.The detailed instruction set manual is here:[size=78%]http://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 17, 2019, 12:09:34 pm
content: Programming an AVR on bare metal (using only open-source tools like avr-gcc, avr-libc, and avrdude) would force you to learn how to use the peripherals and chip features yourself, instead of just their abstractions in the Arduino environment. You'd definitely need to understand most of the stuff in the datasheet. Interfacing to various digital sensors (for example temperature via DS18x20), and to things like neopixels (WS2811/WS2812/SK6812), small OLED displays, or TFT modules, using your own code instead of ready-made libraries also forces you to understand how to find and decipher information provided in other datasheets.This approach does require that you are not satisfied at "this works but I don't know why", but write your own code, and keep at it until you understand why it does not work, and how to fix it so that it works.

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 12:39:17 pm
content: Quote from: Nominal Animal on December 17, 2019, 12:09:34 pmWell this is interesting post, I will look into how theese works. Thank you!

author: NivagSwerdna
author_rank: Super Contributor
timestamp: December 17, 2019, 12:58:41 pm
content: I enjoyed thesehttps://www.youtube.com/user/DrZAP42/playlists

author: VeryFastSnail
author_rank: Contributor
timestamp: December 17, 2019, 03:15:03 pm
content: Quote from: NivagSwerdna on December 17, 2019, 12:58:41 pmGoing to check those out. Thank you, looks very informative

author: brucehoult
author_rank: Super Contributor
timestamp: December 17, 2019, 03:38:24 pm
content: Quote from: NivagSwerdna on December 17, 2019, 12:58:41 pmomg. He spends 25 minutes explaining how to modify the Java source code for the Arduino IDE to accept .s files, and then rebuild the IDE.

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 17, 2019, 03:42:05 pm
content: Quote from: brucehoult on December 17, 2019, 03:38:24 pm

author: NivagSwerdna
author_rank: Super Contributor
timestamp: December 17, 2019, 03:54:32 pm
content: Quote from: brucehoult on December 17, 2019, 03:38:24 pmI know and I'm not sure it works anymore in later versions but you have to admit... it's got some style to do Arduino in assembler.

author: Nominal Animal
author_rank: Super Contributor
timestamp: December 17, 2019, 04:17:58 pm
content: Quote from: NivagSwerdna on December 17, 2019, 03:54:32 pmYup, like racing stripes on a car to make it go faster.You do know thatasm volatile ("\n.include \"/full-path-to/source.s\"\n");in the.inosketch, in the global scope (outside any function body), does the same thing?

author: rx8pilot
author_rank: Super Contributor
timestamp: December 17, 2019, 04:44:22 pm
content: My strategy on AVR (and most other tech projects) is to review the fundamentals from the manufacturer - ie the datasheet. Then get applications specific overviews from the casual Google/YouTube searches to gain an understanding of what is generally possible and the rough magnitude of the complexity. At this point, in the context of AVR you will have discovered that your C skills need to be fairly sharp, your understanding of the typical IDE's (AVR Studio or similar) needs to be pretty good, your understanding of the basic electrical side of things needs to be decent. For example - you need to know enough about analog electronics and ADC's if you plan to design/layout a PCB and have a chance of getting usable results.A newcomer could spend days-weeks-years getting through all of this. If you already have considerable experience in CS, C, electronics, etc......easy. If your only experience is with Arduino and you are hoping to build something complex with high-reliability - it could take years.After that initial self-analysis....I dive head-first in to a project that I have designed. Something with modest complexity, but something that I can be very confident is in the range of capability for the device I am designing around. It will very quickly reveal what skills I am needing to gain. YouTube-Stack Exchange-Books-Datasheets-forums to the rescue.HINT: When you ask a question to a forum group -A: Make sure you ask a clear and specific questionB: Make sure you brief the readers on what you have done so far to find an answer (If you have done nothing, you should expect nothing from everyone else)

author: rstofer
author_rank: Super Contributor
timestamp: December 17, 2019, 04:45:19 pm
content: You can learn a lot about AVR and code by simply looking at the Arduino library sources.For example, the servo library code for the avr is here:C:\Program Files (x86)\Arduino\libraries\Servo\src\avrIt's all right there for the looking.As to assembly language, you should probably get used to programming bare metal in C and then work down.You can search the arduino directory tree for files ending in .S if you want to see assembly code. I have TeensyDuino installed so here is the location of one file:C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\TeensyThreads

author: rstofer
author_rank: Super Contributor
timestamp: December 17, 2019, 05:02:22 pm
content: A long time ago i was playing with the ATmega128 - it's a great chip with lots of peripherals and pins. I was doing all the work with the Linux version of the tools so gedit as the editor, avr-gcc as the compiler/linker, avr-libc as the C libraries, avr-dude as the serial programmer and Makefiles. It was an ideal platform for writing code. These very same tools (Windows version), except the editor, are used by the Arduino IDE to program the Arduino chips.My board wasn't anything like this but the Olimex version will work:https://www.olimex.com/Products/AVR/Proto/AVR-PX128A1/This is the board I was using:https://www.olimex.com/Products/AVR/Proto/AVR-PX128A1/Look at the number of pins on that board!This is about as far from Arduino as you can get and still be in the AVR lineup. Which brings up a question: Why mess around with ATmega, the world has moved on to ARM and derivatives. Yes, the ATmega is easier but it's about hit the end of the road now that ARM chips are so cheap. Better yet, the Cypress PSOC line, especially the one with dual ARM cores. Now we're getting somewhere! The PSOC infrastructure is fun to play with.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 17, 2019, 05:46:34 pm
content: The "professional grade" approach has mostly been mentioned: read the datasheet. But also, the manual where applicable, which contains most of the programming info; the datasheet contains device-specific info, characteristics and all that, but less info about programming. (Example, I currently have both XMEGA-D3 docs open. The m328 seems to have just one main doc, which is nice.)Don't be afraid to read the headers, either, cryptic though they are. #include <avr/io.h> fans out based on which CPU type is selected (according to the compiler, in turn from your project settings), which then includes e.g. iom328p.h, which contains all the register defs and structs. The naming conventions are usually straightforward and complete, though I've found some omissions and corrections in the XMEGA I'm using, and the way they represent group options seems odd. Many times I've had to search for some define I'm looking for (or use the IDE's autocompletion to the same end).The newer MEGAs are using peripheral structures similar to the now older XMEGA family, so if you're migrating, keep that in mind -- there are differences between the older, simpler, easy to use MEGA328 and such, and newer ones.Product page for '328:https://www.microchip.com/wwwproducts/en/ATmega328PEverything can be found under documents; the appnotes are a gentler introduction including examples. For other, gentler introductions, perhaps others can provide examples, or perhaps you can find some good pages or videos by searching?Tim

author: westfw
author_rank: Super Contributor
timestamp: December 18, 2019, 12:30:16 pm
content: (The arduino IDE has supported .S files for quite a while now. Use the capital S to get preprocesssing of symbols like PORTB.)

author: madires
author_rank: Super Contributor
timestamp: December 18, 2019, 12:47:01 pm
content: Besides reading the datasheet I'd recommend to have a look at the application notes. There are a lot of them for specific topics.

author: brucehoult
author_rank: Super Contributor
timestamp: December 18, 2019, 01:59:53 pm
content: Quote from: westfw on December 18, 2019, 12:30:16 pmYeah, those videos are getting on for six years old.

author: rstofer
author_rank: Super Contributor
timestamp: December 18, 2019, 03:11:19 pm
content: But first, you need a project. A reason for reading all that stuff. Some application that is worthy of the effort.Break your project down into modules (timers, PWM, port IO, etc) and tackle each component by itself. If you don't mind creating your own header(s), you can simply rip what you need and add your own definitions. When you are done, you actually OWN the code - not so much in the legal sense but in the sense of understanding.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: December 18, 2019, 03:13:46 pm
content: Quote from: brucehoult on December 18, 2019, 01:59:53 pmI watched them when they were originally published; I liked the creator's style even though I was never going to do any ASM myself (A year or so as a commercial Z80 assembly programmer was enough for me!).Sadly he is no longer with us so don't expect an update.

author: rx8pilot
author_rank: Super Contributor
timestamp: December 18, 2019, 03:49:22 pm
content: Quote from: rstofer on December 18, 2019, 03:11:19 pmExactly this.Creating an actual mission is remarkably motivating and productive.

author: macboy
author_rank: Super Contributor
timestamp: December 18, 2019, 04:54:20 pm
content: +1 on having a project interesting to you. This is the motivator to keep on learning, experimenting, coding, and tweaking until it's right.+1 on getting the datasheet and studying it. That is how I cut my teeth on PIC micros way back. Note however that unless you plan to write in assembly code, then ignore the entire large section about the instruction set. If you aren't sure, then the answer is resoundingly "NO". Chances are you will write in C/C++ etc. Let the compiler worry about the instruction set! You need mainly to know aboutperipherals, interrupts, and I/O.If you are sticking with Arduino, do yourself a favor and install a good IDE like Eclipse. You can use it just as an external editor, continuing to use Arduino IDE to build/compile and upload, or you can install an add-on and use it independently of the Arduino IDE. Either way, one main benefit this provides is a powerful editor which will index and cross-reference all the code, including the Arduino sources, libraries you are using, and your own sketch files and other source files. Without that, coding in Arduino can make you feel lost and confused. Ever wonder which code actually calls that "loop()" function over and over (and what else is it doing?), or how millis() or delay() or digitalWrite() is implemented? It's so easy to look at that code once you are inside a proper cross-referencing IDE. Then you will start to understand what is - and isn't - happening on your micro, outside of your sketch. You also absolutely must look at the source for any library that you will use. Partly, this is because most of them are poorly documented. Mostly, it is so that you understand what hardware resources are being used by that library. You can't simultaneously use two libraries which both depend upon Timer2, for example. Some are coded so inefficiently that you might choose not to use them. For example, a very popular infra-red remote control library sets up a timer to sample the input every 50 microseconds. That 20000 interrupts per second, to look for IR signals that might be minutes or hours apart. The MCU spends a significant portion of clock cycles servicing that interrupt for no good reason. If you need high performance elsewhere, such a library might frustrate your efforts.

author: rstofer
author_rank: Super Contributor
timestamp: December 18, 2019, 06:12:58 pm
content: Windows users...There is a plug-in Microsoft Visual Studio and it works very well. Visual Studio is a standard environment for application development so a lot of people already have it and know how to use it.https://www.visualmicro.com/Microsoft Visual Studio Code Community Edition is a very sophisticated IDE which is much like the editor component of Visual Studio above. There is a Microsoft Arduino plug-in that allows Arduino development. It's going to take a bit of time wandering through the menus before you tumble to View->Command Pallete. This is where you set up the board and port. The normal Arduino IDE must be installed in order to have the compiler/linker and most of the example programs. The only gripe I have with VS Code is that it won't print files - none of them. I have to open them with Notepad++ and print them from there. Despite being highly annoyed with the lack of print capability, I suspect this is the IDE I'm going to sue.Why dump the Arduino IDE? The STUPID choice of colors for error messages does it for me! Yes, after wandering around Google, it is possible to figure out how to fix the colors but it's a PITA that shouldn't be unnecessary.I have been using VS Code to write VHDL because a tutorial series I was working used it. It's a really nice IDE - I think I'll stay with it. BTW, I don't know what code snippets are included for Arduino but in VHDL mode, the IDE will lay down chunks of code that are repetitive and boring to type.I haven't mastered column editing (changing an entire column of text) nor any of the other features. I don't need the features but it's kind of cool that they are there when I come across a need.If you are not using the Arduino bootloader and you want to use Makefiles (and you probably do), VS Code has a plug-in for that as well. This means you can write any code you want for the avr and use the Arduino tools (avr-gcc, avr-libc, avr-dude) to build and download the binary. When I was fooling around with the ATmega128, the Arduino hadn't even been invented. Everything I did was done by way of a Makefile.Using avr-dude without the Arduino bootloader is probably an advanced topic but it is all over Google.