📄 rfdesign.c
字号:
// 5444 iterations = 2 ms
// Wait for at least 2 ms
WaitUS(2000); // wait 2 ms
// writes 24-bit frequency value used for TX to 868 MHz.
// Check CC1000 data sheet for equations used to find
// 0x583000.
SETREG(FREQ_2A, 0x58);
SETREG(FREQ_1A, 0x30);
SETREG(FREQ_0A, 0x00);
// writes 24-bit frequency value used for RX to 868 MHz.
// Check CC1000 data sheet for equations used to find
// 0x583313.
SETREG(FREQ_2B, 0x58);
SETREG(FREQ_1B, 0x33);
SETREG(FREQ_0B, 0x13);
// sets frequency separation between 0 value and 1 value
SETREG(FSEP1, 0x01);
SETREG(FSEP0, 0xAB);
// sets some internal current levels, and enables RSSI output to pin
SETREG(FRONT_END,0x02);
// sets the PLL reference divider to divide by 6
SETREG(PLL,0x30);
// sets continuous lock indicator to output on the CHP_OUT pin
SETREG(LOCK,0x10);
// sets threshold level for peak detector (not used in this design)
SETREG(MODEM2, 0xC1);
// sets the averaging filter to free-running and controlled by writes
// to bit 4 of this register.
// Sets averaging filter sensitivity to .6dB worst-case loss of sensitivity
SETREG(MODEM1, 0x6B);
// baud rate to 76.8, Transparent Asyn. UART, and crystal freq. to 14 MHz
SETREG(MODEM0, 0x58);
// sets capacitor array values for RX and TX
SETREG(MATCH, 0x10);
// disables dithering and data shaping
SETREG(FSCTRL, 0x01);
// sets prescaling to nominal values
SETREG(PRESCALER,0);
// sets charge pump current scaling factor, which determines the bandwidth
// of the PLL.
SETREG(TEST4,0x3F);
// Calibration Process
// RX Calibration
// set transceiver to RX mode
SETREG(MAIN,0x11);
SETREG(CURRENT,0x88); // RX mode current
SETREG(CAL,0xA6); // begin calibration
cal_complete = 0;
do
{
cal_complete = READREG(CAL);
} while (!(cal_complete & 0x08)); // spin until calibration is complete
SETREG(CAL,0x26);
// TX Calibration
SETREG(MAIN,0xE1); // set to TX mode
SETREG(CURRENT,0xF3); // TX mode current
SETREG(PA_POW,0x00); // set output power to 0 during
// calibration
SETREG(CAL,0xA6); // begin calibration
cal_complete = 0;
do
{
cal_complete = READREG(CAL);
} while (!(cal_complete & 0x08)); // spin until finished
SETREG(CAL,0x26);
}
//-----------------------------------------------------------------------------
// C1000_POWERDOWN
//-----------------------------------------------------------------------------
// This function powers down all components of the transceiver,
// including all RX, TX, Oscillator, and bias components.
//
void C1000_POWERDOWN(void)
{
SETREG(MAIN,0xFE); // shutdown RF transceiver
SETREG(PA_POW,0x00); // set output power to 0
}
//-----------------------------------------------------------------------------
// C1000_RSSI
//-----------------------------------------------------------------------------
// C1000_RSSI functions almost like C1000_RX_MODE, except
// that it spins for 250 microseconds instead of using
// the ADC ISR as a counter. This method is used because
// ADC interrupts are not enabled when C1000_RSSI is called.
//
void C1000_RSSI(void)
{
SETREG(MAIN, 0x11); // set to RX mode
SETREG(PA_POW,0x00); // no output power needed
SETREG(CURRENT,0x88); // set RX current level
// wait 250 microseconds
WaitUS(250);
}
//-----------------------------------------------------------------------------
// C1000_RX_MODE
//-----------------------------------------------------------------------------
// C1000_RX_MODE sets the transceiver to RX mode and
// then initializes the 250 microsecond counter
// that gets incremented in the ADC ISR.
//
void C1000_RX_MODE(void)
{
SETREG(MAIN, 0x11); // set to RX mode
SETREG(PA_POW,0x00); // no output power needed
SETREG(CURRENT,0x88); // set RX current level
TRANSITION_IN_PROGRESS = 1; // begin counter in ADC interrupt
TO_TX_INDICATOR = 0; // set flag to TX->RX
}
//-----------------------------------------------------------------------------
// C1000_TX_MODE
//-----------------------------------------------------------------------------
// C1000_TX_MODE sets the transceiver to TX mode and
// then initializes the 250 microsecond counter
// found in the ADC interrupt.
//
void C1000_TX_MODE(void)
{
SETREG(PA_POW,0x00); // set output power to 0
SETREG(MAIN,0xE1); // set to TX mode
SETREG(CURRENT,0xF3); // TX current level
TRANSITION_IN_PROGRESS = 1; // begin counter in ADC interrupt
TO_TX_INDICATOR = 1; // set flag to RX->TX
}
//-----------------------------------------------------------------------------
// FIFO Routines
//-----------------------------------------------------------------------------
// All FIFO functions pass a pointer to the fifo. Pull functions return
// either a short or a char, and push functions have the data to be pushed
// as an additional parameter.
// Pushes and pulls update EMPTY, COUNT, and OF variables.
//
//-----------------------------------------------------------------------------
// Pull Functions
//-----------------------------------------------------------------------------
// Pull functions save the value at element FIRST in the fifo array, then
// increment FIRST by one element. This increment wraps around the
// array when FIRST equals the highest element of the FIFO before the
// increment.
// COUNT is decremented, and if the new value of COUNT = 0, the EMPTY
// flag for the FIFO is set.
//
unsigned char UTXFIFO_Pull(void)
{
unsigned char output;
// wrap FIFO pointer if necessary
if (UTXFIFO.FIRST==RX_TX_FIFOSIZE-1) UTXFIFO.FIRST = 0;
else (UTXFIFO.FIRST)++;
// pull value from fifo
output = UTXFIFO.FIFO[UTXFIFO.FIRST];
// set empty indicator if necessary
if (--(UTXFIFO.COUNT) == 0) UTXFIFO.EMPTY = 1;
return output;
}
unsigned char URXFIFO_Pull(void)
{
unsigned char output;
if (URXFIFO.FIRST==RX_TX_FIFOSIZE-1) URXFIFO.FIRST = 0;
else (URXFIFO.FIRST)++;
output = URXFIFO.FIFO[URXFIFO.FIRST];
if (--(URXFIFO.COUNT) == 0) URXFIFO.EMPTY = 1;
return output;
}
unsigned short ADCRXFIFO_Pull(void)
{
unsigned short output;
if (ADCRXFIFO.FIRST==ADC_DAC_FIFOSIZE-1) ADCRXFIFO.FIRST = 0;
else (ADCRXFIFO.FIRST)++;
output = ADCRXFIFO.FIFO[ADCRXFIFO.FIRST];
if (--(ADCRXFIFO.COUNT) == 0) ADCRXFIFO.EMPTY = 1;
return output;
}
unsigned short DACTXFIFO_Pull(void)
{
unsigned short output;
if (DACTXFIFO.FIRST==ADC_DAC_FIFOSIZE-1) DACTXFIFO.FIRST = 0;
else (DACTXFIFO.FIRST)++;
output = DACTXFIFO.FIFO[DACTXFIFO.FIRST];
if (--(DACTXFIFO.COUNT) == 0) DACTXFIFO.EMPTY = 1;
return output;
}
//-----------------------------------------------------------------------------
// Push Functions
//-----------------------------------------------------------------------------
// Push functions always set EMPTY = 0. They increment LAST,
// taking into account the possibility of wrap-around. Then
// the value num is written into the LAST element of the FIFO.
// COUNT is incremented, and if COUNT's new value is greater
// than the size of the FIFO, then an overflow error has
// occurred. The OF flag is thenset and will remain set on
// the FIFO until reset.
//
void UTXFIFO_Push(unsigned char num)
{
UTXFIFO.EMPTY = 0; // FIFO is no longer empty
(UTXFIFO.LAST)++; // increment, wrap if necessary
if (UTXFIFO.LAST == RX_TX_FIFOSIZE)
{
UTXFIFO.LAST = 0;
}
UTXFIFO.FIFO[UTXFIFO.LAST]=num; // push value to FIFO
// test for overflows
if(!(UTXFIFO.OF)) ++UTXFIFO.COUNT;
if ( UTXFIFO.COUNT == RX_TX_FIFOSIZE) UTXFIFO.OF = 1;
}
void URXFIFO_Push(unsigned char num)
{
URXFIFO.EMPTY = 0;
(URXFIFO.LAST)++;
if (URXFIFO.LAST == RX_TX_FIFOSIZE)
{
URXFIFO.LAST = 0;
}
URXFIFO.FIFO[URXFIFO.LAST]=num;
if(!(URXFIFO.OF)) ++URXFIFO.COUNT;
if ( URXFIFO.COUNT == RX_TX_FIFOSIZE) URXFIFO.OF = 1;
}
void ADCRXFIFO_Push(unsigned short num)
{
ADCRXFIFO.EMPTY = 0;
(ADCRXFIFO.LAST)++;
if (ADCRXFIFO.LAST == ADC_DAC_FIFOSIZE)
{
ADCRXFIFO.LAST = 0;
}
ADCRXFIFO.FIFO[ADCRXFIFO.LAST]=num;
if(!(ADCRXFIFO.OF)) ++ADCRXFIFO.COUNT;
if ( ADCRXFIFO.COUNT == ADC_DAC_FIFOSIZE) ADCRXFIFO.OF = 1;
}
void DACTXFIFO_Push(unsigned short num)
{
DACTXFIFO.EMPTY = 0;
(DACTXFIFO.LAST)++;
if (DACTXFIFO.LAST == ADC_DAC_FIFOSIZE)
{
DACTXFIFO.LAST = 0;
}
DACTXFIFO.FIFO[DACTXFIFO.LAST]=num;
if(!(DACTXFIFO.OF)) ++DACTXFIFO.COUNT;
if ( DACTXFIFO.COUNT == ADC_DAC_FIFOSIZE) DACTXFIFO.OF = 1;
}
//-----------------------------------------------------------------------------
// CLEAR_FIFOS
//-----------------------------------------------------------------------------
// CLEAR FIFOs resets all FIFOs to their microcontroller reset values.
//
void CLEAR_FIFOS(void)
{
int x;
// reset DAC FIFO
DACTXFIFO.EMPTY = 1;
DACTXFIFO.FIRST = 0;
DACTXFIFO.LAST = 0;
DACTXFIFO.COUNT = 0;
DACTXFIFO.OF = 0;
for(x = 0;x<ADC_DAC_FIFOSIZE; x++) DACTXFIFO.FIFO[x] = 0;
// reset the UART RX FIFO
URXFIFO.EMPTY = 1;
URXFIFO.FIRST = 0;
URXFIFO.LAST = 0;
URXFIFO.COUNT = 0;
URXFIFO.OF = 0;
for(x = 0;x<RX_TX_FIFOSIZE;x++) URXFIFO.FIFO[x] = 0;
// reset the ADC RX FIFO
ADCRXFIFO.EMPTY = 1;
ADCRXFIFO.FIRST = 0;
ADCRXFIFO.LAST = 0;
ADCRXFIFO.COUNT = 0;
ADCRXFIFO.OF = 0;
for(x = 0;x<ADC_DAC_FIFOSIZE; x++) ADCRXFIFO.FIFO[x] = 0;
// reset the UART TX FIFO
UTXFIFO.EMPTY = 1;
UTXFIFO.FIRST = 0;
UTXFIFO.LAST = 0;
UTXFIFO.COUNT = 0;
UTXFIFO.OF = 0;
for(x = 0;x<RX_TX_FIFOSIZE;x++) UTXFIFO.FIFO[x] = 0;
}
//-----------------------------------------------------------------------------
// Compression Algorithms
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// RXalgorithm
//-----------------------------------------------------------------------------
// RXalgorithm pulls 1 ADC sample and pushes 1 bit of a UART TX byte per
// iteration.
//
void RXalgorithm(void)
{
static short RXI1; // integrator I1 for RXAlgorithm
static short RXI2; // integrator I2 for RXAlgorithm
static unsigned char output_byte; // stores 8 compressed ADC samples
// used to update bits of output_byte
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -