📄 spi.c
字号:
#include <spi.h>
uchar jindu = 1;
/*延迟程序12M 大约5uS*/
void delay5us()
{
uint i;
for (i=0;i<5;i++)
_nop_();
}
//从SI发数据
void SPISendByte(uchar ch)
{
uchar idata n=8;
SCLK=0;
CSn=0;
delay5us();
while(n--)
{
delay5us();
SCLK=0;
if((ch&0x80)==0x80)
{
SI=1;
}
else
{
SI=0;
}
delay5us();
ch=ch<<1;
SCLK=1;
}
delay5us();
SCLK=0;
}
//SO上接收数据
uchar SPIreceiveByte()
{
uchar idata n=8;
uchar tdata;
SCLK=0;
CSn=0;
while (n--)
{
delay5us();
SCLK=0;
delay5us();
tdata=tdata<<1;
if(SO==1)
tdata=tdata|0x01;
else
tdata=tdata&0xfe;
SCLK=1;
}
delay5us();
SCLK=0;
return(tdata);
}
uchar spisend_receivebyte(uchar ch) //发送数据 ,返回状态
{
uchar idata n = 8;
uchar tdata;
SCLK = 0;
CSn = 0;
delay5us();
while (n--)
{
delay5us();
SCLK = 0;
delay5us();
tdata = tdata << 1;
if (SO == 1)
{
tdata = tdata | 0x01;
}
else
{
tdata = tdata & 0xfe;
}
if((ch & 0x80) == 0x80)
{
SI = 1;
}
else
{
SI = 0;
}
ch = ch << 1;
SCLK = 1;
}
delay5us();
SCLK = 0;
return(tdata);
}
/*void autoreset(void) //上电自动复位
{
SCLK=1;
SI=0;
CSn = 1;
CSn = 0;
while (SO)
{
}
delay5us();
delay5us();
CSn = 1;
SCLK = 0;
SI=1;
}*/
void power_on(void)
{
int i;
CSn = 1;
SCLK = 1;
SI = 0;
CSn = 0;
delay5us();
CSn = 1;
for (i = 0; i <100; i++) //持续>40us
{
delay5us();
}
CSn = 0;
while (SO) //将CSn拉低,等待SO变低(CHIP_RDYn)
{}
SPISendByte(0x30);
while (SO) //当SO再次为低时,时钟与电压都稳定了
{}
CSn=1;
SCLK=0;
SI=1;
SO=1;
}
void LEDSHOW(uchar ch)
{
uchar i=100;uint j;
while (i--)
{
P1 = 0xef;
switch(ch&0x0f)
{
case 0x00: P0 = 0xc0; break;
case 0x01: P0 = 0xf9; break;
case 0x02: P0 = 0xa4; break;
case 0x03: P0 = 0xb0; break;
case 0x04: P0 = 0x99; break;
case 0x05: P0 = 0x92; break;
case 0x06: P0 = 0x82; break;
case 0x07: P0 = 0xf8; break;
case 0x08: P0 = 0x80; break;
case 0x09: P0 = 0x90; break;
case 0x0a: P0 = 0x88; break;
case 0x0b: P0 = 0x83; break;
case 0x0c: P0 = 0xc6; break;
case 0x0d: P0 = 0xa1; break;
case 0x0e: P0 = 0x86; break;
case 0x0f: P0 = 0x8e; break;
}
for(j=0;j<100;j++) { delay5us(); delay5us(); }
P1 = 0xdf;
switch((ch>>4)&0x0f)
{
case 0x00: P0 = 0xc0; break;
case 0x01: P0 = 0xf9; break;
case 0x02: P0 = 0xa4; break;
case 0x03: P0 = 0xb0; break;
case 0x04: P0 = 0x99; break;
case 0x05: P0 = 0x92; break;
case 0x06: P0 = 0x82; break;
case 0x07: P0 = 0xf8; break;
case 0x08: P0 = 0x80; break;
case 0x09: P0 = 0x90; break;
case 0x0a: P0 = 0x88; break;
case 0x0b: P0 = 0x83; break;
case 0x0c: P0 = 0xc6; break;
case 0x0d: P0 = 0xa1; break;
case 0x0e: P0 = 0x86; break;
case 0x0f: P0 = 0x8e; break;
}
for(j=0;j<100;j++) { delay5us(); }
}
for(j=0;j<10000;j++) { P1=0xff; delay5us(); }
}
void init(void) //初始化 2500
{
CSn=1;
SPISendByte(0x40);
SPISendByte(0x29); //00,IOCFG2
SPISendByte(0x2e); //01,IOCFG1
SPISendByte(0x06); //02,IOCFG0
SPISendByte(0x07); //03,FIFOTHR
SPISendByte(0xd3); //04,SYNC1
SPISendByte(0x91); //05 ,SYNC0
SPISendByte(0x20); //06,PKLEN ////
SPISendByte(0x0C); //07,PKTCTRL1///////////autoflush
SPISendByte(0x05); //08,PKTCRL0 //fixed length-04
SPISendByte(0x00); //09,ADDR
SPISendByte(0x00); //0a,CHANNR
SPISendByte(0x09); //0b,FSCTRL1
SPISendByte(0x00); //0c,FSCTRL0
SPISendByte(0x5d); //0d,FREQ2
SPISendByte(0x93); //0e,FREQ1
SPISendByte(0xb1); //0f,FREQ0
SPISendByte(0x2d); //10,MDMCFG4
SPISendByte(0x3b); //11,MDMCFG3
SPISendByte(0x73); //12,MDMCFG2
SPISendByte(0x22); //13,MDMCFG1
SPISendByte(0xf8); //14,MDMCFG0
SPISendByte(0x01); //15,DEVIATN
SPISendByte(0x07); //16,MCSM2
SPISendByte(0x30); //17,MCSM1
SPISendByte(0x18); //18,MCSM0
SPISendByte(0x1d); //19,FOCCFG
SPISendByte(0x1c); //1a,BSCFG
SPISendByte(0xc7); //1b,AGCCTRL2
SPISendByte(0x00); //1c,AGCCTRL1
SPISendByte(0xb2); //1d,AGCCTRL0
SPISendByte(0x87); //1e,WOREVT1
SPISendByte(0x6b); //1f,WOREVT0
SPISendByte(0xf8); //20,WORCTRL
SPISendByte(0xb6); //21,FREND1
SPISendByte(0x10); //22,FREND0
SPISendByte(0xea); //23,FSCAL3
SPISendByte(0x0a); //24,FSCAL2
SPISendByte(0x00); //25,FSCAL1
SPISendByte(0x11); //26,FSCAL0
SPISendByte(0x41); //27,RCCTRL1
SPISendByte(0x00); //28,RCCTRL0
SPISendByte(0x59); //29,FSTEST
SPISendByte(0x7f); //2a,PTEST
SPISendByte(0x3f); //2b,AGCTST
SPISendByte(0x88); //2c,TEST2
SPISendByte(0x31); //2d,TEST1
SPISendByte(0x0d); //2e,TEST0
CSn=1;
}
void service_int0()interrupt 0 using 2 //中断源0
{
jindu=0;
}
//接受数据 调试中
void main(void)
{
uchar rec[30]={0,0,0,0,0,0,0,0,0,0};
uchar ere;
power_on();
init();
A6 = 0;
while(1)
{ CSn=0;
ere = spisend_receivebyte(0x34);
LEDSHOW(ere);
// spisend_receivebyte(0x38);
while(1)
{if(A3 == 0) A2 = 0;
else
A1 = 0;
}
CSn=1;
ere = spisend_receivebyte(0xcf); //burst,rXFIFO
LEDSHOW(ere);
ere = SPIreceiveByte();
CSn = 0;
}
// LEDSHOW(rec[2]);
// rec[0] = SPIreceiveByte();
// rec[1] = SPIreceiveByte();
// rec[2] = SPIreceiveByte();
// rec[3] = SPIreceiveByte();
// rec[4] = SPIreceiveByte();
// rec[5] = SPIreceiveByte();
// rec[6] = SPIreceiveByte();
// rec[7] = SPIreceiveByte();
//spisend_receivebyte(0x35); //STX
// LEDSHOW(ere);
// LEDSHOW(rec[0]);
// LEDSHOW(rec[1]);
// LEDSHOW(rec[2]);
// LEDSHOW(rec[3]);
// LEDSHOW(rec[4]);
while(1);
}
/* delay5us();
LEDSHOW(0X00);
LEDSHOW(0X00);
LEDSHOW(0X00);
LEDSHOW(0X00);
LEDSHOW(0X00);LEDSHOW(0X00);
CSn=0; A1=0 ;
// spisend_receivebyte(0x35);
// delay5us();
spisend_receivebyte(0x3f); //single,TXFIFO
//b=SPIreceiveByte();
spisend_receivebyte(0x05);
CSn=1;
CSn=0;
spisend_receivebyte(rec[0]); //burst,TXFIFO
spisend_receivebyte(rec[1]);
spisend_receivebyte(rec[2]);
spisend_receivebyte(rec[3]);
spisend_receivebyte(rec[4]);
//spisend_receivebyte(rec[5]);
CSn=1;
spisend_receivebyte(0x35); //STX
// LEDSHOW(b);
// for(i=0;i<255;i++)
// {
// delay5us();
// }
//LEDSHOW(b); */
//发送数据已通过void SPISendByte(uchar ch)uchar SPIreceiveByte()spisend_receivebyte
/*void main(void)
{ uchar ere;
power_on();
init();
A1=0;
// spisend_receivebyte(0x3b); //SFTX
// unsigned char i;
CSn=0;
// spisend_receivebyte(0x35);
// delay5us();
spisend_receivebyte(0x3f); //single,TXFIFO
//b=SPIreceiveByte();
spisend_receivebyte(0x05);
CSn=1;
CSn=0;
spisend_receivebyte(0x7f); //burst,TXFIFO
spisend_receivebyte(0x13);
spisend_receivebyte(0x14);
spisend_receivebyte(0x15);
spisend_receivebyte(0x14);
spisend_receivebyte(0x15);
CSn=1;
ere =spisend_receivebyte(0x35); //STX
delay5us();
LEDSHOW(ere);
// LEDSHOW(b);
// for(i=0;i<255;i++)
// {
// delay5us();
// }
//LEDSHOW(b);
while(1);
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -