Code:
' Atmega8
' VDD -> VCC
' GND -> GND
' SDI -> MOSI (PB3, Pin 17)
' SDO -> MISO (PB4, Pin 18)
' SCK -> SCK (PB5, Pin 19)
' nSel -> SS (PB2, Pin 16)
' Fsk - > R 10k nach VDD
' Atmega16
' VDD -> VCC
' GND -> GND
' SDI -> MOSI (PB5, Pin 6)
' SDO -> MISO (PB6, Pin 7)
' SCK -> SCK (PB7, Pin 8)
' nSel -> SS (PB4, Pin 5)
' Fsk - > R 10k nach VDD
$regfile = "m8def.dat"
$crystal = 1000000 '
$hwstack = 32
$swstack = 32
$framesize = 40
Declare Sub Rf12_init
Declare Function Rf12_trans(byval Wert As Word) As Word
Declare Sub Rf12_setfreq(byval Freq As Single)
Declare Sub Rf12_setbandwith(byval Bandwith As Byte , Byval Gain As Byte , Byval Drssi As Byte)
Declare Sub Rf12_setbaud(byval Rfbaud As Long)
Declare Sub Rf12_setpower(byval Outpower As Byte , Byval Fskmod As Byte)
Declare Sub Rf12_ready
Declare Sub Rf12_readys
Declare Sub Rf12_txdata(byval Maxchar As Byte)
Declare Sub Rf12_rxdata(byval Maxchar As Byte)
Const Rf12freq = 433.92 ' Sende-/Empfangsfrequenz in 0,0025MHz-Schritten
Const Rf12baud = 700 ' Baudrate (>663)
Const Maxchar = 17 ' Anzahl der übertragenen Zeichen
'Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5 , E = Portc.1 , Rs = Portc.0
'Config Lcd = 16 * 2 , Chipset = Dogm162v3
'Cursor Off Noblink
'Cls
' config the SPI in master mode.The clock must be a quarter of the slave cpu
' Hier ggf. den SoftSPI reinmachen
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 128 , Noss = 1
Config Portb.1 = Output ' LED zur Schleifenkontrolle
' werden benötigt für rf12_ready
Spi_cs Alias Portb.2 ' SS-Pin
Config Spi_cs = Output
Spi_sdo Alias Pinb.4 ' MISO-PIN
Set Spi_cs
Spiinit
Dim Count As Byte
Dim Temp As Word
Dim Rfdata(32) As Byte
Dim Text As String * Maxchar At Rfdata Overlay
'Locate 1 , 1 : Lcd "Init RFM12 "
'Waitms 100
Call Rf12_init ' ein paar Register setzen
'Locate 1 , 1 : Lcd "Set Frequenz "
'Waitms 100
Call Rf12_setfreq(rf12freq) ' Sende/Empfangsfrequenz einstellen
'Locate 1 , 1 : Lcd "Set Bandwith "
'Waitms 100
Call Rf12_setbandwith(4 , 0 , 4) ' 200kHz Bandbreite, 0dB Verstärkung, DRSSI threshold: -79dBm
'Locate 1 , 1 : Lcd "Set Baudrate "
'Waitms 100
Call Rf12_setbaud(rf12baud) ' Setzt Baud
'Locate 1 , 1 : Lcd "Set Tx-Power "
'Waitms 100
Call Rf12_setpower(0 , 6) ' 0dBm Ausgangangsleistung, 105kHz Frequenzshift
'Text = "Dies ist ein 433MHz Test !!!!!{013}{010}"
' Je nachdem ob Sender oder Empfänger die entsprechenden Zeilen aktivieren
Do ' Ewigschleife
' Pulseout Portb , 1 , 500 ' Schleifenkontrolle
' Locate 1 , 1 : Lcd "Empfange "
' Locate 2 , 1 : Lcd " "
Call Rf12_rxdata(maxchar)
' Locate 2 , 1 : Lcd Text
Waitms 25
' Pulseout Portb , 1 , 500 ' Schleifenkontrolle
' Locate 1 , 1 : Lcd "Sende "
Call Rf12_txdata(maxchar)
' Locate 1 , 1 : Lcd "Ready "
' Waitms 100
Loop
End
' ########################################################################
' ###### Unterroutinen
' ########################################################################
Sub Rf12_init:
Waitms 150
Temp = Rf12_trans(&Hc0e0) ' Low Battery Detector and Microcontroller Clock Divider Command
Temp = Rf12_trans(&H80d7) ' Configuration Setting Command
Temp = Rf12_trans(&Hc2ab) ' Data Filter Command
Temp = Rf12_trans(&Hca81) ' Output and FIFO mode Command
Temp = Rf12_trans(&He000) ' Wake-Up Timer Command
Temp = Rf12_trans(&Hc800) ' Low Duty-Cycle Command
Temp = Rf12_trans(&Hc4f7) ' AFC Command Part 1
End Sub
Sub Rf12_setfreq(byval Freq As Single)
Freq = Freq - 430.00
Temp = Freq / 0.0025
If Temp < 96 Then
Temp = 96
Elseif Temp > 3903 Then
Temp = 3903
End If
Temp = Temp + &HA000 ' Frequency Setting Command
Temp = Rf12_trans(temp)
End Sub
Sub Rf12_setbandwith(byval Bandwith As Byte , Byval Gain As Byte , Byval Drssi As Byte)
Drssi = Drssi And 7
Gain = Gain And 3
Temp = Bandwith And 7
Shift Temp , Left , 2
Temp = Temp + Gain
Shift Temp , Left , 3
Temp = Temp + Drssi
Temp = Temp + &H9400 ' Receiver Control Command, VDI Output
Temp = Rf12_trans(temp)
End Sub
Sub Rf12_setbaud(byval Rfbaud As Long )
Local Ltemp As Long
If Rfbaud < 663 Then Exit Sub
If Rfbaud < 5400 Then
Temp = 43104 / Rfbaud
Temp = Temp + &HC680 ' Data Rate Command
Else
Ltemp = 344828 / Rfbaud
Temp = Ltemp
Temp = Temp + &HC600 ' Data Rate Command
End If
Decr Temp
Temp = Rf12_trans(temp)
End Sub
Sub Rf12_setpower(byval Outpower As Byte , Byval Fskmod As Byte)
Outpower = Outpower And 7
Temp = Fskmod And 15
Shift Temp , Left , 4
Temp = Temp + Outpower
Temp = Temp + &H9800 ' AFC Command Part 2
Temp = Rf12_trans(temp)
End Sub
Sub Rf12_txdata(byval Maxchar As Byte)
Temp = Rf12_trans(&H8238) ' Power Management Command, Sender einschalten
Rf12_ready
Temp = Rf12_trans(&Hb8aa) ' Transmit Register Write Command (sendet AA zur Synchronisation)
Rf12_ready
Temp = Rf12_trans(&Hb8aa) ' Transmit Register Write Command (sendet AA zur Synchronisation)
Rf12_ready
Temp = Rf12_trans(&Hb8aa) ' Transmit Register Write Command (sendet AA zur Synchronisation)
Rf12_ready
Temp = Rf12_trans(&Hb82d) ' Transmit Register Write Command (sendet 2D für was auch immer)
Rf12_ready
Temp = Rf12_trans(&Hb8d4) ' Transmit Register Write Command (sendet D4 für was auch immer)
Rf12_ready
For Count = 1 To Maxchar
Rf12_ready
Temp = &HB800 + Rfdata(count) ' Transmit Register Write Command (sendet Daten)
Temp = Rf12_trans(temp)
Next Count
Rf12_ready
Temp = Rf12_trans(&H8208) ' Power Management Command, Sender + Empfänger ausschalten
End Sub
Sub Rf12_rxdata(byval Maxchar As Byte)
Temp = Rf12_trans(&H82c8) ' Power Mangement Command, Empfänger einschalten
Temp = Rf12_trans(&Hca81) ' Output and FIFO Mode Command
Temp = Rf12_trans(&Hca83) ' Output and FIFO Mode Command
For Count = 1 To Maxchar
Rf12_ready
Temp = Rf12_trans(&Hb000) ' Receiver FIFO Read Command
Rfdata(count) = Temp
Next Count
Temp = Rf12_trans(&H8208) ' Power Management Command, Sender + Empfänger ausschalten
End Sub
Function Rf12_trans(byval Wert As Word) As Word
Local Lowbyte As Byte
Local Highbyte As Byte
Lowbyte = Wert And 255
Shift Wert , Right , 8
Reset Spi_cs
Highbyte = Spimove(wert)
Lowbyte = Spimove(lowbyte)
Set Spi_cs
Temp = Highbyte * 256
Temp = Temp + Lowbyte
Rf12_trans = Temp
End Function
Sub Rf12_ready
Local I As Word
Reset Spi_cs
Do
Loop Until Spi_sdo = 1
End Sub
Hab den Code ein wenig dokumentiert. Jetzt sollte es eigentlich jedem möglich sein, raus zu finden was das Programm macht. Ein paar Fragen sind aber trotzdem noch offen. Warum muß man in der Senderoutine noch ein paar Daten vorher senden ???
Lesezeichen