原理图:http://download.csdn.net/detail/cp1300/7496509
下面是STM8程序
CC1101.C
- #include "system.h"
- #include "delay.h"
- #include "CC1101.h"
-
- #define WRITE_BURST 0x40 //连续写入
- #define READ_SINGLE 0x80 //读
- #define WRITE_SINGLE 0x00 //写
- #define READ_BURST 0xC0
-
-
-
- #define BURST_READ_FIFO 0xff //突发读取RX FIFO
- #define BYTE_READ_FIFO 0xBF //单字节读取 RX FIFO
- #define BURST_WRITE_FIFO 0x7f //突发写TX FIFO
- #define BYTE_WRITE_FIFO 0x3f //单字节写 TX FIFO
-
- #define CC1101_DATA_LEN 64
-
-
-
- #define CC1101_CS_H() (GPIOA->ODR|=BIT3) //PA3=1
- #define CC1101_CS_L() (GPIOA->ODR&=~BIT3) //PA3=0
- #define CC1101_MOSI_H() (GPIOC->ODR|=BIT6) //PC6
- #define CC1101_MOSI_L() (GPIOC->ODR&=~BIT6) //PC6
- #define CC1101_SCLK_H() (GPIOC->ODR|=BIT5) //PC5
- #define CC1101_SCLK_L() (GPIOC->ODR&=~BIT5) //PC5
- #define CC1101_GetMISO() (GPIOC->IDR&BIT7) //PC7
-
-
- u8 CC1101_ReadWriteByte(u8 data)
- {
- u8 i;
- u8 temp = 0;
-
- for(i = 0;i < 8;i ++)
- {
- if(data & 0x80)
- {
- CC1101_MOSI_H();
- }
- else
- {
- CC1101_MOSI_L();
- }
- data <<= 1;nop;
- CC1101_SCLK_H();
- temp <<= 1;nop;
- if(CC1101_GetMISO()) temp ++;
- CC1101_SCLK_L();
- }
-
- return temp;
- }
-
-
- u8 CC1101_Command(CC1101_CMD_TYPE Cmd)
- {
- u8 status;
-
- CC1101_CS_L();
- while(CC1101_GetMISO());
- status = CC1101_ReadWriteByte((u8)Cmd);
- while(CC1101_GetMISO());
- CC1101_CS_H();
- return status;
- }
-
-
-
- u8 CC1101_ReadReg(CC1101_REG_TYPE RegAddr)
- {
- u8 data;
-
- CC1101_CS_L();
- CC1101_ReadWriteByte((u8)READ_SINGLE|RegAddr);
- data = CC1101_ReadWriteByte(0xff);
- CC1101_CS_H();
- return data;
- }
-
-
- u8 CC1101_WriteReg(CC1101_REG_TYPE RegAddr, u8 data)
- {
- u8 status;
-
- if(RegAddr > 0x80) return 0;
- CC1101_CS_L();
- while(CC1101_GetMISO());
- status = CC1101_ReadWriteByte((u8)WRITE_SINGLE|RegAddr);
- CC1101_ReadWriteByte(data);
- CC1101_CS_H();
- return status;
- }
-
-
- #include "LED.h"
- void CC1101_Init(u8 Addr)
- {
-
-
- GPIOx_Init(GPIOA, BIT3, OUT_PP_10M);
- CC1101_CS_H();
-
- GPIOx_Init(GPIOC, BIT5, OUT_PP_10M);
- CC1101_SCLK_H();
-
- GPIOx_Init(GPIOC, BIT6, OUT_PP_10M);
- CC1101_MOSI_H();
-
- GPIOx_Init(GPIOC, BIT7, IN_UP);
-
- CC1101_SCLK_L();
- CC1101_MOSI_L();
-
- GPIOx_Init(GPIOC, BIT3, IN_UP);
- GPIOx_Init(GPIOC, BIT4, IN_UP);
-
-
- CC1101_Command(CC1101_CMD_SRES);
- Delay_MS(10);
-
- while(CC1101_ReadReg(CC1101_REG_AGCTEST) != 0x3F)
- {
- LED_ON();
- Delay_MS(10);
- LED_OFF();
- Delay_MS(100);
- }
- LED_OFF();
-
- CC1101_WriteReg(CC1101_REG_IOCFG0,0x06);
- CC1101_WriteReg(CC1101_REG_IOCFG2,0x01);
-
- CC1101_WriteReg(CC1101_REG_FIFOTHR,0x0f);
- CC1101_WriteReg(CC1101_REG_SYNC1,0xD3);
- CC1101_WriteReg(CC1101_REG_SYNC0,0x91);
- CC1101_WriteReg(CC1101_REG_PKTLEN,CC1101_DATA_LEN);
- CC1101_WriteReg(CC1101_REG_PKTCTRL1,0x04);
- CC1101_WriteReg(CC1101_REG_PKTCTRL0,0x04);
- CC1101_WriteReg(CC1101_REG_ADDR,0x00);
- CC1101_WriteReg(CC1101_REG_CHANNR,0x00);
- CC1101_WriteReg(CC1101_REG_FSCTRL1,0x06);
- CC1101_WriteReg(CC1101_REG_FSCTRL0,0x00);
- CC1101_WriteReg(CC1101_REG_FREQ2,0x10);
- CC1101_WriteReg(CC1101_REG_FREQ1,0xb1);
- CC1101_WriteReg(CC1101_REG_FREQ0,0x3b);
-
-
- CC1101_WriteReg(CC1101_REG_MDMCFG4,0xF6);
- CC1101_WriteReg(CC1101_REG_MDMCFG3,0x83);
-
- CC1101_WriteReg(CC1101_REG_MDMCFG2,0x13);
- CC1101_WriteReg(CC1101_REG_MDMCFG1,0x22);
- CC1101_WriteReg(CC1101_REG_MDMCFG0,0xf8);
-
- CC1101_WriteReg(CC1101_REG_DEVIATN,0x15);
- CC1101_WriteReg(CC1101_REG_MCSM2,0x07);
- CC1101_WriteReg(CC1101_REG_MCSM1,0x30);
- CC1101_WriteReg(CC1101_REG_MCSM0,0x18);
- CC1101_WriteReg(CC1101_REG_FOCCFG,0x16);
- CC1101_WriteReg(CC1101_REG_BSCFG,0x6c);
- CC1101_WriteReg(CC1101_REG_AGCTRL2,0x03);
- CC1101_WriteReg(CC1101_REG_AGCTRL1,0x40);
- CC1101_WriteReg(CC1101_REG_AGCTRL0,0x91);
- CC1101_WriteReg(CC1101_REG_WOREVT1,0x87);
- CC1101_WriteReg(CC1101_REG_WOREVT0,0x6b);
- CC1101_WriteReg(CC1101_REG_WORCTRL,0xfb);
- CC1101_WriteReg(CC1101_REG_FREND1,0x56);
- CC1101_WriteReg(CC1101_REG_FREND0,0x10);
- CC1101_WriteReg(CC1101_REG_FSCAL3,0xe9);
- CC1101_WriteReg(CC1101_REG_FSCAL2,0x2a);
- CC1101_WriteReg(CC1101_REG_FSCAL1,0x00);
- CC1101_WriteReg(CC1101_REG_FSCAL0,0x1f);
- CC1101_WriteReg(CC1101_REG_RCCTRL1,0x41);
- CC1101_WriteReg(CC1101_REG_RCCTRL0,0x00);
- CC1101_WriteReg(CC1101_REG_FSTEST,0x59);
-
-
-
-
-
- Delay_MS(10);
- }
-
-
-
- void CC1101_WriteTxFIFO(u8 *pBuff,u8 len)
- {
- u16 i;
-
- CC1101_CS_L();
- CC1101_ReadWriteByte(BURST_WRITE_FIFO);
- for(i = 0;i < len;i ++)
- {
- CC1101_ReadWriteByte(pBuff[i]);
- }
- CC1101_CS_H();
- }
-
-
-
- void CC1101_ReadRxFIFO(u8 *pBuff,u8 len)
- {
- u16 i;
-
- CC1101_CS_L();
- CC1101_ReadWriteByte(BURST_READ_FIFO);
- for(i = 0;i < len;i ++)
- {
- pBuff[i] = CC1101_ReadWriteByte(0xff);
- }
- CC1101_CS_H();
- }
-
-
- void CC1101_RfDataSend(u8 *pBuff,u8 len)
- {
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SFTX);
- CC1101_WriteTxFIFO(pBuff, len);
- CC1101_Command(CC1101_CMD_STX);
-
- while(!CC1101_GDO0);
- while(CC1101_GDO0);
-
- CC1101_Command(CC1101_CMD_SIDLE);
- }
-
-
-
-
-
- void CC1101_RfDataSendPack(u8 *pBuff, u16 len)
- {
- u16 i,m,n,j;
-
- m = len / (CC1101_DATA_LEN-1);
- n = len % (CC1101_DATA_LEN-1);
-
-
- for(i = 0;i < m;i ++)
- {
- Delay_MS(1);
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SFTX);
-
- CC1101_CS_L();
- CC1101_ReadWriteByte(BURST_WRITE_FIFO);
-
- CC1101_ReadWriteByte(CC1101_DATA_LEN-1);
- for(j = 0;j < (CC1101_DATA_LEN-1);j ++)
- {
- CC1101_ReadWriteByte(*pBuff++);
- }
- CC1101_CS_H();
-
- CC1101_Command(CC1101_CMD_STX);
- while(!CC1101_GDO0);
- while(CC1101_GDO0);
- }
-
- if(n!=0)
- {
- Delay_MS(1);
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SFTX);
- CC1101_CS_L();
- CC1101_ReadWriteByte(BURST_WRITE_FIFO);
-
- CC1101_ReadWriteByte(n);
- for(j = 0;j < n;j ++)
- {
- CC1101_ReadWriteByte(*pBuff++);
- }
- CC1101_CS_H();
-
- CC1101_Command(CC1101_CMD_STX);
- while(!CC1101_GDO0);
- while(CC1101_GDO0);
- }
- CC1101_Command(CC1101_CMD_SIDLE);
- }
-
-
-
-
-
-
-
- u8 CC1101_GetStatus(void)
- {
- return CC1101_WriteReg(CC1101_REG_TEST2, 0x98);
- }
CC1101.H
- #ifndef _CC1101_H_
- #define _CC1101_H_
- #include "system.h"
-
-
- typedef enum
- {
- CC1101_CMD_SRES = 0x30,
- CC1101_CMD_SFSTXON = 0x31,
- CC1101_CMD_SXOFF = 0x32,
- CC1101_CMD_SCAL = 0x33,
- CC1101_CMD_SRX = 0x34,
- CC1101_CMD_STX = 0x35,
- CC1101_CMD_SIDLE = 0x36,
- CC1101_CMD_SAFC = 0x37,
- CC1101_CMD_SWOR = 0x38,
- CC1101_CMD_SPWD = 0x39,
- CC1101_CMD_SFRX = 0x3a,
- CC1101_CMD_SFTX = 0x3b,
- CC1101_CMD_SWORRST = 0x3c,
- CC1101_CMD_SNOP = 0x3d,
- }CC1101_CMD_TYPE;
-
-
-
- typedef enum
- {
-
- CC1101_REG_IOCFG2 = 0x00,
- CC1101_REG_IOCFG1 = 0x01,
- CC1101_REG_IOCFG0 = 0x02,
- CC1101_REG_FIFOTHR = 0x03,
- CC1101_REG_SYNC1 = 0x04,
- CC1101_REG_SYNC0 = 0x05,
- CC1101_REG_PKTLEN = 0x06,
- CC1101_REG_PKTCTRL1 = 0x07,
- CC1101_REG_PKTCTRL0 = 0x08,
- CC1101_REG_ADDR = 0x09,
- CC1101_REG_CHANNR = 0x0a,
- CC1101_REG_FSCTRL1 = 0x0b,
- CC1101_REG_FSCTRL0 = 0x0c,
- CC1101_REG_FREQ2 = 0x0d,
- CC1101_REG_FREQ1 = 0x0e,
- CC1101_REG_FREQ0 = 0x0f,
- CC1101_REG_MDMCFG4 = 0x10,
- CC1101_REG_MDMCFG3 = 0x11,
- CC1101_REG_MDMCFG2 = 0x12,
- CC1101_REG_MDMCFG1 = 0x13,
- CC1101_REG_MDMCFG0 = 0x14,
- CC1101_REG_DEVIATN = 0x15,
- CC1101_REG_MCSM2 = 0x16,
- CC1101_REG_MCSM1 = 0x17,
- CC1101_REG_MCSM0 = 0x18,
- CC1101_REG_FOCCFG = 0x19,
- CC1101_REG_BSCFG = 0x1a,
- CC1101_REG_AGCTRL2 = 0x1b,
- CC1101_REG_AGCTRL1 = 0x1c,
- CC1101_REG_AGCTRL0 = 0x1d,
- CC1101_REG_WOREVT1 = 0x1e,
- CC1101_REG_WOREVT0 = 0x1f,
- CC1101_REG_WORCTRL = 0x20,
- CC1101_REG_FREND1 = 0x21,
- CC1101_REG_FREND0 = 0x22,
- CC1101_REG_FSCAL3 = 0x23,
- CC1101_REG_FSCAL2 = 0x24,
- CC1101_REG_FSCAL1 = 0x25,
- CC1101_REG_FSCAL0 = 0x26,
- CC1101_REG_RCCTRL1 = 0x27,
- CC1101_REG_RCCTRL0 = 0x28,
- CC1101_REG_FSTEST = 0x29,
- CC1101_REG_PTEST = 0x2a,
- CC1101_REG_AGCTEST = 0x2b,
- CC1101_REG_TEST2 = 0x2c,
- CC1101_REG_TEST1 = 0x2d,
- CC1101_REG_TEST0 = 0x2e,
-
- CC1101_REG_PARTNUM = 0xf0,
- CC1101_REG_VERSION = 0xf1,
- CC1101_REG_FREQEST = 0xf2,
- CC1101_REG_LQI = 0xf3,
- CC1101_REG_RSSI = 0xf4,
- CC1101_REG_MARCSTATE = 0xf5,
- CC1101_REG_WORTIME1 = 0xf6,
- CC1101_REG_WORTIME0 = 0xf7,
- CC1101_REG_PKTSTATUS = 0xf8,
- CC1101_REG_VCOVCDAC = 0xf9,
- CC1101_REG_TXBYTES = 0xfA,
- CC1101_REG_RXBYTES = 0xfB,
-
- CC1101_REG_STATUS1 = 0xfc,
- CC1101_REG_STATUS0 = 0xfd,
-
- CC1101_REG_PATABLE0 = 0x40,
- CC1101_REG_PATABLE1 = 0x41,
- CC1101_REG_PATABLE2 = 0x42,
- CC1101_REG_PATABLE3 = 0x43,
- CC1101_REG_PATABLE4 = 0x44,
- CC1101_REG_PATABLE5 = 0x45,
- CC1101_REG_PATABLE6 = 0x46,
- CC1101_REG_PATABLE7 = 0x47,
- }CC1101_REG_TYPE;
-
- #define CC1101_GDO0 (GPIOC->IDR&BIT3) //PC3
- #define CC1101_GDO2 (GPIOC->IDR&BIT4) //PC4
-
- void CC1101_Init(u8 Addr);
- u8 CC1101_ReadReg(CC1101_REG_TYPE RegAddr);
- u8 CC1101_WriteReg(CC1101_REG_TYPE RegAddr, u8 data);
- u8 CC1101_Command(CC1101_CMD_TYPE Cmd);
-
- u8 CC1101_GetStatus(void);
-
- void CC1101_RfDataSend(u8 *pBuff,u8 len);
- void CC1101_ReadRxFIFO(u8 *pBuff,u8 len);
-
- void CC1101_RfDataSendPack(u8 *pBuff, u16 len);
-
- #endif //CC1101
MAIN.C收发透传
- #include "system.h"
- #include "uart1.h"
- #include "delay.h"
- #include "main.h"
- #include "LED.h"
- #include "cc1101.h"
-
- #define UART_BUFF_SIZE 256 //串口接收缓冲区大小
- u8 UartBuff[2][UART_BUFF_SIZE];
- u16 UartLen1 = 0;
- u16 UartLen2 = 0;
-
-
- #define RF_BUFF_SIZE 64 //CC1101缓冲区大小
- u8 RfBuff[RF_BUFF_SIZE];
- u8 RfLen = 0;
-
-
- u8 UART_RxNum = 0;
-
-
- void main(void)
- {
- u8 LastNum = 0;
- u32 delay = 0;
-
- SYS_ClockInit();
-
- LED_Init();
-
- UART1_Init(115200, ENABLE);
- UART1_SetRxBuff(UartBuff[UART_RxNum],UART_BUFF_SIZE);
- CC1101_Init(0Xaa);
-
-
-
- CC1101_WriteReg(CC1101_REG_PATABLE0,0x00);
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SRX);
-
-
- SYS_EnableInterrupt();
- while(1)
- {
-
-
- if(++delay == 120000)
- {
- delay = 0;
- UartLen1 = UART1_GetRxCnt();
- if(UartLen1>0)
- {
- SYS_DisableInterrupt();
- LED_ON();
- LastNum = UART_RxNum;
- UART_RxNum = (~UART_RxNum)&0x01;
- UART1_SetRxBuff(UartBuff[UART_RxNum],UART_BUFF_SIZE);
-
- SYS_EnableInterrupt();
-
- CC1101_WriteReg(CC1101_REG_PATABLE0,0xc0);
-
- CC1101_RfDataSendPack(UartBuff[LastNum], UartLen1);
-
- CC1101_WriteReg(CC1101_REG_PATABLE0,0);
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SRX);
- LED_OFF();
- }
- }
-
-
- if(CC1101_GDO2)
- {
- LED_ON();
- RfLen = CC1101_ReadReg(CC1101_REG_RXBYTES);
- CC1101_ReadRxFIFO(RfBuff,RfLen);
- UART1_SendData(&RfBuff[1], RfBuff[0]);
- CC1101_Command(CC1101_CMD_SIDLE);
- CC1101_Command(CC1101_CMD_SFRX);
- CC1101_Command(CC1101_CMD_SRX);
- LED_OFF();
- }
-
- }
- }
实现透传,调试中发现问题就是接收的时候必须关闭发射增益,否则无法接收,应该是内部发送接收天线进行了切换.
实物图