Skip to content

pneumus/Atmel-AVR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

279 Commits
 
 
 
 

Repository files navigation

Embedded code samples for Atmel AVR microcontrollers

How to setup the environment?


Install Prerequisites

Arch Linux
    sudo pacman -S base-devel usbutils avrdude avr-gcc avr-libc  
    git clone https://aur.archlinux.org/visual-studio-code-bin.git
    cd visual-studio-code-bin
    makepkg -si
Ubuntu Linux
    sudo apt update
    sudo apt install gcc build-essential
    sudo apt install gcc-avr binutils-avr avr-libc gdb-avr
    sudo apt install libusb-dev avrdude
    sudo apt install code
Windows
 Install WinAVR for a Light-Weight Compiler
 Or install the full AVR Toolchain
 Install Visual Studio Code

Hardware Setup

Grant yourself privileges
After connecting the ISP programmer to your PC
you will need to edit the USB port's access rights.
    sudo chmod 666 /dev/ttyUSB0

Configure Visual Studio Code

Install VS Code Extensions
 Open Extensions in the left pane or press Ctrl+Shift+X

 Search for C/C++ Extension Pack and click Install
  

 Search for Makefile Tools and click Install
  
Create New Project
 Create an empty folder anywhere
 Open the empty folder ( Ctrl+O )
 Create an empty C/C++ File and a Makefile ( Right click -> New File )

  
Populate your Source File
 Populate your Source File

  
  Note: Your header files will be red underlined.
  This is an expected behavior.
  To resolve this you must configure VS Code.

 Press F1 and in the searchbox type C/C++
 Then select C/C++: Edit Configurations (UI)

  

 Set Configuration Name ( Linux or Win32 ...etc. )

  

 Locate avr-gcc on your Machine
 Edit the Compiler Path

  
  Note: you will might need to use quotation marks
  for the Compiler Path if there are empty spaces in it

 Select IntelliSense mode

  
  Note: the gcc-x86 (legacy) worked fine for me
  but make sure to test your platform specific IntelliSense mode
  ( i.e. linux-gcc-x86 or windows-gcc-x86 )

 Save the Configuration and check your Source Code

  
  Note: header file names are not underlined anymore
  however methods and some definitions are.
  This is an expected behavior.
  You need to select the proper Microcontroller!

 Press and hold Ctrl and click on the avr/io.h header file in your source
 This will bring you to io.h where you can look up your Microcontroller definition
 Copy your Microcontroller definition

  

 Go back to the C/C++ Configurations and edit the Defines section
 Paste your Microcontroller Definition here and save it

  

 Two json files appeared in the folder structure
  

 Check if IntelliSense and Smart Hints work
 If nothing is underlined and all functionalities work you are done 📗
  

Populate your Makefile

 Copy the name of your Programmer Hardware

avrdude -c ?

  


 Copy the name of your Microcontroller

avrdude -p ?

  


 Construct the terminal command to Flash the HEX File

avrdude -c stk500v2 -p m328p -U main.hex


 Copy the name of your MCU for the Compiler
avr-gcc --target-help

  


 Check the Crystal Oscillator Frequency
 For example if the frequency is 16 Mhz the argument will be this:

16000000UL


 Construct the terminal command to Compile the HEX File
avr-gcc <source_file> <mcu_type> <clock_frequency> <output_file>

avr-gcc main.c -mmcu=atmega328p -DF_CPU=16000000UL -Os -o main.hex

  Note: the -Os argument will minimize the output file size


 Populate your Makefile to Compile and Flash
  

Configure your Project

Optionally: You can execute the Makefile from the Terminal
 by going to the Project Folder and executing this:
sudo make

 But to directly execute your Makefile from VS Code
 Press F1 and search for tasks and select Tasks: Configure Task
  

 Then select Create tasks.json file from template
  

 Then select Others
  

 A tasks.json file will appear in the file explorer
  

 The file content looks like this by default
  

 Edit label and command values and save the file
    ```json
    {
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make",
            "type": "shell",
            "command": "sudo make"
        }
      ]
    }
    ```

 Press F1 and select Configure: Default Build Task
  

 Select make
  
  Note: the command make will might not appear
  if the json file is not saved correctly.

 This will add the problemMatcher and group sections to your json file
  

 Now you can press Ctrl+Shift+B or select Run Build Task
  

 If the compile and upload was successful
 the terminal output will look like this:
    ```bash
    avrdude -c stk500v2 -p m328p -P /dev/ttyUSB0 -U main.hex
    avrdude: AVR device initialized and ready to accept instructions
    avrdude: device signature = 0x1e950f (probably m328p)
    avrdude: Note: flash memory has been specified, an erase cycle will be performed.
           To disable this feature, specify the -D option.
    avrdude: erasing chip
    
    avrdude: processing -U flash:w:main.hex:e
    avrdude: reading input file main.hex for flash
           with 164 bytes in 1 section within [0, 0xa3]
           using 2 pages and 92 pad bytes
    avrdude: writing 164 bytes flash ...
    Writing | ################################################## | 100% 0.19 s 
    avrdude: 164 bytes of flash written
    avrdude: verifying flash memory against main.hex
    Reading | ################################################## | 100% 0.13 s 
    avrdude: 164 bytes of flash verified
    
    avrdude done.  Thank you.
    ```
Project Sample   avr.zip








About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages