📄 rfdesign.c
字号:
{
if ( ((width > BAUDTOLERANCE *(SYSCLK / BAUDRATE) / 10)
&& (width < (20 - BAUDTOLERANCE) * (SYSCLK / BAUDRATE) / 10))
|| ((width > 2 * BAUDTOLERANCE *(SYSCLK / BAUDRATE) / 10)
&& (width < 2 * (20 - BAUDTOLERANCE) * (SYSCLK / BAUDRATE) / 10) ))
{
++preamble_count;
// if most of the bits received are preamble bits, then
// lock the Averaging Filter and look for the Sync byte
if (preamble_count == RX_MINIMUM_PREAMBLE_BAUDS)
{
RX_STATEMACHINE = RX_SEARCH_FOR_SYNCBYTE;
C1000_LOCK_FILTER();
}
}
else
{
// if the bit is not a preamble, decrement the count unless
// it already equals 0
if(preamble_count != 0)
{
preamble_count--;
}
}
}
}
//-----------------------------------------------------------------------------
// Timer3_ISR
//-----------------------------------------------------------------------------
// This ISR updates the DAC output at a rate of DACUPDATERATE.
//
void TIMER3_ISR(void) interrupt 14
{
static unsigned short new_value;
USHORT tempvalue;
TMR3CN &= ~0xC0; // acknowledge interrupt
if (!DACTXFIFO.EMPTY) // if new DAC data is available,
{ // update output information
new_value = DACTXFIFO_Pull();
// DACstartupTimer prevents the output of the
// start-up audio "thump" caused by the compression algorithm
if(DACstartupTimer == STARTUP_SILENCE)
{
// DAC output must be left-justified, and loaded
// low byte first
tempvalue.S = new_value << 6;
IDA0L = tempvalue.C[1];
IDA0H = tempvalue.C[0];
}
else
{
DACstartupTimer++;
}
}
}
//-----------------------------------------------------------------------------
// UART_ISR
//-----------------------------------------------------------------------------
//
// This interrupt checks to see whether TIO or RIO is set. If TIO is set,
// then the next byte of information is transmitted. If RIO is set,
// the received byte of information is stored in a FIFO.
// The ISR also updates the shutdown state machine according to
// REMOTE_SHUTDOWN_STATE and LOCAL_SHUTDOWN_STATE.
//
void UART0_ISR(void) interrupt 4
{
static unsigned char Preamblecount = 0;
static unsigned char TXcount = 0;
static unsigned char RXcount = 0;
if (UART_MODE == TX_MODE) // System is running a Transmit Session
{
if (TI0 == 1)
{
TI0 = 0; // acknowledge interrupt
if(TX_STATEMACHINE == TX_PREAMBLE_BAUDS)
{
SBUF0 = 0x55; // 0x55 = 01010101
++Preamblecount; // count measures number of preamble
// bytes transmitted this session
if(Preamblecount == TX_BYTES_OF_PREAMBLE)
{
Preamblecount = 0; // reset counter for next TX session
TX_STATEMACHINE = TX_SYNCBYTE;
// transmit Sync Byte next
}
}
else if (TX_STATEMACHINE == TX_SYNCBYTE)
{
SBUF0 = 0xF0; // Sync byte = "11110000" but is
// received LSB first.
TX_STATEMACHINE = TX_DATA; // transmit data next
}
else if (TX_STATEMACHINE == TX_DATA)
{
// The first transmitted byte of data is the
// LOCAL_SHUTDOWN_STATE. At this time the shutdown state
// machine is updated and, potentially, the RF transceiver
// is powered down.
if (TXcount == 0)
{
if (LOCAL_SHUTDOWN_STATE == BOTH_ENDPOINTS_QUIET)
{
// channel is idle, power down
CR = 0; // disable PCA clock
RUN_ASLEEP = 1; // set flag to run Asleep()
TXcount = 0; // reset counters
RXcount = 0;
SBUF0 = LOCAL_SHUTDOWN_STATE;
// TX shudown state before
// powering down
REN0 = 0; // disable UART reception
LOCAL_SHUTDOWN_STATE = POWERING_DOWN;
}
else if (ADC_LOW == SHUTDOWN_TOLERANCE)
{
// the local voice stream can be considered quiet
LOCAL_SHUTDOWN_STATE = ONE_ENDPOINT_QUIET;
SBUF0 = LOCAL_SHUTDOWN_STATE;
}
else if(LOCAL_SHUTDOWN_STATE == POWERING_DOWN)
{
// LOCAL_SHUTDOWN_STATE = BOTH_ENDPOINTS_QUIET
// has been transmitted, power down now
EA = 0; // disable interrupts
C1000_POWERDOWN();
}
else
{
// local voice stream is not quiet
LOCAL_SHUTDOWN_STATE = NO_ENDPOINTS_QUIET;
SBUF0 = LOCAL_SHUTDOWN_STATE;
}
}
else if (TXcount != DATA_BYTES_PER_TRANSMISSION)
{
SBUF0 = UTXFIFO_Pull();
// output compressed voice stream
}
if (TXcount++ == DATA_BYTES_PER_TRANSMISSION)
{
TXcount = 0; // Resets the data byte counter
EIE1 |= 0x10; // Enable PCA0 interrupts
ES0 = 0; // Disable UART
TX_IN_PROGRESS = 0; // indicate that TX session has
// completed
CR = 1; // disable PCA clock
TX_STATEMACHINE = TX_PREAMBLE_BAUDS;
C1000_RX_MODE(); // switch transceiver to RX mode
}
}
// This is the wake-up signal transmission state. It only
// runs once after the ASLEEP function resets the
// TX_STATEMACHINE
else if (TX_STATEMACHINE == TX_UNCALIBRATED)
{
// Transmit Preamble
SBUF0 = 0x55;
if (++TXcount == TX_WAKEUP_BYTES)
{
TXcount = 0; // reset counter for next TX
TX_STATEMACHINE = TX_PREAMBLE_BAUDS;
// begin standard TX session
}
}
}
if (RI0 == 1)
{
RI0 = 0;
}
}
else // System is running a Receive Session
{
if(RI0 == 1)
{
RI0 = 0; // acknowledge interrupt
// The first received byte of data is the remote transceiver's
// shutdown state. This variable, and the local transceiver's
// shutdown state, determine the next state of the shutdown
// state machine.
if(RXcount == 0)
{
REMOTE_SHUTDOWN_STATE = SBUF0;
// if both endpoints are quiet, update the state machine
if ((REMOTE_SHUTDOWN_STATE == ONE_ENDPOINT_QUIET)
&& (LOCAL_SHUTDOWN_STATE == ONE_ENDPOINT_QUIET))
{
LOCAL_SHUTDOWN_STATE = BOTH_ENDPOINTS_QUIET;
}
// if the state machine has already been set to
// its BOTH_ENDPOINTS_QUIET state, then both endpoints
// are ready to shutdown.
else if(REMOTE_SHUTDOWN_STATE == BOTH_ENDPOINTS_QUIET)
{
EA = 0; // disable interrupts
CR = 0; // disable PCA clock
RUN_ASLEEP = 1; // set flag to run asleep
TXcount = 0; // reset counters
RXcount = 0;
REN0 = 0; // disable UART reception
C1000_POWERDOWN(); // power down transceiver
}
}
else // byte received is not the
// remote shutdown state, save
// it as a data byte
{
URXFIFO_Push(SBUF0);
}
// if the last byte of the session has been received,
// switch the system to Transmit Mode
if (++RXcount == DATA_BYTES_PER_TRANSMISSION)
{
RXcount = 0; // reset counters
REN0 = 0; // disable UART reception
RX_STATEMACHINE = RX_SEARCH_FOR_SYNCBYTE;
C1000_TX_MODE(); // switch RF transceiver to TX
}
}
}
}
//-----------------------------------------------------------------------------
// C1000 Functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// C1000 Functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SETREG
//-----------------------------------------------------------------------------
// Writing registers in the CC1000 transceiver is accomplished using the
// SPI interface. Two bytes are sent, an address byte and a data byte.
// First PALE is driven low. The address byte contains the 7-bit address,
// and then a logic '1', signifying "write mode". Then PALE is brought
// high and the data byte is transmitted.
//
void SETREG(unsigned char address, unsigned char value)
{
PALE = 0;
SPIF = 0;
SPI0DAT = (address<<1) + 1; // Shift out 6 bit address
// plus Write mode bit
while(!SPIF);
SPIF = 0;
PALE = 1;
SPI0DAT = value;
while(!SPIF);
}
//-----------------------------------------------------------------------------
// READREG
//-----------------------------------------------------------------------------
// Reading registers is accomplished by sending the address of the
// register to be read and then receiving a data byte in return.
// PALE is driven low, and a byte consisting of the 7-bit address
// plus a bit set to '0' signifying "read mode" is sent. PALE is
// then raised high and a data byte is read in through SPI.
//
unsigned char READREG(unsigned char address)
{
unsigned char ad;
PALE = 0;
SPIF = 0;
ad = address;
// shift 7 bit address to upper 7 bits of <address>,
// and leave the lowest bit at value 0 to indicate a
// register read
SPI0DAT = ad << 1;
while(!SPIF);
SPIF = 0;
PALE = 1;
SPI0DAT = 0xFF;
while(!SPIF);
ad = SPI0DAT;
return SPI0DAT;
}
//-----------------------------------------------------------------------------
// C1000_LOCK_FILTER
//-----------------------------------------------------------------------------
// This function sets the LOCK_AVG_IN bit in the MODEM1 register.
//
void C1000_LOCK_FILTER(void)
{
SETREG(MODEM1, 0x7B);
}
//-----------------------------------------------------------------------------
// C1000_UNLOCK_FILTER
//-----------------------------------------------------------------------------
// This function clears the LOCK_AVG_IN bit in the MODEM1 register.
//
void C1000_UNLOCK_FILTER(void)
{
SETREG(MODEM1, 0x6B);
}
//-----------------------------------------------------------------------------
// C1000_Init
//-----------------------------------------------------------------------------
// This function is called at startup and when the transceiver needs
// to be reset.
//
void C1000_Init(void)
{
unsigned char cal_complete;
// MAIN:
// RXTX = 0, F_REG = 0, RX_PD = 1, TX_PD = 1, FS_PD = 1, CORE_PD = 0,
// BIAS_PD = 1, RESET_N = 0
SETREG(MAIN,0x3A);
// SET RESET_N = 1
SETREG(MAIN,0x3B);
// 1 iteration = 9 clock cycles
// 9 clock cycles = 367.3 ns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -