You are on page 1of 26

What have we covered so far??

Getting acquainted with micro controller. Studying the input/output ports and their manipulation. Writing the C-Code for Atmega-16 Compiling and dumping the code on the bootloader kit.

Todays schedule
Lab-1: Interfacing LCD device. Learning Analog to Digital Converter in Atmega-16 Performing a simple experiment to test ADC Lab-2: Studying the concept of Pulse Width modulation A simple program to demonstrate PWM

ADC ANALOG-TO-DIGITAL CONVERTER

ANALOG AND DIGITAL VALUES


In daily life, we encounter analog values. They can take any real value For example, voltage at a point is 4V and if it is between 4 and 5 , it can have infinite number of values between 4 and 5 . A computer can process only digital values, which can have only fixed values, depending on the bits for precision.

NEED FOR A-TO-D CONVERSION


The values in physical world (height, weight, voltage, distance etc.) are analog. The microcontroller deals only with digital values. Hence, the analog values need to be converted to digital for the sake of processing.

ANALOG TO DIGITAL CONVERTOR ( ADC)


The ATMega16 microcontroller has an inbuilt ADC for enhancing its functionalities. The ADC has 10 bit resolution for accuracy. The ADC uses successive approximation technique for the conversion .

SUCCESSIVE APPROXIMATION

FEATURES OF ADC
10 bit successive approximation ADC 8 channels for input (PA7-PA0). Reference voltage provided by AVcc pin = Vcc or 5v AREF = external voltage reference pin 2.56 V (internal reference) Runs in Single Conversion Mode or Free Running Mode

PIN DIAGRAM

ADC REGISTERS

ADMUX :

ADC REGISTERS

ADCSRA : ADC Control and Status Register

ADEN: Writing 1 to this enables the ADC, 0 disables ADSC: Start conversion bit (discussed before) ADATE: When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a conversion on a positive edge of the selected trigger signal. The trigger source is selected by setting the ADC Trigger Select bits, ADTS in SFIOR. ADIF: ADC interrupt flag. Gets set when ADC conversion is complete and data registers are updated. Cleared by hardware when the corresponding interrupt is serviced or by writing a 1 to this flag ADIE: Activates the ADC conversion complete interrupt

ADC REGISTERS

ADC REGISTERS

SFIOR

ADC REGISTERS
ADCH and ADCL : They are the data registers, which store the result (digital value) of ADC. The data configuration depends on the ADLAR bit value of ADMUX register. If ADLAR=0, data is right adjusted and if ADLAR=1, data is left adjusted. Always read ADCL first and then ADCH.

Let us now write a simple code to test ADC

Task: Display the digital value of an output of a Potentiometer and dynamically vary it.

PULSE WIDTH MODULATION


At times we come across applications or situations wherein we need to generate square waves with the microcontroller. For example, flickring the LED with a certain delay. In the last lab, we had used _delay_ms() function in loops to create square waves. Alternatively, the inbuilt feature of AVR timers can be used in square wave generation.

WHY PWM ??
Suppose we need to control the speed of a motor, which can not operate below a certain voltage (say 5V). In such a scenario, we can give high frequency square pulses to the motor and vary their timing to control the speed (remember yesterdays dim LED glow in case of idle input ??)

ROLE OF TIMERS IN PWM


The timers in ATMega 16 control the width of a pulse. The advantage of using AVR timers in wave form generation is that the output pin toggles automatically when the timer condition are fulfilled.

HOW TO DO PWM

There are four specified pins in ATMega16 for waveform generation. Each pin can be operated by their corresponding timer only. The following table provides information about it.
Pin OC0 (PB3) OC1A (PD5) Corresponding timer Timer 0 Timer 1

OC1B (PD4)
OC2 (PD7)

Timer 1
Timer 2

MODES OF PWM
Normal mode CTC mode Fast PWM mode Phase Correct PWM Let us briefly discuss these modes.

MODES OF PWM
Normal

Mode: The simplest mode of

operation is the Normal mode. In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 16-bit value (MAX = 0xFFFF) and then restarts from the BOTTOM (0x0000). Since the range of counting and the clock frequency are constant, this gives a pulse of constant width. Can you guess the time ??

MODES OF PWM

Clear Timer on Compare (CTC) Mode: In Clear Timer on Compare or CTC mode the OCR1A or ICR1 Register are used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT1) matches either the OCR1A or the ICR1. The OCR1A or ICR1 define the TOP value for the counter, hence also its resolution.

MODES OF PWM

The Fast PWM mode Provides a high frequency PWM waveform generation option. The counter counts from BOTTOM to TOP then restarts from BOTTOM. But then how is it different from Normal mode ??

In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF the value in ICR1 or the value in OCR1A (WGM13:0 = 15). The counter is then cleared at the following timer clock cycle.

Understanding Fast PWM

CONFIGURING REGISTERS FOR PWM

Now lets code a simple program to understand PWM properly.

Task:Vary the intensity of an LED from Low to High and then High to Low
.also called as an LED fader

You might also like