Micro Lab 6
Micro Lab 6
Submitted by:
Bilal Ahmad (17PWMCT0541)
Musawir Shah (17PWMCT0545)
Muhammad Tufail (17PWMCT054)
Submitted to:
Engr.Shahbaz Khan
REQUIRED MATERIALS:
1. Arduino UNO board
2. USB Cable
3. Breadboard
4. Power Supply
5. Computer with Atmel Studio 7.
6. Two 5V or 12V DC Motor
7. Transistors
8. L298d Motor-driver IC
MATERIALS DESCRIPTION:
ARDUINO UNO BOARD:
The Arduino UNO is an open-source microcontroller board based on the Microchip Atmega328P
microcontroller and developed by Arduino.cc. The board is equipped with sets of digital and analog
input/output (I/O) pins that may be interfaced to various expansion boards (shields) and other circuits.
TECHNICAL SPECIFICATIONS:
PIN CONFIGURATION:
L298 IC has 15 pins in total, each having different functions
Figure 2 : L298 IC
associated with them.
DC MOTOR:
A DC motor is any of a class of rotary electrical machines that converts direct current electrical energy
into mechanical energy. The most common types rely on the forces produced by magnetic fields.
Nearly all types of DC motors have some internal mechanism, either electromechanical or electronic,
to periodically change the direction of current flow in part of the motor. [2]
A DC motor is an electromechanical device that translates input electrical power (𝑉×𝐼) into output
mechanical power (𝜏×𝜔). When the positive and negative leads of a DC motor are connected to a DC
voltage source (battery), the motor starts moving continuously in one direction. [2]
TYPES:
Figure 4:DC MOTORS TYPES
TASK 1:
Properly comment each line of code in Program 1 and 2. Explain in theory what the program has
achieved. Explain calculation of values in registers TCNTx, TCCRxA, TCCRxB, and OCCRxA registers (where
x=0 or 1) in both programs. Also explain the duty cycle and frequency of resulting PWM signals. Assume
XTAL=16MHz.
CODE 1:
#define F_CPU 16000000UL
#include <avr/io.h>
/********** Main Program *******************/
int main(void)
{
DDRB = 0x0C; //set PORTB as OUTPUT
PORTB = 0xFF; //set PORTB HIGH
TCCR0A = 0xC1; //set Timer0 in CTC mode, no prescaling
OCR0A = 64; //set OCROA value to 64
while(1);
}
EXPLANATION:
In this Program we set the PORTB as OUTPUT and make it HIGH .We set the timer in CTC(Clear
Timer ON Compare Match) mode and no prescalling by setting TCCR0A to 0xC1(10110001 in binary) .
As we know that in CTC mode the TCNTx steps-up to value of OCRxA .The value we save in OCRxA will be
the top value for TCNT.We set the value of OCR0A(Output Compare Register) of timer 0 to 64 , which
means timer will give time delay until the TCNT0 become equal to 64.
We can find the duty cycle and frequency of resulting PWM signal as given below
Duty Cycle:
OCR0A=64
Duty Cycle=ON time/OFF time x 100%.
Duty Cycle =64/255 x 100% = 25.09%
Frequency:
1/16MHz=0.0625 micro sec
256-64 = 192
So
Delay = 192 x 0.0625 micro sec = 12 micro sec
Time period = 12 micro sec x 2 = 24 micro sec
Frequency = 1/ 24 micro sec = 42 KHz approx.
CODE 2:
#include <avr/io.h>
#define uchar unsigned char
#define uint unsigned int
void delays1ms()
{
TCNT1 = 0; //Setting TCNT1 to 0 of timer 1
OCR1A = 10000-1; //Save a top value for TCNT1 in OCR1A of timer 1.
TCCR1A = 0; //Set TCCR1A for setting Timer Mode
TCCR1B = 0x09; //Set the timer to CTC mode and Setting the Clock
while((TIFR1&(1<<OCF1A))==0){} //waiting till the OCF1A goes HIGH
TCCR1B = 0; //Stop the Timer
TIFR1 = 1<<OCF1A; //Set the TIFR1 to run again
}
int main(void)
{
int i;
DDRB = 0xFF; //make PORTB OUTPUT
PORTB = 0xFF; //make PORTB HIGH
sbit(4,PORTB); // set PORTB4 HIGH
cbit(0,PORTB); //Set PORTB0 LOW
sbit(5,PORTB); //Set PORTB5 high
cbit(1,PORTB); //Set PORTB1 LOW
while (1)
{
sbit(3,PORTB); //Set PORTB3 HIGH
sbit(2,PORTB); //Set PORTB2 HIGH
for (i=1;i<=25;i++) //generating large Time delay
{
delays1ms();
}
cbit(3,PORTB); //Set PORTB3 LOW
cbit(2,PORTB); //Set PORTB2 LOW
for (i=1;i<=95;i++)
{
delays1ms();
}
}
}
EXPLANATION:
In this program we are contolling two motors using L298 ic.In this code we generate a time delay of 1ms
in function called delay1ms using Timer 1, as shown from the value of OCR1A i-e 10000 - 1.We use Timer
1 in CTC (Clear Timer on Compare Match) mode and using oscillator Clock frequency with no
prescaler,as we set the value of TCCR1A to 0 and TCCR1B to 0x09(00001001).
We use 4 pins for L298 IC inputs and 2 pins as PWM pins for Enable A and Enable B of L298 IC.We
calculated the value of Duty Cycle and Frequency as given below
DUTY CYCLE:
OCR1A=9999
ON time/OFF time x 100%.
Duty Cycle =9999/65536 x100% = 15.25%
FREQUENCY:
1/16MHz=0.0625 micro sec
As OCR1=10000
So
Delay = 10000 x 0.0625 = 625 micro seconds
Time period = 625 x 2= 1350 micro seconds
Frequency = 1/1350 micro sec= 740.7 Hz
PROTEUS CIRCUIT DIAGRAM:
TASK 2:
Control speed of motors in Figure 9 using Phase-correct PWM technique.
CODE:
#include <avr/io.h>
void delay1ms()
{
TCNT0 = 156; //Set value for TCNT0
TCCR0A =0x01; //set the timer 0 to phase Correct PWM
TCCR0B =0x01; //set the prescaler
while ((TIFR0 & (1<<TOV0))==0){} //wiat for timer to overflow
TCCR0B =0; //stop the timer
TIFR0 |=(1<<TOV0); //set the TIFR0
}
int main(void)
{
int i;
DDRB=0xFF; //set PORTB as Output
PORTB =0xFF; //set PORTB HIGH
PORTB |=(1<<4);
PORTB &=~(1<<0);
PORTB |=(1<<5);
PORTB &=~(1<<1);
while(1)
{
PORTB |=(1<<3);
PORTB |=(1<<2);
for(i=1;i<=25;i++) //generating large time delay
{
delay1ms();
}
PORTB &=~(1<<3);
PORTB &=~(1<<2);
for(i=1;i<=55;i++) //genarating large time delay
{
delay1ms();
}
}
}
EXPLANATION:
We set the timer 0 in Phase Correct PWM mode .
For this purpose we make a delay function and set the TCCR0A to 0x01 and TCCR0A to 0x01.As the
TCCR0 register bits is given below
We control the Motor speed by giving value to TCNT0 register as here we given the value of 156 which
generates a time delay of 1ms.
TASK 3:
Write a program in Arduino IDE to achieve the same behaviour as in Task 2.
CODE:
Figure 13 Code for Task 3
EXPLANATION :
Outside the loops we define the constants for inputs pin for motors and PWM pins.Pin 12 and 8 is input
pins for motor 1 and pin 10 is PWM pin for motor 1.Pin 13 and 9 is input pins for motor 2 and pin 11 is
PWM pin for motor 2.
In setup loop we set all the pin as Output and set initial direction for motors.The motor 1 will move in
Forward direction and motor 2 will move in backward direction.
In loop we send values to motor using PWM pins.We send the value of 155 so motor will not give full
speed and if we change the value the motor speed will change correspondingly.