📄 cc1100.c.bak
字号:
/*
v2.5 halSpiWriteReg(CCxxx0_MCSM2, 0x08);
halSpiWriteReg(CCxxx0_PKTLEN, 0x1F);
v2.6 测试发送:Dly1mS(100); 删去
*/
#include "cc1100_reg.h"
unsigned char paTable[8] = {0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0 ,0xC0};
//unsigned char txBuffer[17] = {16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
unsigned char rxBuffer[32];
unsigned char CC1100_STATUS;
unsigned char txBuffer_CCxx00[4] = {3, 1, 2,3}; //发射缓冲,其中 3 为发送字符个数,接收缓冲收到的只是后面的三个数
unsigned char rxBuffer_CCxx00[2];
unsigned char rd_pin;
//unsigned char packetLength;
unsigned char rd_reg;
unsigned char INTERRUPT_STATE=0x00;
unsigned char SEND_DONE=0x00;
//void GDO0_INPUT() {TOSH_MAKE_CC1100_GDO0_INPUT();}
//void GDO0_OUTPUT() {TOSH_MAKE_CC1100_GDO0_OUTPUT();}
//void SET_GDO0() { TOSH_SET_CC1100_GDO0_PIN(); } //void CLEAR_GDO0() { TOSH_CLR_CC1100_GDO0_PIN(); }
void SET_CLOCK() { TOSH_SET_CC1100_SCLK_PIN(); } void CLEAR_CLOCK() { TOSH_CLR_CC1100_SCLK_PIN(); }
void SET_IN() { TOSH_SET_CC1100_IN_PIN(); } void CLEAR_IN() { TOSH_CLR_CC1100_IN_PIN(); } void SET_CSN() { TOSH_SET_CC1100_CSN_PIN(); } void CLEAR_CSN() { TOSH_CLR_CC1100_CSN_PIN(); }
char GET_OUT() { return TOSH_READ_CC1100_OUT_PIN(); }
char GET_GDO0() { return TOSH_READ_CC1100_GDO0_PIN(); }
//char GET_GDO2() { return TOSH_READ_CC1100_GDO2_PIN(); }
extern uint8_t CC1100_sendDone();
extern uint8_t CC1100_receive(uint8_t *msg);
/******************************************/
uint8_t reg_sfrx;
void Dly10us()
{
unsigned char i;
for(i=0;i<10;i++)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
void Dly1mS(unsigned int l)
{
unsigned int i,j;
while(l--)
for(i=0;i<54;i++)
for(j=0;j<100;j++);
}
/******************************************/
unsigned char SPI_write(unsigned char value)
{
unsigned char i,spi_status,rd_pin;
for(i=0;i<8;i++)
{
if((value&0x80)==0x80)
{
Dly10us();
SET_IN(); // SI=1
}
else
{
CLEAR_IN(); // SI=0
Dly10us();Dly10us();
}
Dly10us();Dly10us();Dly10us();
SET_CLOCK(); //SCLK=1;
Dly10us();Dly10us();Dly10us();Dly10us();Dly10us();
value <<=1;
spi_status<<=1;
rd_pin=GET_OUT();
if((rd_pin&0x01)==0x01)
spi_status=spi_status|0x01;
else
spi_status=spi_status&0xfe;
CLEAR_CLOCK();
Dly10us();Dly10us();
}
return(spi_status);
}
unsigned char SPI_read()
{
unsigned char i,value,rd_pin;
for(i=0;i<8;i++)
{
SET_CLOCK(); //P_SCLK=1;
value <<=1;Dly10us();Dly10us();Dly10us();Dly10us();Dly10us();
rd_pin=GET_OUT();
if((rd_pin&0x01)==0x01) value|=0x01;
else value&=0xfe;
Dly10us();Dly10us();
CLEAR_CLOCK(); //SCLK=0 //P_SCLK=0;
Dly10us();Dly10us();Dly10us();Dly10us();Dly10us();
}
return value;
}
/******************************************/
unsigned char spiGetRxTxStatus()
{
unsigned char RxTxstatus;
CLEAR_CSN(); // CSn=0
while(GET_OUT());
CLEAR_CLOCK();
RxTxstatus=SPI_write(CCxxx0_SNOP);
CLEAR_CLOCK(); //SCLK=0
CLEAR_IN(); // SI=0
SET_CSN(); //CSn=1
return(RxTxstatus);
}
/******************************************/
void halSpiWriteReg(unsigned char addr, unsigned char value)
{
CLEAR_CSN();
while(GET_OUT());
CLEAR_CLOCK();
addr&=0x7F;
SPI_write(addr);
SPI_write(value);
CLEAR_CLOCK();
CLEAR_IN();
SET_CSN();
}
/******************************************/
void halSpiStrobe(unsigned char strobe)
{
CLEAR_CSN(); // CSn=0
CLEAR_CLOCK();
while(GET_OUT());
SPI_write(strobe);
CLEAR_CLOCK();
CLEAR_IN();
SET_CSN();
}
/*******************************************/
void halSpiWriteBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count)
{
unsigned char i,j,value;
CLEAR_CLOCK();
Dly10us();Dly10us();
CLEAR_CSN(); // CSn=0
addr|=WRITE_BURST;
SPI_write(addr);
CLEAR_IN();
for(j=0;j<count;j++)
{
value=buffer[j];
SPI_write(value);
CLEAR_IN();
for(i=0;i<100;i++);
}
SET_CSN();
}
/******************************/
void CC1100_POR()
{
unsigned int i;
halSpiStrobe(CCxxx0_SIDLE);
SET_CSN();
for(i=0;i<100;i++);
CLEAR_CSN();
for(i=0;i<100;i++);
SET_CSN();
for(i=0;i<2000;i++);
CLEAR_CSN();
while(GET_OUT());
halSpiStrobe(CCxxx0_SRES);
CLEAR_CLOCK();
CLEAR_IN();
SET_CSN();
}
/*******************************************/
unsigned char halRfSendPacket(unsigned char *txBuffer, unsigned char size)
{
halSpiStrobe(CCxxx0_SFTX);
Dly1mS(15);
//Dly1mS(25);
halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);
Dly1mS(15);
//Dly1mS(25);
halSpiStrobe(CCxxx0_STX);
while(spiGetRxTxStatus()&0x70)
{
Dly1mS(1);
//Dly1mS(2);
}
SEND_DONE=0x01;
return 0;
}// halRfSendPacket
/*******************************************/
void TxCCxx00()
{
unsigned char i;
halSpiWriteReg(CCxxx0_FREND0, 0x10); //CC1100:POWER=10DBM;CC2500:1DBM
Dly1mS(25);;
for(i=0;i<4;i++) //发四次
{
halRfSendPacket(txBuffer_CCxx00, sizeof(txBuffer_CCxx00));
Dly1mS(50);
}
halSpiWriteReg(CCxxx0_FREND0, 0x15); //CC1100太近会收不到,将发射功率降低到-10DBM //#define CCxxx0_FREND0 0x22
Dly1mS(50);
halRfSendPacket(txBuffer_CCxx00, sizeof(txBuffer_CCxx00));
Dly1mS(100);
}
/*********************************************************/
unsigned char halSpiReadReg(unsigned char addr)
{
unsigned char value;
CLEAR_CLOCK();
Dly10us();Dly10us();
CLEAR_CSN(); // CSn=0
addr|=READ_SINGLE;
SPI_write(addr);
CLEAR_IN();
Dly10us();Dly10us();Dly10us();Dly10us();Dly10us();
value=0;
value=SPI_read();
Dly10us();Dly10us();Dly10us();
CLEAR_CLOCK();
CLEAR_IN();
SET_CSN(); // CSn=0 P_CSn=0;
return value;
}
/**************************************************/
void halSpiReadBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count)
{
unsigned char i,value;
CLEAR_CLOCK();
Dly10us();Dly10us();
CLEAR_CSN(); // CSn=0 P_CSn=0;
addr|=READ_BURST;
SPI_write(addr);
CLEAR_IN();
Dly10us();Dly10us();Dly10us();Dly10us();Dly10us();
value=0;
for(i=0;i<count;i++)
{
value=SPI_read();
buffer[i]=value;
}
Dly10us();Dly10us();Dly10us();
CLEAR_CLOCK();
CLEAR_IN();
SET_CSN(); // CSn=0 P_CSn=0;
}
/**************************************************/
unsigned char halSpiReadStatus(unsigned char addr)
{
unsigned char value,rd_pin;
CLEAR_CSN(); // CSn=0 P_CSn=0;
while(GET_OUT());
CLEAR_CLOCK(); //SCLK=0 //P_SCLK=0;
addr|=READ_BURST;
SPI_write(addr);
value=SPI_read();
CLEAR_CLOCK(); //SCLK=0 //P_SCLK=0;
CLEAR_IN(); // SI=0 P_SI=0;
SET_CSN(); //CSn=1 P_CSn=1;
return value;
}// halSpiReadStatus
/**************************************************/
void halRfWriteRfSettings()
{
l_setCC1100:
halSpiWriteReg(CCxxx0_FSCTRL1, 0x0C);
halSpiWriteReg(CCxxx0_FSCTRL0, 0x00);
halSpiWriteReg(CCxxx0_FREQ2, 0x10);
halSpiWriteReg(CCxxx0_FREQ1, 0x09);
halSpiWriteReg(CCxxx0_FREQ0, 0x7B);
halSpiWriteReg(CCxxx0_MDMCFG4, 0xC6);
halSpiWriteReg(CCxxx0_MDMCFG3, 0x83);
halSpiWriteReg(CCxxx0_MDMCFG2, 0x02);
halSpiWriteReg(CCxxx0_MDMCFG1, 0x22);
halSpiWriteReg(CCxxx0_MDMCFG0, 0xF8);
halSpiWriteReg(CCxxx0_CHANNR, 0x05);
halSpiWriteReg(CCxxx0_DEVIATN, 0x30);
halSpiWriteReg(CCxxx0_FREND1, 0x56);
halSpiWriteReg(CCxxx0_FREND0, 0x10);
halSpiWriteReg(CCxxx0_MCSM0, 0x18);
halSpiWriteReg(CCxxx0_FOCCFG, 0x15);
halSpiWriteReg(CCxxx0_BSCFG, 0x6C);
halSpiWriteReg(CCxxx0_AGCCTRL2, 0x03);
halSpiWriteReg(CCxxx0_AGCCTRL0, 0x91);
halSpiWriteReg(CCxxx0_FSCAL3, 0xA9);
halSpiWriteReg(CCxxx0_FSCAL2, 0x2A);
halSpiWriteReg(CCxxx0_FSCAL0, 0x0D);
halSpiWriteReg(CCxxx0_FSTEST, 0x59);
halSpiWriteReg(CCxxx0_TEST2, 0x86);
halSpiWriteReg(CCxxx0_TEST1, 0x3D);
halSpiWriteReg(CCxxx0_TEST0, 0x09);
halSpiWriteReg(CCxxx0_IOCFG2, 0x24);
halSpiWriteReg(CCxxx0_IOCFG0, 0x06);
halSpiWriteReg(CCxxx0_PKTCTRL1, 0x04);
halSpiWriteReg(CCxxx0_PKTCTRL0, 0x05);
halSpiWriteReg(CCxxx0_ADDR, 0x00);
halSpiWriteReg(CCxxx0_PKTLEN, 0x1F); // hejie ,接收数据长度
//halSpiWriteReg(CCxxx0_PKTLEN, 0xFF);
halSpiWriteReg(CCxxx0_MCSM2, 0x08); // CCxxx0_MCSM2 修改限制时间 默认为07 修改为00
//halSpiWriteReg(CCxxx0_FIFOTHR, 0x07);
//CCxxx0_MCSM1
//halSpiWriteReg(CCxxx0_MCSM0, 0x14); // 状态切换时候 校准频率 。
/*halSpiWriteReg(CCxxx0_FSCTRL1, 0x08);
halSpiWriteReg(CCxxx0_FSCTRL0, 0x00);
halSpiWriteReg(CCxxx0_FREQ2, 0x10);
halSpiWriteReg(CCxxx0_FREQ1, 0x12);
halSpiWriteReg(CCxxx0_FREQ0, 0x34);
halSpiWriteReg(CCxxx0_MDMCFG4, 0xf6);
halSpiWriteReg(CCxxx0_MDMCFG3, 0x75);
halSpiWriteReg(CCxxx0_MDMCFG2, 0x13);
halSpiWriteReg(CCxxx0_MDMCFG1, 0x22);
halSpiWriteReg(CCxxx0_MDMCFG0, 0xe5);
halSpiWriteReg(CCxxx0_CHANNR, 0x00);
halSpiWriteReg(CCxxx0_DEVIATN, 0x14);
halSpiWriteReg(CCxxx0_FREND1, 0x56);
halSpiWriteReg(CCxxx0_FREND0, 0x10);
halSpiWriteReg(CCxxx0_MCSM0, 0x18);
halSpiWriteReg(CCxxx0_FOCCFG, 0x16);
halSpiWriteReg(CCxxx0_BSCFG, 0x6C);
halSpiWriteReg(CCxxx0_AGCCTRL2, 0x03);
halSpiWriteReg(CCxxx0_AGCCTRL1, 0x40);
halSpiWriteReg(CCxxx0_AGCCTRL0, 0x91);
halSpiWriteReg(CCxxx0_FSCAL3, 0xA9);
halSpiWriteReg(CCxxx0_FSCAL2, 0x2A);
halSpiWriteReg(CCxxx0_FSCAL1, 0x00);
halSpiWriteReg(CCxxx0_FSCAL0, 0x11);
halSpiWriteReg(CCxxx0_FSTEST, 0x59);
halSpiWriteReg(CCxxx0_TEST2, 0x81);
halSpiWriteReg(CCxxx0_TEST1, 0x35);
halSpiWriteReg(CCxxx0_TEST0, 0x0b);
halSpiWriteReg(CCxxx0_IOCFG2, 0x24);
halSpiWriteReg(CCxxx0_IOCFG0, 0x06);
halSpiWriteReg(CCxxx0_PKTCTRL1, 0x04);
halSpiWriteReg(CCxxx0_PKTCTRL0, 0x05);
halSpiWriteReg(CCxxx0_ADDR, 0x00);
halSpiWriteReg(CCxxx0_PKTLEN, 0xFF);*/
if(halSpiReadReg(CCxxx0_MDMCFG1)!=0x22)
goto l_setCC1100;
}
/**************************************************/
//接收到数据中断
TOSH_SIGNAL(SIG_INTERRUPT1)
{
unsigned char packetLength;
packetLength = halSpiReadReg(CCxxx0_RXFIFO);
rxBuffer[0]=packetLength;
if(rxBuffer[0] <= 32)
{
halSpiReadBurstReg(CCxxx0_RXFIFO, &rxBuffer[1], packetLength);
CC1100_receive(&rxBuffer[0]);
}
else
{
halSpiStrobe(CCxxx0_SFRX);
}
halSpiStrobe(CCxxx0_SFRX);
halSpiStrobe(CCxxx0_SRX); }
/**************************************************/
//初始化
uint8_t CC1100_init()
{
uint16_t nnn;
SET_CSN();
CLEAR_CLOCK();
CLEAR_IN();
call HPLCC1100.setInterrupt(0x00);
CC1100_POR();
for(nnn=0;nnn<500;nnn++);
halRfWriteRfSettings();
halSpiWriteBurstReg(CCxxx0_PATABLE, paTable, sizeof(paTable));
call HPLCC1100.setInterrupt(0x00);
call HPLCC1100.setStrobe(CCxxx0_SIDLE);
return 1;
}
//设置状态
uint8_t CC1100_setStrobe(uint8_t state)
{
halSpiStrobe(state);
Dly1mS(5);
return 1;
}
//写寄存器
uint8_t HPLCC1100.setRegister(uint8_t address,uint8_t value)
{
halSpiWriteReg(address,value);
return 1;
}
//读寄存器
uint8_t CC1100_readRegister(uint8_t address)
{
rd_reg = halSpiReadReg(address);
return rd_reg;
}
//读状态
uint8_t CC1100_readStatus(uint8_t address)
{
return halSpiReadStatus(address);
}
//连续读
uint8_t CC1100_readBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count)
{
halSpiReadBurstReg(addr, buffer, count);
return 1;
}
//halSpiReadStatus
//设置中断
uint8_t CC1100.setInterrupt(uint8_t flag)
{
if(flag==0x00)
{
EIFR=0x02;EICRA=0x08;EICRB=0x00;EIMSK=0x00;
}
else if(flag==0x01)
{
EIFR=0x02;EICRA=0x08;EICRB=0x00;EIMSK=0x02;
halSpiStrobe(CCxxx0_SRX);
}
else if(flag==0x02)
{
halSpiStrobe(CCxxx0_SRX);
}
return 1;
}
//连续发
uint8_t CC1100_sendRFburst(uint8_t *txPtr,uint8_t size)
{
CC1100_setInterrupt(0x00); //关中断
halSpiStrobe(CCxxx0_SIDLE);
halRfSendPacket(txPtr, size);
//Dly1mS(100);
if(SEND_DONE==0x01) //表明发送完成
{
SEND_DONE=0x00;
CC1100_sendDone(SUCCESS);
CC1100_setInterrupt(0x01); //开中断
}
return 1;
}
/*
uint8_t CC1100_sendDone()
{
return success; }
uint8_t CC1100_receive(uint8_t *msg)
{
return msg; }
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -