|
#include "reg52.h"
#include "uart.h"
sbit sdi= P1^4;
sbit sck= P1^5;
sbit nsel= P1^6;
sbit sdo= P1^7;
unsigned char workflag;
unsigned char rxdata[16]; //16,接收数据存储器组
unsigned char txdata[16]; //16,发送数据存储器组
bit revframe;
unsigned char spirevbyte();
unsigned int spirevtwobyte();
void spisendbyte(unsigned char x);
void spisendcommand(unsigned int x);
void txregframe();
void fiforxframe();
void ia442xinit();
void delayms(unsigned int n);
unsigned char num=0;
unsigned char comdata[16];
bit flag=0;
void main()
{
unsigned char b;
UART_Init();
P1=0xcf;
txdata[0]=0x2d;
txdata[1]=0xd4;
txdata[2]=0xfa;
txdata[3]=0x01;
txdata[4]=0x55;
ia442xinit();
spirevtwobyte();
spisendcommand(0x8288); //;电源管理
while(1)
{
spisendcommand(0xca83); //;FIFO COMMAND
while(1)
{
sdi=0;
nsel=0;
if(sdo)
{
fiforxframe();
if(!revframe) break;
revframe=0;
send(rxdata[2]);
break;
}
if(flag)
{
ES=0;
txregframe();
flag=0;
RI=0;
ES=1;
break;
}
}
}
}
void serial() interrupt 4
{
if(RI)//RI为串行口接收中断请求标志位。在串行口允许接收时,每接收完一个串行帧,RI置1
{
txdata[4]=SBUF;
flag=1;
RI=0;
}
}
void ia442xinit() //;ia442x初始化
{
spisendcommand(0x80dd); //;配置命令,fd-915M,dd-433M
spisendcommand(0x820c); //;电源管理
spisendcommand(0xa080); //;频率设置
spisendcommand(0xc623); //;波特率设置
spisendcommand(0x9483); //;接收控制
spisendcommand(0xc2ec); //;数据滤波
spisendcommand(0xca81); //;FIFO COMMAND
spisendcommand(0xc4e7); //;AFC COMMAND
spisendcommand(0x9840); //;TXCTRL
spisendcommand(0xffff); //;WAKEUP
spisendcommand(0xc80e); //;间歇工作
spisendcommand(0xc060); //;时钟输出
}
void fiforxframe() //;ia442x接收一帧数据
{
unsigned char i,n;
spirevbyte();
spirevbyte();
rxdata[0]=spirevbyte();
nsel=1;
if(rxdata[0]==0xfa)
{
sdi=0;
nsel=0;
while(!sdo);
spirevbyte();
spirevbyte();
rxdata[1]=spirevbyte();
nsel=1;
n=rxdata[1]&0x0f;
for(i=0;i
{
sdi=0;
nsel=0;
while(!sdo);
spirevbyte();
spirevbyte();
rxdata[i+2]=spirevbyte();
nsel=1;
}
revframe=1;
}
spisendcommand(0xca81);
}
void txregframe() //;ia442x发送一帧数据
{
unsigned char i,n;
unsigned int u;
spisendcommand(0x8228);
n=txdata[3]+4;
for(i=0;i
{
nsel=0;
while(!sdo);
u=0xb8;
u<<=8;
u+=txdata;
spisendcommand(u);
}
nsel=0;
while(!sdo);
spisendcommand(0xb8aa);
nsel=0;
while(!sdo);
spisendcommand(0x8288);
spirevtwobyte();
}
void spisendcommand(unsigned int x) //;写指令
{
unsigned char comdata1,comdata2;
unsigned int a;
a=x;
nsel=0; //为什么要置0????nsel低电平有效??
comdata2=a;
comdata1=a>>8;
spisendbyte(comdata1);
spisendbyte(comdata2);
nsel=1;
}
void spisendbyte(unsigned char x) //;写8位数据
{
unsigned char i,bytedata;
bytedata=x;
for(i=0;i<8;i++)
{
sck=0;
sdi=bytedata&0x80;
bytedata=bytedata<<1;
sck=1; //第22页那个时序图
}
sck=0;
sdi=0;
}
unsigned int spirevtwobyte() //;读状态
{
unsigned int u;
nsel=0;
u=spirevbyte();
u<<=8;
u+=spirevbyte();
nsel=1;
return(u);
}
unsigned char spirevbyte() //;读8位数据
{
unsigned char i,bytedata;
sdi=0;
sck=0;
bytedata=0;
for(i=0;i<8;i++)
{
sck=1;
bytedata=bytedata<<1;
if(sdo) bytedata+=1;
sck=0;
}
return(bytedata);
}
void delayms(unsigned int n)
{
unsigned int i,j;
for(j=0;j
{
for(i=0;i<112;i++);
}
}
这是例程,我有点没看明白(主循环)。望解释一下,感谢了!!!
|
|