Microcontrollers, ADC and Interrupts: Professor
Microcontrollers, ADC and Interrupts: Professor
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
• 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 …
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.
• 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.
• 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
• 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
What is my reward?
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.
• Process
• Firstly, the microcontroller is executing the
main program.
• Process
• Firstly, the microcontroller is executing the
main program.
• Process
• Firstly, the microcontroller is executing the
main program.
• Process
• The CPU executes the interrupt handler
code.
• Process
• The CPU executes the interrupt handler
code.
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);