You are on page 1of 10

Keypad :

Jenis: - 1 x 12 keys
- 3 x 4 key matrix

Single Key:

Const Keys = 9 ' Test for 9 keys only


Config Porta = Input ' Porta is input
Porta = 255 ' with internal pull-up
Config Portc = Input ' PortC is input
Portc = 255 ' with internal pull-up
Dim I As Byte
Dim Key As Byte ' Variable contains key number
Do
For I = 0 To Keys ' Query all keys
Key = I
Select Case Key
Case 0 : Debounce Pina.0 , 0 , Display_key , Sub
Case 1 : Debounce Pina.1 , 0 , Display_key , Sub
Case 2 : Debounce Pina.2 , 0 , Display_key , Sub
Case 3 : Debounce Pina.3 , 0 , Display_key , Sub
Case 4 : Debounce Pina.4 , 0 , Display_key , Sub
Case 5 : Debounce Pina.5 , 0 , Display_key , Sub
Case 6 : Debounce Pina.6 , 0 , Display_key , Sub
Case 7 : Debounce Pina.7 , 0 , Display_key , Sub
Case 8 : Debounce Pinc.0 , 0 , Display_key , Sub
Case 9 : Debounce Pinc.1 , 0 , Display_key , Sub
End Select
Next
Loop
End
Display_key:
Cls
Lcd Key
Return
Matrix keypad :

' Query a matrix keypad by AVR


Ddra = &H0F ' PD7-PD4 Input; PD3-PD0 Output
Porta = &HFF ' with internal pull-up
Dim Column As Byte
Dim Row As Byte
Dim Key As Byte ' Variable contains key number
Do ' Query all keys
For Column = 0 To 3
If Column = 0 Then Reset Porta.0
If Column = 1 Then Reset Porta.1
If Column = 2 Then Reset Porta.2
If Column = 3 Then Reset Porta.3
For Row = 4 To 7
Select Case Row
Case 4 : Debounce Pina.7 , 0 , Calc_key , Sub
Case 5 : Debounce Pina.6 , 0 , Calc_key , Sub
Case 6 : Debounce Pina.5 , 0 , Calc_key , Sub
Case 7 : Debounce Pina.4 , 0 , Calc_key , Sub
End Select
Next
Porta = &HFF
Next
Loop
End

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

'This is the key translation table


Keydata:
'normal keys lower case
Data 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , &H5E , 0
Data 0 , 0 , 0 , 0 , 0 , 113 , 49 , 0 , 0 , 0 , 122 , 115 , 97 , 119 , 50 , 0
Data 0 , 99 , 120 , 100 , 101 , 52 , 51 , 0 , 0 , 32 , 118 , 102 , 116 , 114 , 53 , 0
Data 0 , 110 , 98 , 104 , 103 , 121 , 54 , 7 , 8 , 44 , 109 , 106 , 117 , 55 , 56 , 0
Data 0 , 44 , 107 , 105 , 111 , 48 , 57 , 0 , 0 , 46 , 45 , 108 , 48 , 112 , 43 , 0
Data 0 , 0 , 0 , 0 , 0 , 92 , 0 , 0 , 0 , 0 , 13 , 0 , 0 , 92 , 0 , 0
Data 0 , 60 , 0 , 0 , 0 , 0 , 8 , 0 , 0 , 49 , 0 , 52 , 55 , 0 , 0 , 0
Data 48 , 44 , 50 , 53 , 54 , 56 , 0 , 0 , 0 , 43 , 51 , 45 , 42 , 57 , 0 , 0
'shifted keys UPPER case
Data 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
Data 0 , 0 , 0 , 0 , 0 , 81 , 33 , 0 , 0 , 0 , 90 , 83 , 65 , 87 , 34 , 0
Data 0 , 67 , 88 , 68 , 69 , 0 , 35 , 0 , 0 , 32 , 86 , 70 , 84 , 82 , 37 , 0
Data 0 , 78 , 66 , 72 , 71 , 89 , 38 , 0 , 0 , 76 , 77 , 74 , 85 , 47 , 40 , 0
Data 0 , 59 , 75 , 73 , 79 , 61 , 41 , 0 , 0 , 58 , 95 , 76 , 48 , 80 , 63 , 0
Data 0 , 0 , 0 , 0 , 0 , 96 , 0 , 0 , 0 , 0 , 13 , 94 , 0 , 42 , 0 , 0
Data 0 , 62 , 0 , 0 , 0 , 8 , 0 , 0 , 49 , 0 , 52 , 55 , 0 , 0 , 0 , 0
Data 48 , 44 , 50 , 53 , 54 , 56 , 0 , 0 , 0 , 43 , 51 , 45 , 42 , 57 , 0 , 0

Asynchronous Serial Communication :


Initialisasi (setting) serial port dapat dilakukan melalui menu Option pada Bascom atau
melalui command pada program, seperti berikut:
' Serial I/O by AVR and 8051
Dim A As Integer
Do
Input A
Print A
Loop
End

$Baud = 2400 ‘baudrate = 2400


$Crystal = 12000000 ‘crystal frequency = 12 MHz
Dim A As Byte
Dim C As Word ' B is a reserved word for BASCOM-8051
Do
Inputbin A , C
Printbin A , C
Loop
End

' Serial I/O by AVR and 8051


Dim A As Byte
Do
A = Waitkey() ' waits for one character
Print Chr(a) ; " is ASCII " ; A
Loop Until A = 27

Do
A = Inkey() ' reads one character
Print Chr(a) ; " is ASCII " ; A
Waitms 100
Loop Until A = 27
End

' Software UART by AVR


Dim A As Byte
Dim B As Byte
Open "COMA.0:2400,8,N,1,inverted" For Output As #1
Open "COMA.1:2400,8,N,1,inverted" For Input As #2
Do
Print "Input one character: ";
A = Waitkey()
Print Chr(a)
Print " Sent character = " ; Chr(a)
Printbin #1 , A
Inputbin #2 , B
Print " Received character = " ; Chr(b)
Print
Loop Until A = 27
Close #1
Close #2
End
Analog to Digital Converter (ADC)

Register:
ADMUX

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


--- --- --- --- --- MUX2 MUX1MUX0

MUX2 MUX1 MUX0 Selected Input


0 0 0 ADC0
0 0 1 ADC1
0 1 0 ADC2
0 1 1 ADC3
1 0 0 ADC4
1 0 1 ADC5
1 1 0 ADC6
1 1 1 ADC7

Perhatikan:
ADCSR

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


ADEN ADSC ADFR ADIF ADIE ADPS2ADPS1ADPS0

ADEN (ADC Enable)


ADSC (ADC Start Conversion)
ADFR (ADC Free Running)
ADIF (ADC Interrupt Flag)
ADIE (ADC Interrupt Enable)
ADPS (ADC Prescaler Select )

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:

Analog Voltage = -1 Volt → (Vcc + 0.5) Volt

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

'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

Config Adc = Single , Prescaler = Auto

'Now give power to the chip


Start Adc

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

Dim W As Word , Channel As Byte

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

Config Adc = Single , Prescaler = Auto

'Now give power to the chip


On Adc Adc_isr Nosave
Enable Adc
Enable Interrupts

Dim W As Word , Channel As Byte

Channel = 0 'now read A/D value from channel 0


Do
Channel = 0
'idle will put the micro into sleep.
'an interrupt will wake the micro.
Start Adc
Idle
Stop Adc
Print "Channel " ; Channel ; " value " ; W
Waitms 500
Loop
End

Adc_isr:
push r24
in r24,sreg
push r24
push r25
W = Getadc(channel)
pop r25
pop r24
!out sreg,r24
pop r24
Return