Das ist der Code, am Computer läuft ein atmega 8 und auf dem Roboter ein atmega32. Der Code ist derseleb nur jeweils mit kleinen abwandlungen.
Code:
' generated. Take care that the chip supports all fuse bytes.' ########################################################################
' ### RF12-Test in Bascom
' ### Basiert auf Code von Benedikt K.
' ### Joachim Fossie Bär Reiter 04/2007
' ########################################################################
$regfile = "m8def.dat"
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 'default use 10 for the SW stack
$framesize = 40
'$PROG &HFF,&HFF,&HD9,&H00' generated. Take care that the chip supports all fuse bytes.
$crystal = 11059200
$baud = 19200
Baud = 19200
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.5 , Db5 = Portc.4 , Db6 = Portc.3 , Db7 = Portc.2 , E = Portc.6 , Rs = Portc.7
Config Lcdbus = 4
Cursor Off Noblink
Cls
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_readys
Declare Sub Rf12_ready
Declare Sub Rf12_txdata(byval Maxchar As Byte)
Declare Sub Rf12_rxdata(byval Maxchar As Byte)
Const Rf12freq = 433.92
Const Rf12baud = 19200
Const Maxchar = 32
'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
' 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
'init the spi pins
Spiinit
' was so gebraucht wird
Dim Count As Byte
Dim Temp As Word
Dim Rfdata(32) As Byte
Dim Text As String * Maxchar At Rfdata Overlay
Dim S As Byte
Dim A$ As String * 1
Dim A As Byte
Print "Init"
Call Rf12_init ' ein paar Register setzen (z.B. CLK auf 10MHz)
Print "Set Frequenz"
Call Rf12_setfreq(rf12freq) ' Sende/Empfangsfrequenz auf 433,92MHz einstellen
Print "Set Bandwith"
Call Rf12_setbandwith(4 , 1 , 4) ' 200kHz Bandbreite, -6dB Verstärkung, DRSSI threshold: -79dBm
Print "Set Baudrate"
Call Rf12_setbaud(rf12baud) ' 19200 baud
Print "Set Power"
Call Rf12_setpower(0 , 6) ' 1mW Ausgangangsleistung, 120kHz Frequenzshift
' ########################################################################
' ###### Hauptproggi
' ########################################################################
Portd.7 = 1
Text = "bt"
' Je nachdem ob Sender oder Empfänger die entsprechenden Zeilen aktivieren
Do 'Ewigschleife
' Hier ist die Senderoutine
' Print "Sende"
Locate 1 , 1 'Cursor auf 1 Zeile, 1 Spalte
Lcd Text
Call Rf12_txdata(maxchar) 'senden
Wait 1
' Hier ist die Empfangsroutine
' Print "Empfange"
' Call Rf12_rxdata(maxchar)
' For Count = 1 To Maxchar
' A$ = Chr(rfdata(count)) ;
' If A$ = "e" Then
' A$ = ""
' Print "a"
' End If
' Next Count
' Print
Schleife:
'Locate 2 , 1
'Lcd A$
Loop
End 'end program
' ########################################################################
' ###### Unterroutinen
' ########################################################################
Sub Rf12_init:
Waitms 150
Temp = Rf12_trans(&Hc0e0)
Temp = Rf12_trans(&H80d7)
Temp = Rf12_trans(&Hc2ab)
Temp = Rf12_trans(&Hca81)
Temp = Rf12_trans(&He000)
Temp = Rf12_trans(&Hc800)
Temp = Rf12_trans(&Hc4f7)
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
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
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
Else
Ltemp = 344828 / Rfbaud
Temp = Ltemp
Temp = Temp + &HC600
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
Temp = Rf12_trans(temp)
End Sub
Sub Rf12_txdata(byval Maxchar As Byte)
Temp = Rf12_trans(&H8238)
Print "stop1"
Rf12_readys
Print "stop2"
Temp = Rf12_trans(&Hb8aa)
Rf12_readys
Temp = Rf12_trans(&Hb8aa)
Rf12_readys
Temp = Rf12_trans(&Hb8aa)
Rf12_readys
Temp = Rf12_trans(&Hb82d)
Rf12_readys
Temp = Rf12_trans(&Hb8d4)
Rf12_readys
For Count = 1 To Maxchar
Rf12_readys
Temp = &HB800 + Rfdata(count)
Temp = Rf12_trans(temp)
Next Count
Rf12_readys
Temp = Rf12_trans(&H8208)
End Sub
Sub Rf12_rxdata(byval Maxchar As Byte)
Temp = Rf12_trans(&H82c8)
Temp = Rf12_trans(&Hca81)
Temp = Rf12_trans(&Hca83)
For Count = 1 To Maxchar
Rf12_ready
Temp = Rf12_trans(&Hb000)
Rfdata(count) = Temp
Next Count
Temp = Rf12_trans(&H8208)
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_readys
Reset Spi_cs
nop
While Spi_sdo = 0
Wend
End Sub
Sub Rf12_ready 'ready Empfangen
Reset Spi_cs
While Spi_sdo = 0
If Len(s) > 0 Then 'Wenn ein string eingelesen
Temp = Rf12_trans(&Hca81) 'Fifo deaktivieren
Temp = Rf12_trans(&H8208) 'Empf. ausschalten
Goto Schleife 'Springe zum hauptprogramm
Else 'sonst warte weiter auf Empfangsdaten
Wend
End If
End Sub
Lesezeichen