Tampilkan postingan dengan label Pemrograman. Tampilkan semua postingan
Tampilkan postingan dengan label Pemrograman. Tampilkan semua postingan

Komunikasi data AVR dan PC menggunakan RS-485


Project ini hanya sebagai contoh saja, bagaimana menghubungkan beberapa microcontroller (dlm contoh ini Atmega8535) dgn PC melalui port serial dengan menggunakan RS-485.

perbandingan rs232 dan rs485:
-rs232 hanya menghubungkan antara 2  host (pc ke pc atau pc ke microcontroller) dan jarak maximal kira2  15 meter saja
-Rs485 bisa menghubungkan  beberapa host  ( 1  master dan beberapa slave )  master bisa berupa PC atau Microcontroller dan slave bisa beberapa PC atau microcontroller.
Komponen yang dibutuhkan:
1.  Rs232 to rs485 Converter
2.  modul Atmega8535 + IC max485
3.  CodeVision versi trial  (download gratis)
4.  Kabel telpon  (tergantung jarak PC dgn modul Atmega8535)
Rangkaian :
Rs-485 network
Detail rangkaian slave (microcontroller Atmega8535)
Client-RS-485
Client-RS-485
Code program dalam Visaual Basic 6:
Private Declare Function GetTickCount Lib “kernel32″ () As Long
Dim Timeout As Boolean
Dim data_pantulan As String
Dim perintah As String
Dim messagetosend As Variant
Dim transferprogress As Boolean

‘============================
Private Sub Form_Load()
If MSComm1.PortOpen = True Then Exit Sub
MSComm1.Settings = “9600,N,8,1″
MSComm1.CommPort = 1
MSComm1.PortOpen = True

End Sub
Private Sub kirimdata_Click()
txtdata_dari_slave.Text = “”
MSComm1.RTSEnable = False
Sleep (300)
MSComm1.Output = txtdatatosend.Text
Sleep (300)
MSComm1.RTSEnable = True

Call getResponse
End Sub

Private Sub getResponse()
Static responseBuffer As String
Dim inbuff As Variant
Dim length As Integer
Dim pcount As Long
Dim ccount As Long
Dim duration As Long
duration = 1000

pcount = GetTickCount()
Do While True
inbuff = MSComm1.Input
If Len(inbuff) > 0 Then
responseBuffer = responseBuffer + inbuff
length = Len(responseBuffer)
If responseBuffer <> “” Then
txtdata_dari_slave.Text = responseBuffer   ‘ received a complete response string
responseBuffer = “”
Exit Sub
Else
End If
End If
ccount = GetTickCount()
If ccount >= pcount + duration Then Exit Do  ‘
Loop
txtdata_dari_slave.Text = “no response”
End Sub

===================================================
‘jika slave nya banyak ( contoh disini ada 8 slave)
Private Sub PollSlave()
Dim no_slave As Integer
Dim buffer As Variant
Dim alamat As Integer
Dim ack As Boolean
Dim reply As Boolean
Dim data_dari_slave As String
alamat = &H67
transferprogress = True
‘mengirim alamat node tunggu ack ,kirim perintah  dan menunggu reply data
‘kemudian simpan hasil

For no_slave = 1 To 8
‘txtno_slave.Text = no_slave
‘======hapus inbuffer dan outbuffer
MSComm1.OutBufferCount = 0
If MSComm1.InBufferCount > 0 Then
buffer = MSComm1.Input
End If
‘===================================
Call enabletransmitter ‘ –>

alamat = alamat + 1   ‘ alamat = 68h,69h,6Ah,6Bh,6Ch,6Dh,6Eh,6Fh
alamat_slave = Chr(alamat)
txtno_slave.Text = alamat_slave
buffer = Chr(alamat)
‘MsgBox buffer
‘kirim alamat ke i
MSComm1.Output = buffer
‘=======================================================================
Do ‘tunggu pantulan byte alamat yg dikirim
txtstatus.Text = “tunggu pantulan alamat = ” & buffer
tmrTimeout.Enabled = True
Timeout = False
‘===============sampe trima 1 byte alamat pantulan
Call disabletransmitter  ‘<–
Do
DoEvents
Loop Until (MSComm1.InBufferCount >= 1) Or (Timeout = True)
‘=================

If Timeout = False Then
tmrTimeout.Enabled = False
‘baca byte pantulan  dan bandingkan dgn yg dikirim
data_pantulan = MSComm1.Input
txtalamatslave.Text = txtalamatslave.Text & Asc(data_pantulan)
If StrComp(data_pantulan, alamat_slave, vbBinaryCompare) <> 0 Then
ack = True
txtstatus.Text = “alamat sama okehh”
Else
ack = False
txtstatus.Text = ” alamat  beda”
End If

Else
lbtimeout.Caption = “waiting adress timeout slave no ” & no_slave
ack = False
End If
Loop Until ack = True Or Timeout = True
tmrTimeout.Enabled = False
Timeout = False
‘=========================================================================
txtinbuffer.Text = ” count= ” & MSComm1.InBufferCount & “  size = ” & MSComm1.InBufferSize & “  isi = ” & MSComm1.Input

If ack = True Then
‘================================================================
Call enabletransmitter  ‘–>  Out
perintah = “b”  ‘kirim perintah baca data output
messagetosend = perintah
tmrTimeout.Interval = 1000
tmrTimeout.Enabled = True
ack = False
MSComm1.Output = messagetosend

Call disabletransmitter  ‘<– IN
Do

Timeout = False
Do
DoEvents
Loop Until (MSComm1.InBufferCount > 4) Or (Timeout = True)

lbtimeoutdata.Caption = “waiting reply data timeout slave no ” & no_slave
If Timeout = False Then
tmrTimeout.Enabled = False
data_dari_slave = MSComm1.Input

reply = True
If StrComp(Asc(Left(data_dari_slave, 1)), alamat, vbBinaryCompare) <> 0 Then
txtoutput_ok.Text = data_dari_slave
‘ txtoutput_ng.Text = Asc(Val(“&h” & Mid(data_dari_slave, 4, 2)))
‘simpanhasil(no_slave,output_ok,output_ng)
txtdata_dari_slave.Text = txtdata_dari_slave.Text & Asc(data_dari_slave)
txtstatus.Text = “alamat cocok”

Else
‘alamat data tdk cocok
txtstatus.Text = “alamat tdk cocok”
End If

Else
lbtimeoutdata.Caption = “waiting reply data timeout slave no ” & no_slave

End If
Loop Until reply = True Or Timeout = True
tmrTimeout.Enabled = False
‘====================================================
End If
txtoutbuffer.Text = ” count= ” & MSComm1.OutBufferCount & “  size = ” & MSComm1.OutBufferSize

Next no_slave
transferprogress = False
tmrTimeout.Enabled = False
txtstatus.Text = “selesai 1 loop”
End Sub

===================================================
Sofware untuk microcontroller Atmega8535 sebagai slave
=====================================================
contoh  Program Avr Atmega8535
#include <mega8535.h>
#asm
.equ __lcd_port=0×15
#endasm
#include <lcd.h>
#include <stdio.h>
#include <delay.h>
#include <ctype.h>
//char lcd_buffer[33];         //-128 to 127

unsigned char  r_index, r_buffer[40], r_char; //0 to 255
void main(void)
{
lcd_init(16);
UCSRA=0×00;
UCSRB=0×98;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

delay_ms(500);
PORTD.7=0;
/*r_buffer[0]=’c’;
r_buffer[1]=’o’;
r_buffer[2]=’b’;
r_buffer[3]=’a’;
r_buffer[4]=”;
*/
delay_ms(500);
lcd_putsf(“test comm”);
#asm(“sei”)
while(1) {
r_index=0;
//  UCSRB.7=1;

lcd_puts(r_buffer);  //display data di ram, klo di flash pake lcd_putsf
}
}
interrupt [USART_RXC] void usart_rx_complete(void)
{

lcd_clear();
r_char= toascii(UDR);

if(r_char==’u’)
{
delay_ms(100);
PORTD.7=1;
delay_ms(300);
lcd_putsf(“85->replied”); //85     79=”, 80=p 81=q 82=r  83=s 84=t
delay_ms(200);
putchar(‘u’) ;

}
else if(r_char==’y’)
{
delay_ms(100);
PORTD.7=1;
delay_ms(300);
lcd_putsf(“89->replied”); //89     79=”, 80=p 81=q 82=r  83=s 84=t
delay_ms(200);
putchar(‘y’) ;
}
else
{

if (r_char != ‘x’)   //88
{
r_buffer[r_index++]=r_char;
}
else
{
putchar(‘\n’);              //use putchar to avoid overwrite
r_buffer[r_index]=0×00;        //zero terminate
//UCSRB.7=0;
}

delay_ms(100);
PORTD.7=0;
delay_ms(100);

}
}
=============
KLIK tuk melanjutkan »

2x16 LCD display and 4x4 keypad




Scematic diagram of the circuit
Introduction
A very simple circuit to experiment with AT90S2313, 2x16 LCD display and 4x4 keypad. The clock based on 4 MHz crystal, but you can use anyone crystal between 1-4 MHz. The keys with the name "A", "B" ... "F" are typed to the LCD with numbers 10-16.
Because the AVR have only 15 I/O pins we are working the LCD display with 4-bit databus. The 4 resistors (10K) are to protect the AVR from the shortcuts as the coloumn of the keypad is change.
I make the source code with a simple form, that its mean I don't make any economy to the memory, to understand the beginner how does the circuit its working.

How this is works
The AVR configure the PortB as PB0-PB3 inputs and PB4-PB7 outputs. At the firt, the AVR put the pin PB4 at logic '0' to enable the column1 (the first 4 keys) and reading the state of the keys. If we have pressed any of the 4 firt keys then the AVR send the number of the key to the LCD display. If we have not pressed any of 4 first keys, theAVR put the PB4 at logic '1' and PB5 at logic '0' to enable the 2th column. Reading the state of the keys and display the resault to LCD, etc, until to read the 16th key. After that, the circuit start again to read from the 1st key (1st column) .
Dowload the schematic, hex and source code lcd_keys.zip
KLIK tuk melanjutkan »

Interface Keypad 4x4 to LED

 
Gambar 3.22. Interface keypad dengan output ke LED

;Percobaan 3.8.1.

;pada percobaan ini, setiap pengambilan data dari keypad maka data akan dikeluarkan
;pada LED sesuai dengan tombol yang ditekan, sebagai contoh:
;bila tombol 1 ditekan maka = LED akan mempunyai kondisi 00000001,
;artinya LED0 akan nyala, dan seterusnya.
kolom1 bit P1.4
kolom2 bit P1.5
kolom3 bit P1.6
kolom4 bit P1.7
baris1 bit P1.0
baris2 bit P1.1
baris3 bit P1.2
baris4 bit P1.3
;
keydata equ 70h
keybounc equ 71h
org 0h
mulai: Acall keypad4x4 ;memanggil subrutine keypad4x4
Mov A,keydata ;A = keydata
Cjne A,#0FFh,terus ;Bandingkan A dengan FF, bila tidak sama ke Terus
sjmp mulai ;Disinilah awal dari LOOPING FOREVER
terus: CPL A ;A = NOT A
Mov P0,A ;P0 = A
Sjmp mulai ;Disinilah awal dari LOOPING FOREVER
;
delay: mov R0,#0
delay1: mov R2,#50
djnz R2,$
djnz R0,delay1
ret
;
;====================================
; subroutine untuk pembacaan data keypad 4x4
;====================================

Keypad4x4:
mov keybounc,#50 ; keybounc = 50
mov keyport,#0FFh ; keyport=P1= FF
clr kolom1 ; Kolom1= P1.4 = 0
ul1: jb baris1,key1 ; Lompat ke Key1 jika Baris1=P1.0=1
djnz keybounc,ul1
mov keydata,#0ch ; Keydata =0Ch
ret
key1: jb baris2,key2 ; Lompat ke Key2 jika Baris2=P1.1=1
djnz keybounc,key1
mov keydata,#0dh ; Keydata = 0Dh
ret
key2: jb baris3,key3 ; idem
djnz keybounc,key2
mov keydata,#0eh
ret
key3: jb baris4,key4 ; idem
djnz keybounc,key3
mov keydata,#0fh
ret
;
key4: setb kolom1
clr kolom2
jb baris1,key5
djnz keybounc,key4
mov keydata,#0bh
ret
key5: jb baris2,key6
djnz keybounc,key5
mov keydata,#9
ret
key6: jb baris3,key7
djnz keybounc,key6
mov keydata,#6
ret
key7: jb baris4,key8
djnz keybounc,key7
mov keydata,#3h
ret
;
key8: setb kolom2
clr kolom3
jb baris1,key9
djnz keybounc,key8
mov keydata,#0
ret
key9: jb baris2,key10
djnz keybounc,key9
mov keydata,#8
ret
key10: jb baris3,key11
djnz keybounc,key10
mov keydata,#5
ret
key11: jb baris4,key12
djnz keybounc,key11
mov keydata,#02
ret
;
key12: setb kolom3
clr kolom4
jb baris1,key13
djnz keybounc,key12
mov keydata,#09
ret
key13: jb baris2,key14
djnz keybounc,key13
mov keydata,#07
ret
key14: jb baris3,key15
djnz keybounc,key14
mov keydata,#04
ret
key15: jb baris4,key16
djnz keybounc,key15
mov keydata,#01
ret
key16: mov keydata,#0FFh ; bila tombol tidak ada yang menekan maka
; keydata =0FFh
ret
end
KLIK tuk melanjutkan »