|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2008-5-17 23:18:30
|
显示全部楼层
:经过晚上的测试 ,收发是没有什么太大的问题的,下一步继续测试。。。
下面是发送部分的代码:
#include<reg51.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit Led1=P2^0;
sbit Led2=P2^1;
sbit Key=P3^7;
uchar count=19;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//----------------------------------------------------------------------------------------------------------------
#define BYTE_BIT0 0x01
#define BYTE_BIT1 0x02
#define BYTE_BIT2 0x04
#define BYTE_BIT3 0x08
#define BYTE_BIT4 0x10
#define BYTE_BIT5 0x20
#define BYTE_BIT6 0x40
#define BYTE_BIT7 0x80
//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------
bdata unsigned char DATA_BUF;
#define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
//---------------------------------------------------发送数据缓冲区-------------------------------------------------
#define TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len]=
{
0x29,0x30,0x31,0x32,
};
//---------------------------------------------------配置口定义---------------------------------------------------
sbit TXEN=P1^0;
sbit TRX_CE=P3^2;
sbit PWR=P1^1;
sbit MISO=P1^6;
sbit MOSI=P1^5;
sbit SCK=P1^7;
sbit CSN=P1^3;
//---------------------------------------------------NRF905状态IO----------------------------------------------------
sbit AM=P1^4;
sbit DR=P3^3;
sbit CD=P1^2;
//-------------------------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
void keyscan();
//------------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在423MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xCC,0xCC,0xCC,0xCC, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
bit lcdbit;
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
//------------------------------------------------延时------------------------------------------------------------
static void Delay(uchar n)
{
uint i;
while(n--)
for(i=0;i<80;i++)
keyscan();
}
//-------------------------------------------------SPI写函数----------------------------------------------------------
void SpiWrite(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
for (i=0;i<8;i++)
{
if (DATA7) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
SCK=0;
}
}
//------------------------------------------------------初始化nRF905---------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=0; // Init DR for input
AM=0; // Init AM for input
CD=0; // Init CD for input
PWR=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TXEN=0; // set radio in Rx mode
}
//-----------------------------------------------------初始化寄存器-----------------------------------------------
void Config905(void)
{
uchar i;
CSN=0; // Spi enable for write a spi command
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf);
}
CSN=1; // Disable Spi
}
//-----------------------------------------------------发送数据打包---------------------------------------------------
void TxPacket(uchar *TxRxBuf)
{
uchar i;
//Config905();
CSN=0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxRxBuf); // Write 32 bytes Tx data
}// Spi enable for write a spi command
CSN=1;
Delay(1); // Spi disable
CSN=0; // Spi enable for write a spi command
SpiWrite(WTA); // Write address command
for (i=0;i<4;i++) // Write 4 bytes address
{
SpiWrite(TxAddress);
}
CSN=1; // Spi disable
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
Delay(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//----------------------------------------------------------设置发送状态---------------------------------------------
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
Delay(1); // delay for mode change(>=650us)
}
//-----------------------------------------------------------------------------------------------------------------
void delay50ms()
{
uchar i,j;
for(i=5;i>0;i--)
for(j=200;j>0;j--)
keyscan();
}
//------------用于按键销抖--------------------------------------------------------------------------------------
void delay10ms()
{
uchar i,j;
for(i=50;i>0;i--)
for(j=50;j>0;j--)
keyscan();
}
//--------------------------------显示子函数--------------------------------------------------------------------------
void display()
{
uchar highcount,lowcount;
highcount=count%10;
lowcount=count/10;
P0=table[highcount];
Led1=1;
Led2=0;
delay50ms();
P0=table[lowcount];
Led1=0;
Led2=1;
delay50ms();
}
//------------------------按键扫描---------------------------------------------------
void keyscan()
{
if(Key==0)
{
delay10ms();
if(Key==0)
{
count++;
if(count==100)
{count=0;}
while(Key==0);
}
}
}
//----------------主函数-------------------------------------------------
//--------------------------------------------------------------------------------------------------
void main()
{
uchar i,j;
nRF905Init();//初始化nRF905
Config905();//初始化nRF905寄存器
while(1)
{
keyscan();
for(j=20;j>0;j--)
{
display();
}
TxRxBuf[0]=count;
TxRxBuf[1]=count;
TxRxBuf[2]=count;
TxRxBuf[3]=count;
for(i=0;i<2;i++)
{
SetTxMode();// Set nRF905 in Tx mod
TxPacket(TxRxBuf);// Send data by nRF905
}
}
} |
|