0% found this document useful (0 votes)
37 views

Microcontrollers, ADC and Interrupts: Professor

This document discusses microcontrollers, analog to digital converters (ADCs), and interrupts. It provides sample code for using a 7-segment display and ADC polling. It describes different types of ADCs, properties of ADCs like resolution and conversion time, and preliminary concepts of using an ADC and interrupts on a microcontroller. It also provides exercises for modifying sample codes to read multiple analog signals and show data on an LCD using interrupts instead of polling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Microcontrollers, ADC and Interrupts: Professor

This document discusses microcontrollers, analog to digital converters (ADCs), and interrupts. It provides sample code for using a 7-segment display and ADC polling. It describes different types of ADCs, properties of ADCs like resolution and conversion time, and preliminary concepts of using an ADC and interrupts on a microcontroller. It also provides exercises for modifying sample codes to read multiple analog signals and show data on an LCD using interrupts instead of polling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Microcontrollers, ADC and Interrupts

Professor:
Bladimir Bacca Cortes Ph.D.
Perception and Intelligent Systems Research Group
Email:
[email protected]
Contents
• Programming model
• Basic input / output and SFRs
• LCD sample code.
• 7 segment sample code.
• IO Handling
• Poling.
• Analog to digital converter
• Preliminary concepts (ADC, interrupts)
• Temperature reading on 7 segment sample code.
• IO Handling
• Interrupts
• Preliminary concepts
• Sources
• Interrupt vector and tables.
Programming Model
• Microcontrollers: micro-computer on single chip, it means: processing capabilities, data and program
storing capabilities and peripheral handling capabilities.
• Programming philosophy: It is focused on registers. They are the main programming interface with internal
peripherals, I/O management, and memory interface.
• Tools needed:
• Development platform: microcontroller, or evaluation board, or development system or simulator.
• Cross-compiler from C, or Java or C++ to the development platform machine code.
• IDE: Integrated Development Environment
• Debugger (optional)
Basic Input / Output and SFRs
• Basic software structure of I/O ports
• Control data: All information related with operation modes, how to initiate or end operations, how
activate / deactivate data transfers or enable / disable specialized properties.
• State data: It is focused on showing the I/O device state, checking if some operation is ended, or in
progress, and errors occurred.
• Data: Data processed, acquired or received by the I/O device.
Basic Input / Output and SFRs
• Sample code and simulation: 7 Segments
Display
• Simulation: 7segments.DSN
• Code: 7Segments.c

• Goal: To show counter on the 7 segments


display.

• Port listing:
• Port B handle the diodes in the 7
segment display.
• Port A performs the time
multiplexing needed to show stable
numbers.
Basic Input / Output and SFRs
• Sample code and simulation: 7 Segments Display …

• Simulation: 7segments.DSN DDRB = 0xFF;

• Code: 7Segments.c DDRA = 0xFF;

while(1)
/* Code for single pin addressing */
{
typedef struct
num ++;
{
if (num> 99) num = 0;
unsigned char bit0:1;
• EXERCISE:
unsigned char bit1:1;
for (i=0; i<10; i++) – Change the C code
unsigned char bit2:1;
{ inside the WHILE loop
unsigned char bit3:1; in order to show a
D0=0; D1=0;
unsigned char bit4:1; count up 999.
PORTB = SEVEN_SEG[num%10];
unsigned char bit5:1;
D0=1; D1=0;
unsigned char bit6:1;
_delay_ms(10);
unsigned char bit7:1;
D0=0; D1=0;
}io_reg;
PORTB = SEVEN_SEG[num/10];
D0=0; D1=1;
#define D0 ((volatile io_reg*)_SFR_MEM_ADDR(PORTA))->bit4
_delay_ms(10);
#define D1 ((volatile io_reg*)_SFR_MEM_ADDR(PORTA))->bit5
}
#define D2 ((volatile io_reg*)_SFR_MEM_ADDR(PORTA))->bit6
}
#define D3 ((volatile io_reg*)_SFR_MEM_ADDR(PORTA))->bit7
Basic Input / Output and SFRs
• Bit Field Structures • Recommended exercises:
typedef struct • To modify the 7Segments.c program in order
to count up 999.
{
unsigned char bit04:5; // Valid range: 0 to 32
• To design and implement a solution to
unsigned char bit5:1; // Valid range: 0 to 1 measure the rotational speed of a DC motor,
unsigned char bit67:2; // Valid range: 0 to 3 and showing it on the 7 segment displays.
}io_reg;
• Replacing the 7 segment displays for LED
matrices of 5x7 perform the same results of
#define MUX04 ((volatile io_reg*)_SFR_MEM_ADDR(ADMUX))->bit04 the above programs.
#define ADLAR ((volatile io_reg*)_SFR_MEM_ADDR(ADMUX))->bit5
#define REFS01 ((volatile io_reg*)_SFR_MEM_ADDR(ADMUX))->bit67

MUX04 = 0x08;
ADLAR = 0;
REFS01 = 0x01;
IO Handling – Polling
• Properties
• I/O management method previous interrupts.

• The CPU plays a direct role on acquiring data


from external devices.

• Polled I/O introduces busy wait states into CPU


because the CPU is faster than external
devices.

• In some situations, when a RTOS micro-kernel


is available polling could be the right thing
because CPU defines how often to poll.

• In general, it is not recommended since it


depends on how busy is the CPU to interact
with the external device.
Digital to Analog Converter
• Goal: To convert a digital word (8bits, 16bits, etc) to analog voltage.

• Properties:
• Resolution: Minimum change in the DAC input that causes a change in the DAC output.
• Linearity error: It measures how lineal is the DAC output in all its range.
• Bias error: If the input signal is cero, the output must be cero. Otherwise, it is called bias error.
Analog to Digital Converter
• Goal – Getting the digital representation of an analog signal.

• Properties:
• Resolution: Minimum change in the ADC input that causes a change in the ADC output.
• Linearity error: It measures how lineal is the ADC output in all its range.
• Bias error: If the input signal is cero, the output must be cero. Otherwise, it is called bias error.
• Conversion time: Amount of time taken from the beginning of an ADC conversion to its end.
Analog to Digital Converter
• Types of ADC:
• ADC by integration,
• Over a short period of time the input voltage is stored into a capacitor.

• Afterwards, this capacitor is discharged at known rate.

• When the capacitor discharging is started a counter is activated.

• Finally, when the capacitor voltage is cero, an analog comparator deactivates the digital counter.
This counter stores the digital representation of the input voltage.
Analog to Digital Converter
• Types of ADC:
• Up-Down ADC or Tracking ADC
• First, the analog input is compared with the DAC output.

• If the analog input is bigger than the current reference, the up-down counter is incremented.
Otherwise, it is decremented.

• In this way, the digital counter is adjusted to equal the input voltage.
Analog to Digital Converter
• Types of ADC:
• Successive approximation ADC
• This method consists in successive dividing the full conversion range.

• If the current voltage value is bigger than the input value, the first half of the ADC conversion range
is considered. Otherwise, the second half is considered.

• In this way, the current voltage value successively approximates the input voltage value.
Preliminary Concepts – I/O Polling
• Sample code and simulation: ADCpolling • Recommended exercises:
• Simulation: ADCpolling.DSN • To modify the adcPolling.c program in order
• Code: adcPolling.c to show the data on a LCD. But, using all the
channels of the ADC with different signals:
potentiometer, photocells, etc.
• Goal: Showing current temperature in the 7
segment displays.

• Port listing:
• Port A(0) reads the analog variable.
• Port B handle the diodes in the 7 segment
display.
• Port A performs the time multiplexing needed
to show stable numbers.
Analog to Digital Converter
• ATMEGA32 ADC
• Analog input module

• Voltage reference module

• Configuration module

• ADC module.
Analog to Digital Converter
• ATMEGA32 ADC
• It has 8 analog channels with a single 10-bit ADC.
• First, the analog channel is selected from the ADMUX register
• In addition, ADLAR bit is used to left or right adjust the conversion value in the ADC data register.
• Also, the REFS0/1 bits are used to select the reference voltage (internal or external).
• Second, the ADCSRA register is configured. This includes: enabling / disabling the ADC and the triggering
mode, enabling the ADC interrupt and selecting the ADC frequency of operation
Analog to Digital Converter
• ATMEGA32 ADC
• ADC result single ended
• ADC result differential configuration.
Analog to Digital Converter
• ATMEGA32 ADC
• This ADC is designed to operate between 50Khz and 200Khz if full range is required.
• Upper frequencies are allowed for lower resolutions.
• ADPS0-2 bits select the ADC frequency of operation.
• Normally, an ADC conversion takes 13 ADC clock cycles.
Analog to Digital Converter
• ATMEGA32 ADC
• The ADC frequency of operation is implemented as a factor of the main CPU frequency of operation.
• Third, the SFIOR register setting up the condition on which the ADC is triggered. It includes: free running,
analog comparator, external interrupt request and using timers (0 and 1).
• Finally, table 81 includes the conversion times.
Preliminary Concepts – I/O Polling
• Sample code and simulation: ADCpolling
• ADC polling:
• Simulation: ADCpolling.DSN
• Code: adcPolling.c
// Getting temperature
• No interrupts:
ADCSRA |= (1 << ADSC); // Start ADC
while(!(ADCSRA & (1<<ADIF))) // Wait ADC...
/* adc initialization */
{
ADCSRA |= (1 << ADPS0);
PORTC = 0x01;
ADMUX |= (1 << REFS1) | (1 << REFS0);
PORTC = 0x00;
}
//Set ADC to free run mode
SFIOR &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); PORTC = 0x00;
ADCSRA|=(1<<ADIF); // Clear EOC

// Enables ADC for use temperature = ADCL>>2; // Saving ADC


ADCSRA |= (1 << ADEN); garbage = ADCL;
garbage= ADCH;
// Enable ADC Interrupt
//ADCSRA |= (1 << ADIE);

// ADC Auto Trigger Enable


ADCSRA |= (1 << ADATE);

// Enable Global Interrupts


//sei();
Microcontroller Programming
• To develop a microcontroller based solution to do the following tasks: measuring the
current temperature, and showing in three LEDs if the temperature corresponds to cold,
chilly or hot.
Programming Sprint!!!!!

What is my reward?

1/10 in the final note to both members


of the team.
Study Case – Mobile Robot Driven by Photocells
• Goal: Drive a mobile robot depending on • Operation concept:
what part of the environment is lighter.
• Tools:
• Proteus 8.8
→ → →
• Flowchart project. I = I X i + IY j
• Arduino Turtle.

Iy

OXL
OYR

Ix
Study Case – Mobile Robot Driven by Photocells
• Goal: Drive a mobile robot depending on • Operation concept:
what part of the environment is lighter.
• Tools:
• Proteus 8.8
→ → →
• Flowchart project. I = I X i + IY j
• Arduino Turtle.

O XL   cos sin  O X 
 O  = − sin cos   OY 
 YR  
IO Handling – Interrupts
• Properties
• It eliminates the CPU busy wait states when
polling is used.

• It can deal with asynchronous I/O. It means,


I/O devices with irregular data interchange
rate.

• It can introduce priorities. This method


allows hierarchical I/O systems.

• Nowadays, it is fundamental for embedded


systems, real-time micro-kernels and multi-
tasking systems.
IO Handling – Interrupts

• Process
• Firstly, the microcontroller is executing the
main program.

• If an interrupt event happens the CPU halts


the main program execution. The following
information is stored in the stack:
• PC – Program Counter.
• CPU registers
• Flags.
IO Handling – Interrupts

• Process
• Firstly, the microcontroller is executing the
main program.

• If an interrupt event happens the CPU halts


the main program execution. The following
information is stored in the stack:
• PC – Program Counter.
• CPU registers
• Flags.
IO Handling – Interrupts

• Process
• Firstly, the microcontroller is executing the
main program.

• If an interrupt event happens the CPU halts


the main program execution. The following
information is stored in the stack:
• PC – Program Counter.
• CPU registers
• Flags.
IO Handling – Interrupts

• Process
• The CPU executes the interrupt handler
code.

• The CPU restores the following information


in order to resume the main program
execution:
• PC – Program Counter.
• CPU registers
• Flags.
IO Handling – Interrupts

• Process
• The CPU executes the interrupt handler
code.

• The CPU restores the following information


in order to resume the main program
execution:
• PC – Program Counter.
• CPU registers
• Flags.
IO Handling – Interrupts
• Sample code and simulation: External Interrupt • Recommended exercises:
• Simulation: extInt.DSN • Implementing a zero-cross detector. It uses a
• Code: extInt.c sinusoidal input signal.

• Goal: It shows how external interrupts work.


• Using a suitable input signal and the 7 segment
• Port listing: displays, design and implement a digital clock.
• Port D the external interrupt input.
• Port A LEDs
IO Handling – Interrupts
• Interrupt types
• Hardware
• They are generated by external I/O or
internal microprocessor devices.
• They need a physical electrical signal
• They can be enable or disable using flags
(Masking).
• A priority can be assigned to each interrupt
source.
• Software
• They can be generated by user programs.
• They do not need electrical signal, but set
or clear bit flags.
• They can not be maskable.
• Non-maskable
• An electrical signal is specially focused on it.
• It has the highest priority.
• It cannot be maskable.
• The only one interrupt source with higher
priority is the RESET.
IO Handling – Interrupts
• Interrupt types
• Internal CPU interrupts
• In general, they respond to exceptional
situations given by the ALU flags.
• Arithmetic-based: division by zero.
• Flow control: non implemented instruction,
or trap or break points.
• They can be maskable or non-maskable.
• RESET interrupt
• It is generated by pressing the reset button
or when the system is starting up.
• It includes an initialization sequence in
order to stabilize the internal electrical
signals.
• In order to avoid accidental re-
initializations, it requires the reset electrical
signal is set over a pre-defined CPU cycles.
IO Handling – Interrupts
• Interrupt process and Interrupt Table
• Once an external device requested
attention to CPU, a small program is
executed at specified location in memory.

• Interrupt Service Routine (ISR): Small


program to execute when an interrupt
arrives.

• Interrupt Vector (IV): Location in memory


where the ISR is placed. In most
microcontrollers, it is fixed; however, in
standard microprocessors it is
programmable. The CPU automatically
loads this address to continue the
instruction execution.

• Interrupt Vector Table (IVT): Set of IVs for


each interrupt source available.
IO Handling – Interrupts
• Interrupts management
• Different interrupt sources converge into the interrupt enable register.
• In this register, interrupt sources are masked. It is done enabling or disabling specific interrupt sources.
• The microcontroller CPU is interrupted using only one electrical signal.
• Afterwards, the CPU performs a poling sequence in order to check which interrupt source to serve.
• Note: the poling sequence in most microcontrollers is fixed.
IO Handling – Interrupts
• Interrupt latency
• It is defined as the time elapsed between the interrupt request by the I/O device and the interrupt
acknowledge by the CPU.

• Interrupt dispatch latency


• In real-time programming is important considering this latency measured between the ISR ends and the
task resumes.
IO Handling – Interrupts
• ISRs design considerations
• Performed by the CPU
• On service, saving flag, IP (PC) and CPU state registers
• On ISR end, restoring flag, IP (PC) and CPU state registers
• Performed by the C compiler Service ISR end
• On service, saving CPU state and CPU RAM registers
where the C environment is stored.
• On ISR end, restoring those registers.
• Performed by users.
• Keep ISR as short as possible.
• Only data interchange with the main program.
• Do not process data within the ISR.
• Do not depend on data interchange with other I/O
devices.
• Do not wait for flags.
• For data interchange with the main program, it is a good
option using global data structures.
Analog to Digital Converter – Interrupts
• Sample code and simulation: ADC • Recommended exercises:
• Simulation: ADC.DSN • Using one photocell, to write a program to
• Code: adc.c compute the illumination average from the
last 30 samples, extract the standard
deviation and show these data in a LCD.
• Goal: Showing current temperature in the 7 • To write a program which shows in a LCD the
segment displays. maximum peak of a sinusoidal signal. Note:
Please use a wave generator and configure it
with an offset in order to get positive values
• Port listing: only.
• Port A(0) reads the analog variable.
• Port B handle the diodes in the 7 segment
display.
• Port A performs the time multiplexing needed
to show stable numbers.
Analog to Digital Converter
• Sample code and simulation: ADC ISR(ADC_vect) {
• Simulation: ADC.DSN temperature = ADCL>>2; /*collect sample from ADC */
• Code: adc.c garbage = ADCL;
garbage= ADCH;
/* adc initialization */ }
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); …
ADMUX |= (1 << REFS1) | (1 << REFS0); while(1)
{
//Set ADC to free run mode D0 = 0; D1 = 0;
SFIOR &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); PORTB = SEVEN_SEG[temperature/10];
D0 = 0; D1 = 1;
// Enables ADC for use while (++i < delay);
ADCSRA |= (1 << ADEN);

D0 = 0; D1 = 0;
// Enable ADC Interrupt PORTB = SEVEN_SEG[temperature%10];
ADCSRA |= (1 << ADIE); D0 = 1; D1 = 0;
while (--i > 0);
// ADC Auto Trigger Enable }
ADCSRA |= (1 << ADATE);

// Starts ADC conversion


ADCSRA |= (1 << ADSC);
Microcontroller Programming
• To modify the last project to do the following tasks: measuring the current temperature,
and showing in three LEDs if the temperature corresponds to cold, chilly or hot, but using
interrupts.
Questions?

You might also like