Thứ Sáu, 14 tháng 12, 2018

How to use internal ADC of AVR microcontroller using interrupts

Developed By: 

Akshay Daga
This article is in continuation to AVR interrupts. There are two types of interrupts external and internal in AVR microcontroller. The aforesaid article covers external interrupts. AVR microcontrollers have seventeen internal interrupts. These internal interrupts are generated by the internal peripherals of Microcontroller like Timer, ADC etc. The internal interrupts are used for efficient operation of the internal peripherals. This article explains the internal interrupts using the example of an ADC interrupt.

Configuring inbuilt ADC module of AVR microcontroller using interrupts Prototype



Each internal peripheral system consists of one IE (interrupt Enable) bit which activates the internal interrupts of that peripheral. For example, in-built ADC of AVR consists of ADIE (ADC interrupt Enable) bit in ADCSRA register.
In addition, the I-bit of SREG is also activated to activate interrupts. SREG is a status register of AVR microcontroller which contains information about the result of most recently executed arithmetic instructions.
SREG (Status Register):
Bit Configuration of SREG Register of AVR microcontroller to activate interrupts
Fig. 2: Bit Configuration of SREG Register of AVR microcontroller to activate interrupts
Bit 7-I: (Global interrupt Enable):
To activate Global Interrupts this bit must be set to high. If this bit is not enabled, none of the interrupts will work. “sei ()” command is used to enable the Global Interrupt, and “cli()” command is used to disable global interrupt.
For better clarification of internal interrupts, ADC interrupts is explained below:
ADC interrupts:
In the article of AVR ADC, polling method is used to receive converted value. During the polling of a signal, microcontroller cannot perform another task. Hence, it is better to use interrupt method. ADC system consists of ADIE bit in ADCSRA register. ADIE bit is enabled to use ADC interrupts.
ADC in auto-triggering mode:
In A/D conversion ADSC bit remains high till the conversion is not completed. As soon as the, conversion gets completed, ADSC automatically gets cleared by hardware. Before starting the next conversion, ADSC must be set high again. Alternatively, auto triggering can be used to enable the ADSC bit after each conversion. The ADATE (ADC Auto Triggering Enable) bit in ADCSRA register is used to activate auto-triggering mode. There are various triggering options available in AVR ADC, which can be selected by configuring ADTS (ADC Triggering Select) bits in SFIOR register.
SFIOR (Special Function I/O register):
Bt Configuration of ADTS in SFIOR register in AVR
Fig. 3: Bit Configuration of ADTS in SFIOR register in AVR
The following table shows the combination of ADTS [2:0] bits to select triggering source:
Bit combination of ADTS [2:0] to select triggering source
Fig. 4: Bit combination of ADTS [2:0] to select triggering source
Objective: A/D conversion using interrupt method and display 10-bit digital output on LCD as integer form.
Programming steps:
To initiate ADC:
1. Set the value in ADMUX register according to the ADC channel and the reference voltage.
2. Set the Prescaler bits accordingly in ADCSRA register.
3. Set the ADEN bit to enable the ADC.
4. Set ADIE bit to enable ADC interrupt.
5. Set ADATE bit to enable auto triggering.
6. Set ADSC bit to start conversion.
7. Configure SFIOR register to select trigger source. 

void adc_init()// ADC configuration
{
ADMUX=(1<<REFS0);//Vref=AVcc
// ADSC=1 ADC Enable
// ADPS[2:0]=111, prescaler=128
// ADIE=1, ADC interrupt Enable
//ADATE=1, ADC Auto Triggering Enable
ADCSRA=(1<<ADEN)|(7<<ADPS0)|(1<<ADSC)|(1<<ADIE)|(1<<ADATE);
//ADTS[2:0]= 100 , Timer0 overflow select as trigger source
SFIOR=(4<<ADTS0);
}

To read converted value:
1. Start Trigger Source for auto-triggering of ADC.  
2. Enable I-bit of SREG register to enable global Interrupt.
3. Store 10-bit converted data into 16-bit variable.
4. Convert the digital value to its corresponding ASCII value.
5. Display the converted value on LCD.





Circuit:
Circuit Diagram of How to use internal ADC of AVR microcontroller using interrupts

Code:
//Program to use internal ADC of AVR microcontroller using interrupts
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
 
#define lcd PORTB
#define rs PD0
#define rw PD1
#define en PD2
 
void adc_init(void);
unsigned int adc_read(void);
void adc_conversion(uint16_t);
void lcd_init(void);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
 
uint16_t adc_out;
 
int main()
{
unsigned char data[12]= "ADC OUTPUT:";
 
int i=0;
DDRD=0x07;
DDRB=0xFF;
DDRC=0xFF;
lcd_init();
 
while(data[i]!='\0')
{
lcddata(data[i]);
_delay_ms(5);
i++;
}
 
adc_init();
 
/* Configure timer0*/
 
TCCR0=(5<<CS00);
TIMSK=(1<<TOIE0);
 
sei();
while(1);
 
}
 
void adc_init() // ADC configuration
{
ADMUX=(1<<REFS0); //Vref=AVcc
// ADSC=1 ADC Enable
// ADPS[2:0]=111, prescaler=128
// ADIE=1, ADC interrupt Enable
//ADATE=1, ADC Auto Triggering Enable
ADCSRA=(1<<ADEN)|(7<<ADPS0)|(1<<ADSC)|(1<<ADIE)|(1<<ADATE);
 
//ADTS[2:0]= 100 , Timer0 overflow select as trigger source
SFIOR=(4<<ADTS0);
}
 
ISR(ADC_vect)
{
adc_conversion((ADC));
 
}
 
ISR(TIMER0_OVF_vect)
{
 
}
 
 
/* this function is written to convert interger value to their corresponding ASCII value*/
void adc_conversion(uint16_t adc_out)  
{
unsigned int adc_out1;
int i=0;
char position=0xC3;
 
for(i=0;i<=3;i++)
{
adc_out1=adc_out%10;
adc_out=adc_out/10;
lcdcmd(position);
lcddata(48+adc_out1);
position--;
}
}
 
void lcd_init()   // fuction for LCD initialization
{
lcdcmd(0x38);
lcdcmd(0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
}
 
void lcdcmd(unsigned char cmdout)
{
lcd=cmdout;
PORTD=(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(10);
PORTD=(0<<rs)|(0<<rw)|(0<<en);
 
}
 
 
void lcddata(unsigned char dataout)
{
lcd=dataout;
PORTD=(1<<rs)|(0<<rw)|(1<<en);
_delay_ms(10);
PORTD=(1<<rs)|(0<<rw)|(0<<en);
}
 
 

Không có nhận xét nào:

Đăng nhận xét

Bài đăng mới nhất

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01 # candientu ,  # fujihatsu ,  # candientufujihatsu  #candientu,  # candientufujhatsu , #fuji...

Bài đăng phổ biến