Skip to main content

8. ADC Assembly Code Example

8.1 KodeFull LengkapCode

BerikutHere adalahis contohan kodeexample of AVR Assembly AVRcode untukto membacaread the ADC darifrom the ADC0 pin ADC0using menggunakan referensithe internal 2.56V danreference prescalerand a CLK/128:128 prescaler:

#define __SFR_OFFSET 0x00
#include "avr/io.h"
;------------------------
.global main

main:
    LDI R20, 0xFF
    OUT DDRD, R20       ; Set Port D sebagaias output (ADC result low byte hasil ADC)byte)
    OUT DDRB, R20       ; Set Port B sebagaias output (ADC result high byte hasil ADC)byte)
    SBI DDRC, 0         ; Set pin PC0 sebagaias input untukfor ADC0

    ;-- InisialisasiADC ADCInitialization --
    LDI R20, 0xC0       ; REFS1:REFS0 = 11 → Internal 2.56V
                        ; ADLAR = 0 → Right-justified
                        ; MUX4:MUX0 = 00000 → ADC0
    STS ADMUX, R20

    LDI R20, 0x87       ; ADEN = 1 → Enable ADC
                        ; ADPS2:ADPS0 = 111 → Prescaler CLK/128
    STS ADCSRA, R20

;-- ADC Reading Loop pembacaan ADC --
read_ADC:
    LDI R20, 0xC7       ; Set ADSC = 1 untukto memulaistart konversiconversion
    STS ADCSRA, R20

wait_ADC:
    LDS R21, ADCSRA     ; BacaRead ADCSRA status register ADCSRA
    SBRS R21, 4         ; Skip jump jikaif ADIF (bit 4) = 1 (konversiconversion selesai)complete)
    RJMP wait_ADC       ; LoopWait tungguloop sampaiuntil ADIF is set

    ;-- Reset ADIF flag --
    LDI R17, 0xD7       ; Set ADIF = 1 agarso the controller bisacan reset the flag
    STS ADCSRA, R17

    ;-- BacaRead hasilconversion konversiresult --
    LDS R18, ADCL       ; BacaRead low-byte darifrom ADCL (WAJIBMUST dibacaread dulu)first)
    LDS R19, ADCH       ; BacaRead high-byte darifrom ADCH

    ;-- Output hasilresult --
    OUT PORTD, R18      ; KirimSend low-byte keto Port D
    OUT PORTB, R19      ; KirimSend high-byte keto Port B

    RJMP read_ADC       ; UlangiRepeat pembacaanreading

8.2 PenjelasanCode KodeExplanation

BagianInitialization Inisialisasi:Section:

LDI R20, 0xC0
STS ADMUX, R20
  • 0xC0 dalamin binerbinary = 1100 0000
  • REFS1=1, REFS0=1 → Tegangan referensi internalInternal 2.56V reference voltage
  • ADLAR=0 → Right-justified output
  • MUX4:MUX0 = 00000 → MembacaReading pin ADC0 (A0)
  • BagianThis inipart hanyaruns dijalankanonly sekalionce saatduring inisialisasi.initialization.
LDI R20, 0x87
STS ADCSRA, R20
  • 0x87 dalamin binerbinary = 1000 0111
  • ADEN=1 → ADC diaktifkanis enabled
  • ADPS2:ADPS0 = 111 → Prescaler CLK/128 (125 kHz padaat 16 MHz)

BagianReading PembacaanSection (Loop):

LDI R20, 0xC7
STS ADCSRA, R20
  • 0xC7 dalamin binerbinary = 1100 0111
  • ADEN=1, ADSC=1 → MulaiStart satuone siklusconversion konversicycle
  • ADPS tetapremains samathe same (CLK/128)
wait_ADC:
    LDS R21, ADCSRA
    SBRS R21, 4
    RJMP wait_ADC
  • Polling loop: teruscontinuously membacareads ADCSRA danand mengecekchecks bit 4 (ADIF)
  • SBRS = Skip if Bit in Register Set → jikaif ADIF=1 (konversiconversion selesai)complete), skip RJMP
  • SelamaAs long as ADIF=0 (konversiconversion belumnot selesai)complete), teruscontinue looplooping
LDI R17, 0xD7
STS ADCSRA, R17
  • ResetResets the ADIF flag ADIFby denganwriting cara menulis nilaia 1 keto the ADIF bit ADIF
  • IniThis diperlukanis agarnecessary konversiso berikutnyathe bisanext terdeteksiconversion kembalican be detected
LDS R18, ADCL
LDS R19, ADCH
OUT PORTD, R18
OUT PORTB, R19
  • BacaRead ADCL terlebih dahulufirst (wajib)mandatory), baruthen ADCH
  • KirimSend hasilnyathe keresults to Port D (low-byte) danand Port B (high-byte)
  • KarenaSince it is right-justified: ADCH hanyaonly berisicontains 2 bitbits (bitbits 9 danand 8)