OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4724|回复: 1

STM8S103K3与CC1101 硬件SPI控制,不成功

[复制链接]

1

主题

2

帖子

0

精华

新手入门

积分
26
金钱
26
注册时间
2015-9-7
在线时间
0 小时
发表于 2015-9-7 22:03:24 | 显示全部楼层 |阅读模式
5金钱
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]


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165353
金钱
165353
注册时间
2010-12-1
在线时间
2108 小时
发表于 2015-9-7 22:19:56 | 显示全部楼层
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-25 16:25

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表