[ Log In ]
Image of the Atmega324p

Atmega324P

$8.50
Qty:
Serial to USB converter with Micro USB cable

USB to Serial Converter

$10.95
Qty:
Thumbnail: Crystal Oscillator 18.432 MHz for UART

18.432 MHz Crystal Oscillator 18pf 30ppm

$0.94
Qty:
Thumbnail: 22 pF Capacitor

22 pF Multilayer Ceramic Capacitor

$0.43
Qty:
Thumbnail: Quartz crystal oscillator - 16 MHz

16 MHz Crystal Oscillator 20 pF Through Hole

$0.75
Qty:
Thumbnail: 4x4 keypad top view.

4x4 Keypad with Adhesive Backing

$3.80
Qty:
Thumbnail: quad buffer line driver 74HC126E

Quad Buffer Line Driver (Through Hole)

$0.69
Qty:
USB AVR programmer

USB AVR Programmer

$9.95
Qty:
3 pin slide switch

SPDT Slide Switch 3 pin 30V

$1.49
Qty:
Handheld auto range multimeter

Handheld Auto Ranging Digital Multimeter

$17.95
Qty:
Skip Navigation Links

Reading Multiple ADC Channels on the AVR Atmega32

What does multiple channels mean? If you have more than one analog voltage source, more than one sensor, for instance, you can read all of them as long as the number of sources is fewer than the number of ADC pins you have on your microcontroller. With the Atmega32, there are 8 ADC pins. The only thing to keep in mind when programming the ADC to read multiple channels is that only one channel can be used in the conversion at a time.

If we had two sensors, and we are using channel (pin) 3 and 4 to read each sensor, we would first need to set the channel for 3 and then start a conversion. After the conversion is finished and the number is captured, then the channel can be switch to 4 and that number can be captures. These processes of getting the ADC results for each channel can be done in a loop and displayed on the LCD.

The ADC is initialized as we did with the accelerometer readings and in this case, we are connecting the X axis of the accelerometer to channel 0 and the Y axis to channel 1. There is not preinitialization of the ADC channel before we enable the ADC because we are starting with channel 0 which is the default channel.

In the interrupt service routine (the routine starting with ISR), you will see a few new statements, such as switch and case. This is a very nice way to make a selection according to a value contained by a variable. It's like making a restaurant menu selection. The variable that is contained within parentheses after the word switch is the restaurant customer's selection and the various cases listed below are all the menu selections.

#include <avr/io.h>
#include <avr/interrupt.h>
#include "MrLCD.h"
int main(void)
{
InitializeMrLCD();
Send_A_StringToMrLCDWithLocation(1,1,"X:");
Send_A_StringToMrLCDWithLocation(1,2,"Y:");

ADCSRA |= 1<<ADPS2;
ADMUX |= 1<<REFS0 | 1<<REFS1;
ADCSRA |= 1<<ADIE;
ADCSRA |= 1<<ADEN;

sei();

ADCSRA |= 1<<ADSC;

while (1)
{
}
}
ISR(ADC_vect)
{
uint8_t theLow = ADCL;
uint16_t theTenBitResult = ADCH<<8 | theLow;

switch (ADMUX)
{
case 0xC0:
Send_An_IntegerToMrLCD(4, 1, theTenBitResult, 4);
ADMUX = 0xC1;
break;
case 0xC1:
Send_An_IntegerToMrLCD(4, 2, theTenBitResult, 4);
ADMUX = 0xC0;
break;
default:
//Default code
break;
} ADCSRA |= 1<<ADSC;
}