![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
what's wrong with my code using hope RFM12B |
Posted: Sat Sep 12, 2015 6:16 am |
|
|
Hello friends,
I'm trying to get two PICs to communicate together using HOPE RFM12B
modules...
And this simple test program ...
But no success ... nothing happens .. so I 'll be happy if you can help me solve this problem
here's my code for RX:
I have check all hardware +3.3 V for RFM12 and +5 V for PIC.
Code: |
#include <16F73.h>
#fuses HS,NOWDT,PUT
#use delay(clock = 10000000)
//use spi_xfer!!
#use SPI(MASTER, BAUD = 4800,DI = PIN_B5, DO = PIN_B4, CLK = PIN_B3, ENABLE = PIN_B2, BITS = 16)
#define nIRQ PIN_B1
#define LED PIN_A0
void Init_RF12_RX(void);
int16 RX_RFBUF[19];
//************************************************************
void main(void){
int16 CheckSum = 0;
int16 result = 0;
int1 k = TRUE;
int8 i = 0;
Init_RF12_RX();
while(k){ //k to avoid compiler warning!
while(input(nIRQ) == 0){
CheckSum = 0;
for(i = 0; i < 5; ++i){ //ignore sync. words 3 x 0xAA,0x2D, 0xD4 (16 bits)
result = spi_xfer();
}
for(i = 0; i < 16; ++i){ //datawords 16 x 16 bits to memory buffer
RX_RFBUF[i] = spi_xfer();
CheckSum = CheckSum + RX_RFBUF[i];
}
RX_RFBUF[16] = spi_xfer(); //checksum to memory
//CheckSum = RX_RFBUF[16];
if( CheckSum == RX_RFBUF[16]){
output_high(LED);
}
delay_us(1);
output_low(LED);
}
}
}
//************************************************************void Init_RF12_RX(void){ //init RX
output_low(LED);
spi_xfer(0x80E8); //868 Mhz band
spi_xfer(0x82D8); //enable receive !PA
spi_xfer(0xA640);
spi_xfer(0xC647); //bitrate 4800
spi_xfer(0x94A0); //VDI, FAST, 134 kHZ,0dBm -103 dBm (receiver control command ?)
spi_xfer(0xC2AC); //data filter command
//spi_xfer(0xCC77); //10 Mhz clock
spi_xfer(0xCC67); //10 Mhz clock both tried no success
spi_xfer(0xCA80); //FIFO and reset mode command
spi_xfer(0xCA83); //FIFO8, SYNC
spi_xfer(0xC49B);
spi_xfer(0x9850); //!mp,9810 = 30 kHz, MAX OUT WRONG!
spi_xfer(0xE000); //WAKEUP TIMER , NOT USED
spi_xfer(0xC800); //low duty command , NOT USED
spi_xfer(0xC000); //1.0 Mhz, 2,2 V
}
|
and here's my code for TX:
Code: |
#include <16F73.h>
#fuses HS,NOWDT,PUT
#use delay(clock = 10000000)
#define LED PIN_A0
#define nIRQ PIN_B1
//use spi_xfer!!
#use SPI(MASTER, BAUD = 4800,DI = PIN_B5, DO = PIN_B4, CLK = PIN_B3, ENABLE = PIN_B2, BITS = 16)
void Init_RF12(void); //init tx
//************************************************************
void main(void){
int16 ChkSum;
int8 i = TRUE; //to avoid warning while(1)...
delay_ms(100); //0.1s delay for startup
for( i = 0; i < 5; ++i){ //blink led 5 times for test
output_high(LED);
delay_ms(100);
output_low(LED);
delay_ms(100);
}
Init_RF12();
i = TRUE;
while(i){
ChkSum = 0;
spi_xfer(0x8228); //open PA (TX)
delay_us(4);
spi_xfer(0x8238); //synthesizer enabled
spi_xfer(0xAA); //syncronise
spi_xfer(0xAA);
spi_xfer(0xAA);
spi_xfer(0x2D);
spi_xfer(0xD4);
spi_xfer(0x30); //data 0
ChkSum += 0x30;
spi_xfer(0x31); //data 1
ChkSum += 0x31;
spi_xfer(0x32);
ChkSum += 0x32;
spi_xfer(0x33);
ChkSum += 0x33;
spi_xfer(0x34);
ChkSum += 0x34;
spi_xfer(0x35);
ChkSum += 0x35;
spi_xfer(0x36);
ChkSum += 0x36;
spi_xfer(0x37);
ChkSum += 0x37;
spi_xfer(0x38);
ChkSum += 0x38;
spi_xfer(0x39);
ChkSum += 0x39;
spi_xfer(0x3A);
ChkSum += 0x3A;
spi_xfer(0x3B);
ChkSum += 0x3B;
spi_xfer(0x3C);
ChkSum += 0x3C;
spi_xfer(0x3D);
ChkSum += 0x3D;
spi_xfer(0x3E);
ChkSum += 0x3E;
spi_xfer(0x3F); //data 15
ChkSum += 0x3F;
//ChkSum &= 0x0FF;
spi_xfer(ChkSum);
spi_xfer(0xAA);
spi_xfer(0x8208); //close PA (TX)
spi_xfer(0x8200); // ?
output_high(LED);
delay_ms(100);
output_low(LED);
delay_ms(1000);
//}
}
}
//************************************************************void Init_RF12(void){ //init TX
output_low(LED);
spi_xfer(0x80E8); //freq. band 868 Mhz, 12.5 pF
spi_xfer(0x8208); //turn on crystal !PA power management command
spi_xfer(0xA640); //freg. set, freg = 868 Mhz + 1600 * 0.005 Mhz = 876 Mhz
spi_xfer(0xC647); //bitrate 4800
//spi_xfer(0xCC77); //PLL setting command, default value look at page 7 or 0xCC67
spi_xfer(0xCC67); // 10 Mhz clock
spi_xfer(0x94A0); //VDI, FAST, 134 kHZ,0dBm -103 dBm (receiver control command ?)
spi_xfer(0xC2AC); //data filter command
spi_xfer(0xCA80); //FIFO and reset mode command
spi_xfer(0xCA83); //FIFO8, SYNC
spi_xfer(0xC49B); //AFC command
spi_xfer(0x9850); //!mp,9810 = 90 kHz, MAX OUT WRONG!
spi_xfer(0xE000); //WAKEUP TIMER , NOT USED
spi_xfer(0xC80E); //low duty cycle command, NOT USED
spi_xfer(0xC000); //1,0 Mhz, 2.2 V
}
|
I have checked all the hardware with oscilloscope and I believe this is not hardware problem...
RFM12B use 3.3 volts and I have level converters for 3.3 V to 5 V
and 5 v to 3.3 V.
So if you can help me I'll be very pleased.
friendly regards here from Finland
-arto- |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 13, 2015 1:05 am |
|
|
In your RX code, you setup #use spi() as a Master. But then in your code,
you call spi_xfer() without a parameter. If there's no parameter, it means no clock will be sent out the SCLK pin by the master. So, there
will be no communication with the RFM12B spi slave device. Add a
parameter of 0x0000.
I don't know what compiler version you're using, but if I try to compile
your code, which has no parameter for some of the spi_xfer() statements,
I get a few of these error messages:
Quote: | *** Error 51 "PCM_Test.c" Line 34(31,32): A numeric expression must appear here |
|
|
![](templates/subSilver/images/spacer.gif) |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Sun Sep 13, 2015 7:08 am |
|
|
PCM programmer wrote: | In your RX code, you setup #use spi() as a Master. But then in your code,
you call spi_xfer() without a parameter. If there's no parameter, it means no clock will be sent out the SCLK pin by the master. So, there
will be no communication with the RFM12B spi slave device. Add a
parameter of 0x0000. |
I don't know what compiler version you're using, but if I try to compile
your code, which has no parameter for some of the spi_xfer() statements,
I get a few of these error messages:
Quote: | *** Error 51 "PCM_Test.c" Line 34(31,32): A numeric expression must appear here[
|
Thank's for your answer ...
my compiler version is rather old 4.018 ... and it compilers no warning no errors...
It seems to me that spi_xfer and
#use SPI(MODE don' work at all in this version because I can't start TX
to work at all ... (because there's no current from 3.3 V source)
Is it so that master DO must go to slave DI and master DI to slave DO ?
I have tried both but it do not work ...
I'll be happy if you have any idea to test ?
best regards
-arto- |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 14, 2015 5:24 pm |
|
|
Quote: |
It seems to me that spi_xfer and #use SPI(MODE) don't work at all in this version. |
It works, but needs some more coding.
I don't have a 16F73, so I used a 16F877. I made a test program and
compiled it with vs. 4.018. I looked at SCLK, SDO, and ENABLE with
an oscilloscope. I immediately saw that ENABLE defaults to high true.
That's because you have an older version of the compiler. On vs. 5.048,
ENABLE defaults to low-true, which is far more commonly used for SPI chips.
To fix it for your version, you need to add the parameter shown in bold below:
Quote: |
#include <16F877.h>
#fuses HS,NOWDT,PUT
#use delay(clock = 10000000)
#use SPI(MASTER, BAUD = 4800, DI = PIN_B5, DO = PIN_B4, CLK = PIN_B3, ENABLE = PIN_B2, BITS = 16, ENABLE_ACTIVE=0)
//=================================
void main()
{
while(TRUE)
{
spi_xfer(0x8C3C);
delay_ms(1);
}
} |
Also, I would recommend that you get an oscilloscope. This type of
problem is quickly found with a scope. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|