ARDUINO IDE programming and AVR microcontroller program download

Let me start by explaining the origin of this project. Initially, I built the system using an ARDUINO MEGA2560 along with various modules, and the code was written in the ARDUINO IDE. However, this kind of patchwork setup wasn't very reliable—especially when it came to power supply. The previous ARDUINO board had a power issue that eventually caused the power supply to burn out. After checking the schematic, I realized that when both the computer's USB power and a 12V power supply were connected, the 5V line was being overloaded due to parallel connection. Although the voltage was the same, current distribution wasn't even because of internal resistance differences, which could lead to damage. That’s why I decided to redesign the circuit board based on open-source schematics, improving the power supply configuration and integrating all components onto a single board. I also updated the firmware and programmed the microcontroller accordingly.

Now, I’d like to explain the relationship between AVR and ARDUINO. ARDUINO is an open-source platform based on ATMEL’s AVR microcontrollers. It also includes its own development environment, the ARDUINO IDE. For example, the ARDUINO MEGA2560 uses the AT MEGA2560 as its main controller. The board selects specific I/O pins from the AVR MCU and assigns them new names. For instance, PD0 becomes digital pin 21, and PF0 corresponds to ADC0 on the analog input A0. These settings are pre-configured for each board version, and you can find details in the boards.txt file located in the Arduino hardware folder. When you choose a board in the IDE, it automatically applies the corresponding settings, such as fuse bit configurations, baud rate, and bootloader location.

After discussing the I/O ports, let’s move on to how programs are uploaded to ARDUINO boards. AVR microcontrollers support several programming methods, including ISP, JTAG, and high-voltage parallel programming. Earlier versions used CH340G for USB-to-serial conversion, but later models, like the MEGA2560, incorporated USB-to-serial functionality directly via the ATmega8 or ATmega16. Here’s a schematic of the download module for the ARDUINO MEGA2560:

ARDUINO IDE编写和AVR单片机程序的下载

The BOOTLOADER is a small program stored in a dedicated area of the FLASH memory. It runs before the main application and is often used for initialization tasks. In the case of ARDUINO, the BOOTLOADER enables serial port uploads. The exact process will be detailed later.

Now, let me walk you through my development process.

I. Using PROGISP to upload programs from ARDUINO IDE

Since my micro USB port wasn’t soldered, and I didn’t want to waste the already soldered board, I opted to use PROGISP to upload the compiled hex file from the ARDUINO IDE. Normally, the ARDUINO board is programmed via the USB port, but with PROGISP, I needed to first generate the hex file. Here’s how to set it up:

Open the ARDUINO IDE, go to File > Preferences, click on the path to the C drive, and open preferences.txt with Notepad.

ARDUINO IDE编写和AVR单片机程序的下载

It’s crucial to close the ARDUINO IDE before making any changes. Then, open preferences.txt, add a line like build.path=D:\arduino\MyHexDir (choose your own directory), and save the file. If the change doesn’t appear after reopening, make sure the IDE was closed during editing. Once done, the hex file will be generated in the specified folder.

Next, some settings for the AVR microcontroller are required, especially the fuse bits. This was my first time working with an AVR chip, so I had a lot to learn. I initially thought I could just use a CH340G, but that didn’t work. The fuse bits determine clock selection, system clock division, startup address, and more. My board uses an external 16MHz crystal, so I had to configure the fuse bits to select that instead of the internal RC oscillator. To do this, I added an ISP programming port to my second PCB design and purchased two USB ISP programmers. Unfortunately, the computer didn’t recognize the devices at first, but the seller provided a driver-free burning software, which worked well.

ARDUINO IDE programming and AVR microcontroller program download

This software included a wizard mode for setting the fuse bits, which made the process much easier. You don’t need to understand every detail of the fuse settings—just follow the steps. With this tool, I successfully changed the fuse bits and burned the program. Of course, there were some issues along the way. For example, I mistakenly selected “system clock 8 divide,” which reduced the actual clock speed to one-eighth of the crystal frequency. Also, moving the reset vector to the boot section caused the microcontroller to skip my program entirely. The key takeaway here is understanding the difference between internal and external clock sources. An "external low-frequency crystal" refers to a passive crystal with a low frequency, while an "external full-scale crystal" is an active one. It's important to know what each fuse bit represents to avoid similar mistakes in the future.

两种晶振

All In One PC

all in one computers, all in one desktops,all in one computer, all in one desktop,aio pc

Guangdong Elieken Electronic Technology Co.,Ltd. , https://www.elieken.com

Posted on