?? f360_cc1100.c
字號:
//-----------------------------------------------------------------------------
// Wireless Voice Transmitter/Receiver with C8051F360
//-----------------------------------------------------------------------------
//
// This software system is written to control the PCB designed with C8051F360.
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f360.h> // SFR declarations
#include <intrins.h> // includes the _nop_() command
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // speed of internal clock
#define SAMPLE_RATE 8000 // sampling rate for the ADC
#define SPI_DATARATE 153600 // used for timing in SPI state machine
#define TransmitFIFO_FIFOSIZE 236 // sets the size of data buffers
#define ReceiveFIFO_FIFOSIZE 236
#define ADCRXFIFO_FIFOSIZE 10
#define DACTXFIFO_FIFOSIZE 10
#define DAC_UPDATERATE 8000 // rate of DAC sample output
// Transmission-Related Constants
#define RXTX_BytesOfData 200 // number of bytes of compressed audio
// data to be transmitted per data
// packet
#define TransmitFIFO_TARGET 200 // target size of TransmitFIFO, used to make
// sampling rate adjustments to avoid
// FIFO over- and underflows
#define ReceiveFIFO_TARGET 10 // target size of ReceiveFIFO, used to make
// sampling rate adjustments to avoid
// FIFO over- and underflows
#define RXTX_NoPreambleLevel 20 // consecutive number of data packets
// that will be missed before the
// RF signal is considered lost and
// transmission shuts down
#define I_TX 0x02 // this value is written to the RF
// transceiver's Interface register when
// the RF switch is to be placed in the
// TX position
#define I_RX 0x01 // this value is written to the RF
// transceiver's Interface register when
// the RF switch is to be placed in the
// RX position
#define Audio_QuietThreshold 2 // difference threshold in ADC codes
// between two consecutive samples that
// is used to determine whether a
// signal is "loud" enough to transmit
#define AudioStateQuietToLoud 10 // number of instances where
// the difference between consecutive
// ADC samples is GREATER than
// <Audio_QuietThreshold> that must be
// accumulated before a "quiet"
// audio signal should be considered
// "loud"
#define AudioStateLoudToQuiet 400 // number of instances where
// the difference between consecutive
// ADC samples is LESS than
// <Audio_QuietThreshold> that must
// CONSECUTIVELY occur before a "loud"
// audio signal should be considered
// "quiet"
#define RX_MinBytesInitPreamble 5 // number of consecutively received
// preamble bytes that must be received
// before the incoming data for the
// first received packet can be
// considered valid
#define TX_NumBytesInitPreamble 50 // number of preamble bytes sent during
// a RXTX_Master's first data packet
// transmission
#define TX_NumBytesPreamble 8 // number of preamble bytes transmitted
// before each data packet, excluding
// the RXTX_Master's first packet
#define RX_MinBytesPreamble 2 // minimum number of preamble bytes
// the must be received before incoming
// data can be considered valid,
// excluding the reception of the
// RXTX_Master's first packet
#define RXTX_SyncWordSize 3 // used to re-sync the slave <SPI_Timer>
#define Audio_StateSize 1 // used to re-sync the slave <SPI_Timer>
#define DPCM_MULTIPLIER 2 // used to amplify the decompressed
// audio samples
// 10-bit DPCM quantization codes
#define low_low 1
#define low_mid 2
#define low_high 4
#define middle 8
#define high_low 16
#define high_mid 32
#define high_high 64
//-----------------------------------------------------------------------------
// CC1100 Registers
//-----------------------------------------------------------------------------
#define MAIN 0x00
#define INTERFACE 0x01
#define RESETT 0x02
#define SEQUENCING 0x03
#define FREQ_2A 0x04
#define FREQ_1A 0x05
#define FREQ_0A 0x06
#define CLOCK_A 0x07
#define FREQ_2B 0x08
#define FREQ_1B 0x09
#define FREQ_0B 0x0A
#define CLOCK_B 0x0B
#define VCO 0x0C
#define MODEM 0x0D
#define DEVIATION 0x0E
#define AFC_CONTROL 0x0F
#define FILTER 0x10
#define VGA1 0x11
#define VGA2 0x12
#define VGA3 0x13
#define VGA4 0x14
#define LOCK 0x15
#define FRONTEND 0x16
#define ANALOG 0x17
#define BUFF_SWING 0x18
#define BUFF_CURRENT 0x19
#define PLL_BW 0x1A
#define CALIBRATE 0x1B
#define PA_POWER 0x1C
#define MATCH 0x1D
#define PHASE_COMP 0x1E
#define GAIN_COMP 0x1F
#define POWERDOWN 0x20
#define TEST1 0x21
#define TEST2 0x22
#define TEST3 0x23
#define TEST4 0x24
#define TEST5 0x25
#define TEST6 0x26
#define TEST7 0x27
#define STATUS 0x40
#define RESET_DONE 0x41
#define RSS 0x42
#define AFC 0x43
#define GAUSS_FILTER 0x44
#define STATUS1 0x45
#define STATUS2 0x46
#define STATUS3 0x47
#define STATUS4 0x48
#define STATUS5 0x49
#define STATUS6 0x4A
#define STATUS7 0x4B
#define TEST_NFC 0x28
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F33x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 IDA0 = 0x96; // IDAC0 data
sfr16 ADC0 = 0xbd; // ADC0 data
sfr16 ADC0GT = 0xc3; // ADC0 Greater-Than
sfr16 ADC0LT = 0xc5; // ADC0 Less-Than
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void); // initialize system clock to 24.5 MHz
void PORT_Init (void); // initialize crossbar
void ADC0_Init (void); // ADC captures on Tmr2 overflows
// interrupts enabled
void Timer0_Init(void); // used for WaitUS()
void Timer2_Init (unsigned int); // ADC start-of-conversion clock source
void Timer3_Init(unsigned int); // sets DAC output rate
void IDAC0_Init(void); // enables IDAC output on P0.1
void SPI_Init(void); // enable 3-wire Slave SPI for RF trans.
// DATA interface
void Variables_Init(void); // set global variables to reset values
void PCA0_Init(void); // configure PCA0 to edge-triggered
// capture mode
void Timer3_ISR (void); // updates the DAC
void UART0_ISR (void); // RX and TX with RF transceiver
void ADC0_ISR (void); // checks for a quiet signal,
// pushes samples onto ADCRXFIFO
// FIFO Routines
unsigned char TransmitFIFO_Pull(); // pulls compressed sample to be TXed
unsigned char ReceiveFIFO_Pull(); // pulls RXed compressed sample
unsigned short ADCRXFIFO_Pull(); // pulls ADC sample
unsigned short DACTXFIFO_Pull(); // pulls decompressed sample
void TransmitFIFO_Push(unsigned char); // pushes compressed sample to be TXed
void ReceiveFIFO_Push(unsigned char); // pushes RXed compressed sample
void ADCRXFIFO_Push(unsigned short); // pushes ADC sample
void DACTXFIFO_Push(unsigned short); // pushes decompressed sample
unsigned short ADCRXFIFO_Newest(void); // returns the value most recently
// pushed onto the ADC FIFO, but does
// not alter any of the index values
void CLEAR_FIFOS(void); // resets all FIFOs to default values
// DPCM Compression Algorithms
void DPCM_Decompress(void); // Compresses an ADC sample
void DPCM_Compress(void); // Decompresses received samples
// RF Transceiver Register Routines
void SETREG(unsigned char, unsigned char);
// updates register to value parameter
unsigned char READREG(unsigned char); // return register value
unsigned char CC1100_Init(void); // initializes RF transceiver
void CC1100_SwitchToRX(void); // sets RF transceiver settings to RX
void CC1100_SwitchToTX(void); // sets RF transceiver settings to TX
void C1020_POWERDOWN(void); // shuts down components of the RF
// transceiver to conserve power
void ASLEEP(void); // function that runs when the
// communications channel is idle
void WaitUS(unsigned int); // function will delay controller
// for a set number of microseconds
void WaitMS_360(unsigned int);
void FIFO_ManagementRoutine(void); // encapsulates all foreground function
// calls concerning buffers, and
// all checks and tests on buffer
// sizes
void RXTX_InitMaster(void);
void RXTX_InitSlave(void);
//-----------------------------------------------------------------------------
// User-Defined Types
//-----------------------------------------------------------------------------
typedef union USHORT { // access a short variable as two
unsigned short S; // 8-bit integer values
unsigned char C[2];
} USHORT;
enum Logic{ FALSE, TRUE};
enum RXTX_Classes{
RXTX_Master,
// designates the endpoint that initiates the creation of the Communication
// Channel
RXTX_Slave,
// designates the endpoint that joins an initiated Communication Channel
RXTX_Searching
// designates an endpoint that is searching for a master
};
typedef enum RXTX_StateMachineStates {
RX_SearchForMaster,
// Search for <RX_MinBytesInitPreamble> consecutive preamble bytes in
// the RF transceiver's output data stream
TX_InitPreamble,
// Transmit <TX_NumBytesInitPreamble> preamble bytes before transmitting
// the first data packet
TX_Preamble,
// Transmit <TX_NumBytesPreamble> preamble bytes
TX_SyncWord,
// Transmit sync word 0xFFFFEE
TX_SwitchTime,
// Delay for a time period specified in the SPI ISR's Transmission State
// Machine, using the timer variable <SPI_Timer> as the time base
TX_AudioState,
// Transmit local shutdown state
TX_Data,
// Transmit <RXTX_BytesOfData> bytes from the TransmitFIFO
RX_Preamble,
// Search for <RX_MinBytesPreamble> consecutive bytes of received preamble
RX_SyncWord,
// Search for and synchronize receiver to the sync word 0xFFFFEE
RX_AudioState,
// Receive the remote (transmitter's) Shutdown State
RX_Data,
// Receive <RXTX_BytesOfData> bytes and push each to the ReceiveFIFO
RXTX_TimeOut,
// state is entered when <SPI_TIMER> times out. Variable
// <TimeOut_EntryMode> should be set before entering state <RXTX_TimeOut>
Switch_ToTX01,
Switch_ToTX02,
Switch_ToTX03,
Switch_ToTX04,
Switch_ToTX05,
Switch_ToTX06,
Switch_ToTX07,
// These states configure the RF Transceiver to its transmit mode
Switch_ToRX01,
Switch_ToRX02,
Switch_ToRX03,
Switch_ToRX04,
Switch_ToRX05,
Switch_ToRX06,
// These states configure the RF Transceiver to its receive mode
RXTX_WaitForTimeOut,
// this state will pause state machine progress until the
// RXTX_StateMachine's time base <SPI_Timer> reaches its time-out value
RXTX_Shutdown,
// Entered when the Communication Channel is to be terminated
Uninitialized
// Reset state of the RF state machine
} RXTX_StateMachineStates;
typedef enum TimeOut_EntryModes{
TimeOut_RXSuccessful,
// indicates that the state machine has just successfully completed the
// reception of a data packet
TimeOut_TXSuccessful,
// indicates that the state machine has just successfully completed the
// transmission of a data packet
TimeOut_RXNoPreamble,
// state machine failed to find the minimum number of bytes of preamble
// needed to determine that a valid data packet has been received before
// SPI_Timer reached its terminal value
TimeOut_RXNoSyncWord
// state machine failed to find a valid Sync Word before SPI_Timer reached
// its terminal value
} TimeOut_EntryModes;
typedef enum Audio_States{
Audio_Quiet = 0x10,
// state indicates that audio is below the audible threshold
Audio_Loud = 0x30,
// audio is above audible threshold
Audio_ChannelShutdown = 0x40
// special state transmitted after both Endpoints' Audio States have
// been set to
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -