Mengakses ADC pada AVR ATMega16

Posted on Updated on

ADC digunakan untuk mengubah input analog(tegangan kontinyu) menjadi data digital(ex: 10110011) atau binary. pada uC ATMega16 sudah terintegrasi ADC 8 input dengan resolusi sebesar 10 bit didalamnya sehingga kita bisa memanfaatkannya tanpa menggunakan IC ADC tambahan + menghemat pengeluaran. ADC pada uC ATMega16 berada di PORTA.

Pada project ini input menggunakan potensiometer sebagai simulasi tegangan dari sensor. Vinput dimasukkan pada PORTA.0 ATMega16. Aref dan AVcc dihubungkan ke Vcc 5 volt. Tampilan pada project ADC ini menggunakan LCD 16×2 yang dihubungkan ke PORTC.

dibawah ini adalah sourcecode untuk mengakses ADC ATMega16 yang ditulis dalam bahasa C dengan compiler CodeVisionAVR :

Link untuk download Full Sourcecode :

donlod

/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.4a Advanced
Automatic Program Generator
© Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 26-Jan-2017
Author  : Adiholix
Company : WEIP
Comments:

Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 11.059200 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega16.h>
#include <stdio.h>
#include <delay.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
char lcd_buffer[33];
float vinput;
unsigned int dataadc;

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

// LCD module initialization
lcd_init(16);

while (1)
{
// Place your code here
dataadc=read_adc(0);
vinput=((float)dataadc*(5/1024)); // vcc = 5v ; adc 10 bit = 1024 desimal
lcd_gotoxy(0,0);
sprintf(lcd_buffer,"Volt :%0.0002f V",vinput);
lcd_puts(lcd_buffer);
printf("%0.0002f",vinput);
delay_ms(300);

};
}
Advertisements

10 thoughts on “Mengakses ADC pada AVR ATMega16

    KonstantinMiller said:
    July 6, 2009 at 6:59 pm

    I think I will try to recommend this post to my friends and family, cuz it’s really helpful.

    ADIHOLIX responded:
    October 1, 2009 at 1:48 am

    ok,thank for your visit…

    Cah Ndeso said:
    October 10, 2009 at 9:16 am

    mas adi….
    lamo tak jumpo…..
    mana ne artikel2 trbarunya??
    saya tunggu slalu lho!
    accelerometer nya mohon di share donk!
    I always wait your action…..

      ADIHOLIX responded:
      October 11, 2009 at 11:04 am

      hehehe….sampeyan aja yang kirim artikel ke saya..ntar tak upload..ini masih proses mas prita..nyante aja ya..

    bheny said:
    November 16, 2010 at 7:48 am

    mas adi… bgmn dengan sourcecode untuk AVR ATmega8535? apakah sama saja or?
    untuk mengakses adc, input LM35..
    thks

    rep

    Bheny Shiregar said:
    December 5, 2010 at 9:16 am

    Mas adi.. bgmn dg sourcecode adc pada atmega8535???
    input LM35, output LCD
    apakah sama saja??

    hendra said:
    July 12, 2011 at 9:14 pm

    mas bisa mnta contoh penggunaan password dinamis(pasword dpat d rubah) menggunakan atmega8535 gg?/coding nya d codevision,,klo bsa sama gambar rankaian nya,,pliis,,

    thx

      ADIHOLIX responded:
      July 13, 2011 at 3:57 pm

      Bisa mas,,biayanya 500rb..

      ADIHOLIX responded:
      August 8, 2011 at 9:13 pm

      Maaf,,kalo request project lain kali aja ya..makasih

    rian adiputra said:
    March 22, 2015 at 12:47 am

    pak. saya menggunakan mikro atmega16 untuk menyalakan led dengan data analog dari ldr, tapi led selalu menyala walaupun data analog ke PINA.0 sudah berubah. program juga tidak ada masalah waktu dicompile. kira2 apa ya penyebabnya? terima kasih
    while (1)
    {
    // Place your code here
    static float vin;
    vin=((float)read_adc(0)*5/(1024));
    if(vin>=2){
    PORTD.0=1; //output led
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s