//UNAL 2010 //RFM22B Demo example by juan64bits, based on HOPE RF examples for PIC //and datasheet. http://www.hoperf.com/rf_fsk/rfm22b.htm //FIXME: // *¿? //TODO: // *In order to avoid using while() loops is necessary to // implement ISR (interrupt service routine) for RF_IRQ pin // * #include #include #define DEBUG 0 // Debug mode. #define DEMO_RX 0 // Rx demo mode. #define DEMO_TX 1 // Tx demo mode. #define F_CPU 1000000UL // 1 MHz #define SPI_PORT PORTB // SPI PORT #define SPI_DDR DDRB // PORT REGISTER #define SPI_CS PB0 // RF_nSEL #define RF_PORT PORTD // RF PORT #define RF_DDR DDRD // RF DDRD #define RF_PIN PIND // RF PIN #define RF_RXEN PD4 // Rx enable #define RF_TXEN PD3 // Tx enable #define RF_IRQ PD7 // IRQ #define RF22B_PWRSTATE_POWERDOWN 0x00 #define RF22B_PWRSTATE_READY 0x01 #define RF22B_Rx_packet_received_interrupt 0x02 #define RF22B_PACKET_SENT_INTERRUPT 0x04 #define RF22B_PWRSTATE_RX 0x05 #define RF22B_PWRSTATE_TX 0x09 void initUSART0(unsigned long baud) { UBRR0 = F_CPU/16/baud-1; // Set Baudrate UCSR0C = (3<>4)<10) transmit1byteUSART0('0'+(value>>4)); else transmit1byteUSART0('A'+(value>>4)-10); if((value&0x0F)<10) transmit1byteUSART0('0'+(value&0x0F)); else transmit1byteUSART0('A'+(value&0x0F)-10); transmit1byteUSART0(' '); transmit1byteUSART0(0); } unsigned char spiWriteRead(unsigned char dataout) { unsigned char datain; // Start transmission (MOSI) SPDR = dataout; // Wait for transmission complete while(!(SPSR & (1<gpio spiWrite(0x71, 0x22); spiWrite(0x72, 0x38); // frequency deviation setting to 45k = 72*625 spiWrite(0x73, 0x0); spiWrite(0x74, 0x0); // no offset //band 434 spiWrite(0x75, 0x53); // hbsel = 0, sbsel =1 ???, fb = 19 spiWrite(0x76, 0x64); // 25600= 0x6400 for 434Mhz spiWrite(0x77, 0x00); } void rfm22bSetReady(void) { #if DEBUG==1 transmitStrUSART0("RFM22B Setting Ready Mode\r\n"); transmitStrUSART0(" Register 0x03:"); sendByteToHexUart(spiRead(0x03)); transmitStrUSART0(" Register 0x04:"); sendByteToHexUart(spiRead(0x04)); transmitStrUSART0("\r\n"); #else spiRead(0x03); spiRead(0x04); #endif spiWrite(0x07, RF22B_PWRSTATE_READY); } void rfm22bSetSleep(void) { spiWrite(0x07, RF22B_PWRSTATE_READY); #if DEBUG==1 transmitStrUSART0("RFM22B Setting Sleep Mode\r\n"); transmitStrUSART0(" Register 0x03:"); sendByteToHexUart(spiRead(0x03)); transmitStrUSART0(" Register 0x04:"); sendByteToHexUart(spiRead(0x04)); transmitStrUSART0("\r\n"); #else spiRead(0x03); spiRead(0x04); #endif spiWrite(0x07, RF22B_PWRSTATE_POWERDOWN); } void rfm22bSendByte(unsigned char value) { transmitStrUSART0("RFM22B Sending Data:"); sendByteToHexUart(value); transmitStrUSART0("\r\n"); rfm22bSetReady(); RF_PORT &= ~(1<