Lab # 09 Using Timers/Counters of AVR MCU for Waveform Generation

 

COMSATS UNIVERSITY ISLAMABAD


MICROPROCESSOR SYSTEMS AND INTERFACING

 

LAB REPORT #09

 

SUBMITTED TO:

SIR KHIYAM IFTIKHAR

SUBMITTED BY: -

S/NO

NAME

REG NO

1

JUNAID AHMAD

CIIT/FA19-BEE-089/ISB

2

IBRAR AHMAD

CIIT/FA19-BEE-083/ISB

3

JARRAR MALIK

CIIT/FA19-BEE-087/ISB

 

 

DATE:

26-11-2021

 

Lab # 09 Using Timers/Counters of AVR MCU for

Waveform Generation

Objectives:

      To Introduction to Timers/Counters

      Delay and Waveform generation using timers

      Using PWM signals to control Servo Motors

 

Software Tools:

              Microchip Studio/AVR Studio

              Arduino

              Proteus ISIS

              AVR DUDESS

 

Hardware Tools:

 

Name

Value

Quantity

Arduino Nano

-

1

Potentiometer

-

1

Servo Motor

-

1

Oscilloscope

 

1

Table 7.1: List of Components

 

Task 1:

A servomotor is connected to Timer OC0 pin and a potentiometer is connected to ADC channel 0. Generate a PWM wave at Timer 0. The duty cycle of generated waveform should vary according to the input voltage at channel 0 of the ADC (The duty cycle should vary as you rotate the Potentiometer). By varying the On-Time of the signal the servo can be rotated to different positions.

 

IN-LAB TASKS:

 

TASK 1:

 

A servomotor is connected to Timer OC0 pin, and a potentiometer is connected to ADC channel 0. Generate a PWM wave at Timer 0. The duty cycle of generated waveform should vary according to the input voltage at channel 0 of the ADC (The duty cycle should vary as you rotate the Potentiometer). By varying the On-Time of the signal the servo can be rotated to different positions.

 

 

CODE

 

* Created: 25/11/2020 9:10:39 PM

 * Author : Aegon

 */

 

#include <inttypes.h>

#include <stdlib.h>

#include <avr/io.h>

#define F_CPU 16000000UL

#include <util/delay.h>

#include <string.h>

#include <math.h>

#define ADC_CHANNEL0 0

#define ADC_VREF 5 // Ref voltage for ADC is 5 Volts

#define ADC_RES 10 // Resoulution of ADC in bits

#define ADC_QLEVELS 1024 // Quantization levels for the ADC

unsigned char

ADC_Initialize(); // Initialize ADC. Mode 1 = Single Conversion, Mode 2 = Free Running

unsigned int ADC_Read(unsigned char channel); // Reads the result of a single conversion from the ADC

/******************* Function Prototypes ***********

**********/

void init_timer0(); // Initialize Timer0 for Fast PWM mode

void move_motor(int adc_value); // vlaue = 0 to 1023 for90 to 90 degrees angle

#define SERVO_OCR OCR0A

#define MIN_ANGLE_OCR 17.0

#define SERVO_OCR_OFFSET 14.0

// ***********************************************************

// Main program

int main(void)

{ init_timer0(); // timer0 configured for Fast PWM mode

ADC_Initialize();

int adc_value;

while(1)

{ // Infinite loop; define here the

adc_value = ADC_Read(0);

_delay_ms(20);

// Set motor Position

move_motor(adc_value); }}

void init_timer0()

{ OCR0A = (unsigned char) SERVO_OCR;

DDRD|=(1<<PD6);// Configure OC0A for output(PortD.6)

TCCR0A|=0x83;//Initialize timer0 for Fast PWM Mode, Non inverting,

TCCR0B|=0x05;// Set Prescaler = 1024

}

/* Function Initializes the ADC for 10

 

Bit Single Conversion mode*/

unsigned char ADC_Initialize()

{ DIDR0=0x00;

ADMUX|=(1<<REFS0)|(1<<ADLAR);// Left adjust result. Vref = AVCC = 5V

ADCSRA|=(1<<ADEN); // Enable ADC

ADCSRA|=(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2);//Select the prescaler for 16 MHz System Clock

return(0); }

/* Function reads the result of a single conversion from the ADC

channel given as an arg

ument*/

unsigned int ADC_Read(unsigned char channel)

{

unsigned char ADC_lo;

unsigned char ADC_hi;

unsigned int result;

ADMUX &= ~(0x07); // clear previous selction of channel

ADMUX |= channel; // Select the new channel

// Delay needed for the stabilization of the ADC input voltage

_delay_us(10);

//wait for ADC to finish any ongoing opeartion

while((ADCSRA & (1<<ADSC)) != 0);

ADCSRA |= (

1 << ADSC); //start conversion

while((ADCSRA & (1<<ADIF)) == 0);

ADCSRA |= (1<<ADIF); // clear the flag by writing 1 to it

 

//result = (ADCH<<8)|(ADCL & 0xC0); // Left adjust result

ADC_lo = ADCL;

ADC_hi = ADCH;

result = (ADC_hi<<2)|(ADC_lo >> 6); // Right adjust result

return result; }

void move_motor(int adc_value) // vlaue = 0 to 1023 for-90 to 90 degrees angle

{ SERVO_OCR=((float)adc_value/1023)*17+14; //if we use 8-bit timer then-90 degree (1 ms) correspond toOCR

//value of approximately 14

}


PROTEUS -SIMULATION:



HARD-WARE:




Critical Analysis / Conclusion:

 

In this lab experiment we got to know how to do interfacing of servo motor with Arduino and learned about different types of mode. There are four different modes of operation of Timer/Counter0 module are Normal Mode, Clear Timer on Compare Match (CTC) Mode, Fast PWM Mode, Phase Correct PWM Mode. PWM and its important features, PWM is pulse width modulation, and it is one of the most important mode of timer’s mode, The fast Pulse Width Modulation or fast PWM mode (WGM01:0 = 3) provides a high frequency PWM waveform generation option. The phase correct PWM mode (WGM01:0 = 1) provides a high-resolution phase correct PWM waveform generation option

 

إرسال تعليق

Post a Comment (0)

أحدث أقدم