STM32驱动CC1101的程序,保证可用
发送程序部分预览(完整版请下载本帖最后的附件):
/*
** CC1101 433MHz无线模块相互通信
** 2014-11-16
*/
#include "stm32f10x.h"
#include "cc1101.h"
#include "led.h"
/*************NVIC控制器配置***************************/
//组号 抢占位数 子优先级数量
// 组0 0 4
// 组1 1 3
// 组2 2 2
// 组3 3 1
// 组4 4 0
#define NVIC_PRIORITY_GROUP_0 (7 - 0)
#define NVIC_PRIORITY_GROUP_1 (7 - 1)
#define NVIC_PRIORITY_GROUP_2 (7 - 2)
#define NVIC_PRIORITY_GROUP_3 (7 - 3)
#define NVIC_PRIORITY_GROUP_4 (7 - 4)
#define NVIC_PRIORITY_GROUP (NVIC_PRIORITY_GROUP_2) /* 设置为组2 */
//#include "RF_Send.h"
//#include "task.h"
//***************更多功率参数设置可详细参考DATACC1100英文文档中第48-49页的参数表******************
//INT8U PaTabel[8] = {0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04 ,0x04}; //-30dBm 功率最小
//INT8U PaTabel[8] = {0x17,0x17 ,0x17 ,0x17 ,0x17 ,0x17,0x17 ,0x17}; //-20dBm
//INT8U PaTabel[8] = {0x1D,0x1D ,0x1D ,0x1D ,0x1D ,0x1D,0x1D ,0x1D}; //-15dBm
//INT8U PaTabel[8] = {0x26,0x26 ,0x26 ,0x26 ,0x26 ,0x26,0x26 ,0x26}; //-10dBm
//INT8U PaTabel[8] = {0x37,0x37 ,0x37 ,0x37 ,0x37 ,0x37,0x37 ,0x37}; //-6dBm
//INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};//0dBm
//INT8U PaTabel[8] = {0x86,0x86 ,0x86 ,0x86 ,0x86 ,0x86,0x86 ,0x86}; //5dBm
const u8 PaTabel[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};//12dBm
u8 RF_Read_Buff[64];//接收缓冲区
_RF_Sta RF_Sta;//RF状态
/////////////////////////////////////////////////////////////////
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.
// 0x0F, // MCSM1 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.
RFDATLEN // PKTLEN Packet length.
};
//*****************************************************************************************
//函数名:delayus(unsigned int s)
//输入:时间
//输出:无
//功能描述:普通廷时,内部用
//*****************************************************************************************
void delayus(u16 timeout)
{
u8 i;
do
{
for(i=0; i<20; i++);
}
while (--timeout);
}
/*****************************************************************************************/
//函数名:CC1101_SpiInit()
//输入:无
//输出:无
//功能描述:SPI初始化程序
/*****************************************************************************************/
void CC1101_SpiInit(void)
{
RCC->APB2ENR|=1<<4; //使能PORTC时钟
GPIOC->CRL&=0Xff000000;
GPIOC->CRL|=0X00833388;
GPIOC->ODR|=0x00000023;
}
void Open_GD0_Interrupt(void)
{
uint32_t priority;
/* 使能io复用时钟 */
RCC->APB2ENR |= 1 << 0;
/* 配置为输外部中断5在PC5上 */
AFIO->EXTICR[1] &= ~(0xF << 4);
AFIO->EXTICR[1] |= (2 << 4);
/* 开启外部中断5 */
EXTI->IMR |= (1 << 5);
/* 上升沿触发 */
EXTI->RTSR |= (1 << 5);
EXTI->FTSR &= ~(1 << 5);
NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUP); /* 设置为组2 */
priority = NVIC_EncodePriority (NVIC_PRIORITY_GROUP, 2, 3);
NVIC_SetPriority(EXTI9_5_IRQn,priority); /* EXTIx_IRQn 在stm32f10x.h中有定义 */
NVIC_EnableIRQ(EXTI9_5_IRQn);
}
//*****************************************************************************************
//函数名:SpisendByte(INT8U dat)
//输入:发送的数据
//输出:无
//功能描述:SPI发送一个字节
//*****************************************************************************************
u8 SpiTxRxByte(u8 dat)
{
u8 i,j,temp= 0;
SCK = 0;
for(i=0; i<8; i++)
{
if(dat & 0x80)MOSI = 1;
else MOSI = 0;
j++;j++;
dat <<= 1;
j++;j++;
SCK = 1;
j++;j++;
temp <<= 1;
if(MISO)temp++;
SCK = 0;
j++;j++;
}
return temp;
}
//*****************************************************************************************
//函数名:void RESET_CC1100(void)
//输入:无
//输出:无
//功能描述:复位CC1100
//*****************************************************************************************
u8 RESET_CC1100(void)
{
u16 n=0;
CSN = 0;
while(MISO)
{
if(++n>300)return 0;
delayus(5);
}
n=0;
SpiTxRxByte(CCxxx0_SRES); //写入复位命令
while(MISO)
{
if(++n>300)return 0;
delayus(5);
}
CSN = 1;
return 1;
}
//*****************************************************************************************
//函数名:void POWER_UP_RESET_CC1100(void)
//输入:无
//输出:无
//功能描述:上电复位CC1100
//*****************************************************************************************
u8 POWER_UP_RESET_CC1100(void)
{
CSN = 1;
delayus(10);
CSN = 0;
delayus(10);
CSN = 1;
delayus(100);
if(RESET_CC1100())return 1; //复位CC1100
return 0;
}
//*****************************************************************************************
//函数名:void halSpiWriteReg(INT8U addr, INT8U value)
//输入:地址和配置字
//输出:无
//功能描述:SPI写寄存器
//*****************************************************************************************
void halSpiWriteReg(u8 addr, u8 value)
{
u16 n=0;
CSN = 0;
while(MISO)
{
if(++n>300)break;
delayus(5);
}
SpiTxRxByte(addr); //写地址
SpiTxRxByte(value); //写入配置
CSN = 1;
}
//*****************************************************************************************
//函数名:void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
//输入:地址,写入缓冲区,写入个数
//输出:无
//功能描述:SPI连续写配置寄存器
//*****************************************************************************************
void halSpiWriteBurstReg(u8 addr, u8 *buffer, u8 count)
{
u8 i, temp;
temp = addr | WRITE_BURST;
CSN = 0;
while(MISO)
{
if(++i>250)break;
delayus(5);
}
SpiTxRxByte(temp);
for (i = 0; i < count; i++)
{
SpiTxRxByte(buffer[i]);
}
CSN = 1;
}
//*****************************************************************************************
//函数名:void halSpiStrobe(INT8U strobe)
//输入:命令
//输出:无
//功能描述:SPI写命令
//*****************************************************************************************
void halSpiStrobe(u8 strobe)
{
u8 n=0;
CSN = 0;
while(MISO)
{
if(++n>250)break;
delayus(5);
}
SpiTxRxByte(strobe); //写入命令
CSN = 1;
}
//*****************************************************************************************
//函数名:INT8U halSpiReadReg(INT8U addr)
//输入:地址
//输出:该寄存器的配置字
//功能描述:SPI读寄存器
//*****************************************************************************************
u8 halSpiReadReg(u8 addr)
{
u8 temp, value=0;
temp = addr|READ_SINGLE;//读寄存器命令
CSN = 0;
while(MISO)
{
if(++value>250)break;
delayus(5);
}
SpiTxRxByte(temp);
value = SpiTxRxByte(0);
CSN = 1;
return value;
}
//*****************************************************************************************
//函数名:void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
//输入:地址,读出数据后暂存的缓冲区,读出配置个数
//输出:无
//功能描述:SPI连续写配置寄存器
//*****************************************************************************************
void halSpiReadBurstReg(u8 addr, u8 *buffer, u8 count)
{
u8 i=0,temp;
temp = addr | READ_BURST; //写入要读的配置寄存器地址和读命令
CSN = 0;
while(MISO)
{
if(++i>250)break;
delayus(5);
}
SpiTxRxByte(temp);
for (i = 0; i < count; i++)
{
buffer[i] = SpiTxRxByte(0);
}
CSN = 1;
}
//*****************************************************************************************
//函数名:INT8U halSpiReadReg(INT8U addr)
//输入:地址
//输出:该状态寄存器当前值
//功能描述:SPI读状态寄存器
//*****************************************************************************************
u8 halSpiReadStatus(u8 addr)
{
u8 value=0,temp;
temp = addr | READ_BURST; //写入要读的状态寄存器的地址同时写入读命令
CSN = 0;
while(MISO)
{
if(++value>250)break;
delayus(5);
}
SpiTxRxByte(temp);
value = SpiTxRxByte(0);
CSN = 1;
return value;
}
//*****************************************************************************************
//函数名:void CC1101_Set_PKTLEN(u8 len)
//输入:无
//输出:无
//功能描述:配置CC1101数据长度
//*****************************************************************************************
void CC1101_Set_PKTLEN(u8 len)//配置CC1101数据长度
{
halSpiWriteReg(CCxxx0_PKTLEN,len);
}
//*****************************************************************************************
//函数名:void halRfWriteRfSettings(RF_SETTINGS *pRfSettings)
//输入:无
//输出:无
//功能描述:配置CC1100的寄存器
//*****************************************************************************************
void halRfWriteRfSettings(void)
{
halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL2);//自已加的
// Write register settings
halSpiWriteReg(CCxxx0_FSCTRL1, rfSettings.FSCTRL1);
halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL0);
halSpiWriteReg(CCxxx0_FREQ2, rfSettings.FREQ2);
halSpiWriteReg(CCxxx0_FREQ1, rfSettings.FREQ1);
halSpiWriteReg(CCxxx0_FREQ0, rfSettings.FREQ0);
halSpiWriteReg(CCxxx0_MDMCFG4, rfSettings.MDMCFG4);
halSpiWriteReg(CCxxx0_MDMCFG3, rfSettings.MDMCFG3);
halSpiWriteReg(CCxxx0_MDMCFG2, rfSettings.MDMCFG2);
halSpiWriteReg(CCxxx0_MDMCFG1, rfSettings.MDMCFG1);
halSpiWriteReg(CCxxx0_MDMCFG0, rfSettings.MDMCFG0);
halSpiWriteReg(CCxxx0_CHANNR, rfSettings.CHANNR);
halSpiWriteReg(CCxxx0_DEVIATN, rfSettings.DEVIATN);
halSpiWriteReg(CCxxx0_FREND1, rfSettings.FREND1);
halSpiWriteReg(CCxxx0_FREND0, rfSettings.FREND0);
halSpiWriteReg(CCxxx0_MCSM0 , rfSettings.MCSM0 );
halSpiWriteReg(CCxxx0_FOCCFG, rfSettings.FOCCFG);
halSpiWriteReg(CCxxx0_BSCFG, rfSettings.BSCFG);
halSpiWriteReg(CCxxx0_AGCCTRL2, rfSettings.AGCCTRL2);
halSpiWriteReg(CCxxx0_AGCCTRL1, rfSettings.AGCCTRL1);
halSpiWriteReg(CCxxx0_AGCCTRL0, rfSettings.AGCCTRL0);
halSpiWriteReg(CCxxx0_FSCAL3, rfSettings.FSCAL3);
halSpiWriteReg(CCxxx0_FSCAL2, rfSettings.FSCAL2);
halSpiWriteReg(CCxxx0_FSCAL1, rfSettings.FSCAL1);
halSpiWriteReg(CCxxx0_FSCAL0, rfSettings.FSCAL0);
halSpiWriteReg(CCxxx0_FSTEST, rfSettings.FSTEST);
halSpiWriteReg(CCxxx0_TEST2, rfSettings.TEST2);
halSpiWriteReg(CCxxx0_TEST1, rfSettings.TEST1);
halSpiWriteReg(CCxxx0_TEST0, rfSettings.TEST0);
halSpiWriteReg(CCxxx0_IOCFG2, rfSettings.IOCFG2);
halSpiWriteReg(CCxxx0_IOCFG0, rfSettings.IOCFG0);
halSpiWriteReg(CCxxx0_PKTCTRL1, rfSettings.PKTCTRL1);
halSpiWriteReg(CCxxx0_PKTCTRL0, rfSettings.PKTCTRL0);
halSpiWriteReg(CCxxx0_ADDR, rfSettings.ADDR);
halSpiWriteReg(CCxxx0_PKTLEN, rfSettings.PKTLEN);
//halSpiWriteReg(CCxxx0_IOCFG0 , rfSettings.PKTLEN);
}
//******************************************************************************************
//函数名:void Set_TxMode(void)
//输入:无
//输出:无
//功能描述:设置CC1100为发射模式
//*****************************************************************************************
void Set_TxMode(void)
{
RF_Sta.mode=RF_TXMODE;
RF_Sta.Busy=BUSY;//置忙
GDO0_INTON;//开中断
halSpiStrobe(CCxxx0_SIDLE);//强制进入空闲模式
halSpiStrobe(CCxxx0_STX); //进入发送模式发送数据
}
//*****************************************************************************************
//函数名:void Set_RxMode(void)
//输入:无
//输出:无
//功能描述:设置CC1100为发射接收模式
//*****************************************************************************************
void Set_RxMode(void)
{
RF_Sta.mode=RF_RXMODE;
halSpiStrobe(CCxxx0_SFRX);//清洗接收缓冲区
halSpiStrobe(CCxxx0_SIDLE);//强制进入空闲模式
halSpiStrobe(CCxxx0_SRX);//进入接收模式
GDO0_INTON;//开中断
}
//*****************************************************************************************
//函数名:void halRfSendPacket(INT8U *txBuffer, INT8U size)
//输入:发送的缓冲区,发送数据个数
//输出:OK 已发送,其它,错误标志
//功能描述:CC1100发送一组数据
//*****************************************************************************************
u8 halRfSendPacket(u8 *txBuffer, u8 size)
{
u16 value;
halSpiWriteReg(CCxxx0_TXFIFO, size);
halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size); //写入要发送的数据
Set_TxMode();//进入发送模式发送数据
RF_Sta.Busy=BUSY;
while(RF_Sta.Busy&BUSY)
{
if(++value>350)return ERR;
delayus(5);
}
return OK;
}
|
|