main函数就是:
void main( void )
{
while(!CC1100_Init());
while(1)
{
asm("NOP");
}
}
CC1101一直无反应,写完寄存器之后去读,读不到数据
是STM8S103K3的 SPI配置错了??还是 SPI读写函数错了?还是读写CC1100寄存器函数错了?
[mw_shl_code=c,true]#include "Include\RF_CC1101.h"
#include "Include\RF_CC1101_REG.h"
/********************************************************************************************/
//SPI系列函数
//void SPI_ForCC1100_Init(void)
//void SPI_ForCC1100_Send_Byte(unsigned char byte)
//unsigned char SPI_ForCC1100_Rec_Byte(void)
/********************************************************************************************/
#define SPI_ForCC1100_CS_HIGH() (GPIO_WriteHigh(GPIOD,GPIO_PIN_0)) //定义CS脚高
#define SPI_ForCC1100_CS_LOW() (GPIO_WriteLow(GPIOD,GPIO_PIN_0)) //定义CS脚低
#define NOPByte 0x00 //CC1100掩码,用于SPI读数据
//SPI初始化
void SPI_ForCC1100_Init(void)
{
/* 配置CS管脚:PD0 */
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_FAST);
/* CS默认高 */
SPI_ForCC1100_CS_HIGH();
/* SPI寄存器复位 */
SPI_DeInit();
/* 关闭SPI */
SPI_Cmd(DISABLE);
/* 初始化SPI */
//高位前---256分频---主模式---时钟默认低---第一边沿采样---双线全双工---NSS软件控制---多项式
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_256, SPI_MODE_MASTER,\
SPI_CLOCKPOLARITY_LOW, SPI_CLOCKPHASE_1EDGE, \
SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
/* CRC校验关闭 */
SPI_CalculateCRCCmd(DISABLE);
/* 关闭SPI中断 */
SPI_ITConfig(SPI_IT_WKUP,DISABLE);
SPI_ITConfig(SPI_IT_OVR,DISABLE);
SPI_ITConfig(SPI_IT_MODF,DISABLE);
SPI_ITConfig(SPI_IT_CRCERR,DISABLE);
SPI_ITConfig(SPI_IT_TXE,DISABLE);
SPI_ITConfig(SPI_IT_RXNE,DISABLE);
SPI_ITConfig(SPI_IT_ERR,DISABLE);
/* 使能SPI */
SPI_Cmd(ENABLE);
}
//SPI 发送数据
void SPI_ForCC1100_Send_Byte(unsigned char byte)
{
/* Loop while DR register in not emplty */
while (SPI_GetFlagStatus( SPI_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_SendData(byte);
/* Wait to receive a byte */
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
unsigned char buf=SPI_ReceiveData();
}
//SPI接收数据
unsigned char SPI_ForCC1100_Rec_Byte(void)
{
/* Loop while DR register in not emplty */
while (SPI_GetFlagStatus( SPI_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_SendData(NOPByte);
/* Wait to receive a byte */
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_ReceiveData();
}
/********************************************************************************************/
//SPI系列函数
/********************************************************************************************/
/***************************************
****函数功能:延时
****入口参数:unsigned char : 0-256
****出口参数:无
****函数备注:
***************************************/
void halWait(unsigned char timeout)
{
do {
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
} while (--timeout);
}
/***************************************
****函数功能:CC1100复位
****入口参数:
****出口参数:
****函数备注:
***************************************/
void CC1100_Reset(void)
{
CC1100_Write_Cmd(CC1101_SRES);//写入复位命令
}
/***************************************
****函数功能:初始化RF芯片
****入口参数:
****出口参数:
****函数备注:
***************************************/
bool CC1100_Init(void)
{
SPI_ForCC1100_Init(); //设置SPI
/* 配置GDO0管脚:PA3 */
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT);
/* 配置GDO2管脚: */
/* CC1100重启 */
CC1100_Reset();
halWait(10);
//检测通信
// Rf settings for CC1101
CC1100_Write_SingleReg(CC1101_IOCFG0,0x06); //GDO0 Output Pin Configuration
CC1100_Write_SingleReg(CC1101_FIFOTHR,0x47); //RX FIFO and TX FIFO Thresholds
CC1100_Write_SingleReg(CC1101_SYNC1,0x7A); //Sync Word, High Byte
CC1100_Write_SingleReg(CC1101_SYNC0,0x0E); //Sync Word, Low Byte
CC1100_Write_SingleReg(CC1101_PKTLEN,0x14); //Packet Length
CC1100_Write_SingleReg(CC1101_PKTCTRL0,0x05);//Packet Automation Control
CC1100_Write_SingleReg(CC1101_FSCTRL1,0x06); //Frequency Synthesizer Control
CC1100_Write_SingleReg(CC1101_FREQ2,0x21); //Frequency Control Word, High Byte
CC1100_Write_SingleReg(CC1101_FREQ1,0x62); //Frequency Control Word, Middle Byte
CC1100_Write_SingleReg(CC1101_FREQ0,0x76); //Frequency Control Word, Low Byte
CC1100_Write_SingleReg(CC1101_MDMCFG4,0xCA); //Modem Configuration
CC1100_Write_SingleReg(CC1101_MDMCFG3,0xF8); //Modem Configuration
CC1100_Write_SingleReg(CC1101_MDMCFG2,0x16); //Modem Configuration
CC1100_Write_SingleReg(CC1101_DEVIATN,0x40); //Modem Deviation Setting
CC1100_Write_SingleReg(CC1101_MCSM0,0x18); //Main Radio Control State Machine Configuration
CC1100_Write_SingleReg(CC1101_FOCCFG,0x16); //Frequency Offset Compensation Configuration
CC1100_Write_SingleReg(CC1101_AGCCTRL2,0x43);//AGC Control
CC1100_Write_SingleReg(CC1101_AGCCTRL1,0x49);//AGC Control
CC1100_Write_SingleReg(CC1101_WORCTRL,0xFB); //Wake On Radio Control
CC1100_Write_SingleReg(CC1101_FSCAL3,0xE9); //Frequency Synthesizer Calibration
CC1100_Write_SingleReg(CC1101_FSCAL2,0x2A); //Frequency Synthesizer Calibration
CC1100_Write_SingleReg(CC1101_FSCAL1,0x00); //Frequency Synthesizer Calibration
CC1100_Write_SingleReg(CC1101_FSCAL0,0x1F); //Frequency Synthesizer Calibration
CC1100_Write_SingleReg(CC1101_TEST2,0x81); //Various Test Settings
CC1100_Write_SingleReg(CC1101_TEST1,0x35); //Various Test Settings
CC1100_Write_SingleReg(CC1101_TEST0,0x09); //Various Test Settings
/* CC1100是否就绪 */
if(CC1100_Read_SingleReg(CC1101_IOCFG0)==0x06)
return TRUE;
else
return FALSE;
}
/***************************************
****函数功能:读单个寄存器
****入口参数:
****出口参数:
****函数备注:
***************************************/
unsigned char CC1100_Read_SingleReg(unsigned char addr)
{
unsigned char value;
SPI_ForCC1100_CS_LOW();
SPI_ForCC1100_Send_Byte(addr|READ_SINGLE);
halWait(10);
value=SPI_ForCC1100_Rec_Byte();
SPI_ForCC1100_CS_HIGH();
return (value);
}
/***************************************
****函数功能:写入RF芯片命令
****入口参数:
****出口参数:
****函数备注:
***************************************/
void CC1100_Write_Cmd(unsigned char cmd)
{
SPI_ForCC1100_CS_LOW();
SPI_ForCC1100_Send_Byte(cmd);
halWait(10);
SPI_ForCC1100_CS_HIGH();
}
/***************************************
****函数功能:写单个寄存器
****入口参数:
****出口参数:
****函数备注:
***************************************/
void CC1100_Write_SingleReg(unsigned char addr, unsigned char value)
{
SPI_ForCC1100_CS_LOW();
SPI_ForCC1100_Send_Byte(addr);//写地址
halWait(10);
SPI_ForCC1100_Send_Byte(value);//写入配置
halWait(10);
SPI_ForCC1100_CS_HIGH();
}
[/mw_shl_code]
|