0% found this document useful (0 votes)
1 views

Lab6_es

The experiment focuses on using Pulse Width Modulation (PWM) with the ATmega 2560 microcontroller to control the velocity of a DC motor on the Firebird V robot. It involves monitoring a boot switch to adjust speed in predefined steps while displaying the current speed on an LCD. Key learnings include PWM motor control, LCD interfacing, and handling digital inputs with debouncing techniques.

Uploaded by

itsmeanish15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab6_es

The experiment focuses on using Pulse Width Modulation (PWM) with the ATmega 2560 microcontroller to control the velocity of a DC motor on the Firebird V robot. It involves monitoring a boot switch to adjust speed in predefined steps while displaying the current speed on an LCD. Key learnings include PWM motor control, LCD interfacing, and handling digital inputs with debouncing techniques.

Uploaded by

itsmeanish15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EC3172: Embedded Systems Lab

Experiment: 6

Name:Anish
kumar
Roll No:
220104016
Section: D
Semester: 6th
Branch: ECE –
VLSI
Under The Guidance of: Dr. Heigrujam Manas Singh

Aim:
To familiarize with Pulse Width Modulation (PWM) using the ATmega 2560
microcontroller on the Firebird V robot by implementing velocity control of
a DC motor. The experiment involves using a boot switch to accelerate and
decelerate the robot from minimum to maximum speed and vice versa
while displaying the current speed on an LCD.

Software/Tools required:
Microchip Studios
Theory:
Pulse Width Modulation (PWM) is a widely used technique in embedded
systems for controlling the speed of DC motors by varying the duty cycle of
a pulse signal. The ATmega2560 microcontroller, present on the Firebird V
robot, generates PWM signals using Timer 5, which allows precise control
of motor speed. By adjusting the values in the Output Compare Registers
(OCR5AL and OCR5BL), the duty cycle of the PWM signal is modified,
thereby controlling the motor velocity.
A 16x2 Liquid Crystal Display (LCD) is used in this experiment to display
the current speed of the robot. The LCD is connected to Port C of the
ATmega2560 microcontroller and is controlled using predefined functions
from "lcd.h" and "lcd.c". The LCD provides a simple interface to display
numerical values corresponding to motor speed, updating dynamically as
the robot accelerates or decelerates.
A push-button switch, such as the boot switch, serves as an input device for
speed control. The boot switch is connected to Port E, Pin 7 (PE7) and
functions as a digital input. When pressed, it pulls the pin LOW (0), and
when released, the internal pull-up resistor ensures it remains HIGH (1).
The microcontroller continuously monitors the state of the boot switch to
increment or decrement the speed in predefined steps.
In this experiment, the robot starts in a stationary position with the LCD
displaying "Speed: 000". When the boot switch is pressed, the speed
increases in steps of 63 up to a maximum of 252. Once the maximum speed
is reached, subsequent presses of the boot switch decrease the speed in the
same steps until it returns to zero, completing a full cycle of acceleration
and deceleration. This real-time control system demonstrates the use of
PWM for motor speed regulation and provides hands-on experience in
embedded programming for I/O interfacing.

I/O Port Configuration:


In this experiment, key I/O ports of the ATmega2560 microcontroller are
configured as follows:

1. PORT C (LCD Interfacing)


o The 16x2 LCD is connected to Port C, which is configured as an
output port to send data and control signals. o The LCD
requires proper initialization using predefined functions from
"lcd.h" and "lcd.c".
o The LCD displays real-time motor speed in three-digit format
at a specified position on the screen.
2. PORT E (Boot Switch Interface)
o The boot switch is connected to Pin 7 of Port E (PE7) and
functions as a digital input. o The pin is configured as an input
using the Data Direction Register (DDRE).
o An internal pull-up resistor is enabled to ensure that the pin
reads HIGH (1) when the switch is not pressed and LOW (0)
when pressed. o The microcontroller continuously checks the
status of PE7 to adjust the motor speed and update the LCD
display accordingly.
3. PORT L (PWM Generation for Motor Control)
o Pins PL3 and PL4 of Port L are configured as outputs for PWM
signal generation. o The PWM duty cycle is controlled using the
values stored in OCR5AL and OCR5BL registers. o The motor
speed is proportional to the duty cycle of the PWM signal,
allowing smooth acceleration and deceleration.

Procedure:
Step 1: Project Setup in Atmel Studio
i. Open Atmel Studio and create a new project named
"Experiment6.atsln".
ii. Select the ATmega2560 microcontroller as the target device.
iii. Ensure that the necessary header files (lcd.h, lcd.c) and function
stubs are included in the project.
Step 2: Hardware Initialization
i. Configure the LCD o Initialize the 16x2 LCD by calling the
lcd_init() function.
o Set the LCD cursor to display "Speed: 000" at row 1, column 3. ii.
Configure the Boot Switch (PE7) o Set Pin 7 of Port E (PE7)
as an input using the DDRE register.
o Enable the internal pull-up resistor to ensure a HIGH (1) state
when the switch is not pressed.
iii. Configure the Motor Control Pins o Set PL3 and PL4
of Port L as output for PWM signal generation.
o Initialize
Timer 5 to generate PWM signals using
motion_timer5_init().
Step 3: Implementing PWM for Motor Speed Control
i. Initialize the motion control function to start with motors in a STOP
condition.
ii. Define the Output Compare Registers (OCR5AL and OCR5BL) to
control PWM duty cycle.
iii. Set an initial speed of 0 in OCR5AL and OCR5BL when the robot is
powered on.
Step 4: Implementing Boot Switch Logic for Speed Control
i. Continuously monitor the state of PE7 (Boot Switch Pin). ii.
When the switch is pressed:
o Increase the speed in steps of 63 and update the LCD
accordingly. o Modify the OCR5A and OCR5B values to adjust
the PWM duty cycle.
o Ensure that after reaching the maximum speed (252),
subsequent presses decrease the speed in reverse order (189
→ 126 → 63 → 0).
iii. When the switch is released:
o Wait for the next press before modifying speed values.
o Ensure debouncing to avoid unintended multiple triggers.
Step 5: Debugging and Testing
i. Write the code inside motion_control() function to implement the
boot switch-based speed control logic.
ii. Compile and build the project in Atmel Studio to check for errors.
iii. Load the compiled hex file onto the Firebird V robot using a suitable
programmer.
iv. Observe the LCD to ensure correct speed values are displayed.
v. Press the boot switch multiple times to check smooth acceleration
and deceleration of the robot.

Embedded C Code:

#define F_CPU 14745600UL


#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.c"

const unsigned char speed_levels[9] = {0x00, 0x3F, 0x7E, 0xBD, 0xFC, 0xBD, 0x7E, 0x3F,
0x00};

void motion_pin_config(void)
{
DDRA = 0x0F; // PA0-PA3 as output
PORTA = 0x00; // Motors off

DDRL = 0x18; // PL3 and PL4 as output


PORTL = 0x18; // Initial state high
}

void boot_switch_config(void)
{
DDRE = 0x00; // PE7 as input
PORTE = 0x80; // Enable pull-up on PE7
}

void port_init(void)
{
motion_pin_config();
boot_switch_config();
}

void motion_timer5_init()
{
TCCR5B = 0x00;
TCNT5H = 0x00;
TCNT5L = 0x00;
OCR5AL = 0x00;
OCR5BL = 0x00;
TCCR5A = 0xA1; // Fast PWM 8-bit, non-inverting
TCCR5B = 0x0B; // Prescaler 64, WGM=011
}

void velocity(unsigned char left_motor, unsigned char right_motor)


{
OCR5AL = left_motor;
OCR5BL = right_motor;
}

void forward(void)
{
PORTA = 0x06; // Forward direction
}
void stop(void)
{
PORTA = 0x00; // Stop motors
}

void motion_control()
{
unsigned char step = 0;
char direction = 1;

lcd_string(1, 3, "Speed: ");


lcd_numeric_value(1, 10, speed_levels[step], 3);
stop();
velocity(0x00, 0x00);

while (1)
{
if ((PINE & 0x80) == 0x00) // Boot switch pressed
{
_delay_ms(300); // Debounce

if (direction)
{
step++;
if (step == 4) direction = 0;
}
else
{
step--;
if (step == 0) direction = 1;
}
unsigned char current_speed = speed_levels[step];
lcd_numeric_value(1, 10, current_speed, 3);

if (current_speed == 0x00)
{
stop();
}
else
{
forward();
velocity(current_speed, current_speed);
}
}
}
}

int main(void)
{
lcd_port_config();
lcd_init(); port_init();
motion_timer5_init();
motion_control();
}

Conclusion:
In this experiment, the velocity control of a DC motor using Pulse Width
Modulation (PWM) was successfully implemented using the ATmega2560
microcontroller on the Firebird V robot. The experiment demonstrated
the use of Timer 5 to generate PWM signals, which were used to control
the motor speed. The boot switch served as an input to increment and
decrement the motor speed in predefined steps, while the 16x2 LCD
displayed real-time speed values.
Key takeaways from this experiment include:
o Understanding PWM-based motor speed control using the
OCR5AL and OCR5BL registers.
o Interfacing and configuring an LCD display to provide real-time
feedback.
o Implementing digital input handling using an internal pull-up
resistor.
o Managing switch debouncing and controlled speed transitions
through proper programming techniques.
By successfully executing this experiment, the concept of embedded
systems-based motor control was reinforced, highlighting the practical
applications of microcontroller-based PWM techniques in robotics and
automation.

You might also like