Avr Adc
Avr Adc
Jenis: - 1 x 12 keys
- 3 x 4 key matrix
Single Key:
Calc_key:
Select Case Row
Case 4 : Key = Column + 1
Case 5 : Key = Column + 4
Case 6 : Key = Column + 7
Case 7 : Key = Column + 10
End Select
Cls
Lcd Key
Return
Menggunakan Getkbd() untuk 4x4 matrix keypad:
Config Kbd = Porta
Dim Value As Byte
Dim I As Byte
For I = 0 To 9
Value = Getkbd()
Cls
Lcd Value
Waitms 30
Next
PC-AT Keyboard
Config Keyboard = Pind.2 , Data = Pind.4 , Keydata = Keydata
Dim B As Byte
Do
B = Getatkbd() 'get a byte and store it into
'byte variable
If B > 0 Then
Print B ; Chr(b)
End If
Loop
End
Do
A = Inkey() ' reads one character
Print Chr(a) ; " is ASCII " ; A
Waitms 100
Loop Until A = 27
End
Register:
ADMUX
Perhatikan:
ADCSR
Division
ADPS2 ADPS1 ADPS0
Factor
0 0 0 2
0 0 1 2
0 1 0 4
0 1 1 8
1 0 0 16
1 0 1 32
1 1 0 64
1 1 1 128
ADC_Init:
ldi r16,3 ; Select ADC3
out ADMUX, r16 ; Enable ADC, Single Mode conversion
ldi r16, 10000101b ; ADC Interrupt disable, Prescaler division factor = 32
out ADCSR,r16 ; this gives an ADC clock frequency of 4e6/32=125kHz.
sbi ADCSR,ADSC ; Start conversion
Wait:
sbis ADCSR,ADIF ; Wait until the conversion is completed
rjmp Wait:
in r16,ADCL ; Place ADCH in r16 & r17
in r17,ADCH
Caution:
Contoh:
org 0x0000 ; reset vector
rjmp reset ; jump to "reset"
.org 0x000E ; ADC Conversion Complete Interrupt vector:
rjmp ADC_ISR ; jump the "ADC_ISR"
;
reset: ; the reset code:
ldi r16, low(RAMEND) ; stack setup; set SPH:SPL to
out SPL, r16 ; RAMEND
ldi r16, high(RAMEND) ;
out SPH, r16 ;
;
ldi r16, 0xFF ; set all PortD pins to output
out DDRD, r16 ;
;
ldi r16, 0 ; write zero
out ADMUX, r16 ; to ADMUX (select channel 0)
ldi r16, 0b11101101 ; from left to right: ADC Enable, Start
;Conversion, Free-Running Mode, write
out ADCSR, r16 ; zero to ADC Int flag, enable int, prescaler: 101
; for XTAL/32
;
sei ; enable interrupts
loop: ; and loop
rjmp loop ; forever
;
ADC_ISR: ; Here it is, our ISR!
push r16 ; save r16
in r16, SREG ; use r16 16 to save SREG
push r16 ; (push both on stack)
push r17 ; also save r17
;
in r16, ADCL ; get the last ADC result, low byte first,
in r17, ADCH ; then high byte
lsr r17 ; shift ADC result right (2 bits)
ror r16 ; by first shifting out bit 0 of r16, then shifting it
; into r17
lsr r17 ;
ror r16 ; (twice)
com r16 ; now invert result
out PortD, r16 ; and write to PortD
;
pop r17 ; restore r17,
pop r16 ; SREG
out SREG, r16 ;
pop r16 ; and r16
reti ; and return
--------------------------------------------------------------------
' ADC.BAS
' demonstration of GETADC() function for 8535 or M163 micro
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------
$regfile = "8535def.dat" ' we use the 8535
'With STOP ADC, you can remove the power from the chip
'Stop Adc
Channel = 0
'now read A/D value from channel 0
Do
W = Getadc(channel)
Print "Channel " ; Channel ; " value " ; W
Incr Channel
If Channel > 7 Then Channel = 0
Loop
End
'--------------------------------------------------------------------
' ADC_INT.BAS
' demonstration of GETADC() function
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------
$regfile = "4433def.dat"
$crystal = 4000000
$baud = 19200
'configure single mode and auto prescaler setting
'The single mode must be used with the GETADC() function
'The prescaler divides the internal clock by 2,4,8,15,32,64 or 128
'Because the ADC needs a clock from 50-200 KHz
'The AUTO feature, will select the highest clockrate possible
Adc_isr:
push r24
in r24,sreg
push r24
push r25
W = Getadc(channel)
pop r25
pop r24
!out sreg,r24
pop r24
Return