单片机STC12LE5A08S2
无线模块si4463
无线模块一直驱动不了求大侠指导
#include "reg51.h"
//#include "radio_config_Si4463.h"
#include <intrins.h>
#define MASTER //define:master undefine:slave
#define FOSC 11059200L
#define BAUD (256 - FOSC / 32 / 2400)
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
sfr AUXR = 0x8e; //Auxiliary register
sfr SPSTAT = 0xcd; //SPI status register
#define SPIF 0x80 //SPSTAT.7
#define WCOL 0x40 //SPSTAT.6
sfr SPCTL = 0xce; //SPI control register
#define SSIG 0x80 //SPCTL.7
#define SPEN 0x40 //SPCTL.6
#define DORD 0x20 //SPCTL.5
#define MSTR 0x10 //SPCTL.4
#define CPOL 0x08 //SPCTL.3
#define CPHA 0x04 //SPCTL.2
#define SPDHH 0x00 //CPU_CLK/4
#define SPDH 0x01 //CPU_CLK/16
#define SPDL 0x02 //CPU_CLK/64
#define SPDLL 0x03 //CPU_CLK/128
sfr SPDAT = 0xcf; //SPI data register
sbit SPISS = P1^4; //SPI slave select, connect to slave' SS(P1.4) pin
sbit Irq = P3^2;
sbit EZRP_SDN = P0^4;
sfr P3M0 = 0xB2; // 0000,0000
sfr P3M1 = 0xB1; // 0000,0000
#define SI_CSN_LOW( ) SPISS=0;
#define SI_CSN_HIGH( ) SPISS=1;
#define MAX_CTS_RETRY 2500
void InitUart();
void InitSPI();
void SendUart(BYTE dat); //send data to PC
BYTE RecvUart(); //receive data from PC
BYTE SPISwap(BYTE dat); //swap SPI data between master and slave
BYTE bApi_SendCommand(BYTE bCmdLength, BYTE *pbCmdData);
void bSpi_SendDataNoResp(BYTE bCmdLength, BYTE *pbCmdData);
void bSpi_SendDataGetResp(BYTE GetRespLength,BYTE *GetResp);
BYTE vApi_WaitforCTS(void);
BYTE bApi_GetResponse(BYTE bRespLength, BYTE *pbRespData);
void Delay10ms();
void Delay100us();
BYTE abApi_Write[64]={0};
BYTE abApi_Read[64]={0};
void main()
{
BYTE i;
WORD wDelay;
InitUart(); //initial UART
InitSPI(); //initial SPI
Irq=1;
// Reset the radio
EZRP_SDN = 1;
// Wait ~300us (SDN pulse width)
for(wDelay=0; wDelay<330; wDelay++);
// Wake up the chip from SDN
EZRP_SDN = 0;
// Wait for POR (power on reset); ~5ms
for(wDelay=0; wDelay<5500; wDelay++);
// Start the radio
for(i=0;i<64;i++)
{abApi_Write[i]=0;}
abApi_Write[0] = 0x02; // Use API command to power up the radio IC
abApi_Write[1] = 0x01; // Write global control registers
abApi_Write[2] = 0x00; // Write global control registers //使用内部晶振
abApi_Write[3] = 0x01; //30M
abApi_Write[4] = 0xc9;
abApi_Write[5] = 0xc3;
abApi_Write[6] = 0x80;
bApi_SendCommand(7,abApi_Write); // Send command to the radio IC
// Wait for boot
while(vApi_WaitforCTS()); // Wait for CTS
// Read CTs, clear pending ones
for(i=0;i<64;i++)
{abApi_Write[i]=0;}
abApi_Write[0] = 0x20; // Use interrupt status command
abApi_Write[1] = 0; // Clear PH_CLR_PEND
abApi_Write[2] = 0; // Clear MODEM_CLR_PEND
abApi_Write[3] = 0; // Clear CHIP_CLR_PEND
bApi_SendCommand(4,abApi_Write); // Send command to the radio IC
for(i=0;i<64;i++)
{abApi_Read[64]=0;}
bApi_GetResponse(8,abApi_Read); // Make sure that CTS is ready, then get the response
while (1)
{
for(i=0;i<8;i++)
{
SendUart(abApi_Read[i]);
}
}
}
///////////////////////////////////////////////////////////
void InitUart()
{
SCON = 0x5a; //set UART mode as 8-bit variable baudrate
TMOD = 0x20; //timer1 as 8-bit auto reload mode
AUXR = 0x40; //timer1 work at 1T mode
TH1 = TL1 = BAUD; //115200 bps
TR1 = 1;
}
///////////////////////////////////////////////////////////
void InitSPI()
{
SPDAT = 0; //initial SPI data
SPSTAT = SPIF | WCOL; //clear SPI status
#ifdef MASTER
SPCTL = SPEN | MSTR | SSIG | CPHA | SPDHH ; //master mode
#else
SPCTL = SPEN | CPHA | SPDH ; //slave mode
#endif
}
///////////////////////////////////////////////////////////
void SendUart(BYTE dat)
{
while (!TI); //wait pre-data sent
TI = 0; //clear TI flag
SBUF = dat; //send current data
}
///////////////////////////////////////////////////////////
BYTE RecvUart()
{
while (!RI); //wait receive complete
RI = 0; //clear RI flag
return SBUF; //return receive data
}
///////////////////////////////////////////////////////////
BYTE SPISwap(BYTE dat)
{
SPDAT = dat; //trigger SPI send
while (!(SPSTAT & SPIF)); //wait send complete
SPSTAT = SPIF | WCOL; //clear SPI status
return SPDAT; //return received SPI data
}
BYTE bApi_SendCommand(BYTE bCmdLength, BYTE *pbCmdData) // Send a command + data to the chip
{
SI_CSN_LOW( ); // select radio IC by pulling its nSEL pin low
bSpi_SendDataNoResp(bCmdLength, pbCmdData); // Send data array to the radio IC via SPI
SI_CSN_HIGH( ); // de-select radio IC by putting its nSEL pin high
return 0;
}
void bSpi_SendDataNoResp(BYTE bCmdLength, BYTE *pbCmdData)
{
while( bCmdLength -- )
{
SPISwap( *pbCmdData++ );
}
}
void bSpi_SendDataGetResp(BYTE GetRespLength,BYTE *GetResp)
{
while( GetRespLength -- )
{
*GetResp++=SPISwap(0xff);
}
}
BYTE vApi_WaitforCTS(void)
{
BYTE bCtsValue = 0;
WORD bErrCnt = 0;
while (bCtsValue!=0xFF) // Wait until radio IC is ready with the data
{
SI_CSN_LOW( ); // select radio IC by pulling its nSEL pin low
SendUart(0x06);
SPISwap(0x44); // Read command buffer; send command byte
SendUart(0x07);
bSpi_SendDataGetResp(1, &bCtsValue); // Read command buffer; get CTS value
SendUart(0x10);
SI_CSN_HIGH( ); // If CTS is not 0xFF, put NSS high and stay in waiting
if(++bErrCnt>MAX_CTS_RETRY)
{
return 1; // Error handling; if wrong CTS reads exceeds a limit
}
}
return 0;
}
BYTE bApi_GetResponse(BYTE bRespLength, BYTE *pbRespData) //Get response from the chip (used after a command)
{
BYTE bCtsValue = 0;
WORD bErrCnt = 0;
while (bCtsValue!=0xFF) // Wait until radio IC is ready with the data
{
SI_CSN_LOW( ); // select radio IC by pulling its nSEL pin low
_nop_();
_nop_();
SPISwap(0x44); // Read command buffer; send command byte
bSpi_SendDataGetResp(1, &bCtsValue); // Read command buffer; get CTS value
if(bCtsValue!=0xFF)
SI_CSN_HIGH( ); // If CTS is not 0xFF, put NSS high and stay in waiting
if(++bErrCnt>MAX_CTS_RETRY)
{
return 1; // Error handling; if wrong CTS reads exceeds a limit
}
}
bSpi_SendDataGetResp(bRespLength, pbRespData); // CTS value ok, get the response data from the radio IC
SI_CSN_HIGH( ); // de-select radio IC by putting its nSEL pin high
return 0;
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Delay100us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
更多 0 |