Zegdus
Newbie level 2
Hi, I've a problem with communication between two modules RFM12BP. For communication between the MCU (Atmega644PA) and RFM12BP i use hardware SPI. Problem :
- In transmitter mode I can send (or second RFM recieves) only one byte followed by 0xAA, 0xAA, 0x2D, 0xD4, (byte), 0xAA, 0xAA.
- Modules work only with 4.8 kbps baudrate (if I increase or decrease baudrate I've got no data on reciever - i've tried changing reciever baseband bandwitch and dFSK - bottom page 37 in Si4421 datasheet)
To receieve data I use polling mode - page 31 in Si4421 datasheet.
Datasheet : https://www.silabs.com/Support Documents/TechnicalDocs/Si4421.pdf
Thanks in advance! Code below:
Pinout:
- In transmitter mode I can send (or second RFM recieves) only one byte followed by 0xAA, 0xAA, 0x2D, 0xD4, (byte), 0xAA, 0xAA.
- Modules work only with 4.8 kbps baudrate (if I increase or decrease baudrate I've got no data on reciever - i've tried changing reciever baseband bandwitch and dFSK - bottom page 37 in Si4421 datasheet)
To receieve data I use polling mode - page 31 in Si4421 datasheet.
Datasheet : https://www.silabs.com/Support Documents/TechnicalDocs/Si4421.pdf
Thanks in advance! Code below:
Code:
#include <avr/io.h>
#include <util\delay.h>
#include "Pinout.h"
#include "RF_CTRL.h"
#include "Frame.h"
#define RF12FREQ(freq) ((freq-860.0)/0.005)
#ifndef F_CPU
#define F_CPU 20000000UL
#endif
#define RS_BAUD 9600
#define RS_UBRR F_CPU / 16 / RS_BAUD - 1
void SPI_MasterInit(void);
void SPI_MasterTransmit(uint8_t cData);
void SPI_MasterTransmit_DATA16(uint16_t cData);
uint8_t SPI_sendbyte (uint8_t data);
uint16_t RF_write_cmd(uint16_t aCmd);
void InitPins(void);
void RF12Init(void);
uint16_t RF_write_cmd(uint16_t aCmd);
uint16_t RF_write_data(uint16_t aCmd);
uint16_t RF_Read_FIFO(void);
void rf12_setbandwidth(unsigned char bandwidth, unsigned char gain, unsigned char drssi);
void rf12_setfreq(unsigned short freq);
void rf12_setbaud(unsigned short baud);
void rf12_setpower(unsigned char power, unsigned char mod);
uint16_t status_read(void);
void RF_send_packet(uint8_t *tab,uint8_t len);
void RF12_direct_data_in(void);
uint8_t countByte(void);
void USART0_Init(unsigned int myubrr);
void USART0_Transmit(unsigned char data );
char USART0_Receive( void );
void checkBuff(void);
uint8_t dataBuff[32];
uint8_t tempVal = 0;
uint8_t endFrameFlag = 0;
uint8_t packet[4] = {
0x77, //naglowekA
0xFF, //naglowekB - nadawca, odbiorca, dlugosc pakietu
0xAA, //dane
0x55}; //end frame
void InitPins(void)
{
DDRA = 0x00;
DDRB = 0xFF;
DDRC = 0xEF;
DDRD = 0xEF;
PORTB = 0x0F;
RF_RST_H();
nFFS_H();
}
void RF12Init(void)
{
RF_RST_L();
_delay_ms(100);
RF_RST_H();
_delay_ms(100);
RF_write_cmd(0x0000);
RF_write_cmd(0x80E7); //EN_TX, EN_RX_FIFO, 868MHz, 12.5pF
RF_write_cmd(0xC2AC);
RF_write_cmd(0xCA81);
RF_write_cmd(0xE000);
RF_write_cmd(0xC4E7);
}
uint16_t RF_write_cmd(uint16_t aCmd)
{
uint16_t reply;
RF_CS_L();
SPDR = aCmd >> 8;
while (!(SPSR & _BV(SPIF)));
reply = SPDR << 8;
SPDR = aCmd;
while (!(SPSR & _BV(SPIF)));
reply |= SPDR;
RF_CS_H();
USART0_Transmit(reply>>8);
USART0_Transmit(reply);
return reply;
}
uint16_t RF_write_data(uint16_t aCmd)
{
uint16_t reply;
SPDR = aCmd >> 8;
while (!(SPSR & _BV(SPIF)));
reply = SPDR << 8;
SPDR = aCmd;
while (!(SPSR & _BV(SPIF)));
reply |= SPDR;
// USART0_Transmit(reply>>8);
// USART0_Transmit(reply);
return reply;
}
uint16_t RF_Read_FIFO(void)
{
uint16_t reply;
nFFS_L();
SPDR = 0x00;
while (!(SPSR & _BV(SPIF)));
reply = SPDR << 8;
SPDR = 0x00;
while (!(SPSR & _BV(SPIF)));
reply |= SPDR;
if(bit_is_clear(PINC,PC4))
{
nFFS_H();
}
return reply;
}
void rf12_setbandwidth(unsigned char bandwidth, unsigned char gain, unsigned char drssi)
{
RF_write_cmd(0x9400|((bandwidth&7)<<5)|((gain&3)<<3)|(drssi&7));
}
void rf12_setfreq(unsigned short freq)
{ if (freq<96)
freq=96; //860,48 MHz
else if (freq>3903)
freq=3903; //879,515 MHz
RF_write_cmd(0xA000|freq);
}
void rf12_setbaud(unsigned short baud)
{
if (baud<663)
return;
if (baud<5400) // Baudrate= 344827,58621/(R+1)/(1+CS*7)
RF_write_cmd(0xC680|((43104/baud)-1));
else
RF_write_cmd(0xC600|((344828UL/baud)-1));
}
void rf12_setpower(unsigned char power, unsigned char mod)
{
RF_write_cmd(0x9800|(power&7)|((mod&15)<<4));
}
uint16_t status_read(void)
{
return RF_write_cmd(0x0000);
}
void RF_send_packet(uint8_t *tab,uint8_t len)
{
uint8_t data[4];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x2D;
data[3] = 0xD4;
RF_write_cmd((RF_CONFIGSET_CMD)|(0x0080)); //80e7
RF_write_cmd((RF_POWERMAN_CMD)|(0x0020));
RF_TX_H();
LED_Y_H();
_delay_ms(100);
for(uint8_t a = 0; a<len ; a++)
{
for(uint8_t i=0; i<4 ; i++)
{
WAIT_IRQ_L();
RF_write_cmd((0xB800)|(data[i]));
}
WAIT_IRQ_L();
RF_write_cmd(0xB800|(tab[a]));
_delay_ms(10);
for(uint8_t i=0; i<2 ; i++)
{
WAIT_IRQ_L();
RF_write_cmd((0xB800)|(data[i]));
}
}
RF_TX_L();
LED_Y_L();
_delay_ms(100);
RF_write_cmd(RF_POWERMAN_CMD);
RF_write_cmd(RF_CONFIGSET_CMD);
status_read();
}
void RF_send(uint8_t *tab,uint8_t len)
{
uint8_t data[4];
data[0] = 0xAA;
data[1] = 0xAA;
data[2] = 0x2D;
data[3] = 0xD4;
RF_write_cmd((RF_CONFIGSET_CMD)|(0x0080)); //80e7
RF_write_cmd((RF_POWERMAN_CMD)|(0x0020));
RF_TX_H();
LED_Y_H();
_delay_ms(100);
for(uint8_t i=0; i<4 ; i++)
{
WAIT_IRQ_L();
RF_write_cmd((0xB800)|(data[i]));
}
for(uint8_t a = 0; a<len ; a++)
{
RF_write_cmd((0xB800)|(tab[a]));
WAIT_IRQ_L();
_delay_ms(10);
}
for(uint8_t i=0; i<2 ; i++)
{
WAIT_IRQ_L();
RF_write_cmd((0xB800)|(data[i]));
}
RF_TX_L();
LED_Y_L();
_delay_ms(100);
RF_write_cmd(RF_POWERMAN_CMD);
RF_write_cmd(RF_CONFIGSET_CMD);
status_read();
}
void RF12_direct_data_in(void)
{
unsigned char temp = 0;
LED_A_L();
while((PINC&(1<<PC4)))
{
RF_CS_H();
LED_G_H();
temp = RF_Read_FIFO();
if(temp!=0)
{
dataBuff[tempVal] = temp;
if((temp == END_F)&&(tempVal >=3))
{
endFrameFlag = 1;
checkBuff();
tempVal = 0;
}
tempVal++;
}
}
_delay_ms(10);
LED_G_L();
}
void checkBuff(void)
{
if(((dataBuff[tempVal -3])&0xF0) == START_F)
{
USART0_Transmit(dataBuff[tempVal -2]);
USART0_Transmit(dataBuff[tempVal -1]);
}
}
void RF_enableRX(void)
{
RF_RX_H();
RF_write_cmd(0x82C8); // RX on
RF_write_cmd(0xCAF1); // set FIFO mode
RF_write_cmd(0xCAF3); // enable FIFO
}
void Init_SPI_com(void)
{
RF_CS_H();
SPI_MasterTransmit_DATA16(0xFF00);
}
void RF_enableTX(void)
{
RF_write_cmd((0x8209)|(0x0020));
}
uint16_t _crc16_update(uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
uint16_t CRC16(uint8_t *data, uint8_t len)
{
uint16_t crc16=0xffff;
while(len--) crc16=_crc16_update(crc16,*data++);
return crc16;
}
int main(void)
{
InitPins();
SPI_MasterInit();
RF12Init();
rf12_setfreq(RF12FREQ(869.40));
rf12_setbandwidth(6, 0, 0); //6,0,0
rf12_setbaud(4800); //4800
rf12_setpower(0, 5); //dewiacja 45kHz+/-
unsigned char zegde[17] ="TEST 869.40 MHz\n";
USART0_Init(RS_UBRR);
RF_enableRX(); //recieve
while(1)
{
/*Transmitt
for(uint8_t i = 0; i<5; i++)
{
_delay_ms(200);
}
RF_send_packet(packet,4);
*/
RF12_direct_data_in();
// RF_write_cmd(0x0000);
}
return 0;
}
Pinout:
Code:
#define PORT_SPI PORTB
#define DDR_SPI DDRB
#define DD_MOSI DDB5
#define DD_SCK DDB7
#define DD_CS DDB4
#define DD_RST DDB1
#define DD_LEDA DDB0
#define DD_LEDB DDB1
#define DD_LEDC DDB2
#define DD_LEDD DDB3
#define RF_CS_H() PORTB |= (1<<PB4)
#define RF_CS_L() PORTB &=~(1<<PB4)
#define RF_RST_H() PORTC |= (1<<PC2)
#define RF_RST_L() PORTC &=~(1<<PC2)
#define RF_TX_H() PORTC |= (1<<PC0)
/*---------PINOUT-------------
nSEL 5 PB4 O
nIRQ 18 PD4 I
FSK(nFFS) 25 PC3 O
DCLK(FFIT) 26 PC4 I
RXEN 23 PC1 O
TXEN 22 PC0 O
nInt 21 PD7 O
nRes 24 PC2 O
LEDA 19 PD5 O
LEDB 20 PD6 O
SW 40 PA0 I
*/
#define RF_TX_L() PORTC &=~(1<<PC0)
#define RF_RX_H() PORTC |= (1<<PC1)
#define RF_RX_L() PORTC &=~(1<<PC1)
#define nFFS_H() PORTC |= (1<<PC3)
#define nFFS_L() PORTC &= ~(1<<PC3)
#define LED_G_H() PORTD |= (1<<PD6)
#define LED_G_L() PORTD &= ~(1<<PD6)
#define LED_Y_H() PORTD |= (1<<PD5)
#define LED_Y_L() PORTD &= ~(1<<PD5)
#define LED_A_L() PORTB |= (1<<PB0)
#define LED_A_H() PORTB &= ~(1<<PB0)
#define LED_B_L() PORTB |= (1<<PB1)
#define LED_B_H() PORTB &= ~(1<<PB1)
#define LED_C_L() PORTB |= (1<<PB2)
#define LED_C_H() PORTB &= ~(1<<PB2)
#define LED_D_L() PORTB |= (1<<PB3)
#define LED_D_H() PORTB &= ~(1<<PB3)
#define WAIT_IRQ_L() while(PIND&(1<<PD4))
#define WAIT_IRQ_H() while(!(PIND&(1<<PD4)))
#define WAIT_FFIT_L() while(PINC&(1<<PC4))
#define FFIT_S() bit_is_set(PINC,PC4)
#define WAIT_FIFO() while(!(PINB&(1<<PB6)))
#define CHECK_MISO_H() bit_is_set(PINB,PB6)
#define WAIT_MISO_H() while(!(PINB&(1<<PB6)))
Last edited: