📄 basicdriver_signal_strength.c
字号:
/*****************************************************************************
*
* Main.c -- user main program
*
*****************************************************************************
* FileName: BasicDriver.c
* Dependencies:
* Processor: PIC18F
* Compiler: C18 02.20.00 or higher
* Linker: MPLINK 03.40.00 or higher
* Company: Microchip Technology Incorporated
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the "Company") is intended and supplied to you, the Company's
* customer, for use solely and exclusively with products manufactured
* by the Company.
*
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*****************************************************************************/
#include "MSPI.h"
#include "Console.h"
#include "MRF24J40.h"
#include "BasicDriverDefs.h"
#include <stdio.h>
#include <delays.h>
#include <string.h>
#include <p18cxxx.h>
#if defined(MCHP_C18)
#if defined(__18F4620)
#pragma romdata CONFIG1H = 0x300001
const rom unsigned char config1H = 0b00000110; // HSPLL oscillator
#pragma romdata CONFIG2L = 0x300002
const rom unsigned char config2L = 0b00011111; // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN disabled
#pragma romdata CONFIG2H = 0x300003
const rom unsigned char config2H = 0b00001010; // HW WD disabled, 1:32 prescaler
#pragma romdata CONFIG3H = 0x300005
const rom unsigned char config3H = 0b10000000; // PORTB digital on RESET
#pragma romdata CONFIG4L = 0x300006
const rom unsigned char config4L = 0b10000001; // DEBUG disabled,
// XINST disabled
// LVP disabled
// STVREN enabled
#else // for PIC18F67J11
#pragma config DEBUG = OFF
#pragma config XINST = OFF
#pragma config STVREN = OFF
#pragma config WDTEN = OFF
#pragma config CP0 = ON
#pragma config IESO = OFF
#pragma config FCMEN = OFF
#pragma config FOSC = HSPLL
#pragma config WDTPS = 1024
#pragma config CCP2MX = DEFAULT
#define SWTXD PORTG
#define SWTXDpin 1
#define TRIS_SWTXD TRISG
#endif
#endif
#pragma romdata longAddressLocation = 0x0E
rom unsigned char myLongAddress[8] = {EUI_0,EUI_1,EUI_2,EUI_3,EUI_4,EUI_5,EUI_6,EUI_7};
#pragma romdata
//Revision history
//v2.0 - change menu system. add single tone modulation and sequential transmit mode
//v1.5 - got the sleep mode working for the MRF24J40
//v1.6 - added option to slow down the tranmission rate of continuous transmission
//v1.7 - added option to change the number of packets used in the RSSI calculation
//v1.8 - added FIFO read tests (option 'p' under MRF menu)
//v1.9 - added test for all non-changing RAM locations
//v1.10 - set the default interpacket spacing to as fast as possible,
// created the SEASOLVE definition to always remove the menu after reset
//v1.11 - added LO Leakage, Carrier and SideBand Suppression test
// MRF main menu option 'q'
//v1.12 - added "Teds Tests" menu
//v1.13 - added the UBEC FIFO read workaround of clearing ShortReg0x18[7]
//v1.14 - modified to use 100us before TX or RX when warming up the PA/LNA
#define VERSION "v2.00"
//#define SEASOLVE
/********************** FUNCTION PROTOTYPES *************************/
void BoardInit(void);
void MRF24J40Init(void);
void SetChannel(BYTE channel);
void PHYSetLongRAMAddr(WORD address, BYTE value);
void PHYSetShortRAMAddr(BYTE address, BYTE value);
BYTE PHYGetShortRAMAddr(BYTE address);
BYTE PHYGetLongRAMAddr(WORD address);
void PHYSetDeviceAddress(WORD PANID, WORD shortAddress);
/********************** CONSTANTS ***********************************/
rom char PredefinedPacket[] = {0x03,0x08,0xC4,0xFF,0xFF,0xFF,0xFF,0x07,0x00,0x00,0x00,0x00,0x00};
rom char PERCommand[] = {0x03,0x08,0xC4,0xFF,0xFF,0xFF,0xFF,0xAA};
rom unsigned char testShortRegisters [] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x26,0x27,0x28,0x29,0x2E,0x34,0x3D,0x3F};
rom unsigned short int testLongRegisters [] = {0x0222,0x0223,0x0224,0x0225,0x0226,0x0227,0x0228,0x022A,0x022B,0x022C,0x0230,0x0231,0x0232,0x0233,0x0234,0x0235,0x0236,0x0237,0x0238,0x0239,0x0240,0x0241,0x0242,0x0243,0x0244,0x0245,0x0246,0x0247,0x0248,0x0249,0x024A,0x024B,0x024C};
//1-100 only
#define RSSI_BUFFER_SIZE 50
/********************** Variables ***********************************/
volatile BYTE rssiWrite;
volatile BYTE rssiBuffer[RSSI_BUFFER_SIZE];
BYTE numRSSISamples;
BYTE input;
volatile DWORD_VAL numPacketsRx;
volatile DWORD_VAL PERnum;
volatile DWORD_VAL PERnumFailed;
volatile BOOL foundPERPacket;
volatile BYTE StartSendingPER = 0;
BOOL PrintMenus;
volatile BOOL ReceivingMode;
BYTE RxPrintMenu = 20;
typedef union
{
BYTE txData[128];
struct
{
BYTE ShortReg[sizeof(testShortRegisters)];
BYTE LongReg[sizeof(testLongRegisters)];
}RegTest;
} TEST_DATA;
TEST_DATA testData;
void PrintHeader(rom char *app)
{
BYTE i;
BYTE startIndex;
BYTE stringlen = 0;
while( i = app[stringlen++] );
stringlen--;
ConsolePutROMString((rom char*)"\r\n****************************************\r\n");
ConsolePutROMString((rom char*)" Zigbee Network Signal Strength Analyzer \r\n");
ConsolePutROMString((rom char*)" ");
ConsolePutROMString((rom char*)" ");
ConsolePutROMString((rom char*)" \r\n");
if( stringlen )
{
startIndex = (38 - stringlen) / 2;
ConsolePutROMString((rom char *)" ");
for(i = 0; i < startIndex; i++)
{
ConsolePutROMString((rom char*)" ");
}
ConsolePutROMString(app);
for(i = startIndex + stringlen; i < 38; i++)
{
ConsolePut(' ');
}
ConsolePutROMString((rom char *)" \r\n");
}
ConsolePutROMString((rom char*)"****************************************\r\n");
}
/********************** FUNCTIONS ***********************************/
void main(void)
{
BYTE channel;
BYTE power;
BYTE mode;
BOOL UseExternal;
BOOL AutoCRC;
BYTE packetDelay;
BYTE i;
#if defined(__18F67J11)
char buf[32];
#endif
for(i=0;i<RSSI_BUFFER_SIZE;i++)
{
rssiBuffer[i] = 0;
}
// decide if it is a hard reset or soft reset
if((RCON & 0b00000011) != 0b00000011)
{
// hard reset, intialize the variables
AutoCRC = TRUE;
PrintMenus = FALSE;
UseExternal = FALSE;
ReceivingMode = FALSE;
RCON |= 0b00000011;
}
else
{
// soft reset, keep them the way they were
RCON |= 0b00000011;
}
numPacketsRx.Val = 0;
rssiWrite = 0;
PERnumFailed.Val = 0;
PERnum.Val = 0;
foundPERPacket = FALSE;
numRSSISamples = 1;
// Enable PORTB pull-ups (INTCON2bits.RBPU)
RBPU = 0;
// Make the PORTB switch connections inputs.
TRISB4 = 1;
TRISB5 = 1;
packetDelay = 1;
#if defined(SEASOLVE)
PrintMenus = FALSE;
#else
{
PrintMenus = TRUE;
}
#endif
BoardInit();
ConsoleInit();
MainMenu:
if(PrintMenus)
{
nl();
nl();
nl();
}
else
{
c("+");
}
MRF24J40_Start:
//****************//
if(PrintMenus)
{
c(" ");
}
MRF24J40Init();
MRF24J40_SubMenu:
if(PrintMenus)
{
PrintHeader((rom char *)" ");
c(" (1) Settings Channels\r\n");
c(" (2) Strength Testings\r\n");
c(">>");
}
while(!ConsoleIsGetReady())
{
GIEH = 1;
if( RF_INT_PIN == 0 )
{
INT0IF = 1;
}
input = ' ';
if( StartSendingPER )
{
//goto SendingPER;
while(1)
{
BYTE i;
RA1 = 1;
//tx packets fast
PHYSetLongRAMAddr(0x000,sizeof(PredefinedPacket));
PHYSetLongRAMAddr(0x001,sizeof(PredefinedPacket));
for(i=2;i<(sizeof(PredefinedPacket)+2);i++)
{
PHYSetLongRAMAddr(i, PredefinedPacket[i-2]); /* Dest Address */
}
if(UseExternal)
{
PHYSetShortRAMAddr(WRITE_GPIO, 0b00000011); //TX
Delay100TCYx(4);
c("Using external PA\r\n");
}
//PHYSetShortRAMAddr(WRITE_TXNMTRIG,0b00000001); //transmit packet without ACK requested
{
unsigned int blah;
blah = 0;
while(1)
{
DWORD j;
DWORD k;
if(ConsoleIsGetReady())
{
if(PrintMenus)
{
goto MRF24J40_SubMenu;
}
else
{
goto MRF24J40_NewOption;
}
}
k = ((DWORD)packetDelay)*((DWORD)packetDelay);
do
{
for(j=0;j<2000;j++)
{
}
}
while(k--);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -