0% found this document useful (0 votes)
100 views20 pages

AVR

Here are the steps to enable interrupts in the code: 1. Set the global interrupt enable bit (I-bit) in SREG register: SREG |= (1<<7); 2. Enable the ADC interrupt by setting the ADIE bit in the ADCSRA register: ADCSRA |= (1<<ADIE); 3. Define an ISR for the ADC interrupt: #pragma vector=ADC_vect __interrupt void adc_isr(void) { // code to handle ADC conversion complete ADCSRA |= (1<<ADIF); // clear interrupt flag } 4. The ADC interrupt will now trigger when conversion is

Uploaded by

Ahsan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views20 pages

AVR

Here are the steps to enable interrupts in the code: 1. Set the global interrupt enable bit (I-bit) in SREG register: SREG |= (1<<7); 2. Enable the ADC interrupt by setting the ADIE bit in the ADCSRA register: ADCSRA |= (1<<ADIE); 3. Define an ISR for the ADC interrupt: #pragma vector=ADC_vect __interrupt void adc_isr(void) { // code to handle ADC conversion complete ADCSRA |= (1<<ADIF); // clear interrupt flag } 4. The ADC interrupt will now trigger when conversion is

Uploaded by

Ahsan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

AVR

VIRTUAL
(ADVANCED
RISC )-- 8 BIT RISC
ATMEL ATMEGA32
MICROCONTROLLER

ATMEGA32 - I/O ports , Timer and ADC


A LITTLE
HISTORY
 The PIC (Programmable Interrupt Controller) appeared around 1980.
→ 8 bit bus , RISC architecture
→ Executes 1 instruction in 4 clk cycles
→ Harvard architecture . (separate bus for program and data memory)
→ Compilers  MPLAB C-18, Hitech-C, MikroC etc

 AVR (1994)
→ 8 bit bus, RISC architecture
→ one instruction per cycle
→ Harvard architecture . (separate bus for program and data memory)
→ Compilers  Avr studio GCC, WinAVR, MikroC etc
INTRO TO ATMEGA32
 8- bit Microcontroller with 4 I/O ports , PORTA,PORTB,PORTC,PORTD.
 RAM = 2Kb , Flash = 32Kb , EEPROM = 1k
  Maximum Crystal Frequency = 16MHz
 8 ADC’s with 10-bit Resolution
 One 16-bit timer/counter with separate prescaler, compare mode and
capture mode.
 Two 8-bit timers/counters with separate prescalers and compare
modes
 4 PWM channels
 1- USART
 Master/slave SPI serial interface
 External interrupts
II: Atmega8 – Basic features
Pin and Port Overview
ATMEGA32:
GND: Ground (0V)
VCC: Digital Supply Voltage (2,7 – 5,5V)
AVCC: Analog Supply Voltage for PORTA and ADC’s.
connect to low-pass filtered VCC
AREF: Analog Reference Voltage, usually AVCC
Reset’: Low level on this pin will generate a reset

Port B, Port C, Port D:


General Purpose 8 Bit bidirectional I/O - Ports,
optional internal pullup-resistors when configured
as input
output source capability: 20mA

Special Functions of the Ports available as configured


using the SFRs:

Port D: Uart, external Interrupts, Analog


Comparator
Port B: External Oscillator/Crystal, SPI
THREE I/O PORT
REGISTERS
 PORTx : To write on PORT , x is Port i.e A,B,C,D
 DDRx : Data direction register , Low means I/P, High means O/P
 PINx : To Read from PORT
REGISTER AND BIT ADDRESSING
AVR Studio GCC - Compiler MIKRO-C Compiler
(Used in Mazidi’s Avr Book)
Register Addressing: Register Addressing:
Same as used in GCC compiler and
Format is  REGNAME = Value; PIC
e.g DDRC = 0xff; // make PORTC output
PORTC = 0x00; //Move logic 0 to PORTC
Bit Addressing: Achieved using logical bit operations. Bit Addressing: same as used in
Format is:: PIC.
X |= (1 << BitnumberOrName); // a bit in x is set One of the Format is
X &= ~(1 << BitnumberOrName) ; // a bit in x is reset REGNAME.BITNAME = Value;

Examples: Example :
PORTA |= (1 << 2); // sets bit#2 at PortA DDRC.DDRC1=1; //make PC1 pin o/p
PORTA &= ~(1 << 3); //reset bit#3 at PortA
DDRA &= ~( (1<<0) | (1<<3) ); //PA0 and PA3 as inputs
PORTA |= (1<<0) | (1<<3); // internal Pull UP for both switch
on

For practice try to do 7.23 to 7.25


Write a MIKROC code to throw data from switches attached at PORTB to LEDs attached at
PORTC of ATMEGA32?
Code:
void main()
{
DDRB = 0x00; //configure PORTB as input
DDRC = 0xFF; //configure PORTC as output

while(1)
{
PORTC = PINB; //Read PORTB and send it to PORTC
}
}
A door sensor is connected to the PA1 pin, and a buzzer is connected to PA7. Write an MIKROC program to
monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square
wave of a few hundred Hz.
Code:
sbit Dsensor at PINA.PINA1;
void main(void)
{
DDRA.DDRA1=0; //Make PA1 (Door sensor pin) as input
DDRA.DDRA7=1; // Make PA7 (Buzzer pin) output
while (1)
{
while (Dsensor==1) //while it opens means switch is pressed
{
PORTA.PORTA7=0; //buzzer attached with PA7
delay_ms(200);
PORTA.PORTA7=1;
delay_ms(200);
}
}
}
LETS DISCUSS ADC’s
OF ATMEGA32
INTRO to ADC’S of
ATMEGA32
 8 ADC’s (ADC0-ADC7) multiplexed with PORTA pins.
 The A/D module has five 8-bit registers. These registers are:

• ADCSRA to control ADCs


• ADMUX to select one input channel for conversion
• ADCL and ADCH to keep 10-bit digital data
• SPIO, special function register
 Formula for Step-Size and Output Data (DOUT) are same as
discussed in PIC Microcontrollers
SOME TERMINOLOGIES
1.
(REVISION)
Resolution (n-bit): width of output data given by ADC
2. Conversion time: Time taken by ADC to convert analog signal to digital
3. Step Size: smallest step after which ADC provide us a new binary number
Step Size = (Vref+ - Vref-)/(2^n)
e.g for Vref=5v, n=10 step= 5/1024 = 4.88mv

4. Digital Data Output:


Dout = (Vin/StepSize)
e.g. if n=10-bit , vref = 2.56v , calculate binary output generated by ADC if a.)Vin= 1.7v, b.)
Vin=2.1v
a.) step= 2.56/1024 =2.5mv Dout=1.7v/2.5mv = 680 in decimal
ADC output= binary of 680=1010101000

5. Sampling clock frequency:


 In AVR, sampling frequency have to be less than 200 kHz
INSIDE ADMUX REGISTER

• Bit 7:6 – REFS1:0 Reference Selection Bits


REFS1: REFS0 Voltage Reference Selection
00 AREF, Internal Vref turned off
01 AVCC with external capacitor at AREF pin
10 Reserved
11 Internal 2.56V Voltage Reference with external capacitor at AREF pin

• Bit 5 – ADLAR ADC Left Adjust Result


1= Left justified result
0= Right Justified result
INSIDE ADMUX REGISTER

• Bits 4:0 – MUX4:0


Analog Channel and Gain Selection Bits

Table:84 From Datasheet


INSIDE ADCSRA REGISTER

• Bit 7 – ADEN: ADC Enable


1= ADC is powered on
0= ADC is off

• Bit 6 – ADSC: ADC Start Conversion


1= Start Conversion
This bit becomes zero when conversion completed.
• Bit 5 – ADATE: ADC Auto Trigger Enable
1= Auto Triggering of the ADC is enabled (ADC start conv on +ve edge of selected
trigger signal in SFIO reg)
0= Auto Triggering of the ADC is disabled
• Bit 4 – ADIF: ADC Interrupt Flag : Become 1 when ADC conv completed. Should be cleared in
Software
•Bit 3 – ADIE: ADC Interrupt Enable
1= Enable ADC interrupt (Remember: Also enable I bit in SREG register to enable interrupt module)
0= Disable ADC interrupt
• Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits
These bits determine the division factor between the XTAL frequency and the input clock to the
ADC. (Rule to select this  Sampling Frequency<200Khz)
ADPS2: ADPS1: ADPS0 Division Factor
000 2
001 2
010 4
011 8
100 16
101 32
110 64
111 128
INSIDE SFIOR REGISTER

• Bit 7:5– ADTS2:0: ADC Auto Trigger Source


If ADATE in ADCSRA is written to one, the value of these bits selects which source will trigger
an ADC conversion . A conversion will be triggered by the rising edge of the selected Interrupt
Flag. If ADEN in ADCSRA is set, this will start a conversion. Switching to Free Running mode
(ADTS[2:0]=0) will not cause a trigger event, even if the ADC Interrupt Flag is set.

ADTS2: ADTS1 :ADTS0 Trigger Source


000 Free Running mode
001 Analog Comparator
010 External Interrupt Request 0
011 Timer/Counter0 Compare Match
100 Timer/Counter0 Overflow
101 Timer/Counter1 Compare Match B
110 Timer/Counter1 Overflow
111 Timer/Counter1 Capture Event
ADC CODING STEPS
Load ADCSRA reg Load ADMUX reg
DDRx setting
of channel • Clk prescaler? • Result format?
• ON bit? • Channel?
• Pin and Ref settings?

Goto next Take binary Wait till


Write 1 to conversion is Start
step result from
ADIF to complete by conversion by
according ADCL and ADCH
make it checking ADIF setting ADSC
your registers in
clr. flag bit. bit to hi
requirement sequence
PROBLEM: Write a code in Mikro-C to show digitized data of analog signal given at ADCO to
PORTB and PORTD?
SOLUTION:
void main()
{
DDRB=0xff; //make PORTB o/p
DDRD=0xff; //make PORTD o/p
DDRA.DDRA0=0; //make ADCO i/p
ADCSRA=0x87; //make ADC enable , clk/128 selected
ADMUX=0xC0; //2.56 as Vref, ADCO single ended i/p, Right Justified Result
while(1)
{
ADCSRA.ADSC=1; //start Conversion
while(ADCSRA.ADIF==0); // wait for Conv to be completed
PORTD=ADCL; //show LOW byte of Result @ PORTD
PORTB=ADCH; //show Hi byte of Result @ PORTB
ADCSRA.ADIF=1; //write 1 to clr ADIF flag
}
}
SAME CODE with Interrupt
SOLUTION:
Question: HOW to Enable Interrupt of
void main() particular peripheral in AVR??
{
ANS: we have to enable two bits:
DDRB=0xff; //make PORTB o/p
• SREG.I=1 to enable interrupt
DDRD=0xff; //make PORTD o/p module.
DDRA.DDRA0=0; //make ADCO i/p • Interrupt enable bit of that
peripheral.
SREG.I=1; //Enbale interrupt module
ADCSRA=0x8f; //make ADC enable with INTR, clk/128 selected
ADMUX=0xC0; //2.56 as Vref, ADCO single ended i/p, Right Justified Result
ADCSRA.ADSC=1; //start Conversion
while(1){}; void ADCinterrupt() org 0x20 //This function runs when adc interrupt
} occurs
{
if(ADCSRA.ADIF=1); // chk if ADC intr occurs?
{
PORTD=ADCL; //show LOW byte of Result @ PORTD
PORTB=ADCH; //show Hi byte of Result @ PORTB
ADCSRA.ADIF=1; //write 1 to clr ADIF flag
ADCSRA.ADSC=1; //start Conversion
}
ABOUT MIKROC
AVR LIBRARIES……

You might also like