You are on page 1of 9

Heaven’s Light Is Our Guide

Rajshahi University of Engineering &


Technology
Dept. of Mechatronics Engineering
Course No: MTE 3104
Course Title: Microcontroller and Interfacing Sessional

LAB REPORT
Submitted By:
Name: Swapnil Saha Kotha
Roll: 1708049
Class: Third Year Odd Semester
Session: 2017-18
Date of Experiment: 06/04/2021
Date of Submission: 12/04/2021
Submitted To
Md. Firoj Ali
Assistant Professor
Dept. of MTE, RUET
No of Experiment: 04
Name of the Experiment: Implementation of Up- Down 4-Bit Binary
Counter Using Atmega32.

Aims
• To learn about AVR microcontroller and binary up-down counter.
• To create a program using Atmel Studio for 4-bit up-down counter.
• To run the simulation of the circuit using ATmega32 microcontroller in Proteus
schematic capture.

Required Software
Atmel Studio 7
Atmel Studio is an integrated development platform for Atmel AVR and ARM
microcontrollers. You can easily get started by exploring the included example
projects and run your solution on a starter or evaluation kit. The refactor and
smart features in the editor make editing easier.
Proteus 8
The Proteus Design Suite is a proprietary software tool suite used primarily for
electronic design automation. The software is used mainly by electronic design
engineers and technicians to create schematics and electronic prints for
manufacturing printed circuit boards.

Theory
AVR microcontroller
AVR microcontroller is an electronic chip manufactured by Atmel, which has several
advantages over other types of microcontroller. Atmel was an acronym for “advanced
technology for memory and logic”. AVR microcontroller comes in different configuration,
some designed using surface mounting and some designed using hole mounting. It is
available with 8-pins to 100-pins, any microcontroller with 64-pin or over is surface mount
only.
ATmega32- 8 Bit AVR Microcontroller
The AVR Microcontroller is based on the advanced Reduced Instruction Set Computer
(RISC) architecture. ATmega32 Microcontroller is a low power CMOS technology-based
controller. Due to RISC architecture AVR microcontroller can execute 1 million of
instructions per second if cycle frequency is 1 MHz provided by crystal oscillator.
Key Features of ATmega32 Microcontroller
• 2 Kilo bytes of internal Static RAM
• 32 X 8 general working purpose registers
• 32 Kilo bytes of in system self-programmable flash program memory
• 1024 bytes EEPROM
• Programmable serial USART
• 8 Channel, 10bit ADC
• One 16-bit timer/counter with separate pre-scaler, compare mode and capture mode
• Available in 40 pin DIP, 44-pad QFN/MLF and 44-lead QTFP
• Two 8-bit timers/counters with separate pre-scalers and compare modes
• 32 programmable I/O lines
• In system programming by on-chip boot program
• Master/slave SPI serial interface
• 4 PWM channels
• Programmable watch dog timer with separate on-chip oscillator

Special Features of ATmega32 Microcontroller


• External and internal interrupt sources
• Six sleep modes: Idle, ADC noise reduction, power-save, power-down, standby and
extended standby.
• Power on reset and programmable brown-out detection.
• Internal calibrated RC oscillator

ATmega32 Microcontroller Pin Diagram


For explaining the ATmega32 Microcontroller Pin diagram, a 40-pin Dual Inline Package
(DIP) of microcontroller integrated circuit is considered:
ATmega32 Microcontroller Pin Description
Port A (PA7-PA0): Port A serves as analog inputs for A/D converter. It also acts as an 8-bit
bidirectional I/O port if the A/D converter is not used internally.
Port B (PB7-PB0) and Port D (PD7-PD0): These ports are 8-bit bidirectional I/O ports.
Their output buffers have symmetrical drive characteristics with high source and sink
capability. As inputs, these are pulled low if the pull-up resistors are used. It also provides
various special functional features of the ATmega32.
Port C (PC7-PC0): Port C is an 8-bit bidirectional I/O port. If the Joint Test Action Group
(JTAG) interface is enabled, the pull-up resistors on pins PC2 (TCK), PC3 (TMS), and PC5
(TDI) will be activated.
Vcc: Digital voltage supply
GND: Ground
RESET: It is a RESET pin which is utilized to set the microcontroller ATmega32 to its
primary
value. During the beginning of an application it is to be set elevated for two machine
rotations.
XTAL1: It is an input for the inverting oscillator amplifier and input to an internal clock
operating circuit.
XTAL2: It is an output from an inverting oscillator amplifier.
AVcc: It is a supply voltage pin for A/D converter and Port A. It must be connected with
Vcc.
AREF: AREF is an analog signal reference pin for the analog to digital converter.
ATmega32 Registers
ATmega32 is 8bit microcontroller therefore all its ports are 8bit wide. Every port has 3
register associated with it. Each one has size 8 bits. Every bit in those registers configures the
pins of particular port. Bit0 of these registers are associated with Pin0 of the port. Bit1 of
these registers are associated with Pin1of the port, and same as for other bits.
The three registers available in AVR microcontroller are as follows:
• DDRx Register.
• PORTx Register.
• PINx Register.

I/O Port Registers


• There are three I/O registers associated with each port. These are as follows:
• Data register PORTx
• Data direction register DDRx
• Input pins address register PINx
• Where x can be A, B, C, or D depending on which port is being addressed.
DDRx: Data Direction Registers
• These are 8-bit registers.
• These are used to configure the pins of the ports as input or output.
• Writing a one to the bits in this register sets those specific pins as output pins.
• Writing a zero to the bits in this register sets those specific pins as input pins.
• All bits in these registers can be read as well as written to.
• The initial value of these bits is zero.
Examples
1. Setting Port D as an output port:
DDRD = 0xFF;
Writing 1 to the bits of this register configures the pins corresponding to those pins as output
pins.
Here we have configured all 8 pins of Port D as output pins by writing 1 to each of them.
(0xFF = 0b11111111).
2. Setting Port D as an input port:
DDRD = 0x00;
Writing 0 to the bits of this register configures the pins corresponding to those pins as
output pins.
Here we have configured all 8 pins of Port D as input pins by writing 0 to each of them.
(0x00 = 0b00000000).
PORTx: Data Registers
• These are 8-bit registers.
• These are used to put the pins of the ports in a logic HIGH or logic LOW state.
• Writing a one to the bits in this register puts a HIGH logic (5V) on those pins.
• Whereas writing a zero to the bits in this register puts a LOW logic (0V) on those
pins.
• All bits in these registers can be read as well as written to.
• The initial value of these bits is zero.
Example:
We will use the PORTx register of Port D to write a value 0x55 to Port D.
PORTD = 0x55;
PINx: Input Pins Address Registers
• These are 8-bit registers.
• These are used to read the values on the specific pins of the port.
• These bits are read-only bits and cannot be written to.
Example:
We will read the value on Port D in an 8-bit variable named ‘port value’.
Port value = PIND;
AVR I/O Port Programming
In AVR microcontroller family, there are many ports available for I/O operations, depending
on which family microcontroller you choose. For the ATmega32 40-pin chip 32 Pins are
available for I/O operation. The four ports PORTA, PORTB, PORTC, and PORTD are
programmed for performing desired operation.

Counter
In digital logic and computing, a counter is a device which stores (and sometimes displays)
the number of times a particular event or process has occurred, often in relationship to a
clock. The most common type is a sequential digital logic circuit with an input line called the
clock and multiple output lines. The values on the output lines represent a number in the
binary or BCD number system. Each pulse applied to the clock input increments or
decrements the number in the counter.
A counter circuit is usually constructed of a number of flip-flops connected in cascade.
Counters are a very widely used component in digital circuits, and are manufactured as
separate integrated circuits and also incorporated as parts of larger integrated circuits.
Electronic Counters
An electronic counter is a sequential logic circuit which has a clock input signal and a group
of output signals that represent an integer "counts" value. Upon each qualified clock edge, the
circuit will increment (or decrement, depending on circuit design) the counts. When the
counts have reached the end of the counting sequence (maximum counts when incrementing;
zero counts when decrementing), the next clock will cause the counts to overflow or
underflow and the counting sequence will start over. Internally, counters use flip-flops to
represent the current counts and to retain the counts between clocks. Depending on the type
of counter, the output may be a direct representation of the counts (a binary number) or it
may be encoded.
Bidirectional Counter
Bidirectional counters, also known as Up/Down counters, are capable of counting in either
the up direction or the down direction through any given count sequence. An ‘N’ bit binary
counter consists of ‘N’ T flip-flops. If the counter counts from 0 to 2𝑁 − 1, then it is called as
binary up counter. Similarly, if the counter counts down from 2𝑁 − 1 to 0, then it is called as
binary down counter.
Fig 4.1: Output bit pattern in 4-bit binary up down counter

Program and Code Description


#include <avr/io.h>
#define F_CPU 8000000
#include <util/delay.h>

int main(void)

{
unsigned char z;

DDRD &= ~0x0C; // DDRD= DDRD & ~0x0C(0b11110011), PD2 PD3 as input
DDRC = 0x0F; // 00001111, PC0-3 sets as output

z=0;

while (1)
{
if((PIND & 0x04) == 0) // 0000 0100 PD2 is pressed
{
_delay_ms(250);

if(z < 15) z= z+1;


else continue;
PORTC &= 0xF0; // Clear PC0-3
PORTC |= (z & 0x0F); // show count value
}

if((PIND & 0x08) == 0)


{
_delay_ms(250);

if(z >0) z = z-1;


else continue;
PORTC &= 0xF0; // Clear PC0-3
PORTC |= (z & 0x0F); // show count value
}
}

return 0;
}

Procedure
1. At first the above code was implemented in Atmel Studio.
2. For ATmega32 microcontroller, a hex file was generated.
3. The correct circuit was designed in Proteus.
4. The .hex file was used to simulate the circuit.
5. Then the circuit was checked whether it works properly or not.

Output
The circuit simulated by the Proteus software:

Fig. 4.2: Proteus schematic diagram of 4-bit binary up down counter.


Result
Implementation of Up- down 4-bit binary counter using ATmega32 was successful.

Observation
In this experiment, we leant about the basics of ATmega32 microcontroller. In the code,
DDRC and DDRD registers was used to direct the pins that are used as input and output in
the project respectively. The PORTD register was used to write data on the pin as output. The
PINC register was used to read the value of PORTC and then this was written in the PORTD
as output. Here, all registers were declared in hexadecimal form. Here, PORTC and PORTD
were used, but any other PORT can used for this project too. After inserting the .hex file in
Proteus circuit diagram, the desired output was produced.

Discussion
This experiment can be conducted very easily using ATmega32 microcontroller. It is very
easy to program this microcontroller. The action of up and down counting can be controlled
by the switches. Proteus simulation of the microcontroller circuit also showed the required
result. Finally, it can be concluded that the objectives of this experiment were achieved
successfully.

References
• https://en.wikipedia.org/wiki/AVR_microcontrollers
• https://www.circuitstoday.com/atmega32-avr-microcontroller-an-introduction
• https://www.javatpoint.com/atmega32-avr-microcontroller
• https://en.wikipedia.org/wiki/Counter_(digital)
• https://www.tutorialspoint.com/digital_circuits/digital_circuits_counters.htm
• https://learnabout-electronics.org/Digital/dig56.php

You might also like