0% found this document useful (0 votes)
14 views23 pages

ESL Expt. 5

The document outlines a series of experiments using the Atmega 2560 microcontroller, focusing on I/O operations, including controlling LEDs, interfacing a switch with a buzzer, and controlling a DC motor. Each experiment includes objectives, software and hardware requirements, theoretical background, algorithms, and example code. The final experiment demonstrates Pulse Width Modulation (PWM) for velocity control of a robot while indicating speed changes with bar graph LEDs.

Uploaded by

Nandhini P
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)
14 views23 pages

ESL Expt. 5

The document outlines a series of experiments using the Atmega 2560 microcontroller, focusing on I/O operations, including controlling LEDs, interfacing a switch with a buzzer, and controlling a DC motor. Each experiment includes objectives, software and hardware requirements, theoretical background, algorithms, and example code. The final experiment demonstrates Pulse Width Modulation (PWM) for velocity control of a robot while indicating speed changes with bar graph LEDs.

Uploaded by

Nandhini P
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/ 23

Ex.

No: 5a Date:

GETTING FAMILIAR WITH I/O PORT OPERATION – BARGRAPH LEDS

Aim: This experiment demonstrates simple I/O operation using Atmega 2560.
The task is to selectively turn ON each LED one by one from a set of eight LEDs
connected to Port J. Initially all the eight LEDs will be off. Then each LED has to be
serially turned ON after a delay of 500 milliseconds. Once turned ON, the LED will
remain ON. After all the eight LEDs have been serially turned ON, the pattern repeats
itself.

Software Requirements:

1. AVR Studio
2. AVR Bootloader

Hardware Requirements:

1. Personal Computer
2. Firebird V ATMEGA 2560 Robotic Kit

Theory:

1. LED bargraphs are interfaced to PORTJ 7 to PORTJ 0


2. Make sure that in the configuration options the following settings are done for the
proper operation of the code.

Micocontroller : Atmega 2560

Frequency: 14745600

Optimization : -O0

3. Jumper J3 is in place to enable LED bargraph display on the Atmega 2560


micocontroller adapter board.

Algorithm:

1. Start

2. Configure PORTJ as output port.

3. Initially switch off all the LEDs in Port J.

4. Inside the infinite loop, do the following.

5. Glow the LEDs one by one from LSB with a delay of 500 ms .

6. Once all the eight LEDs are turned on, switch off all the LEDs and repeat the process.

7. Stop

5-1
Program:

#include <avr/io.h>

#include<avr/interrupt.h>

#include<util/delay.h>

int main(void)

DDRJ=0XFF;

PORTJ=0X00;

while(1){

PORTJ=0X01;

_delay_ms(500);

PORTJ=0X03;

_delay_ms(500);

PORTJ=0X07;

_delay_ms(500);

PORTJ=0X0F;

_delay_ms(500);

PORTJ=0X1F;

_delay_ms(500);

PORTJ=0X3F;

_delay_ms(500);

PORTJ=0X7F;

_delay_ms(500);

PORTJ=0XFF;

_delay_ms(500);

PORTJ=0X00;

}}

5-2
Result:
Each LED connected to Port J has been serially turned ON after a delay of 500
milliseconds. After all the eight LEDs have been serially turned ON, the pattern repeats
itself. Thus, simple I/O operation is demonstrated using Atmega 2560.

5-3
Program:

#include <avr/io.h>

#include<avr/interrupt.h>

#include<util/delay.h>

int main(void)

int x;

DDRE=0X00;

DDRC=0XFF;

while(1)

x=PINE;

if((x & 0X80)==0)

PORTC=0X08;

_delay_ms(200);

PORTC=0X00;

_delay_ms(200);

else

PORTC=0X00;

5-4
Ex. No: 5b Date:

INTERFACE A SWITCH TO TURN ON BUZZER

Aim: This experiment demonstrates simple I/O operation using Atmega 2560.
The task is to interface the boot switch with the microcontroller To implement a “Push
to ON” indicator using the Buzzer and a boot switch. Buzzer will turn ON and turn
OFF for 200 milliseconds and continue the same as long as the boot switch is pressed
and turn off as soon as the boot switch is released.

Software Requirements:

1. AVR Studio
2. AVR Bootloader

Hardware Requirements:

1. Personal Computer
2. Firebird V ATMEGA 2560 Robotic Kit

Theory:

1. Buzzer is connected to Pin 3(PC3) of Port C and a boot switch is connected to Port E
pin number 7.
2. Make sure that in the configuration options the following settings are done for the
proper operation of the code.

Micocontroller : Atmega 2560

Frequency: 14745600

Optimization : -O0

3. The boot switch (Refer to Figure 1) is located right besides the reset switch on the
Firebird V robot.

Figure-1: Boot switch on Firebird-V robot (marked in red rectangle)

5-5
5-6
Algorithm:

1. Start

2. Declare a variable called x.

3. Configure PORTE as input port.

4. Configure PORTC as output port.

5. Inside the infinite loop, do the following.

6. Assign the value of PINE register to variable x.

7. Check whether boot switch is pressed or not.

8. If the boot switch is pressed, toggle the buzzer with a delay of 200 ms.

9. If the boot switch is not pressed, turn off the buzzer.

10. Stop.

Result:
Thus buzzer is toggled whenever boot switch is pressed.

5-7
Program:

#include <avr/io.h>

#include<avr/interrupt.h>

#include<util/delay.h>

void forward(void){

PORTA=0X06;

void left(void){

PORTA=0X05;

int main(void)

DDRA=0XFF;

DDRL=0X18;

PORTA=0X00;

PORTL=0X18;

forward();

_delay_ms(5000);

forward();

_delay_ms(5000);

left();

_delay_ms(1000);

forward();

_delay_ms(5000);

PORTA=0X00;

_delay_ms(5000);

5-8
Ex. No: 5c Date:

DC MOTOR INTERFACING FOR MOTION CONTROL

Aim: The aim of this experiment is interface DC Motor on the Atmega 2560 based firebird
V robotic kit. The task is to trace the shape of alphabet “L” using Firebird V Robot.

Software Requirements:

1. AVR Studio
2. AVR Bootloader

Hardware Requirements:

1. Personal Computer
2. Firebird V ATMEGA 2560 Robotic Kit

Theory:

The DC Motor is connected to L293D (motor driver IC) and the pins of L293D are
connected to Port A and Port L of Atmega 2560.
1. Four Pins for Direction control are connected at PORT A.

PA0 - Left Motor Control


PA1 - Left Motor Control
PA2 - Right Motor Control
PA3 - Right Motor Control

2. Two Pins for Enabling Motor Driver IC are connected at PORT L.

PL3 - Left Channel Enable


PL4 - Right Channel Enable

3. Make sure that in the configuration options the following settings are done for the
proper operation of the code.
Micocontroller : Atmega 2560

Frequency: 14745600

Optimization : -O0

5-9
Figure 1. Various Motions

L293D IC Pin Connections:

5-10
Logic Table:

The robot should start from node “A” following the path and direction shown in
Figure 2. The sequence of traversal should be A-B-C with the robot stopping at “C”. Use time
delay to navigate between two nodes. The robot has to complete the whole task in a
maximum of 30 seconds and make sure no side is navigated in less than 2 seconds.

Figure 2. Path that needs to be followed by Firebird V Robot

Note: Horizontal line of “L” is half of its vertical side. You have to use _delay_ms() for
turning and navigating the sides of the “L” shape. The 90° angle need not be exact. The
Length of the sides will depend on the delay you choose

Algorithm:

1. Start

2. Define the forward function by assigning PORTA=0X06.

3. Define the left function by assigning PORTA=0X05.

4. Inside the main function, configure PORTA as output port.

5-11
5-12
5. Configure pin 3 and 4 of PORTL as output.

6. Assign PORTA=0X00.

7. Activate pin 3,4 of PORTL.

8. Call the forward function with necessary delay.

9. Call the left function with necessary delay.

10. Again call the forward function.

11. Stop the robot.

12. Stop.

Result:
DC Motor is interfaced on the Atmega 2560 based firebird V robotic kit. Thus, the
shape of alphabet “L” is traced using Firebird V Robot.

5-13
Program:

#include<avr/io.h>

#include<avr/interrupt.h>

#include<util/delay.h>

void motion_timer5_init()

TCCR5B=0x00;

TCNT5H=0xFF;

TCNT5L=0x00;

OCR5AL=0x00;

OCR5BL=0x00;

TCCR5A=0xA9;

TCCR5B=0x0B;

void forward(void){

PORTA=0x06;

void stop(void){

PORTA=0x00;

void velocity(unsigned char left_motor, unsigned char right_motor){

OCR5AL=(unsigned char)left_motor;

OCR5BL=(unsigned char)right_motor;

5-14
Ex. No: 5d Date:

VELOCITY CONTROL USING PULSE WIDTH MODULATION IN FIREBIRD V

Aim: The aim of this experiment is to get familiar with Pulse Width Modulation (PWM)
using Atmega 2560 microcontroller. The task is to decelerate the robot using PWM and
indicate this on the Bar Graph LEDs.
The robot should start with maximum velocity and with all the Bar Graph LEDs turned
ON. Decrease the velocity by an equal amount and with each decrement switch off one
of the Bar Graph LEDs starting from the top i.e. Pin no. 8 (MSB) of Port J. All the Bar
Graph LEDs should be turned off when the minimum velocity is reached. This
experiment has to be completed in a maximum of 30 seconds; hence the time after
which a velocity decrement occurs has to be selected appropriately. The minimum
velocity can be assumed to be zero or experimentally determined.

Software Requirements:

1. AVR Studio
2. AVR Bootloader

Hardware Requirements:

1. Personal Computer
2. Firebird V ATMEGA 2560 Robotic Kit

Theory:

Pulse Width Modulation (PWM), is a method of transmitting information on a series of


pulses 2 The data that is being transmitted is encoded on the width of these pulses to control
the amount of power being sent to a load 3 Examples: Electric stoves, Lamp dimmers, and
Robotic Servos.

Pulse width waveform generated for motion control of Firebird V is:

5-15
void motion_barLED(){

int i;

for(i=255;i>50;i--){

velocity(i,i);

forward();

if(i>240 && i<255){

PORTJ=0xFF;

if(i>220 && i<240){

PORTJ=0xFE;

if(i>180 && i<220){

PORTJ=0xFC;

if(i>150 && i<180){

PORTJ=0xF8;

if(i>125 && i<150){

PORTJ=0xF0;

if(i>100 && i<125){

PORTJ=0xE0;

if(i>80 && i<100){

PORTJ=0xC0;

5-16
Its generation involves the use of following registers:

Timer/Counter register 5(TCNT5)

Output Comparator register 5(OCR5A and OCR5B)

Timer Counter Control register(TCCR5A and TCCR5B)

Timer / Counter Register 5 (TCNT5):

The Timer/Counter is a register that increments its value after every clock cycle. The value in
the Timer/Counter is compared with a reference value to generate PWM. This value depends
upon the resolution of Counter. For example, a 3-bit counter will have 8 values (i.e. 0-7). Its
waveform will be seen as follows:

For n-bit counter, maximum value = 2n − 1. The Timer/Counter 5 is a 16-bit register. We use
it in 8-bit mode, for PWM generation.

Output Compare Register 5 (OCR5A, OCR5B and OCR5C):

The value of the Timer/Counter 5 is constantly compared with a reference value. This
reference value is given in the Output Compare Register(OCR). Output Compare Registers
associated with Timer 5 for PWM generation: OCR5A, OCR5B and OCR5C.

For motion control of Firebird V, we use OCR5A and OCR5B registers. OCR5A is
associated with the OC5A pin (PORTL.3). This pin is connected to the enable(EN2) pin of
motor driver, which is associated with the left motor. Similarly, OCR5B is associated with
the OC5B pin (PORTL.4), This pin is connected to the enable(EN1) pin of motor driver,
which is associated with the right motor.

Timer / Counter Control Register 5A (TCCR5A):

There are 2 types of bits in TCCR5A: Compare output mode bit & waveform generation
mode bit.

Compare Output Mode Bit:

Compare Output Mode bits decide the action to be taken when counter(TCNT5) value
matches reference value in Output Compare Register(OCR5).

5-17
if(i>50 && i<55){

PORTJ=0x80;

_delay_ms(1000);

PORTJ=0x00;

stop();

int main(){

DDRJ=0xFF;

DDRA=0x0F;

PORTA=0x00;

DDRL=0x18;

PORTL=0x18;

motion_timer5_init();

motion_barLED();

5-18
There are two modes of PWM waveform generation: Non-inverting mode and inverting mode

This experiment uses non-inverting mode of operation for PWM generation.

Waveform Generation Mode Bit:

The Waveform Generation Mode bits are used to generate the type of PWM signal needed.

This task uses fast 8-bit mode for PWM generation.

5-19
5-20
The control word in TCCR5A register is as follows:-

Timer/Counter Control Register 5B (TCCR5B):

There are 2 types of bits in TCCR5B: Waveform generation mode bit & Clock Select bit.

Waveform Generation Mode Bit:

WGM bits (WGM52 and WGM53), are used for selecting mode of PWM generation.
Note: WGM50 and WGM51 bits also used along with these bits are in TCCR5A register.
Clock Select Bit:

CS52, CS51, CS50 (Clock select) bits are used to select a frequency at which timer/counter
Register will increment its value. Prescalar is used to reduce the frequency of the clock,
suitable for the type of PWM being generated. Clock select bits decide the factor with which
clock frequency will be divided. If we select prescalar as 64 then Clock select bits should be
011.

The control word in TCCR5B register is as follows:-

5-21
5-22
Algorithm:

1. Start
2. Define motion_timer5_init function with following assignments.
3. Assign TCCR5B=0X00, TCNT5H=0XFF and TCNT5L=0X00
4. Assign OCR5AL and OCR5BL as 0X00.
5. Assign TCCR5A=0XA9 and TCCR5B=0X0B.
6. Define the forward function by assigning PORTA=0X06.
7. Define the stop function by assigning PORTA=0X00.
8. Define velocity function with two parameters to left_motor and right_motor.
9. Assign the value of left motor to OCR5AL and right motor to OCR5BL.
10. Define the motion_barLED function and do the following.
11. Declare the variable i and initialise to 255.
12. Initially switch on all the bar graph LEDs.
13. Decrement the value of 'i' and call velocity function and forward function until velocity(i)
reaches a minimum threshold.
14. As the velocity decreases, serially switch off the bar graph LEDs.
15. Call stop function.
16. Inside the main function, do the following.
17. Configure PORTJ as output port, lower four pins of port A as output and Configure 3rd and 4th
pin of port L as output.
18. Assign port L=0X18.
19. Call motion_timer5_init function and
20. Call motion_barLED function.
21. stop

Result:
Thus, the robot is decelerated using PWM and this is indicated on the Bar Graph LEDs.

5-23

You might also like