von
Floxx (Gast)
10.11.2012 17:10
Hallo,
ich möchte auf den Beitrag Beitrag "Beispielprogramm für RFM12 433MHz Funk-Module"
verweisen, wo eine Art Lib fürs Senden und Empfangen mit den RF01 und
RF02 Modulen von Pollin zur Verfügung steht.
Nachdem ich, nicht wie der Autor die 433MHz, sondern die 868MHz Platinen
habe, habe ich den Code so gut ich konnte angepasst.
Nachdem dieses Thema hier schon ein paar Mal bearbeitet wurde, gibt es
sicher den einen oder anderen unter euch, der diese Dinger zum Laufen
(bzw Funken ;-) ) bekommen hat. Kann mir mal jemand bitte über meinen
Code drüber schauen und kurz bericht(ig)en, was ich noch bei der
Initialisierung falsch gemacht habe?
Die SPI läuft jedenfalls --> mit Oszi getestet.
rf02.c (Senden): 1 void rf02_trans ( unsigned short wert )
2 {
3 unsigned char i ;
4
5 cbi ( RF_PORT , CS );
6 for ( i = 0 ; i < 16 ; i ++ )
7 { if ( wert & 32768 )
8 sbi ( RF_PORT , SDI );
9 else
10 cbi ( RF_PORT , SDI );
11 sbi ( RF_PORT , SCK );
12 wert <<= 1 ;
13 _delay_us ( 0 . 3 );
14 cbi ( RF_PORT , SCK );
15 }
16 sbi ( RF_PORT , CS );
17 }
18
19 void rf02_init ( void )
20 {
21 RF_PORT = ( 1 << CS );
22 RF_DDR = ( 1 << SDI ) | ( 1 << SCK ) | ( 1 << CS );
23
24 for ( unsigned char i = 0 ; i < 15 ; i ++ )
25 _delay_ms ( 10 ); // wait until POR done
26 rf02_trans ( 0xC0E0 ); // power settings
27
28 rf02_trans ( 0x9080 );
29
30 rf02_trans ( 0xC2A0 ); // enable tx sync bit, disable low bat detector
31 }
32
33 void rf02_setmodfreq ( unsigned char bandwidth )
34 {
35 rf02_trans ( 0x9080 | ( bandwidth & 7 ));
36 }
37
38 void rf02_setfreq ( unsigned short freq )
39 {
40 rf02_trans ( 0xA000 | freq );
41 }
42
43 void rf02_setpower ( unsigned char power )
44 {
45 rf02_trans ( 0xB000 | (( power & 7 ) << 8 ));
46 }
47
48 void rf02_setbaud ( unsigned short baud )
49 {
50 if ( baud < 1345 )
51 baud = 1345 ;
52 if ( baud < 19000 )
53 rf02_trans ( 0xD240 ); // 25% PLL current
54 else if ( baud < 37000 )
55 rf02_trans ( 0xD2C0 ); // 33% PLL current
56 else
57 rf02_trans ( 0xD200 ); // 50% PLL current
58 rf02_trans ( 0xC800 | (( 344828UL / baud ) - 1 )); // Baudrate= 344827,59/(R+1)
59 }
60
61 void rf02_txdata ( unsigned char * data , unsigned char number )
62 {
63 unsigned char i , wert ;
64 wert = 0xC6 ;
65 cbi ( RF_PORT , CS );
66 for ( i = 0 ; i < 8 ; i ++ )
67 {
68 if ( wert & 128 )
69 sbi ( RF_PORT , SDI );
70 else
71 cbi ( RF_PORT , SDI );
72 sbi ( RF_PORT , SCK );
73 wert <<= 1 ;
74 _delay_us ( 0 . 2 );
75 cbi ( RF_PORT , SCK );
76 }
77 rf02_shiftout ( 0xAA );
78 rf02_shiftout ( 0xAA );
79 rf02_shiftout ( 0xAA );
80 rf02_shiftout ( 0x2D );
81 rf02_shiftout ( 0xD4 );
82 for ( i = 0 ; i < number ; i ++ )
83 rf02_shiftout ( * data ++ );
84 sbi ( RF_PORT , CS );
85 while ( RF_PIN & ( 1 << IRQ )); // wait until transfer done
86 rf02_trans ( 0xC464 ); // TX off after 10us
87 }
88
89 void rf02_shiftout ( unsigned char wert )
90 {
91 unsigned char j ;
92 for ( j = 0 ; j < 8 ; j ++ )
93 { while ( RF_PIN & ( 1 << IRQ ));
94 while ( ! ( RF_PIN & ( 1 << IRQ )));
95 if ( wert & 128 )
96 sbi ( RF_PORT , SDI );
97 else
98 cbi ( RF_PORT , SDI );
99 wert <<= 1 ;
100 }
101 }
102
103 int main ( void )
104 {
105 rf02_init (); // CLK auf 1MHz, etc.
106 rf02_setfreq ( 868 ); // Sende/Empfangsfrequenz auf 868MHz einstellen
107 rf02_setpower ( 4 ); // -12dBm Ausgangangsleistung
108 rf02_setmodfreq ( 3 ); // 120kHz Frequenzshift
109 rf02_setbaud ( 19200 ); // 19200 Baud
110 for (;;)
111 {
112 rf02_txdata ( test , 32 );
113 for ( unsigned char i = 0 ; i < 100 ; i ++ )
114 _delay_ms ( 10 );
115 }
116 }
rf01.c (Empfangen): 1 void rf01_trans ( unsigned short wert )
2 { unsigned char i ;
3
4 cbi ( RF_PORT , CS );
5 for ( i = 0 ; i < 16 ; i ++ )
6 { if ( wert & 32768 )
7 sbi ( RF_PORT , SDI );
8 else
9 cbi ( RF_PORT , SDI );
10 sbi ( RF_PORT , SCK );
11 wert <<= 1 ;
12 _delay_us ( 0 . 2 );
13 cbi ( RF_PORT , SCK );
14 }
15 sbi ( RF_PORT , CS );
16 }
17
18 void rf01_init ( void )
19 { unsigned char i ;
20 RF_PORT = ( 1 << CS );
21 RF_DDR = ( 1 << SDI ) | ( 1 << SCK ) | ( 1 << CS ) | ( 1 << PB0 );
22
23 for ( i = 0 ; i < 11 ; i ++ )
24 _delay_ms ( 10 ); // wait until POR done
25
26 // rf01_trans(0xC2E0); // AVR CLK: 10MHz
27 rf01_trans ( 0xC200 ); // AVR CLK: 1MHz
28 rf01_trans ( 0xC42B ); // Data Filter: internal
29 rf01_trans ( 0xCE88 ); // FIFO mode
30 rf01_trans ( 0xC6F7 ); // AFC settings: autotuning: -10kHz...+7,5kHz
31 rf01_trans ( 0xE000 ); // disable wakeuptimer
32 rf01_trans ( 0xCC00 ); // disable low duty cycle
33 }
34
35 void rf01_setbandwidth ( unsigned char bandwidth )
36 {
37 rf01_trans ( 0x8970 | (( bandwidth & 7 ) << 1 ));
38 }
39
40 void rf01_setreceiver ( unsigned char gain , unsigned char drssi )
41 {
42 sdrssi = drssi ;
43 sgain = gain ;
44 }
45
46 void rf01_setfreq ( unsigned short freq )
47 {
48 /*if (freq<96) // 430,2400MHz
49 freq=96;
50 else if (freq>3903) // 439,7575MHz
51 freq=3903;
52 */
53 rf01_trans ( 0xA000 | freq );
54 }
55
56 void rf01_setbaud ( unsigned short baud )
57 {
58 if ( baud < 336 )
59 return ;
60 if ( baud < 5400 ) // Baudrate= 344827,58621/(R+1)/(1+CS*7)
61 rf01_trans ( 0xC880 | (( 43104 / baud ) - 1 ));
62 else
63 rf01_trans ( 0xC800 | (( 344828UL / baud ) - 1 ));
64 }
65
66 void rf01_rxdata ( unsigned char * data , unsigned char number )
67 { unsigned char i , j , c ;
68
69 rf01_trans ( 0xC0C1 | (( sgain & 3 ) << 4 ) | (( sdrssi & 7 ) << 1 )); // RX on
70 rf01_trans ( 0xCE89 ); // set FIFO mode
71 rf01_trans ( 0xCE8B ); // enable FIFO
72 cbi ( RF_PORT , SDI );
73 for ( i = 0 ; i < number ; i ++ )
74 { cbi ( RF_PORT , CS );
75 while ( ! ( RF_PIN & ( 1 << SDO ))); // wait until data in FIFO
76 for ( j = 0 ; j < 16 ; j ++ ) // read and discard status register
77 { sbi ( RF_PORT , SCK );
78 asm ( "nop" );
79 cbi ( RF_PORT , SCK );
80 }
81 c = 0 ;
82 for ( j = 0 ; j < 8 ; j ++ )
83 { c <<= 1 ;
84 if ( RF_PIN & ( 1 << SDO ))
85 c |= 1 ;
86 sbi ( RF_PORT , SCK );
87 _delay_us ( 0 . 2 );
88 cbi ( RF_PORT , SCK );
89 }
90 * data ++= c ;
91 sbi ( RF_PORT , CS );
92 }
93 rf01_trans ( 0xC0C0 | (( sgain & 3 ) << 4 ) | (( sdrssi & 7 ) << 1 )); // RX off
94 }
95
96 int main ( void )
97 {
98 rf01_init (); // CLK auf 1MHz
99 rf01_setfreq ( 868 ); // Sende/Empfangsfrequenz auf 868MHz einstellen
100 rf01_setbandwidth ( 4 ); // 200kHz Bandbreite
101 rf01_setreceiver ( 2 , 4 ); // -6dB Verstärkung, DRSSI threshold: -79dBm
102 rf01_setbaud ( 19200 ); // 19200 Baud
103
104 for (;;)
105 { rf01_rxdata ( data , 32 ); // 32Bytes empfangen
106 // hier die Daten verarbeiten
107 if ( data [ 0 ] == '1' ){
108 PORTB |= ( 1 << PB0 );
109 _delay_ms ( 500 );
110 PORTB &= ~ ( 1 << PB0 );
111 }
112 }
113 }
Danke für die Hilfe!
P.s.: der ATmega8 läuft auf 1MHz mit internem RC Osz.
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.