pia-468x60

pia-728x90

Showing posts with label Microchip Studio. Show all posts
Showing posts with label Microchip Studio. Show all posts

Thursday, June 16, 2022

Programming All External Interrupt Sources of Atmega16

In previous post, I show a simple programming example of INT0. As it's already stated, the Atmega16 has up to 3 external interrupt sources. In this example, I use all its external interrupt sources, the INT0, the INT1, and the INT2.

I select the low logic level of these interrupt source to trigger the ISR. Whenever any interrupt occurs, its corresponding output LEDs will toggle its logic state. The output LEDs are PB5, PB6, and PB7.

Programming All External Interrupt Sources of Atmega16
Program simulation

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * atmega16AllExtIntEx.c
 *
 * Created: 6/10/2022 7:06:23 PM
 * Author : Admin
 */ 

#include <avr/io.h>

#include <avr/interrupt.h>

int main(void)
{
	
	//PD2 and PD3 Input
	DDRD&=~(1<<2);
	DDRD&=~(1<<3);
	//Turn on PD2 and PD3 high
	PORTD|=(1<<2)|(1<<3);
	//PortB output
	DDRB=0xFF;
	//PB2 input
	DDRB&=~(1<<2);
	//Turn PB2 high
	PORTB|=(1<<2);
	//Enable INT0...2 request
	GICR|=(1<<INT0)|(1<<INT1)|(1<<INT2);
	//Enable interrupt
	sei();
	//Clear Flag
	GIFR|=(1<<INTF0);
	while (1)
	{
	}
	return 0;
}

//Interrupt Service Routine - ISR
ISR(INT0_vect){
	//Toggle RB5
	PORTB^=0x20;
}

ISR(INT1_vect){
	//Toggle RB6
	PORTB^=0x40;
}

ISR(INT2_vect){
	//Toggle RB7
	PORTB^=0x80;
}

Click here to download its zip file.

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.


Saturday, May 15, 2021

A DIY ATMega16 Development Board For Students

Overview

The development board for any microcontroller are widely available especially in the online market place. They come with various peripheral device and programming examples. 

The Atmel (Now Microchip) AVR microcontroller is one of the most popular microcontroller in-use today. The development boards for this device are available. They are ready to use with a select-able scale and price.

A DIY ATMega16 Development Board For Students
The final test of the board with error free

 

For an electronic hobbyist or student, a development could be built using a single board PCB with a minimum on-board peripheral devices. Using a development board, the prototyping and testing are safer and time saving.

With a PCB fabrication service support, I decided to design a development board for the Atmel AVR ATMega16 microcontroller for my own microcontroller experiments.

Features



The design comes with many features that fully work with the ATMega16 chip:

  • Digital inputs and outputs
  • analog input devices
  • Display
  • RS-232
  • SPI peripheral device
  • TWI peripheral devices etc.

It mentions only the ATMega16 chip. However, the board supports other AVR devices with the 40-pin DIP package. I have tested this board with some chips I posses:

  • ATMega16
  • ATMega32
  • ATMega644

I think it works with the ATMega1284. But currently I don’t have this chip in my own laboratory.

The PCB Design

The completed design made with a free EDA software. It takes almost a week to finish the schematic and PCB design.



A DIY ATMega16 Development Board For Students
The finished design of this development board

The schematic design need up to two letter size sheet.

A DIY ATMega16 Development Board For Students
Schematic Sheet2



A DIY ATMega16 Development Board For Students
Schematic Sheet1

Within the design most of the resistors and the capacitors are in the SMD package. The PCB size is a little bigger than 10 cm square. I didn’t plan about the target of the PCB.

A DIY ATMega16 Development Board For Students
Top Layer



A DIY ATMega16 Development Board For Students
Bottom Layer

Within the design most of the resistors and the capacitors are in the SMD package. The PCB size is a little bigger than 10 cm square. I didn’t plan about the target of the PCB.

On Board Resource

Beside the DC power supply circuit, all other components have their own unique function connect to the microcontroller.

 

A DIY ATMega16 Development Board For Students
This board is just ready after a few hours of the assembling/soldering.

DC Regulated Power Supply

An AC to DC converter is need to power this board. All on-board component supplies from a +5V regulated IC – AMS1117-5.0. There is an optional +3.3V regulated IC – AMS1117-3.3 that could be useful for others low voltage devices.

The power supply circuit

Microcontroller Clock And Reset Circuit

The on-board ATMega16 chip supplies at +5V DC. Clock and reset circuit are already wired with this chip on board.



Reset and Clock for the ATMega16
An external crystal clock for the ATMega16A has a maximum value of 16 MHz.

In System Programming

The in System Programming (ISP) is an appropriate flash memory upload tool for the Microchip AVR chip. Currently, most of the ISP programmers are very low cost as a result of the open-source tool.

ISP header for this ATMega16 development board – It follows the USBasp programming header.

This header is a 10-pin IDC header/connector. It fits the USBasp programmer that connects externally.

RS-232

The RS-232 communicate between a host PC with the on-board microcontroller. This communication could be very classic now. The recent development replaces this interface with a USB-Serial converter chip. This new technology is very effective in cost and size.

The RS-232 circuit – The Tx/Rx of the host PC side will connect to the on-board microcontroller across a DIP switch.

However I still have the HIN232/MAX232 RS232 level converter that still has a few left in my workshop.

Digital Input/Output

The basic digital input/output on this board made of two distinct ports. PORTB is for output that connects to a 8-bit LED.

A DIP switch enables or disables the output to LED
Port A accepts the digital input from a DIP switch. The switch has only a ground connection when it’s on.



PORTA connects to a DIP switch. When it’s on a low logic value is created.

All digital input pins of the ATMega16 have their own weak pull-up resistor that will be turn on by software.

Multiplexed Display

A multiplexed display created by a numbers of seven-segments LED displays. Each digit of the multiplexed display shares the same segments. Each common of the display turns on and off the digit.

A six-digits multiplexed display. Two distinct DIP swith enables and disables the connection to the microcontroller.

The display in-use here is a 0.36″ common cathode display. PORTB connects the segments across a DIP switch while PORTC controls the six-digits common of the seven-segments. The SW8 DIP switch has a two remaining pins, allowing the user to configure the voltage reference pins for the microcontroller’s ADC.

Analog to Digital Converter

The Analog to Digital Converter (ADC) module built inside the ATMega16 chip. PORTA is multiplexed with the ADC inputs – ADC0 to ADC7.

Two ADC inputs – The LM35DZ and a trim POT
On this board I put only two analog input devices – The LM35DZ analog temperature converter and the trim POT.

External Interrupts

The external interrupts of the ATMega16 creates by three distinct pins, name INT0, INT1 and INT2.

External interrupt mechanism

A typical tactile switch creates an interrupt to the microcontroller. On this board it happens at the low logic level of the input.

Character LCD

A character LCD developed by Hitachi has been popular for a few decades. It still in-use now for education purpose.


The Hitachi HD44780 character LCD interfaces in 4-bit mode.

Serial Peripheral Interface

The Serial Peripheral Interface (SPI) of the ATMega16 chip here connects to a 12-bit Digital to Analog Converter (DAC) across a DIP switch. The preset analog voltage reference of the DAC chip is to +5V.

The MCP4922 dual 12-bit DAC connects to the ATMega16 SPI
The analog output voltage of these two DAC connect to the outside world via a male header block.

Two-wire Serial Communication

The Two-wire serial communication (TWI) originally known as the Inter-integrated Circuit (I2C) requires only two electrical communication lines on a single bus to command and exchange the data. On this board I put two TWI chips – a DS1307 Real Time Clock (RTC) and an AT24C16 EEPROM.

The TWI communication interface to the ATMega16

A DIP switch SW5 allows the connection between the TWI block and the microcontroller. Optionally this gate is bridge between the RS-232 and the microcontroller.

Connectors And Headers

This board allow an external connection to the outside device via many pre-soldered connectors/headers. The main ATMega16 chip with its 40-pin wires to a 2×20 on-board male header. Other headers are,

  • The DC power supplies outputs – +12V, +5V and +3.3V DC.
  • The SPI output
  • The TWI external connection
  • The USART external connection etc

They are labeled on the development board.

Ordering A PCB

As it’s often mentioned, using a service from a PCB fab gives more advantage for a complex design circuit board. The PCB of this ATMega16 development board was ordered from PCBWay. The company provides the PCB fabrication service up to 14 layers. Getting the PCB delivered to my warehouse within a few days saves a lot of time and engineering cost.



A pack of PCB from PCBWay

Unboxing the PCB #1


Unboxing the PCB #2

Soldering And Assembling

Ordering the PCB from a fabrication service gives some advantages to the students and hobbyists:

  • It’s easy to solder as the soldering pads are plated with some soldering friendly materials – lead, lead free, immersion silver and up to the hard gold. These materials are very hard to get oxidized in the air if the PCB are kept for a long period.
  • The components legend on both sides give the information to the solder man and the end user about the components placement and about the printed circuits.
  • With the advantage of solder mask the PCB is very hard to become corrosive cause by some other chemical materials that it suffered. The solder mask color is select-able between various colors – green, blue, yellow etc. The green one’s is very convenient as it’s very easy for the user to observe the tracks.

I use a 40 W electrical soldering iron to solder all the components. The overall soldering/assembling processes take only a few hours.

A 40W gun type iron solder is suitable for this job
Electric soldering iron looks like a gun. It comes with a switch useful for thermal recovery, or even a high temperature requirement.



Some components for soldering

A finished assembling at top layer



A finished assembling at the bottom layer

I'm writing and testing sample program for Atmega16 for this board. They are releasing gradually.


ATMega644P Programming using AVR-LibC in Microchip Studio IDE (2025)

Recently I tested  my ATMega644P chip that left from previous project. For more information please visit this page.

Microchip Studio is free of charge compiler provides by device manufacture. It comes with the GNU AVR-LibC that's widely used by many AVR programmers. However it only contains the standard C library functions. It has a little peripheral libraries. We need to write our own functions to interface with external hardware.

ATMega644P Tutorials


I recently test this chip by programming it using C. Some examples I tested using software simulator while others are tested using my AVR Prototype Board.

Basic I/O

ATMega644P Pin Change Interrupt Example
Test Program

 

  1. ATMega644P Input Output Programming Example 
  2. ATMega644P External Interrupt Programming 
  3. ATMega644P Pin Change Interrupt Example

LCD and Keypad Interfacing

ATMega644P LCD Display and Matrix Keypad Interface

  1. ATMega644P HD44780 Character LCD Interfacing 
  2. ATMega644P LCD Display and Matrix Keypad Interface 
  3. ATMega644P Graphical LCD Interfacing

Analog to Digital Converter (ADC)

ATMega644P Analog to Digital Converter (ADC) Example
Program Tested on my AVR Prototype Board

 

  1. ATMega644P Analog to Digital Converter (ADC) Example 

Two Wire Interface (TWI) or Inter Integrated Circuit (I2C)

ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing
Date and Time Reading
  1. ATMega644P TWI DS1307 and AT24C16 EEPROM Interfacing 
  2. ATMega644P TWI PCF8574 I/O Expansion Interfacing 
  3. ATMega644P TWI PCF8574AP Matrix Keypad LCD 
  4. ATMega644P TWI LCD RTC and Keypad Example 
  5. ATMega644P TWI MCP23017 GPIO Example 
  6. ATMega644P TWI MCP23017 LCD Keypad 
  7. ATMega644P TWI MCP23017 Character LCD 

Labels

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