Wednesday, July 21, 2021

Getting Started With C In Atmel Studio 7 Using ATMega16

 

An Overview Of ATMega16

ATMega16 is an 8-bit microcontroller built using RISC architecture bases on flash technology. Its original manufacture Atmel, now become a part of the Microchip Technology since 2016. However those AVR microcontroller series are still continuous to develop. 

Getting Started With C In Atmel Studio 7 Using ATMega16
The ATMega16 in DIP and SMD Package

ATMega16 has three storage memory parts:

  • Flash memory, or program memory for program storage (firmware) of 16 kBytes with ISP feature.
  • 1 kBytes of general purpose registers
  • 512 Bytes of SRAM

The CPU could clock up to 16 MHz when it’s supplied about 5 V, yield the executing speed 16 MIPS.

It comes with 40-pin DIP version and also a SMD version.

Microcontroller communication interfaces built inside this device:

  • Two-wire Serial Interface – TWI
  • Serial Peripheral Interface – SPI
  • Universal Synchronous Asynchronous Receiver Transmitter – USART

Digital input output (I/O) is divided into four 8-bit ports:

  • PORTA
  • PORTB
  • PORTC
  • PORTD

 

Getting Started With C In Atmel Studio 7 Using ATMega16
Pin Diagram of the most common used ATMega16 40-Pin DIP Package

They are bi-directional read/write port register.



Analog input channels are multiplexed with those digital I/O. The Analog to Digital Converter (ADC) yields an 10-bit resolution available between its 8 input channels. The ADC gain is select-able. 

Analog output, Pulse Width Modulation (PWM) modules generate an analog output voltage via their output compare pins. They work with the three timer modules inside this MCU.

Interrupt is a effective way to notifies the MCU. In ATMega32 the interrupts are the external interrupt and the peripheral interrupts triggered by its inside peripheral modules.

Programming the ATMega16 AVR

The assembly language is design with the target MCU with the description of the internal hardware operations. Currently most the 8-bit MCU is programmed using the C language targeting a specific architecture.

Atmel (now becomes a part Microchip Technology) develop and IDE, Atmel Studio enable the programmer the code their AVR and AVR32 products for free. It bases on the AVR GCC. However this IDE supports the assembly, C and C++ programming languages. Now, in 2020 the current version is Atmel Studio 7.



I know a few third party compiler for the AVR MCU series:

  • MikroC for AVR
  • CodeVision AVR
  • IAR Embedded Workbench for AVR

Some of them offer a free version of their compiler with a limit coding. But for a full features access of the resources the programmers need to pay for a license.

For me I prefer Atmel Studio for my programming and project developments.

Getting Started With Atmel Studio 7

The Atmel Studio support both a low level and a high level C/C++ coding. For most programmers, they like to program in C due to its ease of coding density.

To get start the programmer need to download the Atmel Studio IDE from the Microchip Technology website, and install it on their PC.

After the installation, open the IDE and make a new project.

 

Getting Started With C In Atmel Studio 7 Using ATMega16
Make a new project

Getting Started With C In Atmel Studio 7 Using ATMega16
Select GCC Executable Project, its location and name



Getting Started With C In Atmel Studio 7 Using ATMega16
Select the ATMega16 as a target device

Getting Started With C In Atmel Studio 7 Using ATMega16
Then coding window will appeared.



Getting Started With C In Atmel Studio 7 Using ATMega16
Let write a sample code as shown in this picture. This code blink PORTB of the ATMega16.

Getting Started With C In Atmel Studio 7 Using ATMega16
Now it’s ready to build the project

Getting Started With C In Atmel Studio 7 Using ATMega16
An error-free coding creates the binary file for the target MCU to execute.



AVR C source code in Atmel Studio 7:

/*
 * m16_blink.c
 *
 * Created: 10/10/2020 7:39:16 PM
 * Author : Admin
 */ 

#include <avr/io.h>

#define F_CPU 16000000UL
#include <util/delay.h>

int main(void)
{
    //PORTB AS OUTPUT
    DDRB=0xFF;
    while (1) 
    {
        PORTB=0x00;        //Clear PORTB
        _delay_ms(1000);//Wait For 1000 mS
        PORTB=0xFF;        //Set PORTB
        _delay_ms(1000);//Wait For 1000mS
    }
}

Within the code above the label F_CPU is a parameter used by the delay.h file below the delay is created.

The schematic below is full assembling symbol for the breadboard system assembling.



Schematic diagram for this example to wire on the breadboard

The Hardware Test

Development Board and ISP Programmer

I use my own development board I designed/assembled. The code is written to fit this board. However, it doesn’t come with a pre-burned boot-loader nor an on-board block of ISP. I putted an ISP header, compatible to the USBasp USB-based ISP programmer. The USBasp is open-source. We can make it by hand, or buying from any store for a few Dollars.

Getting Started With C In Atmel Studio 7 Using ATMega16
Getting Started With C In Atmel Studio 7 Using ATMega16



I also made my own one using the ATMega8 with a dozen of components. The host PC side ISP software is widely available. I have been using many software that the USBasp ISP hardware. Currently, I switched to a GUI version of AVRdude. This software supports many ISP hardware for the Microchip AVR devices.

The AVRdude

As mentioned earlier, the AVRdude has a user-friendly GUI version that made by an author.


Getting Started With C In Atmel Studio 7 Using ATMega16
A GUI version of the AVRdude

For a first use of the USBasp ISP programmer, the user need to install the USB driver of this USB programmer. Previously, I used Windows 7 32-bit OS. Installing the USBasp driver on this OS is easy because the driver file comes with downloaded zip file from the USBasp website.



Currently, I migrate my computer’s OS to Windows 7 64-bit and up to Windows 10 64-bit. Installing the USB driver with those conventional driver file is very hard, and doesn’t work for me. I saw a solution from the online forum. It’s the Zadig, USB driver installation.

Plug your USBasp to the host PC operates the Windows7/10 64-bit, and install the USB driver with an appropriate one’s. Then open the AVRdude to test the device reading/writing. 

Getting Started With C In Atmel Studio 7 Using ATMega16
A successful writing to the ATMega16



After this successful hex file uploading let see the real hardware test.

 

Getting Started With C In Atmel Studio 7 Using ATMega16
A program test on development board

Click here to download this example. To get started coding the ATMega16 using C in Atmel Studio 7, see this video.


No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)