Soooo:
2 ATMega8 an einem ein Taster am anderen eine LED
2 Pollin RFM12 mit 434Mhz
Code:
'Elektor RFM12 Sender
$regfile = "m8def.dat"
$hwstack = 32
$swstack = 10
$framesize = 40
$crystal = 16000000
$baud = 19200
Nsel Alias Portb.2
Sdi Alias Portb.3
Sdo Alias Pinb.4
Sck Alias Portb.5
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 Nsel = Output
Config Sdi = Output
Config Sck = Output
Config Portc.5 = Output
Config Pinc.4 = Input
Pinc.4 = 1
'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
Do
For N = 1 To 10
Data_out(n) = 6
Next N
If Pinc.4 = 0 Then
Data_out(2) = 12
Else
Data_out(2) = 10
End If
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
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
Empfänger:
Code:
'Elektor RFM12 Empfänger
$regfile = "m8def.dat"
$hwstack = 32
$swstack = 10
$framesize = 40
$crystal = 16000000
$baud = 19200
Nsel Alias Portb.2
Sdi Alias Portb.3
Sdo Alias Pinb.4
Sck Alias Portb.5
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 Nsel = Output
Config Sdi = Output
Config Sck = Output
Config Portc = Output
'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
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
If Data_in(2) = 12 Then
Portc.5 = 1
Else
Portc.5 = 0
End If
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
Es lief erst zuverlässig, nachdem ich die Fusebits SUT0 und SUT1 gelöcht (also aktiviert) habe. Der Prozessor hat jetzt mehr Zeit zum Einschwingen.
Günter
Lesezeichen