Simple Hello World! application on AVR ATmega16.
- ATmega16A
- 220𝛀 Resistor
- LED
- Breadboard
- avrdude compatible AVR programmer (I used usbasp)
- VSCode + Extensions(C/C++, CMake, Commands)
- CMake
- avr-gcc
- avrdude
Notes:
- Using Port C7 for emiting LED is arbitrary
Note that I used Ubuntu as my OS, so all commands here are Ubuntu commands.
-
Installation:
- install VSCode and extensions listed above. If you are a rookie in vscode see Install Visual Studio Code extensions
- install avr-gcc, avrdude and some auxiliary apps:
sudo apt-get install binutils gcc-avr avr-libc uisp avrdude
-
Project Creation:
- Create a directory for your project
- open VSCode and click **Open Folder..." on Get started page.
- Select created directory.
- Hit Ctrl+N to create a new file.
- type your C Code. (Use my code in main.c as a start)
-
Config C/C++ Extension:
- In the Command Palette (Open using Ctrl+Shift+P) type Edit Configuration. You should see an item titled C/C++: Edit Configurations(UI). Click on it.
- You should provide some information:
- Compiler path: You can find via
which avr-gcc
. It is normally pointed to /usr/bin/avr-gcc - IntelliSense mode: Select gcc-x86(legacy)
- Defines: for current project I used Atmega16. So I entered AVR_ATmega16A. Mind the underscores! You can find your chip on https://www.nongnu.org/avr-libc/user-manual/using_tools.html
- C standard: Select c99
- C++ standard: Select c++11
- Compiler path: You can find via
-
Config Task Extension:
- Open Command Palette and type Tasks: Configure Task select proper result and then select Create tasks.json file from template and then select Others
- Change the code to something like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "MakeAndFlash",
"type": "shell",
"command": "cd build && make flash",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- Config Commands Extension:
- Open Command Palette and type Commands: Edit Configuration
- Change opened file to something like this:
{
"commands": [
{
"command": "workbench.action.tasks.build",
"text": "Build & Flash",
"tooltip": "Build the project & flash To uc",
"color": "#FFCC00"
}
]
}
3. Open Command Palette and Search **Commands: Refresh** and click on it. now you have see a button on status bar labled **Build & Flash** or whatever you selected.
- Create Workspace Settings file:
- From menu select File -> Preferences -> Settings
- There is two tab on the page: User, and Workspace. Select Workspace and on the search bar enter CMake and select CMake Tools under Extensions
- Scroll Down and find CMake: Configure Settings then click Edit in settings.json
- Here you can set some global configuration variables for using in CMake.
- Change it to something like:
{
"cmake.configureSettings": {
"MCU": "atmega16a",
"F_CPU": "8000000",
"BAUD": "9600",
"AVR_PART": "m16"
}
}
- Create CMake file:
- Create a file on root directory of your project name it: CMakeLists.txt
- You can download it from my codes for starting point from here
- RUN!:
- Connect your programmer to your system.
- Hit Ctrl+Shift+B or click Build & Flash in the status bar.
- Congratulations! you have made your fist embedded application!
Preview:
So my challenge was mixing AVR blink project with STM32 version as the code should be compiled on both without any code change!
- STM32 Development Board (I used a board based on STM32F107VC)
- STM32 Debugger & Programmer (ST-LINK or J-Link)
- VSCode + Extensions(C/C++, CMake, Commands, Cortex-Debug)
- STM32CubeMX
- CMake
- arm-gcc
- Jlink/stlink programmer software
- Setup Vscode: You can use some tutorials on the web. I suggest this one.
- Create STM32CubeMX Project: Project creation in STM32CubeMX is not so hard! Click proper pin and select GPIO_Output for enabling a pin as output pin. On the Clock Configuration tab you can calculate divider and multiplier values for your desired frequency or let CubeMX Calculate it for you. for the second option just type your desired frequency in HCLK (MHz) field and hit enter! Save your project and hit GENERATE CODE on top right corner of application.
- Mixing Codes: Now you have your first ready to code stm32 project! File structure of this project is not suitable for merging with my previous projects so I changed it! also note that compiling and linking file inclusion will be checked on cmake!
- Profile Selection: I added a PROFILE setting to settings.js, so then created a list of hardware profiles there. Now based on hardware and project i want to build just will update this file and cheers!
Preview: