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
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:

  1. AVR Chip you will program, here we will program ATmega328P with external 8MHZ oscillator.
  2. 8 MHZ crystal oscillator and two 15pF capacitor
  3. 4 220 ohm resistors
  4. 4 LEDS
  5. A Programmer. (we will use an Arduino as ISP )

Software:

  1. Visual studio code
  2. AVR D

Starting a New Visual Studio Code Project

2021-03-06 11_01_38-Window.png
2021-03-08 00_39_52-Window.png
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
    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:

    1. On the second line match the MAIN_APP to your match your main c file name (blink).
    2. 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
    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.