• LinkedIn
  • Join Us on Google Plus!
  • Subcribe to Our RSS Feed

Sunday 4 December 2016

belajar rs 485

December 04, 2016 // by futut // // No comments

Project ini hanya sebagai salah satu contoh saja   (contoh2  yang sejenis ada pada artikel2 lainya) ,   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)
Code program dalam Visaual Basic 6:
clientrs485
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  (codevision)
#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
{
r_buffer[r_index]=0×00;     //
//UCSRB.7=0;
}
delay_ms(100);
PORTD.7=0;
delay_ms(100);
}
}
================================================
References :
– Serial Port Complete Programming & Circuits Rs-232 & Rs485,  Jan Axelson
– Codevision AVR  , help file

0 comments:

Post a Comment