Programming AVR Chips Using Visual Studio Code
by BavlyS in Circuits > Microcontrollers
19152 Views, 7 Favorites, 0 Comments
Programming AVR Chips Using Visual Studio Code
![pic.jpg](/proxy/?url=https://content.instructables.com/FDI/DQYP/KM1408YH/FDIDQYPKM1408YH.jpg&filename=pic.jpg)
![2021-03-08 12_53_59-Makefile - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/FEH/1NQ0/KLZ60JTW/FEH1NQ0KLZ60JTW.png&filename=2021-03-08 12_53_59-Makefile - blinkproject - Visual Studio Code.png)
Visual Studio code is one of the powerful IDEs you can work with. You may want to program your AVR Chips using VS Code. Fortunately, it is easy to setup your environment to accomplish this task. Here I'am using Arduino as ISP to program the AVR chips on a bread board.
Supplies
Hardware:
- AVR Chip you will program, here we will program ATmega328P with external 8MHZ oscillator.
- 8 MHZ crystal oscillator and two 15pF capacitor
- 4 220 ohm resistors
- 4 LEDS
- A Programmer. (we will use an Arduino as ISP )
Software:
- Visual studio code
- AVR D
Starting a New Visual Studio Code Project
![2021-03-06 11_01_38-Window.png](/proxy/?url=https://content.instructables.com/F4S/TOTR/KLWB8D59/F4STOTRKLWB8D59.png&filename=2021-03-06 11_01_38-Window.png)
![2021-03-08 00_39_52-Window.png](/proxy/?url=https://content.instructables.com/F5K/9WQU/KLZ602EQ/F5K9WQUKLZ602EQ.png&filename=2021-03-08 00_39_52-Window.png)
![2021-03-08 00_41_48-C_C++ Configurations - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/FRR/J0IY/KLZ602F5/FRRJ0IYKLZ602F5.png&filename=2021-03-08 00_41_48-C_C++ Configurations - blinkproject - Visual Studio Code.png)
- Start by opening a new window in VS code.
- Select file--->New Folder
- Press select folder, after choosing your project folder.
- Select File-->New File
Name it blink.c
- copy the following code into the blink.c file
#define F_CPU 8000000UL
#ifndef __AVR_ATmega328P__
#define __AVR_ATmega328P__
#endif
#include
#include
int main(void)
{
//connecting led on port B
DDRB |= (1< while(1)
{
_delay_ms(1000);
PORTB ^= 1< }
return 0;
}
- Now press F1, select C/C++ :edit configurations GUI
Change the compiler path to avr-gcc location: if Arduino is present on you pc this will probably be the path:
"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-gcc"
- Scroll down and change IntelliSense mode to gcc-x86(legacy)
Configuring Make File for Compiling and Burning the .hex File to the AVR Chip
![2021-03-08 11_41_31-Makefile - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/F6R/N9LT/KLZ60E7N/F6RN9LTKLZ60E7N.png&filename=2021-03-08 11_41_31-Makefile - blinkproject - Visual Studio Code.png)
![2021-03-08 11_42_54-Makefile - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/FCM/NJV8/KLZ60E88/FCMNJV8KLZ60E88.png&filename=2021-03-08 11_42_54-Makefile - blinkproject - Visual Studio Code.png)
In the make file we will edit some lines to match our project:
- On the second line match the MAIN_APP to your match your main c file name (blink).
- On the fourth line change the MAIN_HEX_PATH to match your project's folder(where your blink.c exists), as shown in the image.
This is the contents of the makefile:
#Main application file name
MAIN_APP = blink
#Main hex file path in windows format MAIN_HEX_PATH =
# Compiler and other Section CC = avr-gcc
OBJCOPY = avr-objcopy.exe
AVRDUDE := avrdude -C "C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"
#Options for avr-gcc CFLAGS = -g -Os -o
#Linking options for avr-gcc LFLAGS = -Os -mmcu=atmega328p -o
#Options for HEX file generation HFLAGS = -j .text -j .data -O ihex
#Options for avrdude to burn the hex file
#MMCU model here according to avrdude options
DUDEFLAGS = -c
DUDEFLAGS += avrisp
DUDEFLAGS += -p
DUDEFLAGS += m328p
DUDEFLAGS += -P
DUDEFLAGS += COM3
DUDEFLAGS += -b
DUDEFLAGS += 19200
DUDEFLAGS += -U
flash:w:$(MAIN_HEX_PATH):i
# Sources files needed for building the application
SRC = $(MAIN_APP).c
SRC +=
# The headers files needed for building the application
INCLUDE = -I.
#INCLUDE +=
# commands Section Burn :
Build $(AVRDUDE) $(DUDEFLAGS)
Build : $(MAIN_APP).elf
$(OBJCOPY) $(HFLAGS) $< $(MAIN_APP).hex
$(MAIN_APP).elf: $(MAIN_APP).o
$(CC) $(SRC) $(INCLUDE) $(LFLAGS) $@
$(MAIN_APP).o:$(SRC)
$(CC) $^ $(INCLUDE) $(CFLAGS) $@
Configure Task File
![2021-03-08 12_06_02-tasks.json - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/FVQ/8H2G/KLZ60F2O/FVQ8H2GKLZ60F2O.png&filename=2021-03-08 12_06_02-tasks.json - blinkproject - Visual Studio Code.png)
![2021-03-08 13_03_13-Makefile - blinkproject - Visual Studio Code.png](/proxy/?url=https://content.instructables.com/F85/NB8Q/KLZ60L0B/F85NB8QKLZ60L0B.png&filename=2021-03-08 13_03_13-Makefile - blinkproject - Visual Studio Code.png)
- Press F1
- press tasks: configure tasks(choose any task)
- modify as shown in the image rename the label as you like, here we have chosen Build and burn App
- Notice that make program must be installed for the command to work and specify the path
Now you can press Terminal from the task bar then Run Task and run the task that you have configured.