CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Code MicroC MRF24J40MA

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
picprogrammer



Joined: 10 Sep 2003
Posts: 31

View user's profile Send private message

Code MicroC MRF24J40MA
PostPosted: Sun Feb 17, 2013 5:31 am     Reply with quote

Hi,

Here my ported code for the MRF24J40MA. Load the same code at both boards and the characters 0..9 are send to the other side, bidirectional.
Please share your further development!
Code:

/*
 * Project name:
     Receiver (Using mikroE's EasyBee Board)
 * Copyright:
     (c) Mikroelektronika, 2009.
 * Revision History:
     20091030:
       - initial release;
       - Ported to CCS by Picprogrammer
 * Description:
     This project is a simple demonstration of working with the EasyBee3 board.
     The trasmitter sends the data and the receiver displays it on the LCD.
     The on-board MRF24J40MA is a 2.4 GHz IEEE 802.15.4 radio transceiver module
     and operates in the 2.4GHz frequency band.
     Detailed information about MRF24J40 module is availabe here:
     http://ww1.microchip.com/downloads/en/DeviceDoc/DS-39776b.pdf
 * Test configuration:
     MCU:             PIC16F887
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf#page=123
     Dev.Board:       EasyPIC6
                      http://www.mikroe.com/en/tools/easypic6/
     Oscillator:      HS, 8.000 MHz
     Ext. Modules:    EasyBee3 board
                      http://www.mikroe.com/en/tools/zigbee/easybee3/
                      Character LCD 2x16
                      http://www.mikroe.com/en/tools/components/#other
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/en/compilers/mikroc/pro/pic/
 * NOTES:
     Connect EasyBee3 board connectors CN1 to PORTA and CN2 to PORTC, turn on switches
     SW1.3, SW2.2, SW2.7 on EasyBee3 board.
     Connect LCD on EasyPIC6 development board and turn on backlight switch SW6.8.
*/
#include <18F2680.h>
#device adc=16
 
#FUSES WDT512                 //Watch Dog Timer uses 1:16 Postscale

#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOLPT1OSC                //Timer1 configured for higher power operation
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES BBSIZ1K                  //1K words Boot Block size
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES STVREN                   //Stack overflow will rest
#use delay(int=8000000)

#USE SPI (FORCE_HW, MASTER, SPI1, MODE=0, BITS=8,DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3,ENABLE=PIN_B1)

#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

// EasyBee3 connections
#define CS      pin_B1   // CS pin
#define RST     pin_C0   // RST pin
#define INTQ    pin_B0   // INT pin
#define WAKE    pin_C1   // WAKE pin

char txt[4],
     rx_char;
const unsigned int8 DATA_LENGHT = 3;
const unsigned int8 HEADER_LENGHT = 11;

int1 new_rx;
int16 address_RX_FIFO = 0x300;
int16 address_TX_normal_FIFO = 0x000;
int8 data_RX_FIFO[1 + HEADER_LENGHT + DATA_LENGHT + 2 + 1 + 1],
     lost_data = 0;

int8 ADDRESS_short_1[] = {1,1};               // Source address
int8 ADDRESS_long_1[]  = {1,1,1,1,1,1,1,1};
int8 ADDRESS_short_2[] = {2,2};               // Destination address
int8 ADDRESS_long_2[]  = {2,2,2,2,2,2,2,2};
int8 PAN_ID_1[] = {3,3};                      // Source PAN ID
int8 PAN_ID_2[] = {3,3};                      // Destination PAN ID

int8 DATA_RX[DATA_LENGHT],
     DATA_TX[DATA_LENGHT],
     data_TX_normal_FIFO[DATA_LENGHT + HEADER_LENGHT + 2];
int8 LQI = 0,
     RSSI2 = 0,
     SEQ_NUMBER = 0x23;

#include "registers.h"
#include "ZigbeeMC.h"


/************************************************************************
*  RX IRQ
*/
#int_EXT
void  EXT_isr(void)
{
      new_rx = true;
}


/*******************************************************************************
 *  RDA  detector
 */
#int_RDA
void  RDA_isr(void)
{
  rx_char = getc();
  if (rx_char == '{') reset_cpu(); //asci 123 for bootboader.   
}


/*******************************************************************************
 *  RX IRQ loop
 */
void handle_rx_IRQ(void)
{
int8 temp1;


   if (rx_char >= 48 && rx_char <= 57)                         // huidige rgb waardes
   {
      DATA_TX[0] = 10;
      DATA_TX[1] = 11;
      DATA_TX[2] = rx_char;
      write_TX_normal_FIFO();                 // Transmiting
   }

   if (rx_char == '?')                       
   {
     printf("Zigbee Tranceiver\n\r");
   }
   
   if (rx_char == 'r')                       
   {
      temp1 = read_ZIGBEE_short(INTSTAT); // Read and flush register INTSTAT
      printf("read %u\n\r",temp1);
     
      read_RX_FIFO();   
      printf("read1 %u\n\r",DATA_RX[0]);
      printf("read2 %u\n\r",DATA_RX[1]);
      printf("read3 %u\n\r",DATA_RX[2]);
      printf("LQI   %u\n\r",LQI);
      printf("RSSI  %u\n\r",RSSI2);
   }
   
   if (rx_char == 't')                       
   {
      printf("Send\n\r");
      DATA_TX[0] = rx_char;
      DATA_TX[1] = 11;
      DATA_TX[2] = 12;
      write_TX_normal_FIFO();                 // Transmiting
   }
   rx_char = '\0';
}


/************************************************************************
*  Main
*/
void main(){
  int8 rc,temp1 = 0;
  rc = RESTART_CAUSE();
  if (rc == WDT_TIMEOUT) printf("WDT\n\r");
     else printf("rc=%u\n\r",rc);
  setup_wdt(WDT_ON);
  setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);      //32.7 ms overflow
  setup_timer_2(T2_DIV_BY_16,0xFF,8);         // routine elke 0.25uS *div16 *255*16= = 16.32mS mS
  setup_spi(SPI_MASTER| SPI_MODE_0 |SPI_CLK_DIV_16); //4

  setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  disable_interrupts(INT_TIMER1);
  disable_interrupts(INT_TIMER2);
  disable_interrupts(INT_TIMER3);
  enable_interrupts(INT_EXT);
  enable_interrupts(INT_RDA);
  disable_interrupts(INT_CANRX1);
  disable_interrupts(INT_CANRX0);
  enable_interrupts(GLOBAL);
  setup_oscillator(OSC_8MHZ|OSC_TIMER1|OSC_31250|OSC_PLL_OFF);

  pin_reset();                            // Activate reset from pin
  RF_reset();                             // RF reset
  set_WAKE_from_pin();                    // Set wake from pin
  set_long_address(ADDRESS_long_2);       // Set long address
  set_short_address(ADDRESS_short_2);     // Set short address
  set_PAN_ID(PAN_ID_2);                   // Set PAN_ID
  init_ZIGBEE_nonbeacon();                // Initialize ZigBee module
  nonbeacon_PAN_coordinator_device();
  set_TX_power(31);                       // Set max TX power
  set_frame_format_filter(1);             // 1 all frames, 3 data frame only
  set_reception_mode(1);                  // 1 normal mode
  pin_wake();                             // Wake from pin

  printf(__DATE__);
  printf("\n\r" );
  printf(__TIME__);
 
  ext_int_edge(H_TO_L);                    // init interrupt triggering for button press RB0

  printf("Zigbee Tranceiver\n\r");;

  while(1)
  {                     // Infinite loop
   restart_wdt();
   if (rx_char != '\0') handle_rx_IRQ();
   if (new_rx)
   {
      new_rx = false;
      temp1 = read_ZIGBEE_short(INTSTAT); // Read and flush register INTSTAT
      read_RX_FIFO();                     // Read receive data     
 //   printf("%u %u %u\n\r",DATA_RX[0],DATA_RX[1],DATA_RX[2]);
      putc(DATA_RX[2]);
   }
  }
}



and the ZigbeeMC.h
Code:

/*
* Functions for reading and writing registers in short address memory space
*/
/************************************************************************
/ write data in short address register
*/
void write_ZIGBEE_short(int8 address, int8 data_r){
 output_low(CS); //CS = 0;

 address = ((address << 1) & 0b01111111) | 0x01; // calculating addressing mode
 SPI_Write(address);       // addressing register
 SPI_Write(data_r);        // write data in register

 output_high(CS); //;CS = 1;
}


/************************************************************************
// read data from short address register
*/
int8 read_ZIGBEE_short(int8 address)
{
 int8 data_r = 0, dummy_data_r = 0;

 output_low(CS);  //CS = 0;

 address = (address << 1) & 0b01111110;      // calculating addressing mode
 SPI_Write(address);                        // addressing register
 data_r = SPI_Read(dummy_data_r);           // read data from register

 output_high(CS); // CS = 1;
 return data_r;
}


/*
* Functions for reading and writing registers in long address memory space
*/
/************************************************************************
/ Write data in long address register
*/
void write_ZIGBEE_long(int16 address, int8 data_r){
 int8 address_high = 0, address_low = 0;

 output_low(CS); //CS = 0;

 address_high = (((int8)(address >> 3)) & 0b01111111) | 0x80;  // calculating addressing mode
 address_low  = (((int8)(address << 5)) & 0b11100000) | 0x10;  // calculating addressing mode
 SPI_Write(address_high);           // addressing register
 SPI_Write(address_low);            // addressing register
 SPI_Write(data_r);                 // write data in registerr

 output_high(CS); //CS = 1;
}

// Read data from long address register
 int8 read_ZIGBEE_long(int16 address){
 int8 data_r = 0, dummy_data_r = 0;
 int8 address_high = 0, address_low = 0;

 output_low(CS);

 address_high = ((int8)(address >> 3) & 0b01111111) | 0x80;  //calculating addressing mode
 address_low  = ((int8)(address << 5) & 0b11100000);         //calculating addressing mode
 SPI_Write(address_high);            // addressing register
 SPI_Write(address_low);             // addressing register
 data_r = SPI_Read(dummy_data_r);    // read data from register

 output_high(CS);
 return data_r;
}

/*
* Reset functions
*/
/************************************************************************
/ Reset from pin
*/
void pin_reset(void)
{
  output_low(RST);;  // activate reset
  Delay_ms(5);
  output_high(RST);;  // deactivate reset
  Delay_ms(5);
}

void PWR_reset(void){
  write_ZIGBEE_short(SOFTRST, 0x04);     //0x04  mask for RSTPWR bit
}

void BB_reset(void){
  write_ZIGBEE_short(SOFTRST, 0x02);     //0x02 mask for RSTBB bit
}

void MAC_reset(void){
  write_ZIGBEE_short(SOFTRST, 0x01);     //0x01 mask for RSTMAC bit
}

void software_reset(void){               //  PWR_reset,BB_reset and MAC_reset at once
  write_ZIGBEE_short(SOFTRST, 0x07);
}

void RF_reset(void){
  int8 temp = 0;
  temp = read_ZIGBEE_short(RFCTL);
  temp = temp | 0x04;                  //mask for RFRST bit
  write_ZIGBEE_short(RFCTL, temp);
  temp = temp & (!0x04);               //mask for RFRST bit
  write_ZIGBEE_short(RFCTL, temp);
  Delay_ms(1);
}

/************************************************************************
*  Interrupt
*/
void enable_interrupt(void)
{
 write_ZIGBEE_short(INTCON_M, 0x00);  //0x00  all interrupts are enable
}


/************************************************************************
*  Set channel
*/
void set_channel(int8 channel_number){               // 11-26 possible channels
  if((channel_number > 26) || (channel_number < 11)) channel_number = 11;
  switch(channel_number){
    case 11:
    write_ZIGBEE_long(RFCON0, 0x02);  //0x02 for 11. channel
    break;
    case 12:
    write_ZIGBEE_long(RFCON0, 0x12);  //0x12 for 12. channel
    break;
    case 13:
    write_ZIGBEE_long(RFCON0, 0x22);  //0x22 for 13. channel
    break;
    case 14:
    write_ZIGBEE_long(RFCON0, 0x32);  //0x32 for 14. channel
    break;
    case 15:
    write_ZIGBEE_long(RFCON0, 0x42);  //0x42 for 15. channel
    break;
    case 16:
    write_ZIGBEE_long(RFCON0, 0x52);  //0x52 for 16. channel
    break;
    case 17:
    write_ZIGBEE_long(RFCON0, 0x62);  //0x62 for 17. channel
    break;
    case 18:
    write_ZIGBEE_long(RFCON0, 0x72);  //0x72 for 18. channel
    break;
    case 19:
    write_ZIGBEE_long(RFCON0, 0x82);  //0x82 for 19. channel
    break;
    case 20:
    write_ZIGBEE_long(RFCON0, 0x92);  //0x92 for 20. channel
    break;
    case 21:
    write_ZIGBEE_long(RFCON0, 0xA2);  //0xA2 for 21. channel
    break;
    case 22:
    write_ZIGBEE_long(RFCON0, 0xB2);  //0xB2 for 22. channel
    break;
    case 23:
    write_ZIGBEE_long(RFCON0, 0xC2);  //0xC2 for 23. channel
    break;
    case 24:
    write_ZIGBEE_long(RFCON0, 0xD2);  //0xD2 for 24. channel
    break;
    case 25:
    write_ZIGBEE_long(RFCON0, 0xE2);  //0xE2 for 25. channel
    break;
    case 26:
    write_ZIGBEE_long(RFCON0, 0xF2);  //0xF2 for 26. channel
    break;
  }
  RF_reset();
}


/************************************************************************
*  Set CCA mode
*/
void set_CCA_mode(int8 CCA_mode){
  int8 temp = 0;
  switch(CCA_mode){
        case 1:                //ENERGY ABOVE THRESHOLD
        {
            temp = read_ZIGBEE_short(BBREG2);
            temp = temp | 0x80;              //0x80 mask
            temp = temp & 0xDF;              //0xDF mask
            write_ZIGBEE_short(BBREG2, temp);

            write_ZIGBEE_short(CCAEDTH, 0x60);    //Set CCA ED threshold to -69 dBm
        }
        break;

        case 2:                //CARRIER SENSE ONLY
        {
            temp = read_ZIGBEE_short(BBREG2);
            temp = temp | 0x40;               // 0x40 mask
            temp = temp & 0x7F;               // 0x7F mask
            write_ZIGBEE_short(BBREG2, temp);

            temp = read_ZIGBEE_short(BBREG2);    // carrier sense threshold
            temp = temp | 0x38;
            temp = temp & 0xFB;
            write_ZIGBEE_short(BBREG2, temp);
        }
        break;

        case 3:                //CARRIER SENSE AND ENERGY ABOVE THRESHOLD
        {
            temp = read_ZIGBEE_short(BBREG2);
            temp = temp | 0xC0;                //0xC0 mask
            write_ZIGBEE_short(BBREG2, temp);

            temp = read_ZIGBEE_short(BBREG2);     // carrier sense threshold
            temp = temp | 0x38;                   // 0x38 mask
            temp = temp & 0xFB;                   // 0xFB mask
            write_ZIGBEE_short(BBREG2, temp);

            write_ZIGBEE_short(CCAEDTH, 0x60);    //Set CCA ED threshold to -69 dBm
        }
        break;

    }

 }


/************************************************************************
*  Set RSSI mode
*/
void set_RSSI_mode(int8 RSSI_mode){        // 1 for RSSI1, 2 for RSSI2 mode
  int8 temp = 0;

  switch(RSSI_mode){
    case 1:
    {
      temp = read_ZIGBEE_short(BBREG6);
      temp = temp | 0x80;               //0x80 mask for RSSI1 mode
      write_ZIGBEE_short(BBREG6, temp);
    }
    break;

    case 2:
    write_ZIGBEE_short(BBREG6, 0x40);   //0x40 data for RSSI2 mode
    break;
  }
}


/************************************************************************
* Set type of device
*/
void nonbeacon_PAN_coordinator_device(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(RXMCR);
  temp = temp | 0x08;                 // 0x08 mask for PAN coordinator
  write_ZIGBEE_short(RXMCR, temp);

  temp = read_ZIGBEE_short(TXMCR);
  temp = temp & 0xDF;                 // 0xDF mask for CSMA-CA mode
  write_ZIGBEE_short(TXMCR, temp);

  write_ZIGBEE_short(ORDER, 0xFF);    // BO, SO are 15
}

void nonbeacon_coordinator_device(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(RXMCR);
  temp = temp | 0x04;                 // 0x04 mask for coordinator
  write_ZIGBEE_short(RXMCR, temp);

  temp = read_ZIGBEE_short(TXMCR);
  temp = temp & 0xDF;                 // 0xDF mask for CSMA-CA mode
  write_ZIGBEE_short(TXMCR, temp);

  write_ZIGBEE_short(ORDER, 0xFF);    // BO, SO  are 15
}

void nonbeacon_device(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(RXMCR);
  temp = temp & 0xF3;             // 0xF3 mask for PAN coordinator and coordinator
  write_ZIGBEE_short(RXMCR, temp);

  temp = read_ZIGBEE_short(TXMCR);
  temp = temp & 0xDF;             // 0xDF mask for CSMA-CA mode
  write_ZIGBEE_short(TXMCR, temp);
}


/************************************************************************
* ACK request
*/
void set_ACK(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(TXNCON);
  temp = temp | 0x04;                    //0x04 mask for set ACK
  write_ZIGBEE_short(TXNCON, temp);
}

void set_not_ACK(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(TXNCON);
  temp = temp & (!0x04);                 //0x04 mask for set not ACK
  write_ZIGBEE_short(TXNCON, temp);
}


/*/************************************************************************
*  Encrypt
*/
void set_encrypt(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(TXNCON);
  temp = temp | 0x02;                   // mask for set encrypt
  write_ZIGBEE_short(TXNCON, temp);
}

void set_not_encrypt(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(TXNCON);
  temp = temp & (!0x02);                // mask for set not encrypt
  write_ZIGBEE_short(TXNCON, temp);
}


/************************************************************************
* Transmit packet
*/
void start_transmit(void){
  int8 temp = 0;

  temp = read_ZIGBEE_short(TXNCON);
  temp = temp | 0x01;                // mask for start transmit
  write_ZIGBEE_short(TXNCON, temp);
}


/************************************************************************
* Interframe spacing
*/
void set_IFS_recomended(void){
  int8 temp = 0;

  write_ZIGBEE_short(RXMCR, 0x93);       // Min SIFS Period

  temp = read_ZIGBEE_short(TXPEND);
  temp = temp | 0x7C;                    // MinLIFSPeriod
  write_ZIGBEE_short(TXPEND, temp);

  temp = read_ZIGBEE_short(TXSTBL);
  temp = temp | 0x90;                    // MinLIFSPeriod
  write_ZIGBEE_short(TXSTBL, temp);

  temp = read_ZIGBEE_short(TXTIME);
  temp = temp | 0x31;                    // TurnaroundTime
  write_ZIGBEE_short(TXTIME, temp);
}

void set_IFS_default(void){
  int8 temp = 0;

  write_ZIGBEE_short(RXMCR, 0x75);       // Min SIFS Period

  temp = read_ZIGBEE_short(TXPEND);
  temp = temp | 0x84;                    // Min LIFS Period
  write_ZIGBEE_short(TXPEND, temp);

  temp = read_ZIGBEE_short(TXSTBL);
  temp = temp | 0x50;                    // Min LIFS Period
  write_ZIGBEE_short(TXSTBL, temp);

  temp = read_ZIGBEE_short(TXTIME);
  temp = temp | 0x41;                    // Turnaround Time
  write_ZIGBEE_short(TXTIME, temp);
}


/************************************************************************
* Reception mode
*/
void set_reception_mode(int8 r_mode){    // 1 normal, 2 error, 3 promiscuous mode
  int8 temp = 0;

  switch(r_mode)
  {
   case 1:
   {
    temp = read_ZIGBEE_short(RXMCR);      // normal mode
    temp = temp & (!0x03);                // mask for normal mode
    write_ZIGBEE_short(RXMCR, temp);
   }
   break;

   case 2:
   {
    temp = read_ZIGBEE_short(RXMCR);      // error mode
    temp = temp & (!0x01);                // mask for error mode
    temp = temp | 0x02;                   // mask for error mode
    write_ZIGBEE_short(RXMCR, temp);
   }
   break;

   case 3:
   {
    temp = read_ZIGBEE_short(RXMCR);      // promiscuous mode
    temp = temp & (!0x02);                // mask for promiscuous mode
    temp = temp | 0x01;                   // mask for promiscuous mode
    write_ZIGBEE_short(RXMCR, temp);
   }
   break;
  }
}

/************************************************************************
*  Frame format filter
*/
void set_frame_format_filter(int8 fff_mode){   // 1 all frames, 2 command only, 3 data only, 4 beacon only
  int8 temp = 0;

  switch(fff_mode)
  {
   case 1:
   {
    temp = read_ZIGBEE_short(RXFLUSH);      // all frames
    temp = temp & (!0x0E);                  // mask for all frames
    write_ZIGBEE_short(RXFLUSH, temp);
   }
   break;

   case 2:
   {
    temp = read_ZIGBEE_short(RXFLUSH);      // command only
    temp = temp & (!0x06);                  // mask for command only
    temp = temp | 0x08;                     // mask for command only
    write_ZIGBEE_short(RXFLUSH, temp);
   }
   break;

   case 3:
   {
    temp = read_ZIGBEE_short(RXFLUSH);      // data only
    temp = temp & (!0x0A);                  // mask for data only
    temp = temp | 0x04;                     // mask for data only
    write_ZIGBEE_short(RXFLUSH, temp);
   }
   break;

   case 4:
   {
    temp = read_ZIGBEE_short(RXFLUSH);      // beacon only
    temp = temp & (!0x0C);                  // mask for beacon only
    temp = temp | 0x02;                     // mask for beacon only
    write_ZIGBEE_short(RXFLUSH, temp);
   }
   break;
  }
}

/************************************************************************
*  Flush RX FIFO pointer
*/
void   flush_RX_FIFO_pointer(void){
  int8 temp;

  temp = read_ZIGBEE_short(RXFLUSH);
  temp = temp | 0x01;                    // mask for flush RX FIFO
  write_ZIGBEE_short(RXFLUSH, temp);
}


/************************************************************************
* FIFO
*/
void read_RX_FIFO(void){
  unsigned int8 temp = 0;
  int8 i = 0;

  temp = read_ZIGBEE_short(BBREG1);      // disable receiving packets off air.
  temp = temp | 0x04;                    // mask for disable receiving packets
  write_ZIGBEE_short(BBREG1, temp);

  for(i=0; i<128; i++)
  {
   if(i <  (1 + DATA_LENGHT + HEADER_LENGHT + 2 + 1 + 1))
     data_RX_FIFO[i] = read_ZIGBEE_long(address_RX_FIFO + i);  // reading valid data from RX FIFO
   if(i >= (1 + DATA_LENGHT + HEADER_LENGHT + 2 + 1 + 1))
     lost_data = read_ZIGBEE_long(address_RX_FIFO + i);        // reading invalid data from RX FIFO
  }

  DATA_RX[0] = data_RX_FIFO[HEADER_LENGHT + 1];               // coping valid data
  DATA_RX[1] = data_RX_FIFO[HEADER_LENGHT + 2];               // coping valid data
  DATA_RX[2] = data_RX_FIFO[HEADER_LENGHT + 3];               // coping valid data
  LQI   = data_RX_FIFO[1 + HEADER_LENGHT + DATA_LENGHT + 2];  // coping valid data
  RSSI2 = data_RX_FIFO[1 + HEADER_LENGHT + DATA_LENGHT + 3];  // coping valid data

  temp = read_ZIGBEE_short(BBREG1);      // enable receiving packets off air.
  temp = temp & (!0x04);                 // mask for enable receiving
  write_ZIGBEE_short(BBREG1, temp);

}

void write_TX_normal_FIFO(void){
   int8 i = 0;

   data_TX_normal_FIFO[0]  = HEADER_LENGHT;
   data_TX_normal_FIFO[1]  = HEADER_LENGHT + DATA_LENGHT;
   data_TX_normal_FIFO[2]  = 0x01;                        // control frame
   data_TX_normal_FIFO[3]  = 0x88;
   data_TX_normal_FIFO[4]  = SEQ_NUMBER;                // sequence number
   data_TX_normal_FIFO[5]  = PAN_ID_2[1];                 // destinatoin pan
   data_TX_normal_FIFO[6]  = PAN_ID_2[0];
   data_TX_normal_FIFO[7]  = ADDRESS_short_2[0];          // destination address 
   data_TX_normal_FIFO[8]  = ADDRESS_short_2[1]; 
   data_TX_normal_FIFO[9]  = PAN_ID_1[0];                 // source pan
   data_TX_normal_FIFO[10] = PAN_ID_1[1];
   data_TX_normal_FIFO[11] = ADDRESS_short_1[0];          // source address
   data_TX_normal_FIFO[12] = ADDRESS_short_1[1];

   data_TX_normal_FIFO[13] = DATA_TX[0];                  // data
   data_TX_normal_FIFO[14] = DATA_TX[1];
   data_TX_normal_FIFO[15] = DATA_TX[2];

   for(i = 0; i < (HEADER_LENGHT + DATA_LENGHT + 2); i++)
   {
    write_ZIGBEE_long(address_TX_normal_FIFO + i, data_TX_normal_FIFO[i]); // write frame into normal FIFO
   }

   set_not_ACK();
   set_not_encrypt();
   start_transmit();
   
}

/************************************************************************
* Address
*/
void set_short_address(int8 * address){
  write_ZIGBEE_short(SADRL, address[0]);
  write_ZIGBEE_short(SADRH, address[1]);
}

void set_long_address(int8 * address){
  int8 i = 0;

  for(i = 0; i < 8; i++)
  {
   write_ZIGBEE_short(EADR0 + i, address[i]);   // 0x05 address of EADR0
  }
}

void set_PAN_ID(int8 * address){
  write_ZIGBEE_short(PANIDL, address[0]);
  write_ZIGBEE_short(PANIDH, address[1]);
}


/************************************************************************
* Wake
*/
void set_wake_from_pin(void)
{
  int8 temp = 0;

  output_low(WAKE);
  temp = read_ZIGBEE_short(RXFLUSH);
  temp = temp | 0x60;                     // mask
  write_ZIGBEE_short(RXFLUSH, temp);

  temp = read_ZIGBEE_short(WAKECON);
  temp = temp | 0x80;
  write_ZIGBEE_short(WAKECON, temp);
}

void pin_wake(void)
{
  output_high(WAKE);
  Delay_ms(5);
}

/************************************************************************
* PLL
*/
void enable_PLL(void){
   write_ZIGBEE_long(RFCON2, 0x80);       // mask for PLL enable
}

void disable_PLL(void){
   write_ZIGBEE_long(RFCON2, 0x00);       // mask for PLL disable
}

/************************************************************************
* Tx power
*/
void set_TX_power(unsigned int8 power){  // 0-31 possible variants
  if((power < 0) || (power > 31)) power = 31;
  power = 31 - power;                                     //0 max, 31 min -> 31 max, 0 min
  power = ((power & 0b00011111) << 3) & 0b11111000;       // calculating power
  write_ZIGBEE_long(RFCON3, power);
}

/************************************************************************
* Init ZIGBEE module
*/
void init_ZIGBEE_basic(void){
   write_ZIGBEE_short(PACON2, 0x98);   // Initialize FIFOEN = 1 and TXONTS = 0x6
   write_ZIGBEE_short(TXSTBL, 0x95);   // Initialize RFSTBL = 0x9
   write_ZIGBEE_long(RFCON1, 0x01);    // Initialize VCOOPT = 0x01
   enable_PLL();                       // Enable PLL (PLLEN = 1)
   write_ZIGBEE_long(RFCON6, 0x90);    // Initialize TXFIL = 1 and 20MRECVR = 1
   write_ZIGBEE_long(RFCON7, 0x80);    // Initialize SLPCLKSEL = 0x2 (100 kHz Internal oscillator)
   write_ZIGBEE_long(RFCON8, 0x10);    // Initialize RFVCO = 1
   write_ZIGBEE_long(SLPCON1, 0x21);   // Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01
}

void init_ZIGBEE_nonbeacon(void){
 init_ZIGBEE_basic();
 set_CCA_mode(1);     // Set CCA mode to ED and set threshold
 set_RSSI_mode(2);    // RSSI2 mode
 enable_interrupt();  // Enables all interrupts
 set_channel(11);     // Channel 11
 RF_reset();
}




and the registers.h
Code:


/////////////////////////////////////////
////// short address registers  /////////
/////////////////////////////////////////

#define RXMCR     0x00
#define PANIDL    0x01
#define PANIDH    0x02
#define SADRL     0x03
#define SADRH     0x04
#define EADR0     0x05
#define EADR1     0x06
#define EADR2     0x07
#define EADR3     0x08
#define EADR4     0x09
#define EADR5     0x0A
#define EADR6     0x0B
#define EADR7     0x0C
#define RXFLUSH   0x0D
#define ORDER     0x10
#define TXMCR     0x11
#define ACKTMOUT  0x12
#define ESLOTG1   0x13
#define SYMTICKL  0x14
#define SYMTICKH  0x15
#define PACON0    0x16
#define PACON1    0x17
#define PACON2    0x18
#define TXBCON0   0x1A
#define TXNCON    0x1B
#define TXG1CON   0x1C
#define TXG2CON   0x1D
#define ESLOTG23  0x1E
#define ESLOTG45  0x1F
#define ESLOTG67  0x20
#define TXPEND    0x21
#define WAKECON   0x22
#define FRMOFFSET 0x23
#define TXSTAT    0x24
#define TXBCON1   0x25
#define GATECLK   0x26
#define TXTIME    0x27
#define HSYMTMRL  0x28
#define HSYMTMRH  0x29
#define SOFTRST   0x2A
#define SECCON0   0x2C
#define SECCON1   0x2D
#define TXSTBL    0x2E
#define RXSR      0x30
#define INTSTAT   0x31
#define INTCON_M  0x32
#define GPIO      0x33
#define TRISGPIO  0x34
#define SLPACK    0x35
#define RFCTL     0x36
#define SECCR2    0x37
#define BBREG0    0x38
#define BBREG1    0x39
#define BBREG2    0x3A
#define BBREG3    0x3B
#define BBREG4    0x3C
#define BBREG6    0x3E
#define CCAEDTH   0x3F

///////////////////////////////////////////
//////// long address registers  //////////
///////////////////////////////////////////

#define RFCON0    0x200
#define RFCON1    0x201
#define RFCON2    0x202
#define RFCON3    0x203
#define RFCON5    0x205
#define RFCON6    0x206
#define RFCON7    0x207
#define RFCON8    0x208
#define SLPCAL0   0x209
#define SLPCAL1   0x20A
#define SLPCAL2   0x20B
#define RFSTATE   0x20F
#define RSSI      0x210
#define SLPCON0   0x211
#define SLPCON1   0x220
#define WAKETIMEL 0x222
#define WAKETIMEH 0x223
#define REMCNTL   0x224
#define REMCNTH   0x225
#define MAINCNT0  0x226
#define MAINCNT1  0x227
#define MAINCNT2  0x228
#define MAINCNT3  0x229
#define ASSOEADR0 0x230
#define ASSOEADR1 0x231
#define ASSOEADR2 0x232
#define ASSOEADR3 0x233
#define ASSOEADR4 0x234
#define ASSOEADR5 0x235
#define ASSOEADR6 0x236
#define ASSOEADR7 0x237
#define ASSOSADR0 0x238
#define ASSOSADR1 0x239
#define UPNONCE0  0x240
#define UPNONCE1  0x241
#define UPNONCE2  0x242
#define UPNONCE3  0x243
#define UPNONCE4  0x244
#define UPNONCE5  0x245
#define UPNONCE6  0x246
#define UPNONCE7  0x247
#define UPNONCE8  0x248
#define UPNONCE9  0x249
#define UPNONCE10 0x24A
#define UPNONCE11 0x24B
#define UPNONCE12 0x24C

Knight_FlyCn



Joined: 10 May 2013
Posts: 16
Location: Việt Nam

View user's profile Send private message AIM Address

Increase the distance data transmission
PostPosted: Sat Sep 28, 2013 12:06 pm     Reply with quote

When I use code, I can transmit 20cm. How do I increase the transmission distance up? If I want to transfer the transfer of 124 356 how, because each transmission only 8bit DATA_TX
_________________
Thinh
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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