📄 radio.c
字号:
/*= radio.c =====================================================================
*
* Written by Greg Hunter, University of Technology, Sydney, 2007 for sdcc compiler
* See radio.h for details
*
*==============================================================================
*/
#include "regs24e1.h"
#include "radio.h"
#include "delay.h"
#include "spi.h"
#include "uart.h"
// Parameter definitions (see radio,h)
tRadioParm RadioParm;
tComParm ComParm;
// Inital configuration set up for radio:
#define RADIO_PACKET_SIZE 14 //size of receive packet in bytes
const unsigned char RFConfigBuf[15] =
{
0x08,RADIO_PACKET_SIZE<<3, //initial data packet size in bits
0x00,0x00,0x00,0x00,0x00, //RX channel 2 address
0x00,0x00,0x00,0x00,0x04, //RX channel 1 address
0x83, //32 address bits, 16 bit CRC
0x6E, //power set to -5dBm
0x33 //RF channel set to #25, RX on
};
//********************************************************
// RadioInit
// Initialise radio buffer
//********************************************************
void RadioInit(void)
{
unsigned char i;
unsigned char *cpt;
unsigned char *bpt;
//initialise configuration buffer
cpt = &RFConfigBuf[0];
bpt = RadioCfgBufPt;
for (i=0;i<15;i++)
{
*bpt = *cpt;
bpt++;
cpt++;
}
}
//********************************************************
// RadioOn
// Turn on radio
//********************************************************
void RadioOn(void)
{
//turn on radio
PWR_UP = 1;
//wait 3ms
Delay100us(30);
//configure spi bus
SpiRadio();
}
//********************************************************
// RadioCfg
// Load configuration
//********************************************************
void RadioCfg(void)
{
//configure radio
CE = 0;
CS = 1;
Delay5us();
SpiWriteBuf(RadioCfgBufPt,RADIO_CFG_BYTES);
CS = 0;
//switch radio to active mode
CE = 1;
Delay5us();
}
//********************************************************
// RadioTXBuf
// Transmit data in buffer pointed to by bufPt and of length bytes
// The radio is switched back to receive mode after transmission
// Note for timing it takes 700us for reciever to be ready after
// a packet is transmitted
//********************************************************
void RadioTXBuf(unsigned char *bufPt, unsigned char bytes)
{
//switch radio to config mode
CE = 0;
Delay100us(3);
//change to transmit mode
//change buffer to TX mode
RadioParm.ChnRxTx &= ~0x01;
//write change ot last byte in radio configuration
CS = 1;
SpiReadWrite(RadioParm.ChnRxTx);
CS = 0;
Delay5us();
//switch radio to active mode
CE = 1;
Delay5us();
//send data buffer to spi starting at top byte address of TXAddress, a 4 byte variable
SpiWriteBuf(bufPt, bytes);
//switch radio back to config mode
CE = 0;
//add delay, worked out by trial and error: 300us does not work, 400us does work, 500us used
Delay100us(5);
//change to receive mode
//change buffer to RX mode
RadioParm.ChnRxTx |= 0x01;
//send channel number and TXRX byte to configuration
CS = 1;
SpiReadWrite(RadioParm.ChnRxTx);
CS = 0;
Delay5us();
//switch radio to active mode
CE = 1;
}
//********************************************************
// RadioRXBuf
// Receive data to buffer bufPt
// Assumes bufPt points to a byte buffer the same size as the receiver packet size setting in RadioParm.Data1
// returns 1 if packet received, 0 otherwise
//********************************************************
unsigned char RadioRXBuf(unsigned char *bufPt)
{
unsigned char r; //return value
//test if packet received, DR1 = 1
if(DR1 == 1)
{
//packet received (0,1)#
//set returned value to 1
r = 1;
//load packet into data buffer
// CE = 1;
SpiReadBuf(bufPt,RadioParm.Data1>>3);
//clear receive buffer
while(DR1==1)
SpiReadWrite(0);
// CE = 0;
}else
{
//packet not received #(0,1)
//set return value to 0
r = 0;
}
//return
return r;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -