| 2112 | 
        kakl | 
        1 | 
        /********************************************* | 
      
      
         | 
         | 
        2 | 
        * | 
      
      
         | 
         | 
        3 | 
        * draft version v 0.1, experimental | 
      
      
         | 
         | 
        4 | 
        * | 
      
      
         | 
         | 
        5 | 
        * code based on the code of "benedikt k." | 
      
      
         | 
         | 
        6 | 
        * this was an avr project from the site: http://www.mikrocontroller.net/topic/65984#541030 | 
      
      
         | 
         | 
        7 | 
        * | 
      
      
         | 
         | 
        8 | 
        * | 
      
      
         | 
         | 
        9 | 
        * code should be matched with RF01 | 
      
      
         | 
         | 
        10 | 
        * | 
      
      
         | 
         | 
        11 | 
        * up to now no transmission between the RF12 modules and Jeelabs.com RF12 lib | 
      
      
         | 
         | 
        12 | 
        * | 
      
      
         | 
         | 
        13 | 
        * this code has worked: transmitting using atmega168 and atmega328 in combination with RF01s and RF02s | 
      
      
         | 
         | 
        14 | 
        * | 
      
      
         | 
         | 
        15 | 
        * arduino 18 | 
      
      
         | 
         | 
        16 | 
        * | 
      
      
         | 
         | 
        17 | 
        * five march, contrechoc.com, june 2010 , october 2010 | 
      
      
         | 
         | 
        18 | 
        * | 
      
      
         | 
         | 
        19 | 
        * | 
      
      
         | 
         | 
        20 | 
        *********************************************/ | 
      
      
         | 
         | 
        21 | 
          | 
      
      
         | 
         | 
        22 | 
        #include <avr/io.h> | 
      
      
         | 
         | 
        23 | 
        #include <avr/interrupt.h> | 
      
      
         | 
         | 
        24 | 
        #include <avr/pgmspace.h> | 
      
      
         | 
         | 
        25 | 
        #include <avr/eeprom.h> | 
      
      
         | 
         | 
        26 | 
        #include <stdlib.h> | 
      
      
         | 
         | 
        27 | 
          | 
      
      
         | 
         | 
        28 | 
        #include "rf01.h" | 
      
      
         | 
         | 
        29 | 
          | 
      
      
         | 
         | 
        30 | 
        #define F_CPU 16000000UL | 
      
      
         | 
         | 
        31 | 
        #include <util/delay.h> | 
      
      
         | 
         | 
        32 | 
          | 
      
      
         | 
         | 
        33 | 
        #define RF_PORT	PORTB | 
      
      
         | 
         | 
        34 | 
        #define RF_DDR	DDRB | 
      
      
         | 
         | 
        35 | 
        #define RF_PIN	PINB | 
      
      
         | 
         | 
        36 | 
          | 
      
      
         | 
         | 
        37 | 
        #define SDI		5	// RF01  SDI,  arduino  13 cannot be changed | 
      
      
         | 
         | 
        38 | 
        #define SCK		4	// RF01  SCK,  arduino  12 cannot be changed | 
      
      
         | 
         | 
        39 | 
        #define CS		3	// RF01  nSEL, arduino  11 cannot be changed | 
      
      
         | 
         | 
        40 | 
        #define SDO		2	// RF01  SDO,  arduino  10 cannot be changed | 
      
      
         | 
         | 
        41 | 
        //----------------- // RF01  niRQ, arduino  02 cannot be changed | 
      
      
         | 
         | 
        42 | 
        //------------------// RF01  nFFS: 1-10k Pullup too Vcc | 
      
      
         | 
         | 
        43 | 
          | 
      
      
         | 
         | 
        44 | 
          | 
      
      
         | 
         | 
        45 | 
        #ifndef cbi | 
      
      
         | 
         | 
        46 | 
        #define cbi(sfr, bit)     (_SFR_BYTE(sfr) &= ~_BV(bit))  | 
      
      
         | 
         | 
        47 | 
        #endif | 
      
      
         | 
         | 
        48 | 
        #ifndef sbi | 
      
      
         | 
         | 
        49 | 
        #define sbi(sfr, bit)     (_SFR_BYTE(sfr) |= _BV(bit))   | 
      
      
         | 
         | 
        50 | 
        #endif | 
      
      
         | 
         | 
        51 | 
          | 
      
      
         | 
         | 
        52 | 
        // maximum receive buffer | 
      
      
         | 
         | 
        53 | 
        #define RF_MAX   32 | 
      
      
         | 
         | 
        54 | 
        unsigned char  rf01_buf[RF_MAX];  // recv buf | 
      
      
         | 
         | 
        55 | 
          | 
      
      
         | 
         | 
        56 | 
        #include <util/delay.h> | 
      
      
         | 
         | 
        57 | 
          | 
      
      
         | 
         | 
        58 | 
        void rf01_receive(){ | 
      
      
        | 2125 | 
        kakl | 
        59 | 
        	rf01_rxdata(rf01_data, 23); //!!!32 | 
      
      
        | 2112 | 
        kakl | 
        60 | 
        } | 
      
      
         | 
         | 
        61 | 
          | 
      
      
        | 2125 | 
        kakl | 
        62 | 
        static unsigned char sdrssi, sgain; | 
      
      
        | 2112 | 
        kakl | 
        63 | 
          | 
      
      
        | 2125 | 
        kakl | 
        64 | 
        void rf01_prepAll() | 
      
      
         | 
         | 
        65 | 
        { | 
      
      
         | 
         | 
        66 | 
        	RF_PORT=(1<<CS); | 
      
      
         | 
         | 
        67 | 
        	RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS); | 
      
      
         | 
         | 
        68 | 
          | 
      
      
         | 
         | 
        69 | 
        	for (unsigned char i=0; i<11; i++) _delay_ms(10);			// wait until POR done | 
      
      
         | 
         | 
        70 | 
          | 
      
      
         | 
         | 
        71 | 
        //	rf01_trans(0xC2E0);			// AVR CLK: 10MHz | 
      
      
         | 
         | 
        72 | 
        //	rf01_trans(0xC42B);			// Data Filter: internal | 
      
      
         | 
         | 
        73 | 
        //	rf01_trans(0xC6F7);			// AFC settings: autotuning: -10kHz...+7,5kHz | 
      
      
         | 
         | 
        74 | 
        //	rf01_trans(0xE000);			// disable wakeuptimer | 
      
      
         | 
         | 
        75 | 
        //	rf01_trans(0xCC00);			// disable low duty cycle | 
      
      
         | 
         | 
        76 | 
        //	rf01_trans(0x8978); // band 433MHz, enable crystal + 12pF, 200kHz bandwidth | 
      
      
         | 
         | 
        77 | 
          | 
      
      
         | 
         | 
        78 | 
        	rf01_trans(0x0000);  | 
      
      
         | 
         | 
        79 | 
        //	rf01_trans(0x898A); // band 433MHz, 134kHz bandwidth | 
      
      
         | 
         | 
        80 | 
        	rf01_trans(0x8000|0x1000|0x70|0x02); //band | 
      
      
         | 
         | 
        81 | 
        	rf01_trans(0xA640); //434MHz	 | 
      
      
         | 
         | 
        82 | 
        	rf01_trans(0xC823); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 9600 Bd | 
      
      
         | 
         | 
        83 | 
        	rf01_trans(0xC69B);	 | 
      
      
         | 
         | 
        84 | 
        	rf01_trans(0xC42A);			 | 
      
      
         | 
         | 
        85 | 
        	rf01_trans(0xC240); //*			 | 
      
      
         | 
         | 
        86 | 
        	rf01_trans(0xC080);	 //*		 | 
      
      
         | 
         | 
        87 | 
        	rf01_trans(0xCE88);	// FIFO mode | 
      
      
         | 
         | 
        88 | 
        	rf01_trans(0xCE8B);	    //* | 
      
      
         | 
         | 
        89 | 
        	rf01_trans(0xC081);	    //* | 
      
      
        | 2112 | 
        kakl | 
        90 | 
        } | 
      
      
         | 
         | 
        91 | 
          | 
      
      
         | 
         | 
        92 | 
        void rf01_trans(unsigned short wert) | 
      
      
         | 
         | 
        93 | 
        {	unsigned char i; | 
      
      
         | 
         | 
        94 | 
          | 
      
      
         | 
         | 
        95 | 
        	cbi(RF_PORT, CS); | 
      
      
         | 
         | 
        96 | 
        	for (i=0; i<16; i++) | 
      
      
         | 
         | 
        97 | 
        	{	if (wert&32768) | 
      
      
         | 
         | 
        98 | 
        			sbi(RF_PORT, SDI); | 
      
      
         | 
         | 
        99 | 
        		else | 
      
      
         | 
         | 
        100 | 
        			cbi(RF_PORT, SDI); | 
      
      
         | 
         | 
        101 | 
        		sbi(RF_PORT, SCK); | 
      
      
         | 
         | 
        102 | 
        		wert<<=1; | 
      
      
         | 
         | 
        103 | 
        		_delay_us(0.2); | 
      
      
         | 
         | 
        104 | 
        		cbi(RF_PORT, SCK); | 
      
      
         | 
         | 
        105 | 
        	} | 
      
      
         | 
         | 
        106 | 
        	sbi(RF_PORT, CS); | 
      
      
         | 
         | 
        107 | 
        } | 
      
      
         | 
         | 
        108 | 
          | 
      
      
         | 
         | 
        109 | 
        void rf01_rxdata(unsigned char *data, unsigned char number) | 
      
      
         | 
         | 
        110 | 
        {	unsigned char i,j,c; | 
      
      
         | 
         | 
        111 | 
          | 
      
      
        | 2125 | 
        kakl | 
        112 | 
          //!!!	 | 
      
      
         | 
         | 
        113 | 
        //	sgain=2;                      //2,4 -6dB LNA gain, DRSSI threshold: -79dBm | 
      
      
         | 
         | 
        114 | 
        //	sdrssi=4; | 
      
      
         | 
         | 
        115 | 
        //!!!	rf01_trans(0xC0C1|((sgain&3)<<4)|((sdrssi&7)<<1));	// RX on | 
      
      
        | 2112 | 
        kakl | 
        116 | 
        	rf01_trans(0xCE89);			// set FIFO mode | 
      
      
         | 
         | 
        117 | 
        	rf01_trans(0xCE8B);			// enable FIFO | 
      
      
         | 
         | 
        118 | 
        	cbi(RF_PORT, SDI); | 
      
      
        | 2125 | 
        kakl | 
        119 | 
        			asm("nop"); | 
      
      
         | 
         | 
        120 | 
        			asm("nop"); | 
      
      
         | 
         | 
        121 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        122 | 
        	for (i=0; i<number; i++) | 
      
      
         | 
         | 
        123 | 
        	{	cbi(RF_PORT, CS); | 
      
      
        | 2125 | 
        kakl | 
        124 | 
        			asm("nop"); | 
      
      
         | 
         | 
        125 | 
        			asm("nop"); | 
      
      
         | 
         | 
        126 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        127 | 
        		while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO | 
      
      
         | 
         | 
        128 | 
        		for (j=0; j<16; j++)	// read and discard status register | 
      
      
        | 2125 | 
        kakl | 
        129 | 
        		{	 | 
      
      
         | 
         | 
        130 | 
              sbi(RF_PORT, SCK); | 
      
      
        | 2112 | 
        kakl | 
        131 | 
        			asm("nop"); | 
      
      
        | 2125 | 
        kakl | 
        132 | 
        			asm("nop"); | 
      
      
         | 
         | 
        133 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        134 | 
        			cbi(RF_PORT, SCK); | 
      
      
        | 2125 | 
        kakl | 
        135 | 
        			asm("nop"); | 
      
      
         | 
         | 
        136 | 
        			asm("nop"); | 
      
      
         | 
         | 
        137 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        138 | 
        		} | 
      
      
         | 
         | 
        139 | 
        		c=0; | 
      
      
         | 
         | 
        140 | 
        		for (j=0; j<8; j++) | 
      
      
         | 
         | 
        141 | 
        		{	c<<=1; | 
      
      
         | 
         | 
        142 | 
        			if (RF_PIN&(1<<SDO)) | 
      
      
         | 
         | 
        143 | 
        				c|=1; | 
      
      
         | 
         | 
        144 | 
        			sbi(RF_PORT, SCK); | 
      
      
        | 2125 | 
        kakl | 
        145 | 
        			asm("nop"); | 
      
      
         | 
         | 
        146 | 
        			asm("nop"); | 
      
      
         | 
         | 
        147 | 
        			asm("nop"); | 
      
      
         | 
         | 
        148 | 
        //!!!			_delay_us(0.2); | 
      
      
        | 2112 | 
        kakl | 
        149 | 
        			cbi(RF_PORT, SCK); | 
      
      
        | 2125 | 
        kakl | 
        150 | 
        			asm("nop"); | 
      
      
         | 
         | 
        151 | 
        			asm("nop"); | 
      
      
         | 
         | 
        152 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        153 | 
        		} | 
      
      
         | 
         | 
        154 | 
        		*data++=c; | 
      
      
         | 
         | 
        155 | 
        		sbi(RF_PORT, CS); | 
      
      
        | 2125 | 
        kakl | 
        156 | 
        			asm("nop"); | 
      
      
         | 
         | 
        157 | 
        			asm("nop"); | 
      
      
         | 
         | 
        158 | 
        			asm("nop"); | 
      
      
        | 2112 | 
        kakl | 
        159 | 
        	} | 
      
      
        | 2125 | 
        kakl | 
        160 | 
        //!!!	rf01_trans(0xC0C0|((sgain&3)<<4)|((sdrssi&7)<<1));	// RX off | 
      
      
        | 2112 | 
        kakl | 
        161 | 
        } |