⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicdriver_1.c

📁 这是一个自动测量zigbee发射信号强度的原代码,可以直接运行,不用修改,测试已成功
💻 C
📖 第 1 页 / 共 5 页
字号:
            }
            break;
        
        default:
            break;
    }
    goto MRF24J40_SubMenu;
}

/*********************************************************************
 * Function:        void BoardInit( void )
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    Board is initialized
 *
 * Overview:        This function configures the board for the PICDEM-z
 *                  MRF24J40 usage 
 *
 * Note:            This routine needs to be called before the function 
 *                  to initialize MiWi stack or any other function that
 *                  operates on the stack
 ********************************************************************/
void BoardInit(void)
{
    #if defined(__18F4620)
    // Switches S2 and S3 are on RB5 and RB4 respectively. We want interrupt-on-change
    INTCON = 0x00;

    // There is no external pull-up resistors on S2 and S3. We will use internal pull-ups.
    // The MRF24J40 is using INT0 for interrupts
    // Enable PORTB internal pullups
    INTCON2 = 0x00;
    INTCON3 = 0x00;

    // Make PORTB as input - this is the RESET default
    TRISB = 0xff;

    // Set PORTC control signal direction and initial states
    // disable chip select
    LATC = 0xfd;

    // Set the SPI module for use by Stack
    TRISC = 0xD0;

    // Set the SPI module
    SSPSTAT = 0xC0;
    SSPCON1 = 0x20;

    // D1 and D2 are on RA0 and RA1 respectively, and CS of TC77 is on RA2.
    // Make PORTA as digital I/O.
    // The TC77 temp sensor CS is on RA2.
    ADCON1 = 0x0F;

    // Deselect TC77 (RA2)
    LATA = 0x04;
    
//======================== Bing's adding parameters=======================
//======================== Bing's adding parameters=======================

	//inti TRIS A
	TRISA0 = 0;
	TRISA1 = 0;
	
	//SETTING THE PORT
	//RA0 = 1;
	LATA0 = 0;
	//RA1 = 1;
	LATA1 = 1;
//======================== Bing's adding parameters=======================

    // Make RA0, RA1, RA2 and RA4 as outputs.
    TRISA = 0xF8;
    
    PHY_CS = 1;             //deselect the MRF24J40
    PHY_CS_TRIS = 0;        //make chip select an output   
    
    RFIF = 0;               //clear the interrupt flag
    
    if(RB0 == 0)
    {
        RFIF = 1;
    }
    
    RFIE = 1;               //set interrupt enable flag

    IPEN = 1;
    
    GIEH = 1;
    #else
    //WDTCONbits.SWDTEN = 0; //disable WDT

    OSCCON = 0b01110110;
    OSCTUNE = 0b01000000;
    
    // Switches are on RB1 to RB4 respectively. We don't want interrupt-on-change
    INTCON = 0x00;
    
    // There is no external pull-up resistors on S2 and S3. We will use internal pull-ups.
    // The MRF24J40 is using INT0 for interrupts
    // Enable PORTB internal pullups
    INTCON2 = 0x80;
    INTCON3 = 0x00;

    // Make PORTB as input - this is the RESET default
    TRISB = 0xff;

    // Make PORTE as output - this drives LEDs
    LATE = 0x00;
    TRISE = 0x00;

    // Set PORTC control signal direction and initial states
    // disable chip select
    //LATC = 0xfd;
    LATD = 0b11111011;

    // Set the SPI module for use by Stack
    //TRISC = 0xD0;
    TRISD = 0b00100000;

    // Set the SPI module
    SSP2STAT = 0xC0;
    SSP2CON1 = 0x20;

    // Deselect TC77 (RA2)
    //LATA = 0x04;
    //PORTA = 0x00;
    //LATA = 0x00;
    
    TRISA = 0xFF;
    
    // D1 and D2 are on RA0 and RA1 respectively, and CS of TC77 is on RA2.
    // Make PORTA as digital I/O.
    // The TC77 temp sensor CS is on RA2.
    //ADCON1 = 0x0F;
    //ADCON0 = 0xCC;
    WDTCONbits.ADSHR = 1;
    ANCON0 = 0xDC;
    ANCON1 = 0xFF;
    WDTCONbits.ADSHR = 0;
    ADCON0 = 0x05;
    ADCON1 = 0xBA;
    //ADCON1 = 0xB9;
    //WDTCONbits.ADSHR = 0;
    
    // Make RA0, RA1, RA2 and RA4 as outputs.
    //TRISA = 0xF8;
    //TRISA = 0xFF;
    
    //DelayMs(1);
    //ADCON1bits.ADCAL = 1;
    //ADCON0bits.GO = 1;
    //while(ADCON0bits.GO);
    //ADCON1bits.ADCAL = 0;
    
    PHY_CS = 1;             //deselect the MRF24J40
    PHY_CS_TRIS = 0;        //make chip select an output   
    
    RFIF = 0;               //clear the interrupt flag

    RFIE = 1;
    
    //RCONbits.IPEN = 1;
    IPEN = 1;
    
    INTCON2bits.INTEDG0 = 0;

    GIEH = 1;
    
    // enable LCD
    
    LATF = 0x00;
    TRISF = 0x00;
    #endif
}

/*********************************************************************
 * Function:        void MRF24J40Init(void)
 *
 * PreCondition:    BoardInit (or other initialzation code is required)
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    MRF24J40 is initialized
 *
 * Overview:        This function initializes the MRF24J40 and is required
 *                  before stack operation is available
 ********************************************************************/
void MRF24J40Init(void)
{  
    BYTE i;
    WORD j;
    
    PHY_RESETn = 0;
    for(j=0;j<(WORD)300;j++){}

    PHY_RESETn = 1;
    for(j=0;j<(WORD)300;j++){}
  
    /* do a soft reset */
    PHYSetShortRAMAddr(WRITE_SOFTRST,0x07);
    do
    {
        i = PHYGetShortRAMAddr(READ_SOFTRST);
    }
    while((i&0x07) != (BYTE)0x00);   

    for(j=0;j<(WORD)1000;j++){}
             
    PHYSetShortRAMAddr(WRITE_RFCTL,0x04);
    PHYSetShortRAMAddr(WRITE_RFCTL,0x00);
    
    /* flush the RX fifo */
    PHYSetShortRAMAddr(WRITE_RXFLUSH,0x01);
    
    /* Program the short MAC Address, 0xffff */
    PHYSetShortRAMAddr(WRITE_SADRL,0xFF);
    PHYSetShortRAMAddr(WRITE_SADRH,0xFF);
    PHYSetShortRAMAddr(WRITE_PANIDL,0xFF);
    PHYSetShortRAMAddr(WRITE_PANIDH,0xFF);
    
    /* Program Long MAC Address*/
    for(i=0;i<(BYTE)8;i++)
    {
        PHYSetShortRAMAddr(WRITE_EADR0+i*2,myLongAddress[i]);
    }

    /* program the RF and Baseband Register */
    //PHYSetLongRAMAddr(RFCTRL4,0x02);
    
    /* Enable the RX */
    //PHYSetLongRAMAddr(RFRXCTRL,0x01);
    
    /* setup */    
    PHYSetLongRAMAddr(RFCTRL1,0x01);
    PHYSetLongRAMAddr(RFCTRL2,0x80);
    
    PHYSetLongRAMAddr(RFCTRL3,0x00);
    /* program RSSI ADC with 2.5 MHz clock */
    //PHYSetLongRAMAddr(RFCTRL6,0x04);
    PHYSetLongRAMAddr(RFCTRL6,0x90);

    PHYSetLongRAMAddr(RFCTRL7,0b00000000);
    //PHYSetLongRAMAddr(RFCTRL7,0b10011000);
    
    PHYSetLongRAMAddr(RFCTRL8,0b00010000);

    /* Program CCA mode using RSSI */
    PHYSetShortRAMAddr(WRITE_BBREG2,0xB8);
    //PHYSetShortRAMAddr(WRITE_BBREG2,0x80);
    /* Enable the packet RSSI */
    PHYSetShortRAMAddr(WRITE_BBREG6,0x40);
    
    /* Program CCA, RSSI threshold values */
    //PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x60);
    PHYSetShortRAMAddr(WRITE_RSSITHCCA,0x00);
    
    /* errata issues workaround */
//    PHYSetShortRAMAddr(WRITE_FFOEN, 0x98);
//    PHYSetShortRAMAddr(WRITE_TXPEMISP, 0x95);

    do
    {
        i = PHYGetLongRAMAddr(RFSTATE);
    }
    while((i&0xA0) != 0xA0);
    
    //PHYSetShortRAMAddr(WRITE_RXMCR, 0b00100011);
    
    SetChannel(CHANNEL_11);

    PHYSetShortRAMAddr(WRITE_INTMSK,0xF6);
}


/*********************************************************************
 * Function:        void SetChannel(BYTE channel)
 *
 * PreCondition:    MRF24J40 is initialized
 *
 * Input:           BYTE channel - this is the channel that you wish
 *                  to operate on.  This should be CHANNEL_11, CHANNEL_12,
 *                  ..., CHANNEL_26. 
 *
 * Output:          None
 *
 * Side Effects:    the MRF24J40 now operates on that channel
 *
 * Overview:        This function sets the current operating channel
 *                  of the MRF24J40
 ********************************************************************/
void SetChannel(BYTE channel)
{
    
    PHYSetLongRAMAddr(RFCTRL0, (channel | 0x02));
    PHYSetShortRAMAddr(WRITE_RFCTL,0x04);
    PHYSetShortRAMAddr(WRITE_RFCTL,0x00);
}

/*********************************************************************
 * Function:        void PHYSetLongRAMAddr(WORD address, BYTE value)
 *
 * PreCondition:    Communication port to the MRF24J40 initialized
 *
 * Input:           WORD address is the address of the LONG RAM address
 *                      that you want to write to
 *                  BYTE value is the value that you want to write to
 *                      that register
 *
 * Output:          None
 *
 * Side Effects:    The register value is changed
 *
 * Overview:        This function writes a value to a LONG RAM address
 ********************************************************************/
void PHYSetLongRAMAddr(WORD address, BYTE value)
{
    volatile BYTE tmpRFIE = RFIE;
    RFIE = 0;
    PHY_CS = 0;
    SPIPut((((BYTE)(address>>3))&0b01111111)|0x80);
    SPIPut((((BYTE)(address<<5))&0b11100000)|0x10);
    SPIPut(value);
    PHY_CS = 1;
    RFIE = tmpRFIE;
}

/*********************************************************************
 * Function:        void PHYSetShortRAMAddr(BYTE address, BYTE value)
 *
 * PreCondition:    Communication port to the MRF24J40 initialized
 *
 * Input:           BYTE address is the address of the short RAM address
 *                      that you want to write to.  Should use the
 *                      WRITE_ThisAddress definition in the MRF24J40 
 *                      include file.
 *                  BYTE value is the value that you want to write to
 *                      that register
 *
 * Output:          None
 *
 * Side Effects:    The r

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -