Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Chalk Up Microcontroller!

Presented By: Khandker M. Qaiduzzaman


CSE Discipline, Khulna University
E-mail: nafees.ku11@gmail.com
Contact No.- 01685679768
Our Concerns Today

• Analog to Digital Conversion (ADC)


• Interrupt
• Simulation and Design
Analog to Digital Conversion
(ADC)
What is ADC?

• An analog-to-digital conversion (ADC, A/D, or A to D) is the conversion of


a continuous physical quantity (usually voltage) to a digital number that
represents the quantity's amplitude.
• We can get a digital value from ADC that can be processed in microcontroller.
• ATmega8 has 10-bit ADC. The lowest value is 20 − 1 = 0 𝑡𝑜 210 −
1 = 1023.

• The ADC reports a ratio-metric value. This means that the ADC
assumes 5V is 1023 and anything less than 5V will be a ratio between
5V and 1023.
• 𝑅𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 𝑜𝑓 𝑡ℎ𝑒 𝐴𝐷𝐶
𝑆𝑦𝑠𝑡𝑒𝑚 𝑉𝑜𝑙𝑡𝑎𝑔𝑒
=
𝐴𝐷𝐶 𝑅𝑒𝑎𝑑𝑖𝑛𝑔
𝐴𝑛𝑎𝑙𝑜𝑔 𝑉𝑜𝑙𝑡𝑎𝑔𝑒 𝑀𝑒𝑎𝑠𝑢𝑟𝑒𝑑

• Analog to digital conversions are dependant on the system voltage.


Because we predominantly use the 10−bit ADC of the ATmega8 on a
5V system, we can simplify this equation slightly:
• 1023
5
=
𝐴𝐷𝐶 𝑅𝑒𝑎𝑑𝑖𝑛𝑔
𝐴𝑛𝑎𝑙𝑜𝑔 𝑉𝑜𝑙𝑡𝑎𝑔𝑒 𝑀𝑒𝑎𝑠𝑢𝑟𝑒𝑑
• If the analog voltage is 2.5V what will the ADC report as a value?

• 1023
5
=
𝑥
2.5
=> 𝑥 = 511.5 ≈ 512

• If the analog voltage is 2.12V what will the ADC report as a value?

• 1023
5
=
𝑥
2.12
=> 𝑥 = 433.752 ≈ 434
Registers Used for ADC
• ADMUX- ADC Multiplexer Selection Register
• ADCSRA- ADC Control and Status Register A
• ADCL & ADCH- ADC Data Registers
ADMUX
(ADC Multiplexer Selection Register)

• ADMUX is an 8 – bit register.

• Bit 0 – MUX0
• Bit 1 – MUX1
• Bit 2 – MUX2
• Bit 3 – MUX 3
• Bit 4 – Reserved
• Bit 5 – ADLAR
• Bit 6 – REFS0
• Bit 7 – REFS1
• Bits 7:6 – REFS1:0 – Reference Selection Bits – These bits are used to
choose the reference voltage. The following combinations are used.
• Bits 3:0 – MUX3:0 - Analog Channel Selection Bits: To select ADC0 – ADC5
channels, we use the combination of MUX0 – MUX3 bits.
• Bit 5 – ADLAR – ADC Left Adjust Result – Make it ‘1’ to Left Adjust the ADC
Result. We will discuss about this a bit later.
ADCSRA
(ADC Control and Status Register A)

• ADCSRA is a 8-bit register.

• Bit 0 – ADPS0
• Bit 1 – ADPS1
• Bit 2 – ADPS2
• Bit 3 – ADIE
• Bit 4 – ADIF
• Bit 5 – ADFR
• Bit 6 – ADSC
• Bit 7 - ADEN
• Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits – The ADC used in Atmega8
supports 250 KHz sampling rate. But, the MCU has 1 MHz or, 4 MHz or, 12
MHz operating frequency that is far more higher than 250 KHz. So, we
have to set prescale rate to reduce the main MCU frequency.
• If the MCU operating frequency = 8 MHz then, setting 111 means the
8 𝑀𝐻𝑧
frequency for ADC will be = = 62.5 KHz < 250 KHz
128
• Bit 7 – ADEN: ADC Enable - Writing this bit to one enables the ADC. By
writing it to zero, the ADC is turned off. Turning the ADC off while a
conversion is in progress, will terminate this conversion
• Bit 6 – ADSC: ADC Start Conversion - Write this bit to one to start each
conversion.
ADCL & ADCH
(ADC Data Register)

• ADCL is a 8 - bit register


• ADCH is a 8 - bit register
• So, we have to make is 10 bit. Rest of 6 bit will stay empty.

• Now, Look back to ADMUX register. There is a bit named ADLAR.


• If ADLAR = 0, the 8 – bit of ADCL will be used, rest 2 – bit will be used from
the ADCH taking the least significant 2 – bits.
• This is used mostly.

• If ADLAR = 1, the 8 – bit of ADCH will be used, rest 2 – bit will be used from
the ADCL taking the most significant 2 – bits.
Now, It’s Initialization Time

• Reference Selection
• Prescale Selection
• Enabling ADC

• We have to write an initialization function for ADC like, init_ADC()


Code 1
#include <inttypes.h>
#include <avr/io.h>

void adc_init(void); // ADC Initialization Routine


uint16_t adc_read(void); // ADC Data Read Routine

void adc_init(void)
{

ADMUX |= (1 << REFS1) | (1 << REFS0); // Internal reference 2.56V is used as reference voltage.

ADCSRA |= (1 << ADPS2) | (1 << ADPS1);


ADCSRA &= ~(1 << ADPS0); // To set division factor = 64, set 110 to ADPS2:0 bits.

ADCSRA |= (1 << ADEN); // To enable the ADC.

}
Then, Read The ADC

• Channel Selection
• Start Conversion
• Read Data

• We have to write an read function for ADC like, uint16_t adc_read(void)


Code 2

uint16_t adc_read(void)
{
ADMUX |= (1 << MUX0);// To select ADC1 as channel set value 0001
ADCSRA |= (1 << ADSC); // Start Conversion
// whenever ADSC will be set, ADC conversion will start. Whenever the
// conversion will end the ADSC will automatically clear. So, we have to check
// when the conversation is going to end using a loop.
while(ADCSRA & (1 << ADSC))
{
}
return ADCW; // 10–bit ADCL and ADCH merges to ADCW
}
int main(void)
{
DDRB |= (1 << DDB0); //Set data Direction
uint16_t adc_value = 0;//Initialize the variable
adc_init();//Initialize ADC.

while(1)
{
adc_value = adc_read();
if(adc_value < 100)
{
PORTB = 0b00000000;
}
else
{
PORTB = 0b00000001;
}
}
return 0;
}
Interrupt
What Is Interrupt?

• Interrupts are basically events that require immediate attention by the


microcontroller.
• When an interrupt event occurs the microcontroller pause its current task
and attend to the interrupt by executing an Interrupt Service Routine
(ISR) at the end of the ISR the microcontroller returns to the task it had
pause and continue its normal operations.
Types of Interrupt

• The RESET interrupt - Triggered from pin 1.


• External Interrupt 0 (INT0) - Triggered from pin 4.
• External Interrupt 1 (INT1) - Triggered from pin 5.
Interrupt Priority
of
ATmega8
3 Registers Are Used For Interrupt

• SREG – Status Register


• MCUCR – MCU Control Register
• GISR – General Interrupt Control Register
SREG
(Status Register)

• Bit 7 – I: Global Interrupt Enable - The Global Interrupt Enable bit must be
set for the interrupts to be enabled.
MCUCR
(MCU Control Register)

• MCUCR is a 8 – bit register

• Bit 0 – ISC00
• Bit 1 – ISC01
• Bit 2 – ISC10
• Bit 3 – ISC11
• Bit 1, 0 – ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0 – This
two bits are used for the interrupt sense control of INT0.
• Different combination of ISC01 and ISC00 is selected to define the Interrupt
Sense Control.
• Bit 3, 2 – ISC11, ISC10: Interrupt Sense Control 1 Bit 1 and Bit 0– This two
bits are used for the interrupt sense control of INT0.
• Different combination of ISC11 and ISC10 is selected to define the Interrupt
Sense Control.
GICR
(General Interrupt Control Register)

• Bit 6 – INT0 – Set to choose External Interrupt 0.


• Bit 7 – INT1 – Set to choose External Interrupt 1.
Steps of Using Interrupt

• Enable Global Interrupt


• Set Interrupt Sense
• Enable Individual Interrupts
Code 1
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

void init_int0(void);

void init_int0()
{
// To enable Global Interrupt.
sei();

// To set Interrupt Sense. If we want to interrupt at rising edge then we have


// to set 11 value of ISC01 and ISC00.
MCUCR |= (1 << ISC01) | (1 << ISC00);

// To enable INT0.
GICR |= (1 << INT0);
}
Code 2
ISR(INT0_vect)
{
int i;

// Blink all the bits of PORTC for five times.


for(i = 0; i <=5; i++)
{
PORTC = 0xFF;
_delay_ms(25);

PORTC = 0x00;
_delay_ms(25);
}
}
Code 3
int main(void)
{
int count;
DDRB = 0xFF;
DDRC = 0xFF;
init_int0();

while(1)
{
for(count = 0; count <= 7; count++)
{
PORTB = (1 << count);
_delay_ms(10);
}

for(count = 7; count >= 0; count--)


{
PORTB = (1 << count);
_delay_ms(10);
}
}
return 0;
}
Thank You!

You might also like