Code:
'Empfang von Daten mit Pollin RFM AVR Funkboard
'angeschlossen wie auf dem Board
'Empfang über fallende Flanke an INT0
'basiert auf dem Code von "Holli" aus dem http://bascom-forum.de/
'geändert und angepaßt von Dino DIeter 26.10.2010
'Status: läuft
'Sender steht ca 8 m entfernt hinter dicken Mauern und sendet alle 2 Sekunden
' 32 Bytes
$regfile = "m8def.dat"
$hwstack = 32
$swstack = 50
$framesize = 50
$crystal = 12000000
$baud = 57600
Baud = 57600
'Nsel Alias Portb.2
'Clk Alias Portb.5
'Mosi Alias Portb.3
'Miso Alias Pinb.4
Config Portd.2 = Input
Nirq Alias Pind.2
Config Portd.3 = Input
Ffit Alias Pind.3
Led2 Alias Portd.5
Config Portd.5 = Output 'LED zur Kontrolle
Led1 Alias Portd.6
Config Portd.6 = Output 'LED zur Kontrolle
Dim Taste As Byte
Dim Fifo(2) As Byte
Dim A As Byte
'Hardware einstellen
Config Com1 = 57600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 64 , Noss = 0 , Spiin = &HB0
'Config Spi = Soft , Din = Pinb.4 , Dout = Portb.3 , Ss = Portb.2 , Clock = Portb.5 , Spiin = 0
'init the spi
Spiinit
'Interrupt 0 einstellen
Config Int0 = Falling
On Int0 Ffit_isr
Waitms 100
'$include "inc_RF12-Treiber.bas" brauchen wir nicht
Declare Sub Rf_init()
Declare Sub Rf_cmd(byval Wert As Word)
Declare Sub Rf12_rxd_off()
Declare Sub Rf12_rxd_on()
Declare Sub Framehandler()
'Definition von Konstanen
Const Rf12freq = 868.35 'Kanal-Einstellung
Const Rf12baud = 9600 'Baudrate
Const Myadress = 20 'Nodeadresse vom Teilnehmer
'Const Snap_sync = &H54 'Synczeichen SNAP-Protokoll
Const Snap_sync = &H31 'Synczeichen
Const Maxchar = 22
'const HDB2_ = &H50
Const Hdb2_ = &H32
Const Sync_byte = &H31
Const My_adress = &H34
Const Recive_buffer_size = 40
Dim Cmd(2) As Byte , Tmpo As Word
Dim Lv As Byte , Readycount As Byte , Count2 As Byte
Dim Temp As Word
Dim Tmp As Word
Dim Rs232_flash As Byte , Za1 As Byte
Dim Rs232_str As Byte '
Dim Ndb As Byte , Framelength As Byte , Crc As Byte
Dim Byteanzahl As Byte 'Anzahl Sendebytes
Dim S As String * 10
Dim Rf12_data(recive_buffer_size) As Byte , Rf12_s As String * Maxchar At Rf12_data Overlay
Sync Alias Rf12_data(1)
Hdb1 Alias Rf12_data(2)
Hdb2 Alias Rf12_data(3)
Dab1 Alias Rf12_data(4)
Sab1 Alias Rf12_data(5)
Dim Rf_rxd As Bit , Rf_sync As Bit
Set Led1
'RFM 12 einstellen auf senden erstmal
Call Rf_init()
Reset Led1
Enable Interrupts
Enable Int0
'Array löschen
Rf12_s = ""
'Endlosschleife
Do
'nur zum debuggen
Taste = Inkey()
If Taste = "a" Then
Call Rf_cmd(&H0000) 'read status
Call Rf_cmd(&Hca81) 'Reset FIFO
Call Rf_cmd(&Hca83) 'enable FIFO
End If
If Taste = "x" Then
'Empfänger an
Call Rf12_rxd_on:
End If
If Taste = "c" Then
'Empfänger aus
Call Rf12_rxd_off:
End If
'empfangenes Frame auswerten
If Rf_rxd = 1 Then
Rf_rxd = 0
Call Framehandler()
End If
Loop
'Befehl zum RFM senden geändert Dino Dieter
Sub Rf_cmd(byval Wert As Word)
Cmd(2) = Wert And 255
Shift Wert , Right , 8
Cmd(1) = Wert
Spiout Cmd(1) , 2
End Sub
'Einstellen des RFM 12
Sub Rf_init()
Call Rf_cmd(&H0000) 'Read Status
Call Rf_cmd(&Hc0e0) 'low battery, clock 10 MHz
Call Rf_cmd(&H80e7) 'Configuration: 868MHzband, 12pf, enable FIFO
Call Rf_cmd(&H82d8) 'power management: enable receiver, enable clock output
Call Rf_cmd(&Hc2ac) 'data filter command
Call Rf_cmd(&Ha686) 'Frequency: 868,35MHz
Call Rf_cmd(&Hc611) 'Datarate: 19,2 kbit
Call Rf_cmd(&H94a1) 'receiver setting: 134kHz, -97dbm
Call Rf_cmd(&Hc2ac) 'data filter:
Call Rf_cmd(&Hc483) 'AFC:
Call Rf_cmd(&H9850) 'TX control
Call Rf_cmd(&He000) 'wake-up
Call Rf_cmd(&Hc800) 'low duty-cycle
Call Rf_cmd(&Hca81) 'Reset FIFO
Call Rf_cmd(&Hca83) 'enable FIFO
Call Rf_cmd(&H0000) 'read Status
End Sub
Sub Framehandler() 'habe ich mir nicht angeschaut
Set Led2
'Print "Frame: ";
'For Lv = 1 To Framelength
' Print Hex(rf12_data(lv)) ; " ";
' Next Lv
Print "Sync alias RF_Data(1) " ; Sync
If Sync = Snap_sync Then
If Hdb2 = Hdb2_ Then
'if HDB2= NDB then
If Dab1 = Myadress Then
Swap Dab1 , Sab1
Crc = Framelength - 2
Crc = Crc8(rf12_data(2) , Crc)
If Crc = Rf12_data(framelength) Then
Print "CRC OK! => " ; Crc
' Send ACK (i.e tell master that packet was OK)
' Set ACKs bit in HDB2 (xxxxxx10)
Hdb2 = Hdb2 Or &B00000010
Hdb2 = Hdb2 And &B11111110
'do ...
' Gosub Senden 'Anwortframe senden--> nur als Slave
'Gosub Cmd_handler
'RF12_data(2 <--- ackno machen
Else
Print "CRC Bad => " ; Crc
' Send NAK (i.e tell master that packet was bad)
' Set ACK bits in HDB2 (xxxxxx11)
Hdb2 = Hdb2 Or &B00000011
' Gosub Senden 'Anwortframe senden--> nur als Slave
End If
Else
Print "geht mich nix an!...von Nr.:" ; Rf12_data(5)
End If
'Else
Print "Framelength NIO!!!"
'End If
Else
Print "no 1Byte Adress!!!"
End If
Else
Print "no Syncbyte!!!"
End If
Waitms 10
Reset Led2
End Sub
Cmd_handler: 'Auswertung der Nutzdaten.... nur als Denkhilfe :)
'(
Select Case Rf12_data(7)
Case 0 :
Case 1 : Gosub Rolloaufmachen
Case 2 : Gosub Rollozumachen
Case 10 : Gosub Holetemperatur
End Select
')
For Lv = 1 To 20
Toggle Led2 'mache was...
Waitms 100
Next Lv
Return
Rf12_rxd_on:
Lv = 1
Rf_sync = 0 'sync-Flag
Rf_rxd = 0
Rf12_s = ""
' Framelength = 6 'erst mit 3.Byte kann die Framelänge berechnet werden
Call Rf_cmd(&H0000) 'Status lesen
Call Rf_cmd(&Hca81) 'FIFO&Reset CMD: sensitiver Reset aus (Brownout)
Call Rf_cmd(&Hca83) 'FIFO&Reset CMD: Synchroner Zeichenemfang (warte auf Startzeichen: 2DD4)
' Print "rf12_rxd_on "
Enable Int0
Return
Rf12_rxd_off:
Call Rf_cmd(&H0000) 'Status lesen
Call Rf_cmd(&Hca81) 'FIFO&Reset CMD: sensitiver Reset aus (Brownout)
' Print "rf12_rxd_off "
Disable Int0
Return
Senden: 'habe ich mir nicht angeschaut Dino Dieter
Set Led2
' Waitms 1
' Locate 2 , 1 : Lcd "Sende: "
For Lv = 1 To Framelength
Print Hex(rf12_data(lv)) ; " ";
Next Lv
' Call Rf12_txdata(framelength)
Waitms 10
Reset Led2
Return
Rxd_isr:
S = S + Chr(udr)
Return
'Interrupt Routine zum empfangen der bytes aus dem RFM 12
Ffit_isr:
Set Led1 ':zum debuggen für LA
Reset Led1
Spiin Fifo(1) , 2 '1 Byte lesen
Temp = Fifo(2)
If Temp = Snap_sync And Rf_sync = 0 Then
Rf_sync = 1
Lv = 1
Framelength = 6
End If
If Lv = 3 Then
Ndb = Temp And &H0F 'Anzahl Datenbytes berechnen
Ndb = 9 'zum debuggen
Select Case Ndb
Case 0 To 8 : Framelength = 6 + Ndb '6-14 (0--8 Datebyte)
Case 9 : Framelength = 6 + 16 '22 (16 Datenbyte)
Case 10 : Framelength = 6 + 32 '38 (32 Datenbyte)
'Case 11 : Framelength = 6 + 64 '70 (64 Datenbyte)
End Select
End If
If Lv >= Framelength And Rf_sync = 1 Then 'alles eingetroffen
Rf_rxd = 1 'Flag setzen und Empfänger zurück setzen
Rf_sync = 0
Call Rf_cmd(&H0000) 'read status
Call Rf_cmd(&Hca81) 'Reset FIFO wir warten wieder auf SYNC vom RFM 12
Call Rf_cmd(&Hca83) 'enable FIFO
End If
Rf12_data(lv) = Temp
Incr Lv
' Überlauf abgefangen , passend zum Empfangsbuffer
If Lv >= Recive_buffer_size Then
'Überlauf
Rf_sync = 0
Lv = 1
Rf12_s = "" 'buffer löschen
End If
Return
End
Das erste Problem, welches mir schnell aufgefallen war, ist die Sub Rf_cmd(byval Wert As Word) . Die hat nur Nullen gesendet. Nach Änderung der Variable ( temp0 oder so ) ging die SUB. Wieso, keine Ahnung.
Lesezeichen