Zdroj Kubanec

RFM modulky s Atmegou

Řeči okolo jsou v článku o PICech


Napsal jsem knihovny i pro Atmegu (v mém případě Atmega16), je použito hardwarové spi rozhraní

Hlavičkový soubor:


#ifndef rfm
#define rfm

//ještě se musi includovat soubor avr/io nebo nějaky .h kde je includovany avr/io, napřiklad main.h

#define NIRQ 3  
#define WAIT_NIRQ_LOW() while(PINB & _BV(NIRQ))
#define MOSI 5
#define SCK 7
#define MISO 6
#define SS 4
#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define wait_spi_complete() while(!(SPSR & _BV(SPIF))); bit_clr(SPSR,SPIF)

unsigned int rf_writecmd(unsigned int cmd);
void rf_init(void);
void rf_send(unsigned char data);
void spi_init(void);
void rf_sleep(void);
void rf_receiver(void);
void rf_transmitter(void);
void rf_fifo_reset(void);
void rf_prepare(void);

#endif 

Zdrojový soubor:


#include "rfm.h"

//------------------------------------
//spi initialization
//-----------------------------------
void spi_init(void){
	DDR_SPI= _BV(MOSI) | _BV(SCK) | _BV(SS);
	SPCR=0b01010000;
	
	SPSR=1;
}
//------------------------------------
//spi send and receive two bytes simultaneously
//-----------------------------------
  unsigned int rf_writecmd(unsigned int cmd){
	unsigned int receive=0;

	bit_clr(PORT_SPI,SS);
	SPDR=(cmd>>8);
	wait_spi_complete();
	receive=SPDR<<8;
	SPDR=cmd;
	wait_spi_complete();
	receive|=SPDR;
	bit_set(PORT_SPI,SS);

	return receive;
  }
//------------------------------------
//rfm initialization
//-----------------------------------
  void rf_init(void) {
    rf_writecmd(0x80E7); //EL,EF,868band,12.0pF
    rf_writecmd(0xA640); //frequency select
    rf_writecmd(0xC623); //4.8kbps
    rf_writecmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm
    rf_writecmd(0xC2AC); //AL,!ml,DIG,DQD4
    rf_writecmd(0xCA81); //FIFO8,SYNC,!ff,DR
    rf_writecmd(0xCED4); //SYNC=2DD4
    rf_writecmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN
    rf_writecmd(0x9850); //!mp,90kHz,MAX OUT
    rf_writecmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDITCBW0
    rf_writecmd(0xE000); //NOT USED
    rf_writecmd(0xC800); //NOT USED
    rf_writecmd(0xC040); //1.66MHz,2.2V
  }
//------------------------------------
//Data send
//-----------------------------------
void rf_send(unsigned char data){
	  
    WAIT_NIRQ_LOW();
    rf_writecmd(0xB800 + data);
  }
//------------------------------------
//Sleep
//-----------------------------------
void rf_sleep(void){
  rf_writecmd(0x8201); 
  rf_writecmd(0x0000);
}
//------------------------------------
//Transmitter
//-----------------------------------
void rf_transmitter(void){
  rf_writecmd(0x8239); //er,!ebb,ET,ES,EX,!eb,!ew,DC 
  rf_writecmd(0x0000);
}
//------------------------------------
//Receiver
//-----------------------------------
void rf_receiver(void){
  rf_writecmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC (bug was here)
  rf_writecmd(0x0000);
}
//----------------------------------
//Buffer reset
//----------------------------------
void rf_fifo_reset() {		
  rf_writecmd(0xCA81);
  rf_writecmd(0xCA83);
  }
//----------------------------------
//Pre send
//----------------------------------
void rf_prepare() {		
	rf_writecmd(0);
	rf_send(0xAA);
	rf_send(0xAA);
	rf_send(0xAA);
	rf_send(0x2D);
	rf_send(0xD4);
  }


| Nahoru | Home | NF technika | Zdroje | Mikroprocesory | Měření | Ostatní | Guitar Pro | Eagle | Něco o mně |