├── .gitignore ├── src └── blink.c ├── Makefile ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | build/ -------------------------------------------------------------------------------- /src/blink.c: -------------------------------------------------------------------------------- 1 | // Default clock source is internal 8MHz RC oscillator 2 | #define F_CPU 8000000UL 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | DDRB |= (1 << PB0); 10 | 11 | while (1) 12 | { 13 | PORTB |= (1 << PB0); 14 | _delay_ms(1000); 15 | PORTB &= ~(1 << PB0); 16 | _delay_ms(1000); 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PROGRAM_NAME = blink 2 | SRC_DIR = src 3 | BUILD_DIR = build 4 | 5 | all: $(PROGRAM_NAME) 6 | 7 | # Create the build directory 8 | $(BUILD_DIR): 9 | mkdir -pv $(BUILD_DIR) 10 | 11 | # Compile and build the program for Atmega328P 12 | $(PROGRAM_NAME): $(BUILD_DIR) 13 | avr-gcc -mmcu=atmega328p -Wall -Os -o $(BUILD_DIR)/$(PROGRAM_NAME).elf $(SRC_DIR)/$(PROGRAM_NAME).c 14 | avr-objcopy -j .text -j .data -O ihex $(BUILD_DIR)/$(PROGRAM_NAME).elf $(BUILD_DIR)/$(PROGRAM_NAME).hex 15 | 16 | # Upload the built program (hex file) to Atmega328P using USBasp 17 | upload: $(PROGRAM_NAME) 18 | avrdude -c usbasp-clone -p m328p -U flash:w:$(BUILD_DIR)/$(PROGRAM_NAME).hex 19 | 20 | # Remove build directory with all built files 21 | clean: 22 | rm -rf $(BUILD_DIR) 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Meysam Parvizi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting started with AVR programming 2 | 3 | If you want to start AVR programming and you don't know how, this repository is created for you. 4 | 5 | There are some great resources to learn AVR programming which I highly recommend them for beginners: 6 | 7 | - [8-bit AVR® Microcontrollers](https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8-bit-avr/) 8 | - [🎞️ Getting Started With AVR](https://www.youtube.com/playlist?list=PLtQdQmNK_0DRhBWYZ32BEILOykXLpJ8tP) 9 | - [🎞️ Fundamentals of Microcontrollers - Arduino bare-metal breakdown](https://www.youtube.com/playlist?list=PLNyfXcjhOAwOF-7S-ZoW2wuQ6Y-4hfjMR) 10 | - [📕 Make: AVR Programming - Elliot Williams](https://www.oreilly.com/library/view/make-avr-programming/9781449356484/) 11 | - [Code examples for the book "Make: AVR Programming"](https://github.com/hexagon5un/AVR-Programming) 12 | 13 | For this project I use Atmega328P which is an old but lovely microcontroller used in Arduino Uno. 14 | Keep in mind that Atmega328P is ***NOT RECOMMENDED FOR NEW DESIGNS*** and we are going to use it only to learn and practice AVR bare-metal programming. 15 | 16 | **If you want to use other AVR microcontrollers, you should modify the [`Makefile`](Makefile)**. 17 | 18 | [AVR-GCC](https://gcc.gnu.org/wiki/avr-gcc) is the most popular toolchain for AVR programming. I recommend you to use Linux to install AVR-GCC to compile and build AVR programs. It is easier and you will have less problems. However, Windows users can use WSL or VirtualBox to virtualize Linux. If you don't want to use Linux at all, you can use the [pre-built AVR-GCC toolchain for Windows presented by Microchip](https://www.microchip.com/en-us/tools-resources/develop/microchip-studio/gcc-compilers). 19 | 20 | - Target microcontroller: **Atmega328P** 21 | - Host OS: **Ubuntu 20.04 (Running on Windows WSL2)** 22 | 23 | ## Install AVR-GCC Compiler 24 | 25 | Ubuntu users: 26 | 27 | ```console 28 | sudo apt-get install gcc build-essential 29 | sudo apt-get install gcc-avr binutils-avr avr-libc gdb-avr 30 | ``` 31 | 32 | Windows users: 33 | 34 | Download and extract [AVR 8-Bit Toolchain (Windows)](https://www.microchip.com/en-us/tools-resources/develop/microchip-studio/gcc-compilers). Add the address of `bin` folder to your system `Path`. 35 | 36 | - [Using the GNU AVR toolchain on Windows 10](http://fab.cba.mit.edu/classes/863.16/doc/projects/ftsmin/windows_avr.html#avr-gcc) 37 | - [AVR GCC Toolchain – Setup for Windows](https://tinusaur.com/guides/avr-gcc-toolchain/) 38 | - [Make for Windows](https://gnuwin32.sourceforge.net/packages/make.htm) 39 | 40 | ## Install AVRDUDE 41 | 42 | Install AVRDUDE, an in-system programming software for AVR microcontrollers. AVRDUDE is a free and open source utility to download/upload/manipulate the ROM and EEPROM contents of AVR microcontrollers using the in-system programming technique (ISP). 43 | 44 | Ubuntu users: 45 | 46 | ```console 47 | sudo apt-get install libusb-dev 48 | sudo apt-get install avrdude 49 | ``` 50 | 51 | Windows users: 52 | 53 | Download and extract [AVRDUDE for Windows](https://github.com/avrdudes/avrdude/releases). Add the address of the folder to your system `Path`. 54 | 55 | There is also a GUI version of AVRDUDE for Windows called [AVRDUDESS](https://github.com/ZakKemble/AVRDUDESS) which can be used to program your microcontroller manually. 56 | 57 | ## Atmega328P Pinout 58 | 59 | ![Atmega328P Pinout](https://github.com/m3y54m/start-avr/assets/1549028/7c222c32-0c19-44ef-be49-052d2cd0fc68) 60 | 61 | ## The `blink.c` Program 62 | 63 | ```c 64 | // Default clock source is internal 8MHz RC oscillator 65 | #define F_CPU 8000000UL 66 | 67 | #include 68 | #include 69 | 70 | int main() 71 | { 72 | DDRB |= (1 << PB0); 73 | 74 | while (1) 75 | { 76 | PORTB |= (1 << PB0); 77 | _delay_ms(1000); 78 | PORTB &= ~(1 << PB0); 79 | _delay_ms(1000); 80 | } 81 | return 0; 82 | } 83 | ``` 84 | 85 | ## Programmer 86 | 87 | > Wait! If you have an AVR based Arduino board (Uno, Nano, etc.), you do not need a programmer! 😀 88 | > 89 | > [**Go to Arduino section**](#arduino) 90 | 91 | To transfer your program to the microcontroller you need a hardware called ***"programmer"***. [USBasp](https://www.fischl.de/usbasp/) is one of the cheap programmers available for the AVR microcontrollers. I used it for this project but if you have access to other programmers that are supported by AVRDUDE, you can use them. 92 | 93 | ![USBasp](https://github.com/m3y54m/start-avr/assets/1549028/0ef402de-c759-4e85-b45b-a7d1f495e17c) 94 | 95 | ### ISP Pinout 96 | 97 | This is the standard pinout for AVR programmers. 98 | 99 | ![ISP Pinout](https://github.com/m3y54m/start-avr/assets/1549028/017c2d6d-ee3a-41b0-8b64-752e97a389b2) 100 | 101 | ### Programmer Connection 102 | 103 | Connect the programmer to your AVR microcontroller according to this diagram: 104 | 105 | ![Programmer Connection](https://github.com/m3y54m/start-avr/assets/1549028/0efd9b1c-5292-42c6-a5ec-60286b23cdf9) 106 | 107 | ### LED Blinky Circuit 108 | 109 | This is the circuit required for our ***Blinky*** program: 110 | 111 | ![LED Blinky Circuit](https://github.com/m3y54m/start-avr/assets/1549028/c2ffe75c-f015-48a5-b35e-3d77a0dabc1d) 112 | 113 | ## Build the Program 114 | 115 | ```console 116 | avr-gcc -mmcu=atmega328p -Wall -Os -o build/blink.elf src/blink.c 117 | avr-objcopy -j .text -j .data -O ihex build/blink.elf build/blink.hex 118 | ``` 119 | 120 | or just use the `Makefile` and execute this command: 121 | 122 | ```console 123 | make 124 | ``` 125 | 126 | ## Upload the Program to Atmega328P 127 | 128 | To ensure that the USBasp programmer is detected and connected to Atmega328, verify the device signature: 129 | 130 | ```console 131 | avrdude -c usbasp-clone -p m328p 132 | ``` 133 | 134 | Upload the program: 135 | 136 | ```console 137 | avrdude -c usbasp-clone -p m328p -U flash:w:build/blink.hex 138 | ``` 139 | 140 | or just use the `Makefile` and execute this command: 141 | 142 | ```console 143 | make upload 144 | ``` 145 | 146 | **⚠️ WARNING:** 147 | 148 | **Do not play with *FUSE BITS*** if you do not know what you are doing! 149 | 150 | Fuse bits can be used to change clock source of the microcontroller and enable / disable some of its functionalities. 151 | If you do something wrong with the fuse bits you may brick your microcontroller. So read these articles before manipulating them: 152 | 153 | - [Fuse bits aren’t that scary](https://embedderslife.wordpress.com/2012/08/20/fuse-bits-arent-that-scary/) 154 | - [AVR® Fuses](https://microchipdeveloper.com/8avr:avrfuses) 155 | 156 | ## Arduino 157 | 158 | The AVR microcontroller on an Arduino board simply can be programmed by AVRDUDE without the need of any external programmer. The Arduino bootloader makes this possible. 159 | 160 | For example if you want to program an Arduino Uno (with Atmega328P on it) use this command: 161 | 162 | ```console 163 | avrdude -c arduino -p m328p -P COM10 -b 115200 -U flash:w:build/blink.hex 164 | ``` 165 | `COMx` is the Arduino boards's serial port name in Windows. In Linux it should be something like `/dev/ttyxxxx` 166 | 167 | Note that for Arduino Uno you should set `F_CPU` to `16000000UL`. 168 | 169 | For more information read this article: 170 | 171 | - [Introduction to Bare Metal Programming in Arduino Uno](https://www.hackster.io/milanistef/introduction-to-bare-metal-programming-in-arduino-uno-f3e2b4) 172 | 173 | ## My AVR Playground 174 | 175 | I have a playground repository for AVR programming where I rewrite my old AVR programs and practice my fundamental embedded programming skills. It could be beneficial for you as well: 176 | 177 | - [My AVR Programming Playground](https://github.com/m3y54m/avr-playground) 178 | 179 | To take your AVR programming skills to the next level and learn how commercial products can be updated remotely, you'll need to dive into **bootloaders**. A great place to start is with this simple AVR bootloader project: 180 | 181 | - [Simple AVR Bootloader Tutorial ](https://github.com/m3y54m/simple-avr-bootloader) 182 | 183 | ## Resources 184 | 185 | - [ATmega48A/PA/88A/PA/168A/PA/328/P Datasheet](https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061B.pdf) 186 | - [AVR Programing Using avrdude in Ubuntu](https://medium.com/@ppatil/avr-programing-using-avrdude-in-ubuntu-93734c26ad19) 187 | - [A simple LED blinking project that uses the AVR toolchain without the Arduino IDE.](https://github.com/tzhenghao/blink-ATmega328p) 188 | - [How to Build an AVR Blinking LED Circuit](http://www.learningaboutelectronics.com/Articles/AVR-blinking-LED-circuit.php) 189 | - [Standalone ATmega328p](https://doc.riot-os.org/group__boards__atmega328p.html) 190 | - [How to Program an AVR chip using a USBASP](http://www.learningaboutelectronics.com/Articles/Program-AVR-chip-using-a-USBASP-with-10-pin-cable.php) 191 | - [Programing AVR on Ubuntu with USBasp for beginers](https://fos.cmb.ac.lk/esl/programing-avr-ubuntu-14-04-usbasp/) 192 | - [AVR-GCC](https://gcc.gnu.org/wiki/avr-gcc) 193 | - [AVRDUDE - AVR Downloader/UploaDEr](https://github.com/avrdudes/avrdude) 194 | - [AVRDUDESS: A GUI for AVRDUDE](https://github.com/ZakKemble/AVRDUDESS) 195 | - [AVR® Fuse Calculator](https://www.engbedded.com/fusecalc/) 196 | - [Attach a USB device to WSL](https://learn.microsoft.com/en-us/windows/wsl/connect-usb) 197 | 198 | --------------------------------------------------------------------------------