📄 tbcmain.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 EXAMPLE PROGRAM *
* *** + + *** Temperature Broadcast *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* This program demonstrates the use of the SPP library, the ADC, random *
* number generation, timers, and the serial port (with use of escape codes) *
* IMPORTANT : Set up your terminal program to emulate a VT100 terminal for *
* this to work properly. The baud rate is 57600 baud. *
* *
* There is a menu option to name the PCB. This name is stored in Flash, *
* so that this name is retained even when power is turned off. This also *
* provides a simple example of how Flash can be used for non-volatile data *
* storage. *
* *
* You can run this example using just two EM modules and an EB evaluation *
* board. Program the Flash of the first EM by plugging it into the EB, and *
* enter a name using the menu. Then remove this module from the EB, and *
* place it a place you want to measure the temperature. Apply a 2.7V - 3.6V *
* voltage to the power supply test pads. Program the second EM, and leave *
* it connected in the EB board. You can now read out the temperature of the *
* other EM module via a wireless link. *
* *
* Make sure that the modules are named differently, as their IDs depend on *
* their names! *
* *
* You can also use up to 16 evaluation boards with EM modules plugged in, *
* and connect each of them to a terminal window via serial port 0. The *
* program will execute the following loop *
* - broadcast the board's temperature (which is acquired via the ADC) *
* - wait for a random period between 0 and 250 * 9 msecs *
* - listen for other boards' temperature broadcasts *
* - update the terminal window with the latest temperature table *
* - wait for a random period between 0 and 250 * 9 msecs *
* Temperatures older than 30 seconds are automatically removed from the *
* table. *
* *
* Packet format: *
* [Node ID = crc16(Node Name)](2) [Node Name](20) [Node Temperature](2) *
*****************************************************************************
* Author: JOL *
*****************************************************************************
* Revision history: *
* 1.0 2002/08/29 First public release *
* *
* $Log: tbcMain.c,v $
* Revision 1.6 2003/08/18 12:13:29 tos
* Synchronisation with library update (reentry issue).
*
* Revision 1.5 2003/05/13 13:33:26 tos
* Corrected node name control to support name change on-the-fly (no reset required).
*
* Revision 1.4 2003/04/03 12:33:58 tos
* Modified for compatibility with CUL version 1.3.
*
* Revision 1.3 2002/11/19 15:34:49 kht
* Added startup macros
*
* Revision 1.2 2002/10/30 14:53:21 kht
* Changed the tempBroadcast example so that it can run on a stand-alone EM module.
* The example now stores the unit name in Flash memory.
*
* Revision 1.1 2002/10/14 11:04:42 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <chipcon/reg1010.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/hal.h>
#include <chipcon/cul.h>
#include <chipcon/vt100.h>
// Temperature packet:
#define TBC_NODE_ID_LENGTH 2 // word
#define TBC_NODE_NAME_LENGTH 20
#define TBC_TEMP_OFFSET (TBC_NODE_ID_LENGTH + TBC_NODE_NAME_LENGTH)
#define TBC_TEMP_LENGTH 2
#define TBC_DATA_LEN (TBC_NODE_ID_LENGTH + TBC_NODE_NAME_LENGTH + TBC_TEMP_LENGTH)
// Radio related:
#define TBC_MY_SPP_ADDRESS 3
#define TBC_RX_INTERVAL 50
#define TBC_PREAMBLE_COUNT 4
// Node registration
#define TBC_INVALID_NODE_INDEX 255
#define TBC_UNUSED_NODE_ID 0x0000
// Speed related
byte xdata waitMultiplier;
// The temperature "table":
#define TBC_MAX_NODE_COUNT 16
word xdata nodeIDs[TBC_MAX_NODE_COUNT];
byte xdata nodeNames[TBC_MAX_NODE_COUNT][TBC_NODE_NAME_LENGTH];
word xdata nodeTemps[TBC_MAX_NODE_COUNT];
word xdata nodeLastT[TBC_MAX_NODE_COUNT];
// SPP variables
SPP_SETTINGS xdata sppSettings;
SPP_RX_INFO xdata RXI;
SPP_TX_INFO xdata TXI;
byte xdata rxDataBuffer[TBC_DATA_LEN];
byte xdata txDataBuffer[TBC_DATA_LEN];
// Function prototypes
void tbcWaitRandom (void);
void tbcTransmit (void);
void tbcReceive (void);
void tbcPrintTable (void);
// Unit name, stored in Flash
byte code flashUnitName[TBC_NODE_NAME_LENGTH];
// RAM buffer for Flash copy
byte xdata ramBufNonAligned[128];
//----------------------------------------------------------------------------
// MAIN PROGRAM
//----------------------------------------------------------------------------
void main (void) {
byte xdata n;
byte xdata m;
#ifdef FREQ868
// X-tal frequency: 14.745600 MHz
// RF frequency A: 868.277200 MHz Rx
// RF frequency B: 868.277200 MHz Tx
// RX Mode: Low side LO
// Frequency separation: 64 kHz
// Data rate: 19.2 kBaud
// Data Format: NRZ
// RF output power: 4 dBm
// IF/RSSI: RSSI Enabled
RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
0xA3, 0x2F, 0x15, // Modem 0, 1 and 2
0x75, 0xA0, 0x00, // Freq A
0x58, 0x32, 0x8D, // Freq B
0x01, 0xAB, // FSEP 1 and 0
0x40, // PLL_RX
0x30, // PLL_TX
0x6C, // CURRENT_RX
0xF3, // CURRENT_TX
0x32, // FREND
0xFF, // PA_POW
0x00, // MATCH
0x00, // PRESCALER
};
#endif
#ifdef FREQ915
// X-tal frequency: 14.745600 MHz
// RF frequency A: 915.027455 MHz Rx
// RF frequency B: 915.027455 MHz Tx
// RX Mode: Low side LO
// Frequency separation: 64 kHz
// Data rate: 19.2 kBaud
// Data Format: NRZ
// RF output power: 4 dBm
// IF/RSSI: RSSI Enabled
RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
0xA3, 0x2F, 0x15, // Modem 0, 1 and 2
0xAA, 0x80, 0x00, // Freq A
0x5C, 0xF4, 0x02, // Freq B
0x01, 0xAB, // FSEP 1 and 0
0x58, // PLL_RX
0x30, // PLL_TX
0x6C, // CURRENT_RX
0xF3, // CURRENT_TX
0x32, // FREND
0xFF, // PA_POW
0x00, // MATCH
0x00, // PRESCALER
};
#endif
#ifdef FREQ433
// X-tal frequency: 14.745600 MHz
// RF frequency A: 433.302000 MHz Rx
// RF frequency B: 433.302000 MHz Tx
// RX Mode: Low side LO
// Frequency separation: 64 kHz
// Data rate: 19.2 kBaud
// Data Format: NRZ
// RF output power: 10 dBm
// IF/RSSI: RSSI Enabled
RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
0xA3, 0x2F, 0x0E, // Modem 0, 1 and 2
0x58, 0x00, 0x00, // Freq A
0x41, 0xFC, 0x9C, // Freq B
0x02, 0x80, // FSEP 1 and 0
0x60, // PLL_RX
0x48, // PLL_TX
0x44, // CURRENT_RX
0x81, // CURRENT_TX
0x0A, // FREND
0xFF, // PA_POW
0xC0, // MATCH
0x00, // PRESCALER
};
#endif
// Calibration data
RF_RXTXPAIR_CALDATA xdata RF_CALDATA;
// Initialize peripherals
WDT_ENABLE(FALSE);
RLED_OE(TRUE);
YLED_OE(TRUE);
GLED_OE(TRUE);
BLED_OE(TRUE);
// Startup macros for speed and low power consumption
MEM_NO_WAIT_STATES();
FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);
// Seed the random generator:
halRandomNumberGen(&n, 1);
halRandomNumberGen(&m, 1);
srand((n << 8) + m);
waitMultiplier = 1;
// ADC setup
halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0);
ADC_SELECT_INPUT(ADC_INPUT_AD1);
ADC_POWER(TRUE);
// RF/SPP setup
sppSetupRF(&RF_SETTINGS, &RF_CALDATA, TRUE);
sppSettings.myAddress = TBC_MY_SPP_ADDRESS;
sppSettings.rxTimeout = TBC_RX_INTERVAL;
sppSettings.txAckTimeout = TBC_PREAMBLE_COUNT;
sppSettings.txPreambleByteCount = TBC_PREAMBLE_COUNT;
RXI.maxDataLen = TBC_DATA_LEN;
RXI.pDataBuffer = rxDataBuffer;
TXI.destination = SPP_BROADCAST;
TXI.flags = 0x00;
TXI.pDataBuffer = txDataBuffer;
TXI.dataLen = TBC_DATA_LEN;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -