|
|
View previous topic :: View next topic |
Author |
Message |
pingza1990
Joined: 31 Oct 2011 Posts: 3 Location: Thailand
|
Need help with RFM12b |
Posted: Mon Oct 31, 2011 8:39 pm |
|
|
Hello everyone, this is my first post and I need a little help.
I'm making a project which involves two pics (16f886) and rfm12b.
Previously, I had to look at http://www.ccsinfo.com/forum/viewtopic.php?t=43563
Which I took extra, code to try.
TRANSMITTER CODE:
Code: | #include <16F886.h>
#device adc=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NOBROWNOUT
#use delay(clock=20000000)
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/// ///
/// #### ##### # # # ## #### ///
/// # # # ## ## ## # # # # ///
/// #### #### # ## # # # # #### ///
/// # # # # # # # # # ///
/// # # # # # # # # # ///
/// # # # # # # #### #### ///
/// ///
/// RFM12B Driver ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Functions: ///
/// ///
/// RFM12B_INIT(int type) ///
/// RFM12B_SEND(int buffer, int size) ///
/// RFM12B_RECEIVE(int buffer, int size) ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Updates: ///
/// ///
/// 09.01.2010 New Driver ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Code was written by Severin Birrer ///
/// More informations at www.blucorazon.ch ///
/// ///
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/// ///
/// PIN settings ///
/// ///
/////////////////////////////////////////////////////////
#define MOSI PIN_C4 //cikis
#define MISO PIN_C5
#define nSEL PIN_C2
#define SCK PIN_C3
#define IRQ PIN_B0
#define LED PIN_A0
/////////////////////////////////////////////////////////
/// ///
/// Definitions ///
/// ///
/////////////////////////////////////////////////////////
#define MOSI_hi() output_high(MOSI)
#define MOSI_lo() output_low(MOSI)
#define nSEL_hi() output_high(nSEL)
#define nSEL_lo() output_low(nSEL)
#define SCK_hi() output_high(SCK)
#define SCK_lo() output_low(SCK)
#define in_MISO() input(MISO)
#define in_IRQ() input(IRQ)
#define TX 1
#define RX 0
int16 RFM12B_READ_STATUS(void);
void RFM12B_FIFO_RESET(void);
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_PORT_INIT ///
/// Function: Initalise ports to defined states ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_PORT_INIT(void)
{
nSEL_hi();
SCK_lo();
MOSI_lo();
delay_ms(100);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_CMD ///
/// Function: Sends command and receives data ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_CMD(int16 cmd)
{
int i;
int16 temp=0;
SCK_lo();
nSEL_lo();
for(i=0;i<16;i++)
{
if(cmd&0x8000)
{
MOSI_hi();
}
else
{
MOSI_lo();
}
delay_us(5);
SCK_hi();
delay_us(5);
temp<<=1;
if(in_MISO())
{
temp|=0x0001;
}
SCK_lo();
cmd<<=1;
}
nSEL_hi();
return(temp);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_WAIT_READY ///
/// Function: Wait for RFM12B to be ready (FFIT) ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_WAIT_READY(void)
{
unsigned int16 data;
nSEL_lo();//test
while(!in_MISO())
;//test
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_READ ///
/// Function: Wait and read a byte from FIFO ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_READ(void)
{
int16 data;
// RFM12B_WAIT_READY();
data = RFM12B_CMD(0xB000); // Read Data
return (data&0x00FF);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_WRITE ///
/// Function: Write a byte ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_WRITE(int abyte)
{
RFM12B_WAIT_READY();
RFM12B_CMD(0xB800 + abyte); // Write Data
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_INIT ///
/// Function: Initalise as a sender or a receiver ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_INIT(int type)
{
RFM12B_PORT_INIT();
delay_ms(200);
if (type)
{
// Init Sender
RFM12B_CMD(0x80A8); // Configuration Command 868MHz 12,pF Enable Tx Register
}
else
{
// Init Receive
RFM12B_CMD(0x8068); // Configuration Command 868MHz 12,5pF Enable Rx FIFO buffer
}
// General Init
RFM12B_CMD(0x82D8); // Power Management Command Enable Crystal Osc.
RFM12B_CMD(0xA640); // Frequency Command Center Frequency 868.3200 MHz
RFM12B_CMD(0xC647); // Datarate Command Data Rate 4.789kbps
RFM12B_CMD(0x94A5); // Receiver Control Command VDI, Fast, 134kHz, Max, -73dBm
RFM12B_CMD(0xC2EC); // Datafilter Command Auto, Slow, Digital, 4
RFM12B_CMD(0xCA8B); // FIFO / Reset Mode Command 8, 1 bytes, Synchrone, Low, FIFO Fill Enabled
RFM12B_CMD(0xCED4); // Synchron Pattern Command Synchron byte(LSB) D4
RFM12B_CMD(0xC483); // AFC Command
RFM12B_CMD(0x9827); // TX Configuration Command
RFM12B_CMD(0xCC77); // PLL Command
RFM12B_CMD(0xE000); // Wake-Up Timer Command
RFM12B_CMD(0xC800); // Low Duty-Cycle Command
RFM12B_CMD(0xC000); // LB Det./Clock Div. Command 2.2V, 1.66MHz
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_SEND ///
/// Function: Send multiple bytes ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_SEND(int *ptr, int16 size)
{
int16 i;
RFM12B_CMD(0x8238); // Enable Sender
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xCE); // Sync HighByte
RFM12B_WRITE(0xD4); // Sync LowByte
for (i=0;i<size;i++)
{
RFM12B_WRITE(*ptr); // DATA Byte
ptr++;
}
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WAIT_READY();
RFM12B_CMD(0x8208); // Disable Sender
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_RECEIVE ///
/// Function: Receive multiple bytes ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_RECEIVE(int *ptr, int16 size)
{
int16 i;
//RFM12B_CMD(0x82C8); // Init Receiver//test
RFM12B_CMD(0x82D8); // Init Receiver//Candy
//RFM12B_CMD(0xCA81); // Enable FIFO//test
//RFM12B_CMD(0xCA83); // Clear FIFO//test
for (i=0;i<size;i++)
{
*ptr = RFM12B_READ(); // Send Data
ptr++;
}
//RFM12B_CMD(0x8208); // Disable Receiver test of we hem wel aan laten staan
//RFM12B_CMD(0xCA83); // Clear FIFO//test
//RFM12B_FIFO_RESET();
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_FIFO_RESET ///
/// Function: Reset the FIFO ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_FIFO_RESET(void)
{
RFM12B_CMD(0xCA81);
RFM12B_CMD(0xCA83);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_READ_STATUS ///
/// Function: Wait and read a byte from FIFO ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_READ_STATUS(void)
{
int16 data;
data = RFM12B_CMD(0x0000); // Read Data//test
return (data);
}
///////////////////////////////////////////////////////////////////////////////
void main()
{
int buffi[16] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
delay_ms(250);
RFM12B_INIT(1);
while(1)
{
RFM12B_SEND(buffi, 16);
output_high(LED);
delay_ms(500);
output_low(LED);
delay_ms(500);
}
} |
RECEIVER CODE:
Code: | #include <16F886.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=mypcconn)
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/// ///
/// #### ##### # # # ## #### ///
/// # # # ## ## ## # # # # ///
/// #### #### # ## # # # # #### ///
/// # # # # # # # # # ///
/// # # # # # # # # # ///
/// # # # # # # #### #### ///
/// ///
/// RFM12B Driver ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Functions: ///
/// ///
/// RFM12B_INIT(int type) ///
/// RFM12B_SEND(int buffer, int size) ///
/// RFM12B_RECEIVE(int buffer, int size) ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Updates: ///
/// ///
/// 09.01.2010 New Driver ///
/// ///
/////////////////////////////////////////////////////////
/// ///
/// Code was written by Severin Birrer ///
/// More informations at www.blucorazon.ch ///
/// ///
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/// ///
/// PIN settings ///
/// ///
/////////////////////////////////////////////////////////
#define MOSI PIN_C4 //cikis
#define MISO PIN_C5
#define nSEL PIN_C2
#define SCK PIN_C3
#define IRQ PIN_B0
#define LED PIN_A0
/////////////////////////////////////////////////////////
/// ///
/// Definitions ///
/// ///
/////////////////////////////////////////////////////////
#define MOSI_hi() output_high(MOSI)
#define MOSI_lo() output_low(MOSI)
#define nSEL_hi() output_high(nSEL)
#define nSEL_lo() output_low(nSEL)
#define SCK_hi() output_high(SCK)
#define SCK_lo() output_low(SCK)
#define in_MISO() input(MISO)
#define in_IRQ() input(IRQ)
#define TX 1
#define RX 0
int16 RFM12B_READ_STATUS(void);
void RFM12B_FIFO_RESET(void);
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_PORT_INIT ///
/// Function: Initalise ports to defined states ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_PORT_INIT(void)
{
nSEL_hi();
SCK_lo();
MOSI_lo();
delay_ms(100);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_CMD ///
/// Function: Sends command and receives data ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_CMD(int16 cmd)
{
int i;
int16 temp=0;
SCK_lo();
nSEL_lo();
for(i=0;i<16;i++)
{
if(cmd&0x8000)
{
MOSI_hi();
}
else
{
MOSI_lo();
}
delay_us(5);
SCK_hi();
delay_us(5);
temp<<=1;
if(in_MISO())
{
temp|=0x0001;
}
SCK_lo();
cmd<<=1;
}
nSEL_hi();
return(temp);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_WAIT_READY ///
/// Function: Wait for RFM12B to be ready (FFIT) ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_WAIT_READY(void)
{
unsigned int16 data;
nSEL_lo();//test
while(!in_MISO())
;//test
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_READ ///
/// Function: Wait and read a byte from FIFO ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_READ(void)
{
int16 data;
//RFM12B_WAIT_READY();
data = RFM12B_CMD(0xB000); // Read Data
return (data&0x00FF);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_WRITE ///
/// Function: Write a byte ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_WRITE(int abyte)
{
RFM12B_WAIT_READY();
RFM12B_CMD(0xB800 + abyte); // Write Data
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_INIT ///
/// Function: Initalise as a sender or a receiver ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_INIT(int type)
{
RFM12B_PORT_INIT();
delay_ms(200);
if (type)
{
// Init Sender
RFM12B_CMD(0x80A8); // Configuration Command 868MHz 12,pF Enable Tx Register
}
else
{
// Init Receive
RFM12B_CMD(0x8068); // Configuration Command 868MHz 12,5pF Enable Rx FIFO buffer
}
// General Init
RFM12B_CMD(0x82D8); // Power Management Command Enable Crystal Osc.
RFM12B_CMD(0xA640); // Frequency Command Center Frequency 868.3200 MHz
RFM12B_CMD(0xC647); // Datarate Command Data Rate 4.789kbps
RFM12B_CMD(0x94A5); // Receiver Control Command VDI, Fast, 134kHz, Max, -73dBm
RFM12B_CMD(0xC2EC); // Datafilter Command Auto, Slow, Digital, 4
RFM12B_CMD(0xCA8B); // FIFO / Reset Mode Command 8, 1 bytes, Synchrone, Low, FIFO Fill Enabled
RFM12B_CMD(0xCED4); // Synchron Pattern Command Synchron byte(LSB) D4
RFM12B_CMD(0xC483); // AFC Command
RFM12B_CMD(0x9827); // TX Configuration Command
RFM12B_CMD(0xCC77); // PLL Command
RFM12B_CMD(0xE000); // Wake-Up Timer Command
RFM12B_CMD(0xC800); // Low Duty-Cycle Command
RFM12B_CMD(0xC000); // LB Det./Clock Div. Command 2.2V, 1.66MHz
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_SEND ///
/// Function: Send multiple bytes ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_SEND(int *ptr, int16 size)
{
int16 i;
RFM12B_CMD(0x8238); // Enable Sender
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xAA); // Preamble
RFM12B_WRITE(0xCE); // Sync HighByte
RFM12B_WRITE(0xD4); // Sync LowByte
for (i=0;i<size;i++)
{
RFM12B_WRITE(*ptr); // DATA Byte
ptr++;
}
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WRITE(0xAA); // Dummy Byte
RFM12B_WAIT_READY();
RFM12B_CMD(0x8208); // Disable Sender
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_RECEIVE ///
/// Function: Receive multiple bytes ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_RECEIVE(int *ptr, int16 size)
{
int16 i;
//RFM12B_CMD(0x82C8); // Init Receiver//test
RFM12B_CMD(0x82D8); // Init Receiver//Candy
//RFM12B_CMD(0xCA81); // Enable FIFO//test
//RFM12B_CMD(0xCA83); // Clear FIFO//test
for (i=0;i<size;i++)
{
*ptr = RFM12B_READ(); // Send Data
ptr++;
}
//RFM12B_CMD(0x8208); // Disable Receiver test of we hem wel aan laten staan
//RFM12B_CMD(0xCA83); // Clear FIFO//test
//RFM12B_FIFO_RESET();
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_FIFO_RESET ///
/// Function: Reset the FIFO ///
/// ///
/////////////////////////////////////////////////////////
void RFM12B_FIFO_RESET(void)
{
RFM12B_CMD(0xCA81);
RFM12B_CMD(0xCA83);
}
/////////////////////////////////////////////////////////
/// ///
/// RFM12B_READ_STATUS ///
/// Function: Wait and read a byte from FIFO ///
/// ///
/////////////////////////////////////////////////////////
int16 RFM12B_READ_STATUS(void)
{
int16 data;
data = RFM12B_CMD(0x0000); // Read Data//test
return (data);
}
///////////////////////////////////////////////////////////////////////////////
void main()
{
int buffi[16];
int g = 0;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
set_tris_c(get_tris_c()&(0b10111111));
delay_ms(250);
RFM12B_INIT(0);
while(1)
{
RFM12B_RECEIVE(buffi, 16); //data receive
for(g = 0; g < 16; g++) //send to pc (USART) received data
{
putc(buffi[g]);
}
}
}
|
But the PC Received Data Incomprehensible.
Here is my circuit.
http://image.ohozaa.com/view/4kdtc
I'm trying for a week but can't succeed yet.
Help me please.
Thank you |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 01, 2011 2:13 pm |
|
|
Delete the lines shown in bold below. Your programs are using software
SPI, and the lines in bold enable Hardware SPI. You don't want
that. Delete those two lines.
Quote: |
// Transmitter
void main()
{
int buffi[16] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// Receiver
void main()
{
int buffi[16];
int g = 0;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1); |
|
|
|
pingza1990
Joined: 31 Oct 2011 Posts: 3 Location: Thailand
|
|
Posted: Wed Nov 02, 2011 1:07 am |
|
|
Thank you PCM programmer
but i still can't send data.
my project is the first PIC receive temperature form ds18b20
then forwarded to the second PIC with rfm12b and second send
the data to computer for processing.
Here is the code of the transmission temperature with rfm12b.
Transmitter
Code: | void ReadTemp_DS18B20(void)
{
byte i, buffer[9];
int temp,sign;
if (touch_present()) { // get present (reset)
touch_write_byte(0xCC); // Skip ROM
touch_write_byte (0x44); // Start Conversion
delay_ms(200); // delay 200 ms
touch_present(); // get present (reset)
touch_write_byte(0xCC); // Skip ROM
touch_write_byte (0xBE); // Read Scratch Pad
for(i=0; i<9;i++) // read 9 bytes
buffer[i] = touch_read_byte();
}
temp = (buffer[1]<<4)|(buffer[0]>>4); // Temperature
if(buffer[1]&0xF0) temp = (-1)*temp; // Sign bit
RFM12B_SEND(temp, 1);
// printf ("\nTemperature %d C",temp);*/
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
delay_ms(250);
RFM12B_INIT(1);
while(1)
{
ReadTemp_DS18B20();
output_high(LED);
delay_ms(500);
output_low(LED);
delay_ms(500);
}
} |
Receiver
Code: | void main()
{
int temp;
int g = 0;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
set_tris_c(get_tris_c()&(0b10111111));
delay_ms(250);
RFM12B_INIT(0);
while(1)
{
RFM12B_RECEIVE(temp, 1);
printf("TEMP %d ",temp);
}
} |
temp at the transmitter is correct value.
but temp at the receiver always -1
Help me please
It will be sent next week.
Thank you
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 02, 2011 11:35 am |
|
|
I don't have the RFM12b modules, so I can't really help very much. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|