Adc PWM
Adc PWM
Adc PWM
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
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
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.
Task: Display the digital value of an output of a Potentiometer and dynamically vary it.
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 ??)
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
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.
Task:Vary the intensity of an LED from Low to High and then High to Low
.also called as an LED fader