CC1101接收程序及相关电路图
相关电路
#include"main.h"
#include"cc1101.h"
#include"lcd1602.h"
void main(void)
{
int i;
UCHAR leng =0; //待接收字节长度
UCHAR TxBuf[8]={0}; // 8字节, 如果需要更长的数据包,请正确设置
UCHAR RxBuf[8]={0}; //接收缓存区
InitLcd1602();
WriteAddressLcd1602(1,0);
WriteCharForLCD1602("The CC1101 Test!");
WriteAddressLcd1602(2,0);
WriteCharForLCD1602("Design by XuJie!");
Delaynms(6000);
ClearLcd1602();
CpuInit();
POWER_UP_RESET_CC1100();
halRfWriteRfSettings();
halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
TxBuf[0] = 1 ;
TxBuf[1] = 1 ;
TxBuf[2] = 1 ;
TxBuf[3] = 1 ;
TxBuf[4] = 1 ;
TxBuf[2] = 1 ;
TxBuf[6] = 1 ;
TxBuf[7] = 1 ;
halRfSendPacket(TxBuf,8); // Transmit Tx buffer data
delay(6000);
InitTimer0();
// 显示
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
// Bulb overturn at
// ***** SEC later!
WriteAddressLcd1602(1,0);
WriteCharForLCD1602("Bulb overturn at");
WriteAddressLcd1602(2,6);
WriteCharForLCD1602("SEC later!");
while(1)
{
led = 1; //指示灯一直熄灭,只有接收到数据时才闪烁
leng =8; // 预计接受8 bytes
ResultToDisplay(SetTime);
WriteAddressLcd1602(2,0);
for(i=0;i<5;i++)
WriteLcd1602(1,DisplayResult[i]);
if(halRfReceivePacket(RxBuf,&leng))
{
if( RxBuf[1]==1) //确认,开始灭定时
{
led = 0;
TR0 = 0;
TR1 = 1;
}
if( RxBuf[2]==1)
{ //确认,开始亮定时
led = 0;
TR0 = 1;
TR1 = 0;
}
if( RxBuf[3]==1) //时间加
{
led = 0;
SetTime ++;
}
if( RxBuf[4]==1) //时间减
{
led = 0;
SetTime --;
}
if( RxBuf[5]==1) //灯亮
{
led = 0;
bulb = 0;
}
if( RxBuf[6]==1) //灯灭
{
led = 0;
bulb = 1;
}
delay(1000);
}
RxBuf[1] = 0xff; // 接收正确数据后复位数据,防止旧数据对新数据影响
RxBuf[2] = 0xff;
RxBuf[3] = 0xff;
RxBuf[4] = 0xff;
RxBuf[5] = 0xff;
RxBuf[6] = 0xff;
}
}
/*---------------------------------------------
定时器0函数:控制灯定时一定时间后点亮 ---------------------------------------------*/
void Timer0() interrupt 1 {
TH0 = (65536-50000)/256; //50ms定时,定时20次,12M晶振约为1s
TL0 = (65536-50000)%256;
count0 ++;
if(20==count0)
{
count0 = 0;
time0 ++;
if(time0==SetTime)
{
time0 = 0;
led = 0;
bulb = 0; //定时时间到,灯亮
}
}
}
/*---------------------------------------------
定时器1函数 :控制灯点亮时定时一段时间后熄灭 ---------------------------------------------*/
void Timer1() interrupt 3 {
TH1 = (65536-50000)/256; //50ms定时,定时20次,12M晶振约为1s
TL1 = (65536-50000)%256;
count1 ++;
if(20==count1)
{
count1 = 0;
time1 ++;
if(time1==SetTime)
{
time1 = 0;
led = 0;
bulb = 1; //定时时间到,灯灭
}
}
}
#ifndef _MAIN_H_
#define _MAIN_H_
#include
#include
#include
//宏定义
#define UCHAR unsigned char #define UINT unsigned int #define ULONG unsigned long
sbit led=P2^2; //LED指示灯,每接收到字节闪烁一次 p sbit bulb = P2^0;
int SetTime = 0; //定时时间,单位:s
int count0,count1,time0,time1; //定时器中断内部变量
UCHAR DisplayResult[5]; //全局显示结果数组
/**************************************************
函数功能:延时函数:延时nms
入口参数:y ms
***************************************************/
void Delaynms(UINT y)
{
UINT x;
for(;y>0;y--)
for(x=110;x>0;x--);
}
/**************************************************
函数功能:将长整型数据转换为字符数组形式以待显示
***************************************************/
void ResultToDisplay(int dat)
{
DisplayResult[4] = dat%10+0x30;
DisplayResult[3] = dat%100/10+0x30;
DisplayResult[2] = dat%1000/100+0x30;
DisplayResult[1] = dat%10000/1000+0x30;
DisplayResult[0] = dat%100000/10000+0x30; }
/*--------------------------------------------- 定时初始函数:初始值50000=50ms
定时器0
---------------------------------------------*/ void InitTimer0()
{
TMOD = 0x11; //定时器0,定时器1工作方式1
TH0 = (65536-50000)/256; //定时器0装初值
TL0 = (65536-50000)%256;
TH1 = (65536-50000)/256; //定时器1装初值
TL1 = (65536-50000)%256;
EA = 1; //开总中断
ET0 = 1; //开Timer0中断
ET1 = 1;
}
#endif
#ifndef _CC1101_H_
#define _CC1101_H_
#define WRITE_BURST 0x40 //连续写入 #define READ_SINGLE 0x80 //读
#define READ_BURST 0xC0 //连续读
#define BYTES_IN_RXFIFO 0x7F //接收缓冲区的有效字节数
#define CRC_OK 0x80 //CRC校验通过位标志
//***********************************
CC1100接口
************************************************* sbit GDO0 =P1^6;
sbit GDO2 =P1^5;
sbit MISO =P1^2;
sbit MOSI =P1^3;
sbit SCK =P1^4;
sbit CSN =P1^1;
//***********************************按键
******************************************************** //sbit KEY1 =P0^0;
//sbit KEY2 =P0^1;
//***********************************数码管位选
************************************************** //sbit led3=P2^0;
//sbit led2=P2^1;
//sbit led1=P2^2;
//sbit led0=P2^3;
//***********************************蜂鸣器
******************************************************* //sbit BELL=P3^4;
//***************更多功率参数设置可详细参考DATACC1100英文文档中第48-49页的参
数
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
******************
//UCHAR PaTabel[8] = {0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04}; //-30dBm 功率
最小
UCHAR PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60}; //0dBm
//UCHAR PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0}; //10dBm
功率最大
//*****************************************************************************
******************
void SpiInit(void);
void CpuInit(void);
void RESET_CC1100(void);
void POWER_UP_RESET_CC1100(void);
void halSpiWriteReg(UCHAR addr, UCHAR value); void halSpiWriteBurstReg(UCHAR addr, UCHAR *buffer, UCHAR count);
void halSpiStrobe(UCHAR strobe);
UCHAR halSpiReadReg(UCHAR addr);
void halSpiReadBurstReg(UCHAR addr, UCHAR *buffer, UCHAR count);
UCHAR halSpiReadStatus(UCHAR addr);
void halRfWriteRfSettings(void);
void halRfSendPacket(UCHAR *txBuffer, UCHAR size);
UCHAR halRfReceivePacket(UCHAR *rxBuffer, UCHAR *length);
//*****************************************************************************************
// CC1100 STROBE, CONTROL AND STATUS REGSITER
#define CCxxx0_IOCFG2 0x00 // GDO2 output pin configuration #define CCxxx0_IOCFG1 0x01 // GDO1 output pin configuration #define CCxxx0_IOCFG0 0x02 // GDO0 output pin configuration #define CCxxx0_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds #define CCxxx0_SYNC1 0x04 // Sync word, high UCHAR #define CCxxx0_SYNC0 0x05 // Sync word, low UCHAR #define CCxxx0_PKTLEN 0x06 // Packet length
#define CCxxx0_PKTCTRL1 0x07 // Packet automation control #define CCxxx0_PKTCTRL0 0x08 // Packet automation control #define CCxxx0_ADDR 0x09 // Device address
#define CCxxx0_CHANNR 0x0A // Channel number
#define CCxxx0_FSCTRL1 0x0B // Frequency synthesizer control #define CCxxx0_FSCTRL0 0x0C // Frequency synthesizer control #define CCxxx0_FREQ2 0x0D // Frequency control word, high UCHAR #define CCxxx0_FREQ1 0x0E // Frequency control word, middle UCHAR #define CCxxx0_FREQ0 0x0F // Frequency control word, low UCHAR #define CCxxx0_MDMCFG4 0x10 // Modem configuration #define CCxxx0_MDMCFG3 0x11 // Modem configuration #define CCxxx0_MDMCFG2 0x12 // Modem configuration #define CCxxx0_MDMCFG1 0x13 // Modem configuration #define CCxxx0_MDMCFG0 0x14 // Modem configuration #define CCxxx0_DEVIATN 0x15 // Modem deviation setting #define CCxxx0_MCSM2 0x16 // Main Radio Control State Machine configuration
#define CCxxx0_MCSM1 0x17 // Main Radio Control State Machine configuration
#define CCxxx0_MCSM0 0x18 // Main Radio Control State Machine configuration
#define CCxxx0_FOCCFG 0x19 // Frequency Offset Compensation configuration
#define CCxxx0_BSCFG 0x1A // Bit Synchronization configuration #define CCxxx0_AGCCTRL2 0x1B // AGC control
#define CCxxx0_AGCCTRL1 0x1C // AGC control
#define CCxxx0_AGCCTRL0 0x1D // AGC control
#define CCxxx0_WOREVT1 0x1E // High UCHAR Event 0 timeout #define CCxxx0_WOREVT0 0x1F // Low UCHAR Event 0 timeout #define CCxxx0_WORCTRL 0x20 // Wake On Radio control #define CCxxx0_FREND1 0x21 // Front end RX configuration #define CCxxx0_FREND0 0x22 // Front end TX configuration
#define CCxxx0_FSCAL3 0x23 // Frequency synthesizer calibration #define CCxxx0_FSCAL2 0x24 // Frequency synthesizer calibration #define CCxxx0_FSCAL1 0x25 // Frequency synthesizer calibration #define CCxxx0_FSCAL0 0x26 // Frequency synthesizer calibration #define CCxxx0_RCCTRL1 0x27 // RC oscillator configuration #define CCxxx0_RCCTRL0 0x28 // RC oscillator configuration #define CCxxx0_FSTEST 0x29 // Frequency synthesizer calibration control #define CCxxx0_PTEST 0x2A // Production test
#define CCxxx0_AGCTEST 0x2B // AGC test
#define CCxxx0_TEST2 0x2C // Various test settings
#define CCxxx0_TEST1 0x2D // Various test settings
#define CCxxx0_TEST0 0x2E // Various test settings
// Strobe commands
#define CCxxx0_SRES 0x30 // Reset chip.
#define CCxxx0_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1).
// If in RX/TX: Go to a wait state where only the synthesizer is
// running (for quick RX / TX turnaround). #define CCxxx0_SXOFF 0x32 // Turn off crystal oscillator. #define CCxxx0_SCAL 0x33 // Calibrate frequency synthesizer and turn it off
// (enables quick start).
#define CCxxx0_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and
// MCSM0.FS_AUTOCAL=1.
#define CCxxx0_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if
// MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled:
// Only go to TX if channel is clear. #define CCxxx0_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit
// Wake-On-Radio mode if applicable. #define CCxxx0_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer
#define CCxxx0_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio)
#define CCxxx0_SPWD 0x39 // Enter power down mode when CSn goes high.
#define CCxxx0_SFRX 0x3A // Flush the RX FIFO buffer. #define CCxxx0_SFTX 0x3B // Flush the TX FIFO buffer. #define CCxxx0_SWORRST 0x3C // Reset real time clock.
#define CCxxx0_SNOP 0x3D // No operation. May be used to pad strobe
commands to two
// UCHARs for simpler software.
#define CCxxx0_PARTNUM 0x30
#define CCxxx0_VERSION 0x31
#define CCxxx0_FREQEST 0x32
#define CCxxx0_LQI 0x33
#define CCxxx0_RSSI 0x34
#define CCxxx0_MARCSTATE 0x35
#define CCxxx0_WORTIME1 0x36
#define CCxxx0_WORTIME0 0x37
#define CCxxx0_PKTSTATUS 0x38
#define CCxxx0_VCO_VC_DAC 0x39
#define CCxxx0_TXBYTES 0x3A
#define CCxxx0_RXBYTES 0x3B
#define CCxxx0_PATABLE 0x3E
#define CCxxx0_TXFIFO 0x3F
#define CCxxx0_RXFIFO 0x3F
// RF_SETTINGS is a data structure which contains all relevant CCxxx0 registers
typedef struct S_RF_SETTINGS
{
UCHAR FSCTRL2; //自已加的
UCHAR FSCTRL1; // Frequency synthesizer control.
UCHAR FSCTRL0; // Frequency synthesizer control.
UCHAR FREQ2; // Frequency control word, high UCHAR.
UCHAR FREQ1; // Frequency control word, middle UCHAR.
UCHAR FREQ0; // Frequency control word, low UCHAR.
UCHAR MDMCFG4; // Modem configuration.
UCHAR MDMCFG3; // Modem configuration.
UCHAR MDMCFG2; // Modem configuration.
UCHAR MDMCFG1; // Modem configuration.
UCHAR MDMCFG0; // Modem configuration.
UCHAR CHANNR; // Channel number.
UCHAR DEVIATN; // Modem deviation setting (when FSK modulation is enabled).
UCHAR FREND1; // Front end RX configuration.
UCHAR FREND0; // Front end RX configuration.
UCHAR MCSM0; // Main Radio Control State Machine configuration.
UCHAR FOCCFG; // Frequency Offset Compensation Configuration.
UCHAR BSCFG; // Bit synchronization Configuration.
UCHAR AGCCTRL2; // AGC control.
UCHAR AGCCTRL1; // AGC control.
UCHAR AGCCTRL0; // AGC control.
UCHAR FSCAL3; // Frequency synthesizer calibration.
UCHAR FSCAL2; // Frequency synthesizer calibration.
UCHAR FSCAL1; // Frequency synthesizer calibration.
UCHAR FSCAL0; // Frequency synthesizer calibration.
UCHAR FSTEST; // Frequency synthesizer calibration control
UCHAR TEST2; // Various test settings.
UCHAR TEST1; // Various test settings.
UCHAR TEST0; // Various test settings.
UCHAR IOCFG2; // GDO2 output pin configuration
UCHAR IOCFG0; // GDO0 output pin configuration
UCHAR PKTCTRL1; // Packet automation control.
UCHAR PKTCTRL0; // Packet automation control.
UCHAR ADDR; // Device address.
UCHAR PKTLEN; // Packet length.
} RF_SETTINGS;
///////////////////////////////////////////////////////////////// const RF_SETTINGS rfSettings =
{
0x00,
0x08, // FSCTRL1 Frequency synthesizer control.
0x00, // FSCTRL0 Frequency synthesizer control.
0x10, // FREQ2 Frequency control word, high byte.
0xA7, // FREQ1 Frequency control word, middle byte.
0x62, // FREQ0 Frequency control word, low byte.
0x5B, // MDMCFG4 Modem configuration.
0xF8, // MDMCFG3 Modem configuration.
0x03, // MDMCFG2 Modem configuration.
0x22, // MDMCFG1 Modem configuration.
0xF8, // MDMCFG0 Modem configuration.
0x00, // CHANNR Channel number.
0x47, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
0xB6, // FREND1 Front end RX configuration.
0x10, // FREND0 Front end RX configuration.
0x18, // MCSM0 Main Radio Control State Machine configuration.
0x1D, // FOCCFG Frequency Offset Compensation Configuration.
0x1C, // BSCFG Bit synchronization Configuration.
0xC7, // AGCCTRL2 AGC control.
0x00, // AGCCTRL1 AGC control.
0xB2, // AGCCTRL0 AGC control.
0xEA, // FSCAL3 Frequency synthesizer calibration.
0x2A, // FSCAL2 Frequency synthesizer calibration.
0x00, // FSCAL1 Frequency synthesizer calibration.
0x11, // FSCAL0 Frequency synthesizer calibration.
0x59, // FSTEST Frequency synthesizer calibration.
0x81, // TEST2 Various test settings.
0x35, // TEST1 Various test settings.
0x09, // TEST0 Various test settings.
0x0B, // IOCFG2 GDO2 output pin configuration.
0x06, // IOCFG0D GDO0 output pin configuration. Refer to SmartRF?Studio User
Manual for detailed pseudo register explanation.
0x04, // PKTCTRL1 Packet automation control.
0x05, // PKTCTRL0 Packet automation control.
0x00, // ADDR Device address.
0x0c // PKTLEN Packet length.
};
//*****************************************************************************
************
//函数名:delay(unsigned int s)
//输入:时间
//输出:无
//功能描述:普通廷时,内部用
//*****************************************************************************
************
static void delay(unsigned int s)
{
unsigned int i;
for(i=0; i sync transmitted
while (!GDO0);
// Wait for GDO0 to be cleared -> end of packet
while (GDO0);
halSpiStrobe(CCxxx0_SFTX);
}
void setRxMode(void)
{
halSpiStrobe(CCxxx0_SRX); //进入接收状态
}
/*
// Bit masks corresponding to STATE[2:0] in the status byte returned on MISO
#define CCxx00_STATE_BM 0x70
#define CCxx00_FIFO_BYTES_AVAILABLE_BM 0x0F
#define CCxx00_STATE_TX_BM 0x20
#define CCxx00_STATE_TX_UNDERFLOW_BM 0x70
#define CCxx00_STATE_RX_BM 0x10
#define CCxx00_STATE_RX_OVERFLOW_BM 0x60
#define CCxx00_STATE_IDLE_BM 0x00
static UCHAR RfGetRxStatus(void) {
UCHAR temp, spiRxStatus1,spiRxStatus2;
UCHAR i=4;// 循环测试次数
temp = CCxxx0_SNOP|READ_SINGLE;//读寄存器命令
CSN = 0;
while (MISO);
SpiTxRxByte(temp);
spiRxStatus1 = SpiTxRxByte(0);
do
{
SpiTxRxByte(temp);
spiRxStatus2 = SpiTxRxByte(0);
if(spiRxStatus1 == spiRxStatus2)
{
if( (spiRxStatus1 & CCxx00_STATE_BM) == CCxx00_STATE_RX_OVERFLOW_BM)
{
halSpiStrobe(CCxxx0_SFRX);
return 0;
}
return 1;
}
spiRxStatus1=spiRxStatus2;
}
while(i--);
CSN = 1;
return 0;
}
*/
UCHAR halRfReceivePacket(UCHAR *rxBuffer, UCHAR *length)
{
UCHAR status[2];
UCHAR packetLength;
UCHAR i=(*length)*4; // 具体多少要根据datarate和length来决定
halSpiStrobe(CCxxx0_SRX); //进入接收状态
//delay(5);
//while (!GDO1);
//while (GDO1);
delay(2);
while (GDO0)
{
delay(2);
--i;
if(i<1)
return 0;
}
if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
{
packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
if (packetLength <= *length) //如果所要的有效数据长度小于等于接收到的数据包的长度
{
halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, packetLength); //读出所有接收到的数据
*length = packetLength; //把接收数据长度的修改为当前数据的长度
// Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); //读出CRC校验位
halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
return (status[1] & CRC_OK); //如果校验成功返回接收成功
}
else
{
*length = packetLength;
halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
return 0;
}
}
else
return 0;
}
#endif
#ifndef _LCD1602_H_
#define _LCD1602_H_
#include"main.h"
sbit lcd1602RSPort=P2^4; //寄存器选择位
sbit lcd1602RWPort=P2^5; //读写选择位
sbit lcd1602EPort=P2^6; //使能信号位
#define LCD1602DATAPORT P0 //数据端口
/**************************************************
函数功能:将模式设置指令或显示地址
或数据写入液晶模块 入口参数:Dat
State=0,写地址 State=1,写数据
***************************************************/
void WriteLcd1602(UCHAR State,UCHAR Dat) {
lcd1602RSPort=State; //根据规定,RS和R/W同时为低电平时,可以写入指令
lcd1602RWPort=0; //根据规定,RS为高电平且R/W为低电平时,可以写入数据
lcd1602EPort=0; //E置低电平(写指令时,就是让E从0到1发生正跳变,所以应先置"0"
_nop_();
_nop_(); //空操作两个机器周期,给硬件反应时间
LCD1602DATAPORT=Dat; //将数据送入数据端口,即写入指令或地址
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
lcd1602EPort=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
lcd1602EPort=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令
}
/**************************************************
函数功能:指定字符显示的实际地址
入口参数:Addr
Row=1,写入第一行 ,Row=2,写入第二行
***************************************************/
void WriteAddressLcd1602(UCHAR Row,UCHAR Addr) {
if(Row==1)
WriteLcd1602(0,0x80+Addr); //第一行位置的确定方法规定为"80H+地址码Addr"
if(Row==2)
WriteLcd1602(0,0x80+0x40+Addr); //第二行位置的确定方法规定为"80H+40H地址码Addr"
}
/**************************************************
函数功能:LCD写入字符串
***************************************************/
void WriteCharForLCD1602(UCHAR *ch) {
while(*ch!=0&&ch>0x20)
WriteLcd1602(1,*ch++);
}
/**************************************************
函数功能:对LCD的显示模式进行初始化设置
***************************************************/
void InitLcd1602()
{
Delaynms(15); //延时15ms,首次写指令时应给LCD一段较长的反应时间
WriteLcd1602(0,0x38); //3次写 设置模式
Delaynms(5);
WriteLcd1602(0,0x38);
Delaynms(5);
WriteLcd1602(0,0x38);
Delaynms(5);
WriteLcd1602(0,0x0c);
Delaynms(5);
WriteLcd1602(0,0x06);
Delaynms(5);
WriteLcd1602(0,0x01);
Delaynms(5);
}
/**************************************************
函数功能:清零LCD1602
***************************************************/
void ClearLcd1602()
{
WriteLcd1602(0,0x01);
Delaynms(5);
}
#endif