Hallo darwin.nuernberg,
die Leute im 2. Link meinen was anderes.
Hier mal ein kleines Beispiel, habe ich mal im Web gefunden:
Ich hoffe, Du kannst es gebrauchen.Code:' ' -----[ Title ]------------------------------------------------------ ' ' File......: SNAP-IO.BAS ' Purpose...: Turns LEDs on and off ' Author....: Christer Johansson ' Version...: 1.01 ' Started...: 980503 ' Updated...: 980918 ' Modified..: 991229 by Claus Kuehnel ' -----[ Program Description ]---------------------------------------- ' ' This program shows how to implement the S.N.A.P protocol in ' BASCOM-AVR and is an simple example to turn LEDs ON or OFF. ' This example uses 16-bit CRC-CCITT as error detection method which ' gives secure data transfer. ' ' The packet structure is defined in the received packets first two ' bytes (HDB2 and HDB1). The following packet structure is used. ' ' DD=01 - 1 Byte destination address ' SS=01 - 1 Byte source address ' PP=00 - No protocol specific flags ' AA=01 - Acknowledge is required ' D=0 - No Command Mode ' EEE=100 - 16-bit CRC-CCITT ' NNNN=0010 - 2 Byte data ' ' Overview of header definition bytes (HDB2 and HDB1) ' ' HDB2 HDB1 ' +-----------------+-----------------+ ' | D D S S P P A A | D E E E N N N N | ' +-----------------+-----------------+ ' ' $regfile = "m128def.dat" $crystal = 16000000 $baud = 19200 $hwstack = 128 $swstack = 128 $framesize = 128 ' -----[ Constants ]-------------------------------------------------- ' Const Preamble_x = &B01010101 ' Preamble byte Const Sbyte_x = &B01010100 ' Synchronisation byte Const Crcpoly = &H1021 ' CRC-CCITT Const Hdb2_x = &H52 Const Hdb1_x = &H48 Const Myaddress = 123 ' Node - Adresse ' -----[ Variables ]-------------------------------------------------- ' Dim Preamble As Byte ' Preamble byte Dim Sbyte As Byte ' Sync byte Dim Crc As Word ' CRC Word Dim Hdb1 As Byte ' Header Definition Byte 1 Dim Hdb2 As Byte ' Header Definition Byte 2 Dim Dab1 As Byte ' Für welche Node-ID ist das Paket Dim Sab1 As Byte ' Wer sendet das Paket Dim Db1 As Byte ' Paket Data Byte 1 Dim Db2 As Byte ' Paket Data Byte 2 Dim Db3 As Byte ' Paket Data Byte 3 Dim Db4 As Byte ' Paket Data Byte 4 Dim Db5 As Byte ' Paket Data Byte 5 Dim Db6 As Byte ' Paket Data Byte 6 Dim Db7 As Byte ' Paket Data Byte 7 Dim Db8 As Byte ' Paket Data Byte 8 Dim Crc2 As Byte ' Paket CRC Hi_Byte Dim Crc1 As Byte ' Paket CRC Lo_Byte Dim Temp1 As Byte ' Temporäre Variable Dim Temp2 As Byte ' Temporäre Variable Dim Tmpw1 As Word Dim Tmpw2 As Word Dim I As Integer Dim Ausgabe As String * 8 Dim Dummy As String * 1 ' -----[ Initialization ]--------------------------------------------- ' Config Portg = Output ' Portb ist output 'Portb = &HFF Preamble = Preamble_x Sbyte = Sbyte_x Ausgabe = " 123.5" '------[ Program ]---------------------------------------------------- ' Sound Portg.0 , 15000 , 15 _start: 'Temp1 = Waitkey() ' Warten auf Daten Temp1 = Inkey() ' Ist das empf. Zeichen das SYNC byte dann die nächsten 8 Byte ' vom Master lesen, sonst zurück zum Start If Temp1 <> Sbyte Then 'Portg.1 = 1 ' Irgendwas empfangen 'Printbin Temp1 Goto _start Else ' Get packet in binary mode Inputbin Hdb2 , Hdb1 , Dab1 , Sab1 , Db8 , Db7 , Db6 , Db5 , Db4 , Db3 , Db2 , Db1 , Crc2 , Crc1 Portg.2 = 1 ' Packet empfangen ' Packet header check routine ' ' Check HDB2 to see if MCU are capable to use the packet ' structure, if not goto Start If Hdb2 <> Hdb2_x Then Goto _start ' Check HDB1 to see if MCu are capable to use the packet ' structure, if not goto Start If Hdb1 <> Hdb1_x Then Goto _start ' Address check routine ' ' Check if this is the node addressed, if not goto Start If Dab1 <> Myaddress Then Goto _start ' Check CRC for all the received bytes Gosub Check_crc ' Check if there was any CRC errors, if so send NAK If Crc <> 0 Then Goto Nak ' No CRC errors in packet so check what to do. ' ' Associated Function (place it between +++ lines) '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' Portb = Db1 Sound Portg.0 , 10000 , 10 'BEEP If Db1 = 255 Then Portg.1 = 1 Else Portg.1 = 0 End If Dummy = Mid(ausgabe , 1 , 1) Db1 = Asc(dummy) Dummy = Mid(ausgabe , 2 , 1) Db2 = Asc(dummy) Dummy = Mid(ausgabe , 3 , 1) Db3 = Asc(dummy) Dummy = Mid(ausgabe , 4 , 1) Db4 = Asc(dummy) Dummy = Mid(ausgabe , 5 , 1) Db5 = Asc(dummy) Dummy = Mid(ausgabe , 6 , 1) Db6 = Asc(dummy) Dummy = Mid(ausgabe , 7 , 1) Db7 = Asc(dummy) Dummy = Mid(ausgabe , 8 , 1) Db8 = Asc(dummy) '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' Ack_: ' Send ACK (i.e tell master that packet was OK) ' Set ACKs bit in HDB2 (xxxxxx10) Hdb2 = Hdb2 Or &B00000010 Hdb2 = Hdb2 And &B11111110 Goto Send Nak: ' Send NAK (i.e tell master that packet was bad) ' Set ACK bits in HDB2 (xxxxxx11) Hdb2 = Hdb2 Or &B00000011 Goto Send Send: ' Swap SAB1 <-> DAB1 address bytes Temp2 = Sab1 Sab1 = Dab1 Dab1 = Temp2 ' Clear CRC variable Crc = 0 ' Put HDB2 in variable Tmp_Byte1 Temp1 = Hdb2 ' Calculate CRC Gosub Calc_crc ' Put HDB1 in variable Tmp_Byte1 Temp1 = Hdb1 ' Calculate CRC Gosub Calc_crc ' Put DAB1 in variable Tmp_Byte1 Temp1 = Dab1 ' Calculate CRC Gosub Calc_crc ' Put SAB1 in variable Tmp_Byte1 Temp1 = Sab1 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 8 Temp1 = Db8 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 7 Temp1 = Db7 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 6 Temp1 = Db6 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 5 Temp1 = Db5 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 4 Temp1 = Db4 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 3 Temp1 = Db3 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 2 Temp1 = Db2 ' Calculate CRC Gosub Calc_crc ' Put Data in variable Tmp_Byte 1 Temp1 = Db1 ' Calculate CRC Gosub Calc_crc ' Move calculated Hi_CRC value to outgoing packet Crc2 = High(crc) ' Move calculated Lo_CRC value to outgoing packet Crc1 = Low(crc) ' Send packet to master, including the preamble and SYNC byte ' Print "Antwort" Printbin Preamble ; Sbyte_x ; Hdb2 ; Hdb1 ; Dab1 ; Sab1 Printbin Db8 ; Db7 ; Db6 ; Db5 ; Db4 ; Db3 ; Db2 ; Db1 Printbin Crc2 ; Crc1 ' Print "Ende" ' Give AVR time to shift out all bits before setting to Rx Waitms 50 ' Done, go back to Start and wait for a new packet Goto _start End If ' -----[ Subroutines ]------------------------------------------------ ' 'Soubroutine for checking all received bytes in packet Check_crc: Crc = 0 Temp1 = Hdb2 Gosub Calc_crc Temp1 = Hdb1 Gosub Calc_crc Temp1 = Dab1 Gosub Calc_crc Temp1 = Sab1 Gosub Calc_crc Temp1 = Db8 Gosub Calc_crc Temp1 = Db7 Gosub Calc_crc Temp1 = Db6 Gosub Calc_crc Temp1 = Db5 Gosub Calc_crc Temp1 = Db4 Gosub Calc_crc Temp1 = Db3 Gosub Calc_crc Temp1 = Db2 Gosub Calc_crc Temp1 = Db1 Gosub Calc_crc Temp1 = Crc2 Gosub Calc_crc Temp1 = Crc1 Gosub Calc_crc Return ' Subroutine for calculating CRC value in variable Tmp_Byte1 Calc_crc: Tmpw1 = Temp1 * 256 Crc = Tmpw1 Xor Crc For Temp2 = 0 To 7 If Crc.15 = 0 Then Goto Shift_only Tmpw2 = Crc * 2 Crc = Tmpw2 Xor Crcpoly Goto Nxt Shift_only: Crc = Crc * 2 Nxt: Next Return
mfg
AVRWalli







Zitieren

Lesezeichen