05/02/10

Aplikasi ADC ATMega8

Aplikasi ADC ATMega8


Yogyakarta-- Untuk aplikasi ADC menggunakan bahasa Bascom AVR sangatlah sederhana, kita tidak perlu memahami lebih dalam tentang proses yang terjadi pada LCD dan ADC. Atau Bisa baca Artikel sebelumnya. Kita cukup tau perintah yang digunakan untuk mengakses perangkat tersebut melalui bahasa pemrograman. Pada aplikasi ini saya mencoba menggunakan ATMega8. Gambar rangkaiannya cukup sederhana seperti gambar dibawah ini:



Gambar Rangkaian Aplikasi ADC menggunakan ATMega8


Berikut ini adalah contoh program sederhana yang dapat di pahami dan dikembangkan dengan mudah dalam aplikasi yang kompleks. Mudah-mudahan bermanfaat. Terima kasih.


Contoh Program Bascom:
$regfile = "m8def.dat"
$crystal = 4000000
Config Portb = Output
Config Lcd = 16 * 2
Config Lcdpin = Pin, Rs = Portd.4, E = Portd.5, Db4 = Portd.0
Config Lcdpin = Pin, Db5 = Portd.1 , Db6 = Portd.2 , Db7 = Portd.3
Config Adc = Single , Prescaler = Auto

Start Adc
'With STOP ADC, you can remove the power from the chip
'Stop Adc

Dim W As Word , Channel As Byte
Channel = 0
'Baca Nilai ADC dari channel 0

Do
Locate 1 , 1
Lcd "Nilai ="
W = Getadc(channel)
Locate 1 , 9
Lcd " "
Locate 1 , 9
Lcd W
Waitms 100
Loop
End












;-------------------------------------------------------------------------------------------------------

Contoh Program Assembler:

;-------------------------------------------------------------------------------------------------------

;-------------------------------------------------------------------------------------------------------

;---------------------------------------------------

;---------------------------------------------------

;**************nYOBIE AD-Converter******************

; LATIHAN

; BASIL

;---------------------------------------------------

;---------------------------------------------------

.dseg

LCD_buff: .byte 6 ;1byte for control--> 5byte for asciidata

ADCBuff_L: .Byte 1

ADCBuff_H: .Byte 1

Volt: .byte 2


.equ AtBCD0 =13 ;for bin2bcd purpose

.equ AtBCD2 =15

.equ ADC_Mux =0b11000001 ;chnl 1


.cseg


.include "m8def.inc" ;AVR-AT Mega8**********


rjmp reset;00.reset vector

reti ;01.INT0 External Interrupt Request 0

reti ;02.INT1 External Interrupt Request 1

reti ;03.TIMER2 COMP Timer/Counter2 Compare Match

reti ;04.TIMER2 OVF Timer/Counter2 Overflow

reti ;05.TIMER1 CAPT Timer/Counter1 Capture Event

reti ;06.TIMER1 COMPA Timer/Counter1 Compare Match A

reti ;07.TIMER1 COMPB Timer/Counter1 Compare Match B

reti ;08.TIMER1 OVF Timer/Counter1 Overflow

reti ;09.TIMER0 OVF Timer/Counter0 Overflow

reti ;10.SPI, STC Serial Transfer Complete

reti ;11.USART, RXC USART, Rx Complete

reti ;12.USART, UDRE USART Data Register Empty

reti ;13.USART, TXC USART, Tx Complete

reti ;14.ADC Conversion Complete

reti ;15.EE_RDY EEPROM Ready

reti ;16.ANA_COMP Analog Comparator

reti ;17.TWI Two-wire Serial Interface

reti ;18.SPM_RDY Store Program Memory Ready


reset:

ldi r16,high(ramend) ; space RAM

out SPH,r16

ldi r16,low(ramend)

out spl,r16

sei

;deley----------------------------------------

ldi r18,1

rcall d100ms


;---------------------------------------------

;definisi portD sbg output

;---------------------------------------------

ser r16

out ddrd,r16

clr r16

out portd,r16


rcall inisLCD


ldi r18,1

rcall d100ms


ADeCe: ;set ADC Multiplexer selection register

ldi r16,ADC_Mux ;0b11000001

out admux,r16

ldi r16,8 ;set ADC control status register

out ADCSRA,r16 ;set prescaller /128


ADeCe1:

sbi ADCSRA,aden

nop

sbi ADCSRA,ADSC ;start conversion

nop


selesai:

sbis ADCSRA,ADIF

rjmp selesai ;loop sampai selesai


in YL,ADCL

in YH,ADCH

andi YH,0b00000011

nop

cbi ADCSRA,aden


sts Volt+1,YL

sts Volt,YH


rcall display

ldi r18,5 ; 1cycle

rcall d100ms ; 4cycle


rjmp ADeCe1


xxx: rjmp xxx


display:

ldi r16,0x85

rcall lcdcmd

lds YL,Volt+1

lds YH,Volt

rcall bin2BCD16

lds r16,LCD_buff+1

rcall LCDdata

lds r16,LCD_buff+2

rcall LCDdata

lds r16,LCD_buff+3

rcall LCDdata

lds r16,LCD_buff+4

rcall LCDdata

lds r16,LCD_buff+5

rcall LCDdata

ret


bin2BCD16:

push r13

push r14

push r15

push r16

push r17

push r18

push r19

push r21

push ZL

push ZH

clr r16

out sreg,r16

mov r16,YL ;low byte

mov r17,YH ;hi byte


ldi r18,16 ;Init loop counter

clr r15 ;clear result (3 bytes)

clr r14

clr r13

clr ZH ;clear ZH (not needed for AT90Sxx0x)

bBCDx_1:

lsl r16 ;shift input value

rol r17 ;through all bytes

rol r13 ;

rol r14

rol r15

dec r18 ;decrement loop counter

brne bBCDx_2 ;if counter not zero



mov r16,r15

andi r16,0x0f

subi r16,-0x30

sts LCD_buff+1,r16 ;Store BCD Digit 4


mov r16,r14

swap r16

andi r16,0x0f

subi r16,-0x30

sts LCD_buff+2,r16 ;Store BCD Digit 3


mov r16,r14

andi r16,0x0f

subi r16,-0x30

sts LCD_buff+3,r16 ;Store BCD Digit 2


mov r16,r13

swap r16

andi r16,0x0f

subi r16,-0x30

sts LCD_buff+4,r16 ;Store BCD Digit 1


mov r16,r13

andi r16,0x0f

subi r16,-0x30

sts LCD_buff+5,r16 ;Store BCD Digit 0


pop ZH

pop ZL

pop r21

pop r19

pop r18

pop r17

pop r16

pop r15

pop r14

pop r13

ret ; return

bBCDx_2:

ldi r30,AtBCD2+1 ;Z points to result MSB + 1

bBCDx_3:

ld r19,-Z ;get (Z) with pre-decrement

subi r19,-$03 ;add 0x03

sbrc r19,3 ;if bit 3 not clear

st Z,r19 ;store back

ld r19,Z ;get (Z)

subi r19,-$30 ;add 0x30

sbrc r19,7 ;if bit 7 not clear

st Z,r19 ;store back

cpi ZL,AtBCD0 ;done all three?

brne bBCDx_3 ;loop again if not

rjmp bBCDx_1

;---------------------------------------------------

;menentukan jmlh character

;---------------------------------------------------

LCD_Word:


ldi r17,16 ;16 character

LCD_Worda:

lpm r16,Z+

rcall LCDdata

dec r17

brne LCD_Worda

ret

lcdclr:

ldi r16,1 ;Clear LCD command

rcall lcdcmd

ldi r18,1

rcall d100ms

ret

;---------------------------------------------------

;inisialisasi lcd

;---------------------------------------------------

inisLCD:

;delay power on

ldi r17,200 ; 1cycle

rcall d100us ; 4cycle

;inisialisasi_1

cbi portd,5

nop

sbi portd,4

nop

ldi r16,0b00000011

ori r16,0b00010000

out portd,r16


rcall dLCD

;inisialisasi_2

cbi portd,5

nop

sbi portd,4

nop

ldi r16,0b00000011

ori r16,0b00010000

out portd,r16


rcall dLCD

;inisialisasi_3

cbi portd,5

nop

sbi portd,4

nop

ldi r16,0b00000011

ori r16,0b00010000

out portd,r16


rcall dLCD

;inisialisasi_4

cbi portd,5

nop

sbi portd,4

nop

ldi r16,0b00000010

ori r16,0b00010000

out portd,r16


rcall dLCD

;inisialisasi_5

ldi r16,0b00101000

rcall lcdcmd

rcall dlcd

;inisialisasi_6

ldi r16,0b00100000

rcall lcdcmd

rcall dlcd

;inisialisasi_7

ldi r16,0b00000001

rcall lcdcmd

rcall dlcd

;inisialisasi_8

ldi r16,0b00000110

rcall lcdcmd

rcall dlcd

;inisialisasi_9

ldi r16,0b00001100

rcall lcdcmd

rcall dlcd

ret

;---------------------------------------------------

lcdcmd:

push r17


cbi portD,5

nop

sbi portd,4

mov r17,r16

swap r17 ;msb-lsb

andi r17,0b00001111 ;hapus nible atas

ori r17,0b00010000

out portd,r17

rcall LCD_Clk


sbi portd,4

mov r17,r16

andi r17,0b00001111 ;hapus nible atas

ori r17,0b00010000

out portd,r17

rcall LCD_Clk


pop r17

ret

;---------------------------------------------------

lcddata:

push r17


sbi portD,5

nop

sbi portd,4

mov r17,r16

swap r17 ;<<<>>>

andi r17,0b00001111 ;hapus nible atas

ori r17,0b00110000

out portd,r17

rcall LCD_Clk


sbi portd,4

mov r17,r16

andi r17,0b00001111 ;hapus nible atas

ori r17,0b00110000

out portd,r17

rcall LCD_Clk


pop r17

ret

;---------------------------------------------------

LCD_Clk:

sbi portd,4 ;pd4

rcall dclk

cbi portd,4

rcall dclk

ret

dLCD:

push r17

ldi r17,50 ; 1cycle

rcall d100us ; 4cycle

pop r17

ret

dclk:

push r17

ldi r17,20

dclk_loop:

dec r17

brne dclk_loop

pop r17

ret

;---------------------------------------------------

;****************************************

; Delay for 1MHz Clock/=<1mips>

;****************************************

;---------------------------------------------------

; Header....

; ldi r18,1 ; 1cycle

; rcall d100ms ; 4cycle

;---------------------------------------------------

d100ms:

push r16

push r17

d100msx:

ldi r16,161 ; 1cycle

d100ms0:

ldi r17,153 ; 1cycle

d100ms1:

nop

dec r17 ; 1cycle

brne d100ms1 ; 2cycle

nop

nop

nop

nop

nop

nop

dec r16 ; 1cycle

brne d100ms0 ; 2cycle

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

nop

dec r18 ; 1cycle

brne d100msx ; 2cycle

pop r17 ; 2cycle

pop r16 ; 2cycle

ret

;---------------------------------------------------

; Header....

; ldi r17,1 ; 1cycle

;---------------------------------------------------

d100us:

push r18

d100usx:

ldi r18,27 ; 1cycle

d100us0:

dec r18 ; 1cycle

brne d100us0 ; 2cycle

dec r17 ; 1cycle

brne d100usx ; 2cycle

pop r18

ret ; 4cycle

;delay 1 second

d1sec:

ldi r18,10 ; 1cycle

rcall d100ms ; 4cycle

ret



1 komentar:

Unknown mengatakan...

Halo Bro, Kalo ngopy artikel, tulis donk sumbernya....
Hargai hasil karya orang lain....