ESL Expt. 5
ESL Expt. 5
No: 5a Date:
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:
Frequency: 14745600
Optimization : -O0
Algorithm:
1. Start
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;
PORTC=0X08;
_delay_ms(200);
PORTC=0X00;
_delay_ms(200);
else
PORTC=0X00;
5-4
Ex. No: 5b Date:
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.
Frequency: 14745600
Optimization : -O0
3. The boot switch (Refer to Figure 1) is located right besides the reset switch on the
Firebird V robot.
5-5
5-6
Algorithm:
1. Start
8. If the boot switch is pressed, toggle the buzzer with a delay of 200 ms.
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:
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.
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
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.
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
5-11
5-12
5. Configure pin 3 and 4 of PORTL as output.
6. Assign PORTA=0X00.
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;
OCR5AL=(unsigned char)left_motor;
OCR5BL=(unsigned char)right_motor;
5-14
Ex. No: 5d Date:
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:
5-15
void motion_barLED(){
int i;
for(i=255;i>50;i--){
velocity(i,i);
forward();
PORTJ=0xFF;
PORTJ=0xFE;
PORTJ=0xFC;
PORTJ=0xF8;
PORTJ=0xF0;
PORTJ=0xE0;
PORTJ=0xC0;
5-16
Its generation involves the use of following registers:
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.
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.
There are 2 types of bits in TCCR5A: Compare output mode bit & waveform generation
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
The Waveform Generation Mode bits are used to generate the type of PWM signal needed.
5-19
5-20
The control word in TCCR5A register is as follows:-
There are 2 types of bits in TCCR5B: Waveform generation mode bit & Clock Select 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.
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