yat315
Newbie level 2
Hi,
I'm new to microcontroller..I'm having difficult time understanding this codes..Can anyone explain how this codes work?Pleaseeeee....And what are the data that being transmitted??Please help me...
nRF905 main
#include <htc.h>
#include "delay.h"
#include "nRF905.h"
#define WRC 0x00 //Write configuration register command
#define RRC 0x10 //Read configuration register command
#define WTP 0x20 //Wrte TX Payload command
#define RTP 0x21 //Read TX Payload command
#define WTA 0x22 //Write TX Address command
#define RTA 0x23 //Read TX address command
#define RRP 0x24 //Read RX Payload command
#define CC 0x80 //Channel configuration
#define OUTPUT 0
#define INPUT 1
#define HIGH 1
#define LOW 0
typedef struct _RFconfig
{
INT8U n;
INT8U buf[10]; //RF-CONFIG REGISTER (refer to page 20 of nRF905 datasheet)
}RFconfig;
/*
* Initialization of the Register Contents at byte# 0 - 9 as stated on page 20 of nRF905 datasheet
*/
const RFconfig RxTxconfig =
{
10,
0x01, //byte# 0 : set the channel # to 1
0x0e, //byte# 1 : 0bxx[AUTO_RETRAN<5>][RX_RED_PWR<4>][PA_PWR<3:2>][HFREQ_PLL<1>][CH_NO[8]<0>]
// 0b00001100 => AUTO_RETRAN=0; No retransmission of data packet
// RX_RED_PWR=0; Normal operation
// PA_PWR=11; +10dBm output power
// HFREQ_PLL=1; chip operating in 868 MHz band
// CH_NO[8]=0; 9th bit of CH_NO is zero
0x44, //byte# 2 : TX_AFW[2:0]=100 => 4 byte TX address field width
// RX_AFW[2:0]=100 => 4 byte RX address field width
0x20, //byte# 3 : RX_payload width of 32 bytes
0x20, //byte# 4 : TX_payload width of 32 bytes
0xcc, //byte# 5 : RX_ADDRESS byte 0
0xcc, //byte# 6 : RX_ADDRESS byte 1
0xcc, //byte# 7 : RX_ADDRESS byte 2
0xcc, //byte# 8 : RX_ADDRESS byte 3; thus the RX ADDRESS is configured to 0xcccccccc in this example
//remarks : The default value is 0xE7E7E7E7
0x58 //byte# 9 : Set the CRC_MODE, CRC_EN, XOF[2:0], UP_CLK_EN, UP_CLK_FREQ[1:0] bit values
// In this configuration, we are doing CRC enable for 8 CRC check bit, 16MHz crystal freq,
// and output clock disable
};
/*
********************************************************************
void rfSetChannel(void)
{
static INT8U tmp;
RACSN = LOW;
spiWrite(RRC|0x01); //read the configuration address starting address 1
tmp = spiRead()&0xfc; //clear the relevant HFREQ_PLL and CH_NO[8] bits
RACSN = HIGH;
RACSN = LOW;
spiWrite(WRC); //write the configuration register starting from address 0
spiWrite((INT8U)CHANNEL); //write bit[7:0] of the channel, leaving CH_NO[8] in the second register
spiWrite(tmp|(HFREQ<<1)|((CHANNEL>>Cool&0x01));
RACSN = HIGH;
}
**********************************************************************************************************
*/
/* declaration of Tx and Rx buffers */
INT8U TxBuf[32];
INT8U RxBuf[32];
/*
ShockBurst TX routine, send the whole paylod buffer in 32 bytes to a designated receiving node (TX-address)
*/
void rfTxPacket(void)
{
INT8U i;
PWR = HIGH;
TXEN = HIGH;
TRX_CE = LOW;
RACSN = LOW; //chip select
spiWrite(WTA); //write 4 bytes TX-ADDRESS
for(i=0; i<4; i++)
{
spiWrite(RxTxconfig.buf[i+5]);
}
RACSN = HIGH;
for(i=0;i<200;i++);
RACSN = LOW; //chip select
spiWrite(WTP);
for (i=0;i<32;i++)
{
spiWrite(TxBuf); //write payload data here
}
RACSN = HIGH; //chip de-select
TRX_CE = HIGH; //radio enabled
DelayUs(20);
TRX_CE = LOW; //Since no auto-transmit in this example
//TRX_CE set low for standby mode
}
void rfRxPacket(void)
{
INT8U i;
TRX_CE = LOW; //switch to standby mode
RACSN = LOW; //chip select
spiWrite(RRP); //read payload command
for(i=0;i<32;i++)
RxBuf = spiRead();
RACSN = LOW;
while(DR||AM);
TRX_CE=HIGH;
}
void rfConfig905(void)
{
INT8U i;
PWR = HIGH;
TRX_CE = LOW;
RACSN = LOW; //chip enable
spiWrite(WRC); //repeat SPI write
for(i=0;i<RxTxconfig.n;i++)
{
spiWrite(RxTxconfig.buf);
}
RACSN = HIGH; //chip disable
}
void rfSetRxMode(void)
{
/* Set Receive mode */
PWR = HIGH;
TXEN = LOW;
TRX_CE = HIGH;
/* After 650us nRF905 is monitoring the air for incoming communication */
DelayUs(200);
DelayUs(200);
DelayUs(200);
DelayUs(50);
}
void rfInitIO(void)
{
RACSN = HIGH; //de-select the RF module to start with
TRIS_RACSN = OUTPUT;
SPI_SCK = LOW; //clock line low to start with
TRIS_SPI_SCK = OUTPUT;
TRIS_SPI_MOSI = OUTPUT;
TRIS_SPI_MISO = INPUT;
TRIS_PWR = OUTPUT;
TRIS_TRX_CE = OUTPUT;
TRIS_TXEN = OUTPUT;
TRIS_CD = INPUT;
TRIS_AM = INPUT;
TRIS_DR = INPUT;
}
void spiWrite(INT8U dat)
{
/* software SPI, send MSB first */
static INT8U i, c;
c = dat;
for(i=0;i<8;i++)
{
if((c&0x80)==0x80)
SPI_MOSI = HIGH;
else
SPI_MOSI = LOW;
SPI_SCK = HIGH;
c=c<<1;
SPI_SCK = LOW;
}
}
INT8U spiRead(void)
{
/* software SPI read, MSB read first */
static INT8U i, dat;
for(i=0;i<8;i++)
{
dat = dat<<1;
SPI_SCK = HIGH;
if(SPI_MISO)
dat = dat+1;
SPI_SCK = LOW;
}
return dat;
}
void rfSetPwrOff(void)
{
PWR = LOW;
}
Here is the code for receiver
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include "nRF905.h"
/*
********************************************************************************************************************
* Configuration Bit Section
*
* Configuration bit setting for 16F690 : Internal OSC with no clock out, watchdog disabled, etc.
* Please consult \\HI-TECH Software\PICC\lite\9.60\include\pic16f685.h for meaning of the arguments.
********************************************************************************************************************
*/
__CONFIG(INTIO&WDTDIS&PWRTDIS&MCLREN&UNPROTECT&BORDIS&IESODIS&FCMDIS);
/*
********************************************************************************************************************
* MAIN
* This program takes care of the Rx side, printing the whole 32 bytes from Rx PayLoad
* Printing is via Silabs CP2102 USB<->UART bridge.
* Baud rate is 9600bps. Launch Docklight or HyperTerminal to see the package received
********************************************************************************************************************
*/
void main(void)
{
unsigned char i;
ANSEL = 0x00;
ANSELH = 0x00; //all digital operation
SCS = 1; //internal oscillator is used for system clock and
//accept the deafulat clock speed of 4MHz defined by
//IRCF<2:0>=110
INTCON=0; // purpose of disabling the interrupts.
rfInitIO();
rfConfig905();
rfSetRxMode();
init_comms(); // set up the USART - settings defined in usart.h
printf("\rnRF905 Receiver Demo:\n");
for(;Wink
{
if(DR==1) {
rfRxPacket();
printf("received packet in main()\n");
for(i=0;i<32;i++)
printf("Character: %c \n", RxBuf);
}
}
}
For reference, the transmit code is shown below.
#include <htc.h> //header files for all supported chips includes PIC16F690 (..\include\pic16f685.h)
#include "nRF905.h" //header file for nRF905 driver
#include "delay.h" //for software delay function
__CONFIG(INTIO&WDTDIS&PWRTDIS&MCLREN&UNPROTECT&BORDIS&IESODIS&FCMDIS);
/*
********************************************************************************************************************
* MAIN
*
********************************************************************************************************************
*/
void main(void)
{
unsigned char i,j=0;
ANSEL = 0x00;
ANSELH = 0x00; //all digital operation
SCS = 1; //internal oscillator is used for system clock and
//accept the deafulat clock speed of 4MHz defined by
//IRCF<2:0>=110
rfInitIO();
rfConfig905();
for(i=0;i<32;i++) TxBuf = 0x30+(j++); //load data in TxBuf
rfTxPacket();
for(;Wink
{
;
}
}
I'm new to microcontroller..I'm having difficult time understanding this codes..Can anyone explain how this codes work?Pleaseeeee....And what are the data that being transmitted??Please help me...
nRF905 main
#include <htc.h>
#include "delay.h"
#include "nRF905.h"
#define WRC 0x00 //Write configuration register command
#define RRC 0x10 //Read configuration register command
#define WTP 0x20 //Wrte TX Payload command
#define RTP 0x21 //Read TX Payload command
#define WTA 0x22 //Write TX Address command
#define RTA 0x23 //Read TX address command
#define RRP 0x24 //Read RX Payload command
#define CC 0x80 //Channel configuration
#define OUTPUT 0
#define INPUT 1
#define HIGH 1
#define LOW 0
typedef struct _RFconfig
{
INT8U n;
INT8U buf[10]; //RF-CONFIG REGISTER (refer to page 20 of nRF905 datasheet)
}RFconfig;
/*
* Initialization of the Register Contents at byte# 0 - 9 as stated on page 20 of nRF905 datasheet
*/
const RFconfig RxTxconfig =
{
10,
0x01, //byte# 0 : set the channel # to 1
0x0e, //byte# 1 : 0bxx[AUTO_RETRAN<5>][RX_RED_PWR<4>][PA_PWR<3:2>][HFREQ_PLL<1>][CH_NO[8]<0>]
// 0b00001100 => AUTO_RETRAN=0; No retransmission of data packet
// RX_RED_PWR=0; Normal operation
// PA_PWR=11; +10dBm output power
// HFREQ_PLL=1; chip operating in 868 MHz band
// CH_NO[8]=0; 9th bit of CH_NO is zero
0x44, //byte# 2 : TX_AFW[2:0]=100 => 4 byte TX address field width
// RX_AFW[2:0]=100 => 4 byte RX address field width
0x20, //byte# 3 : RX_payload width of 32 bytes
0x20, //byte# 4 : TX_payload width of 32 bytes
0xcc, //byte# 5 : RX_ADDRESS byte 0
0xcc, //byte# 6 : RX_ADDRESS byte 1
0xcc, //byte# 7 : RX_ADDRESS byte 2
0xcc, //byte# 8 : RX_ADDRESS byte 3; thus the RX ADDRESS is configured to 0xcccccccc in this example
//remarks : The default value is 0xE7E7E7E7
0x58 //byte# 9 : Set the CRC_MODE, CRC_EN, XOF[2:0], UP_CLK_EN, UP_CLK_FREQ[1:0] bit values
// In this configuration, we are doing CRC enable for 8 CRC check bit, 16MHz crystal freq,
// and output clock disable
};
/*
********************************************************************
void rfSetChannel(void)
{
static INT8U tmp;
RACSN = LOW;
spiWrite(RRC|0x01); //read the configuration address starting address 1
tmp = spiRead()&0xfc; //clear the relevant HFREQ_PLL and CH_NO[8] bits
RACSN = HIGH;
RACSN = LOW;
spiWrite(WRC); //write the configuration register starting from address 0
spiWrite((INT8U)CHANNEL); //write bit[7:0] of the channel, leaving CH_NO[8] in the second register
spiWrite(tmp|(HFREQ<<1)|((CHANNEL>>Cool&0x01));
RACSN = HIGH;
}
**********************************************************************************************************
*/
/* declaration of Tx and Rx buffers */
INT8U TxBuf[32];
INT8U RxBuf[32];
/*
ShockBurst TX routine, send the whole paylod buffer in 32 bytes to a designated receiving node (TX-address)
*/
void rfTxPacket(void)
{
INT8U i;
PWR = HIGH;
TXEN = HIGH;
TRX_CE = LOW;
RACSN = LOW; //chip select
spiWrite(WTA); //write 4 bytes TX-ADDRESS
for(i=0; i<4; i++)
{
spiWrite(RxTxconfig.buf[i+5]);
}
RACSN = HIGH;
for(i=0;i<200;i++);
RACSN = LOW; //chip select
spiWrite(WTP);
for (i=0;i<32;i++)
{
spiWrite(TxBuf); //write payload data here
}
RACSN = HIGH; //chip de-select
TRX_CE = HIGH; //radio enabled
DelayUs(20);
TRX_CE = LOW; //Since no auto-transmit in this example
//TRX_CE set low for standby mode
}
void rfRxPacket(void)
{
INT8U i;
TRX_CE = LOW; //switch to standby mode
RACSN = LOW; //chip select
spiWrite(RRP); //read payload command
for(i=0;i<32;i++)
RxBuf = spiRead();
RACSN = LOW;
while(DR||AM);
TRX_CE=HIGH;
}
void rfConfig905(void)
{
INT8U i;
PWR = HIGH;
TRX_CE = LOW;
RACSN = LOW; //chip enable
spiWrite(WRC); //repeat SPI write
for(i=0;i<RxTxconfig.n;i++)
{
spiWrite(RxTxconfig.buf);
}
RACSN = HIGH; //chip disable
}
void rfSetRxMode(void)
{
/* Set Receive mode */
PWR = HIGH;
TXEN = LOW;
TRX_CE = HIGH;
/* After 650us nRF905 is monitoring the air for incoming communication */
DelayUs(200);
DelayUs(200);
DelayUs(200);
DelayUs(50);
}
void rfInitIO(void)
{
RACSN = HIGH; //de-select the RF module to start with
TRIS_RACSN = OUTPUT;
SPI_SCK = LOW; //clock line low to start with
TRIS_SPI_SCK = OUTPUT;
TRIS_SPI_MOSI = OUTPUT;
TRIS_SPI_MISO = INPUT;
TRIS_PWR = OUTPUT;
TRIS_TRX_CE = OUTPUT;
TRIS_TXEN = OUTPUT;
TRIS_CD = INPUT;
TRIS_AM = INPUT;
TRIS_DR = INPUT;
}
void spiWrite(INT8U dat)
{
/* software SPI, send MSB first */
static INT8U i, c;
c = dat;
for(i=0;i<8;i++)
{
if((c&0x80)==0x80)
SPI_MOSI = HIGH;
else
SPI_MOSI = LOW;
SPI_SCK = HIGH;
c=c<<1;
SPI_SCK = LOW;
}
}
INT8U spiRead(void)
{
/* software SPI read, MSB read first */
static INT8U i, dat;
for(i=0;i<8;i++)
{
dat = dat<<1;
SPI_SCK = HIGH;
if(SPI_MISO)
dat = dat+1;
SPI_SCK = LOW;
}
return dat;
}
void rfSetPwrOff(void)
{
PWR = LOW;
}
Here is the code for receiver
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include "nRF905.h"
/*
********************************************************************************************************************
* Configuration Bit Section
*
* Configuration bit setting for 16F690 : Internal OSC with no clock out, watchdog disabled, etc.
* Please consult \\HI-TECH Software\PICC\lite\9.60\include\pic16f685.h for meaning of the arguments.
********************************************************************************************************************
*/
__CONFIG(INTIO&WDTDIS&PWRTDIS&MCLREN&UNPROTECT&BORDIS&IESODIS&FCMDIS);
/*
********************************************************************************************************************
* MAIN
* This program takes care of the Rx side, printing the whole 32 bytes from Rx PayLoad
* Printing is via Silabs CP2102 USB<->UART bridge.
* Baud rate is 9600bps. Launch Docklight or HyperTerminal to see the package received
********************************************************************************************************************
*/
void main(void)
{
unsigned char i;
ANSEL = 0x00;
ANSELH = 0x00; //all digital operation
SCS = 1; //internal oscillator is used for system clock and
//accept the deafulat clock speed of 4MHz defined by
//IRCF<2:0>=110
INTCON=0; // purpose of disabling the interrupts.
rfInitIO();
rfConfig905();
rfSetRxMode();
init_comms(); // set up the USART - settings defined in usart.h
printf("\rnRF905 Receiver Demo:\n");
for(;Wink
{
if(DR==1) {
rfRxPacket();
printf("received packet in main()\n");
for(i=0;i<32;i++)
printf("Character: %c \n", RxBuf);
}
}
}
For reference, the transmit code is shown below.
#include <htc.h> //header files for all supported chips includes PIC16F690 (..\include\pic16f685.h)
#include "nRF905.h" //header file for nRF905 driver
#include "delay.h" //for software delay function
__CONFIG(INTIO&WDTDIS&PWRTDIS&MCLREN&UNPROTECT&BORDIS&IESODIS&FCMDIS);
/*
********************************************************************************************************************
* MAIN
*
********************************************************************************************************************
*/
void main(void)
{
unsigned char i,j=0;
ANSEL = 0x00;
ANSELH = 0x00; //all digital operation
SCS = 1; //internal oscillator is used for system clock and
//accept the deafulat clock speed of 4MHz defined by
//IRCF<2:0>=110
rfInitIO();
rfConfig905();
for(i=0;i<32;i++) TxBuf = 0x30+(j++); //load data in TxBuf
rfTxPacket();
for(;Wink
{
;
}
}