You are on page 1of 5

MT-359L Microcontroller and Embedded Systems

MT-359L Microcontroller and Embedded System

Lab Manual 10

Muhammad Sarmad Baig


Name Shahzaib Khalil
Abdur Rab
Ayesha Siddiqua
201133
Registration Number 201097
201106
201088
Class BEMTS-F20-VA

Instructor’s Name Engr. Iraj Kainat

Session Fall 22

Air University, Islamabad Page 1


Lab # 10
Introduction and implementation of timers and PWM on
ATMEGA 2560 using Atmel Studio and Proteus

Objectives
In this lab, we will.
 To understand the concept and working of PWM.
 To control the motor speed from 25% to 99% using full PWM.

Equipment/Software Used:
• Microcontroller (ATMEGA 328p/2560)
• Atmel Studio
• Breadboard
• Resistor
• LED
• Jumper wires
• Proteus

Introduction
PWM:
• Pulse Width Modulation (PWM) is a technique by which the width of a pulse is varied while
keeping the frequency constant.
• Pulse width modulation (PWM), or pulse-duration modulation (PDM), is a method of reducing the
average power delivered by an electrical signal, by effectively chopping it up into discrete parts.

Duty Cycle:
A period of a pulse consists of an ON cycle (+V) and an OFF cycle (0V). The fraction for which the
signal is ON over a period is known as the duty cycle.

Duty Cycle (In %) =

Configuring Timer0 for PWM generation:


It is simple to configure PWM mode in Timer. We just need to set some bits in the TCCR0 register.
TCCR0: Timer Counter Control Register 0.
Lab Task 01
To control the speed of motor to vary from 25% to 99% using Fast PWM.

Code:
#define F_CPU 16000000UL
#include "avr/io.h" #include
<util/delay.h>
int main (void)
{
DDRG=0xFF;
DDRC=0x00;
TCCR0A|=(1<<WGM00)|(1<<WGM01)|(1<<COM0B1)|(0<<COM0B0); //set fast PWM mode with non-inverted
output
TCCR0B =0x01;
while(1)
{
if(PINC==0X01)
{
OCR0B=64;
else if(PINC==0x02)
{
OCR0B=128; //motor speed = 50%
}
else if(PINC==0x04)
{
OCR0B=191; //motor speed = 75%
}
else if (PINC==0x08)
{
OCR0B=252; //motor speed = 99%
}
}
}

Procedure

 Open proteus on your computer and place down Arduino Mega 2560, resistors, LED, Push Button,
motor and a ground as shown in the picture.

 Copy the hex file from Atmel Software and paste the hex file in the Arduino Mega configuration in
Proteus.
Output (Proteus Simulation)

Figure 1: Proteus Simulation

CONCLUSION
In this lab we basically studied the background process of PWM and how they can be implemented and
accessed beauty using if AVR studio. In this lab we studied how to access the specific register connected to the
particular PWM and how to store value for different types of operations which includes PWM, and we perform
the task to increase the speed of motor from 25% to 99% by using fast PWM and observe the duty cycle of
motor.

You might also like