Hi,
hier ist mal der Code
Code:
'Elektor RFM12
'$regfile = "M8def.dat"
'$regfile = "M168def.dat" 'es handelt sich um einen ATmega168
$regfile = "M168Pdef.dat" 'es handelt sich um einen ATmega168P
'$crystal = 3686400 'der eingebauter RC-Oszillator läuft mit 3,6864 Mhz
$crystal = 16000000
'$crystal = 1000000
$hwstack = 40 'im Speicher werden für den Hardware-Stack 100 Byte reserviert
$swstack = 10 'im Speicher werden für den Software-Stack 100 Byte reserviert
$framesize = 40 'im Speicher werden für den Frame 100 Byte reserviert
$baud = 9600
'Taster
Config Pind.2 = Input
Pind.2 = 1 'pullup
Taster1 Alias Pind.2
'LED
Led_grün Alias Portc.5
Config Led_grün = Output
Led_rot Alias Portc.4
Config Led_rot = Output
Nsel Alias Portd.7
Sdi Alias Portb.3
Sdo Alias Pinb.4
Sck Alias Portb.5
Config Sdo = Input
Config Nsel = Output
Config Sdi = Output
Config Sck = Output
Dim D As Word
Dim Data_in(10) As Byte
Dim Data_out(10) As Byte
Dim N As Byte
Dim Timeout As Word
Dim T As Word
Dim Tt As Word
Dim Freq As Single
Declare Sub Send_rfm12
Declare Sub Receive_rfm12
Declare Sub Freq_rfm12
Declare Sub Wait_rfm12
Declare Function Spi16(byval Dout As Word) As Word
'Config Portc = Output
'Porta = &HFF
'Init
Nsel = 1
Sck = 0
D = Spi16(&H80d7) ' El , Ef , 11.5pf, 433 MHz band
'D = Spi16(&H80e7) ' El , Ef , 11.5pf, 868 MHz band
D = Spi16(&H82d9) '!er , !ebb , Et , Es , Ex , !eb , !ew , Dc
D = Spi16(&Ha67c) ' 434,15 MHz / 868,3 MHz
D = Spi16(&Hc647) ' 4.8kbps
D = Spi16(&H94a4) ' Vdi , Fast , 134 kHz , 0db , -79dbm
D = Spi16(&Hc2ac) ' Al , !ml , Dig , Dqd4
D = Spi16(&Hca81) ' Fifo8 , Sync , !ff , Dr
D = Spi16(&Hc483) ' @pwr , No Rstric , !st , !fi , Oe , En
D = Spi16(&H9850) ' 90 kHz , power - 0 dB
D = Spi16(&He000) '
D = Spi16(&Hc800) '
D = Spi16(&Hc000) ' 1 MHz , 2.2V
Freq = 434.150
'868.300
Freq_rfm12
'start transmitter, no data
'Goto Test1:
'transmit data
'Goto Test2:
'transmit key data
'Goto Test3:
'start receiver, all data
'Goto Test4:
'start receiver, matched data
'Goto Test5:
Print "RFM12 Test"
Do
For N = 1 To 10
Data_out(n) = N
Next N
Send_rfm12
Waitms 500
For N = 1 To 10
Data_in(n) = 0
Next N
Timeout = 400 + Rnd(1000)
Receive_rfm12
For N = 1 To 10
Print Data_in(n);
Print " ";
Next N
Waitms 700
Toggle Led_grün
Loop
Sub Freq_rfm12
If Freq < 800 Then Freq = Freq * 2
Freq = Freq - 860
D = Freq / 0.0050
If D < 96 Then D = 96
If D > 3903 Then D = 3903
D = D + &HA000
D = Spi16(d)
End Sub
Sub Send_rfm12
D = Spi16(&H8238)
Wait_rfm12
D = Spi16(&Hb8aa)
Wait_rfm12
D = Spi16(&Hb8aa)
Wait_rfm12
D = Spi16(&Hb8aa)
Wait_rfm12
D = Spi16(&Hb82d)
Wait_rfm12
D = Spi16(&Hb8d4)
For N = 1 To 10
Wait_rfm12
D = &HB800 + Data_out(n)
D = Spi16(d)
Next N
Wait_rfm12
D = Spi16(&Hb8aa)
Wait_rfm12
D = Spi16(&Hb8aa)
Wait_rfm12
D = Spi16(&H8208)
End Sub
Sub Wait_rfm12
Nsel = 0
Do
Loop Until Sdo = 1
End Sub
Sub Receive_rfm12
Tt = Timeout * 10
D = Spi16(&H82c8)
D = Spi16(&Hca83)
For N = 1 To 10
Nsel = 0
T = 0
Do
T = T + 1
Waitus 100
If T > Tt Then Goto Nosignal
Loop Until Sdo = 1
D = Spi16(&Hb000)
Data_in(n) = D
Next N
Nosignal:
D = Spi16(&H8208)
End Sub
Function Spi16(byval Dout As Word) As Word
Local Nspi As Integer
Local Dspi As Integer
Local Dsdo As Word
Nsel = 0
Dsdo = 0
For Nspi = 1 To 16
Dspi = Dout And &H8000
If Dspi = 0 Then
Sdi = 0
Else
Sdi = 1
End If
Dout = Dout * 2
Dsdo = Dsdo * 2
Dsdo = Dsdo + Sdo
Sck = 1
Waitus 5
Sck = 0
Next Nspi
Nsel = 1
Spi16 = Dsdo
End Function
'transceiver tests
'start transmitter, no data
Test1:
D = Spi16(&H8238)
Do
Loop
'transmit data
Test2:
D = Spi16(&H8238)
Do
Wait_rfm12
D = Spi16(&Hb8aa)
Loop
'transmit key data
Test3:
D = Spi16(&H8238)
Do
Wait_rfm12
D = Spi16(&Hb82d)
Wait_rfm12
D = Spi16(&Hb8d4)
Loop
'start receiver, all data
Test4:
D = Spi16(&H82c8)
D = Spi16(&Hca87)
For N = 1 To 100
Wait_rfm12
D = Spi16(&Hb000)
Data_in(n) = D
Print Chr(d);
Next N
Do
Loop
'start receiver, matched data
Test5:
D = Spi16(&H82c8)
D = Spi16(&Hca83)
For N = 1 To 100
Wait_rfm12
D = Spi16(&Hb000)
Data_in(n) = D
Print Chr(d);
Next N
Do
Loop
Lesezeichen