Предлагаю разживать данный протокол до мельчайших подробностей.
Наброски кода работают коряво!
Master
'************************************************** ' Servo RS485 ATMega328P '************************************************* $regfile = "m328pdef.dat" $crystal = 1000000 $hwstack = 32 $swstack = 10 $framesize = 32 $baud = 2400 '************************************************** Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.3 , Db6 = Portb.4 , Db7 = Portb.5 , E = Portb.1 , Rs = Portb.0 Cursor Off '********************************************************** '******************************************************** Config Portb.7 = Output Led Alias Portb.7 Config Portc.0 = Input But1 Alias Pinc.0 Config Portc.1 = Input But2 Alias Pinc.1 '************************ Dim Mayadres As Word Dim Adres As Byte Dim Xadc1 As Word Dim Xadc2 As Word Dim A As Byte Dim B As Bit '********************************************** Mayadres = 01 Ucsr0a = &H21 Cls '**************************** '* начинаем * '**************************** Do Set Led Waitms 100 Reset Led Waitms 100 '----------------------------------------------------------------------- A = Ucsr0a Locate 1 , 1 : Lcd Bin(a) Locate 2 , 1 : Lcd Xadc1 If But1 = 0 Then Waitms 200 Printbin 11 Printbin 678 Cls End If '///////////////////////////////////////////////////////// If But2 = 0 Then Waitms 200 Printbin 12 Printbin 789 Cls End If '------------------------------------------------------------------------------ If A = &H61 Then Reset Ucsr0a Ucsr0a = &H21 End If '------------------------------------------------------------------------------- Adres = 0 Ucsr0a = &H21 '---------------------------------------------------------------------------- If A = &HA1 Then Reset Ucsr0a Ucsr0a = &H21 End If '------------------------------------------------------------------------------- If A = &HE1 Then Reset Ucsr0a Ucsr0a = &H21 End If '------------------------------------------------------------------------------ Locate 2 , 10 : Lcd Xadc1 '------------------------------------------------------------------------------ If Mayadres = Adres Then Ucsr0a = &H20 Adres = 0 Inputbin Xadc1 , 2 Waitms 50 Cls End If '------------------------------------------------------------------------------- If A = &H29 Then ' ошибка по переполнению Reset Ucsr0a Ucsr0a = &H21 End If '----------------------------------------------------------------------- Loop End
Slave0
'************************************************** ' СУН отработка RS485 ATMega328P '************************************************* $regfile = "m328pdef.dat" $crystal = 1000000 $hwstack = 32 $swstack = 10 $framesize = 32 $baud = 2400 '************************************************** '************************************************** Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.3 , Db6 = Portb.4 , Db7 = Portb.5 , E = Portb.1 , Rs = Portb.0 Cursor Off '**************************************************************************************************************** Config Adc = Single , Prescaler = 2 , Reference = Avcc Start Adc '********************************************************** Config Print0 = Portd.2 , Mode = Set Config Pind.2 = Output '************************************************************************* Config Portb.7 = Output Led Alias Portb.7 Config Portc.0 = Input '************************ Dim Xadc As Word Dim X_data As Word Dim Adres As Byte Dim Mayadres As Word Dim A As Byte Dim B As Byte Dim C As Byte '*************************************************** Mayadres = 11 Ucsr0a = &H21 Cls '**************************** '* начинаем * '**************************** Osnova: Do Set Led Waitms 100 Reset Led Waitms 100 '---------------------------------------------------------------------- Xadc = Getadc(0) A = Ucsr0a Adres = 0 If A = &HA1 Then Inputbin Adres Ucsr0a = &H21 End If If Mayadres = Adres Then Ucsr0a = &H20 Adres = 0 Ucsr0a = &H21 Inputbin X_data , 2 Cls End If Locate 1 , 1 : Lcd "Data_IN=" ; X_data Locate 2 , 1 : Lcd "Data_Out=" ; Xadc Loop End
Slave1
'************************************************** ' СУН отработка RS485 ATMega328P '************************************************* $regfile = "m328pdef.dat" $crystal = 1000000 $hwstack = 32 $swstack = 10 $framesize = 32 $baud = 2400 '************************************************** '************************************************** Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.3 , Db6 = Portb.4 , Db7 = Portb.5 , E = Portb.1 , Rs = Portb.0 Cursor Off '**************************************************************************************************************** Config Adc = Single , Prescaler = 2 , Reference = Avcc Start Adc '********************************************************** Config Print0 = Portd.2 , Mode = Set Config Pind.2 = Output '************************************************************************* Config Portb.7 = Output Led Alias Portb.7 Config Portc.0 = Input '************************ Dim Xadc As Word Dim X_data As Word Dim Adres As Byte Dim Mayadres As Word Dim A As Byte Dim B As Byte Dim C As Byte '*************************************************** Mayadres = 12 Ucsr0a = &H21 Cls '**************************** '* начинаем * '**************************** Osnova: Do Set Led Waitms 100 Reset Led Waitms 100 '---------------------------------------------------------------------- Xadc = Getadc(0) A = Ucsr0a Adres = 0 Ucsr0a = &H21 If A = &HA1 Then Inputbin Adres End If If Mayadres = Adres Then Ucsr0a = &H20 Adres = 0 Inputbin X_data , 2 Waitms 50 Printbin Xadc Cls End If If A = &H61 Then Reset Ucsr0a Ucsr0a = &H21 End If Locate 1 , 1 : Lcd "IN=" ; X_data Locate 2 , 1 : Lcd "Out=" ; Xadc Locate 1 , 8 : Lcd Bin(a) Loop End