中级会员
- 积分
- 229
- 金钱
- 229
- 注册时间
- 2010-12-14
- 在线时间
- 0 小时
|
楼主 |
发表于 2011-1-21 14:30:49
|
显示全部楼层
<p><strong><span style="font-size:small;">它哥M3终于和它弟M0通上话了(同父异母的)下面是LPC1114上的代码,都是照抄正点原子的,只是对应M0上的寄存器做了相应的修改!实验成功!放心使用!</span><br />
</strong><br />
const uint8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //发送地址<br />
const uint8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //发送地址</p>
<p>/*****************************************/<br />
/* 函数功能:SPI通信 */<br />
/* 说明: 发送一个字节,接收一个字节 */<br />
/*****************************************/<br />
uint8 SPI1_communication(uint8 TxData)<br />
{ <br />
while(((SSP1->SR)&(1<<4))==(1<<4));//忙时等待,SR状态寄存器bit4 BSY:忙时为1<br />
SSP1->DR = TxData; //把要发送的数写入TxFIFO<br />
while(((SSP1->SR)&(1<<2))!=(1<<2));//等待接收完,SR状态寄存器bit2 RNE:接收FIFO非空为1<br />
return(SSP1->DR); //返回收到的数据<br />
}<br />
/*****************************************/<br />
/* 函数功能:SPI1初始化 */<br />
/* 说明: 没有用SSEL1 */<br />
/*****************************************/<br />
void SPI1_Init(void)<br />
{<br />
uint8 i,Clear=Clear;//Clear=Clear:用这种语句形式解决编译产生的Waring:never used! </p>
<p> SYSCON->RESETCTRL |= (0x1<<2); //禁止SSP1复位<br />
SYSCON->SYSAHBCLKCTRL |= (0x1<<18);//允许SSP1时钟<br />
SYSCON->SSP1CLKDIV = 0x06; //6分频:48/6=8Mhz</p>
<p> IOCON->IO2_1 &= ~0x07;<br />
IOCON->IO2_1 |= 0x02; //把PIO2_1选择为SSP CLK <br />
IOCON->IO2_2 &= ~0x07; <br />
IOCON->IO2_2 |= 0x02; //把PIO2_2选择为SSP MISO <br />
IOCON->IO2_3 &= ~0x07; <br />
IOCON->IO2_3 |= 0x02; //把PIO2_3选择为SSP MOSI <br />
// 8位数据传输,SPI模式, CPOL = 0, CPHA = 0,空闲时CLI为1,第一个上升沿采集数据,SCR = 0<br />
SSP1->CR0 = 0x0107; <br />
// 预分频值为254(注意:这里最小为2,而且必须为偶数 2~254) <br />
SSP1->CPSR = 0x02;<br />
SSP1->CR1 &= ~(1<<0);//LBM=0:正常模式<br />
SSP1->CR1 &= ~(1<<2);//MS=0:主机模式 <br />
SSP1->CR1 |= (1<<1);//SSE=1:使能SPI1<br />
//清空RxFIFO,LPC1114收发均有8帧FIFO,每帧可放置4~16位数据<br />
for ( i = 0; i < 8; i++ )<br />
{<br />
Clear = SSP1->DR;//读数据寄存器DR将清空RxFIFO<br />
} <br />
}</p>
<p>/*******************************************/<br />
/* 函数功能;NRF24L01初始化 */<br />
/*******************************************/<br />
void NRF24L01_Init()<br />
{<br />
SPI1_Init();<br />
GPIO0->DIR |= (1<<3); //P0.3脚为输出,用做CSN<br />
GPIO0->DATA |= (1<<3); //CSN=1;<br />
GPIO3->DIR &= ~(1<<4); //NRF24L01_IRQ连接P3.4脚,设置P3.4脚为输入引脚<br />
GPIO3->DIR |= (1<<5); //NRF24L01_CE连接P3.5脚,设置P3.5脚为输出引脚<br />
GPIO3->DATA &= ~(1<<5); //CE置低,使能24L01</p>
<p>}</p>
<p>uint8 NRF24L01_Write_Reg(uint8 reg,uint8 value)<br />
{<br />
uint8 status;</p>
<p> GPIO0->DATA &= ~(1<<3); //CSN=0; <br />
status = SPI1_communication(reg);//发送寄存器值(位置),并读取状态值<br />
SPI1_communication(value);<br />
GPIO0->DATA |= (1<<3); //CSN=1;</p>
<p> return status;<br />
}</p>
<p>uint8 NRF24L01_Read_Reg(uint8 reg)<br />
{<br />
uint8 value;</p>
<p> GPIO0->DATA &= ~(1<<3); //CSN=0; <br />
SPI1_communication(reg);//发送寄存器值(位置),并读取状态值<br />
value = SPI1_communication(NOP);<br />
GPIO0->DATA |= (1<<3); //CSN=1;</p>
<p> return value;<br />
}<br />
<br />
uint8 NRF24L01_Read_Buf(uint8 reg,uint8 *pBuf,uint8 len)<br />
{<br />
uint8 status,u8_ctr;<br />
GPIO0->DATA &= ~(1<<3);//CSN=0 <br />
status=SPI1_communication(reg);//发送寄存器值(位置),并读取状态值 <br />
for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI1_communication(0XFF);//读出数据<br />
GPIO0->DATA |= (1<<3); //CSN=1<br />
return status; //返回读到的状态值<br />
}</p>
<p>uint8 NRF24L01_Write_Buf(uint8 reg, uint8 *pBuf, uint8 len)<br />
{<br />
uint8 status,u8_ctr;<br />
GPIO0->DATA &= ~(1<<3); <br />
status = SPI1_communication(reg);//发送寄存器值(位置),并读取状态值<br />
for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI1_communication(*pBuf++); //写入数据<br />
GPIO0->DATA |= (1<<3); <br />
return status; //返回读到的状态值<br />
}<br />
<br />
uint8 NRF24L01_Check(void)<br />
{<br />
uint8 check_in_buf[5]={0x11,0x22,0x33,0x44,0x55};<br />
uint8 check_out_buf[5]={0x00};</p>
<p> NRF24L01_Write_Buf(WRITE_REG+TX_ADDR, check_in_buf, 5);</p>
<p> NRF24L01_Read_Buf(READ_REG+TX_ADDR, check_out_buf, 5);</p>
<p> if((check_out_buf[0] == 0x11)&&\<br />
(check_out_buf[1] == 0x22)&&\<br />
(check_out_buf[2] == 0x33)&&\<br />
(check_out_buf[3] == 0x44)&&\<br />
(check_out_buf[4] == 0x55))return 0;<br />
else return 1;<br />
}</p>
<p>void NRF24L01_RX_Mode(void)<br />
{</p>
<p> GPIO3->DATA &= ~(1<<5); //CE拉低,使能24L01配置<br />
<br />
NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P0, (uint8*)RX_ADDRESS, RX_ADR_WIDTH);//写RX接收地址<br />
<br />
NRF24L01_Write_Reg(WRITE_REG+EN_AA,0x01); //开启通道0自动应答 <br />
NRF24L01_Write_Reg(WRITE_REG+EN_RXADDR,0x01);//通道0接收允许 <br />
NRF24L01_Write_Reg(WRITE_REG+RF_CH,40); //设置RF工作通道频率 <br />
NRF24L01_Write_Reg(WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度<br />
NRF24L01_Write_Reg(WRITE_REG+RF_SETUP,0x0f);//设置TX发射参数,0db增益,2Mbps,低噪声增益开启 <br />
NRF24L01_Write_Reg(WRITE_REG+CONFIG, 0x0f);//配置基本工作模式的参数WR_UP,EN_CRC,16BIT_CRC,接收模式<br />
NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器 <br />
GPIO3->DATA |= (1<<5); //CE置高,使能接收</p>
<p>}</p>
<p>void NRF24L01_TX_Mode(void)<br />
{<br />
GPIO3->DATA &= ~(1<<5); //CE拉低,使能24L01配置 <br />
NRF24L01_Write_Buf(WRITE_REG+TX_ADDR,(uint8*)TX_ADDRESS,TX_ADR_WIDTH);//写TX节点地址 <br />
NRF24L01_Write_Buf(WRITE_REG+RX_ADDR_P0,(uint8*)RX_ADDRESS,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK <br />
NRF24L01_Write_Reg(WRITE_REG+EN_AA,0x01); //使能通道0的自动应答 <br />
NRF24L01_Write_Reg(WRITE_REG+EN_RXADDR,0x01); //使能通道0的接收地址 <br />
NRF24L01_Write_Reg(WRITE_REG+SETUP_RETR,0x1a);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次<br />
NRF24L01_Write_Reg(WRITE_REG+RF_CH,40); //设置RF通道为40<br />
NRF24L01_Write_Reg(WRITE_REG+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启 <br />
NRF24L01_Write_Reg(WRITE_REG+CONFIG,0x0e); //配置基本工作模式的参数WR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断<br />
GPIO3->DATA |= (1<<5); //CE置高,使能发送<br />
}<br />
//接收数据<br />
uint8 NRF24L01_RxPacket(uint8 *rxbuf)<br />
{<br />
uint8 state;</p>
<p> state=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值 <br />
NRF24L01_Write_Reg(WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志<br />
if(state&RX_OK)//接收到数据<br />
{<br />
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据<br />
NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器 <br />
return 0; <br />
} <br />
return 1;//没收到任何数据<br />
}<br />
//发送数据<br />
uint8 NRF24L01_TxPacket(uint8 *txbuf)<br />
{<br />
uint8 state;<br />
<br />
GPIO3->DATA &= ~(1<<5); //CE拉低,使能24L01配置<br />
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节<br />
GPIO3->DATA |= (1<<5); //CE置高,使能发送 <br />
while((GPIO3->DATA&(1<<4))==(1<<4));//等待发送完成<br />
state=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值 <br />
NRF24L01_Write_Reg(WRITE_REG+STATUS,state); //清除TX_DS或MAX_RT中断标志<br />
if(state&MAX_TX)//达到最大重发次数<br />
{<br />
NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器 <br />
return MAX_TX; <br />
}<br />
if(state&TX_OK)//发送完成<br />
{<br />
return TX_OK;<br />
}<br />
return 0xff;//其他原因发送失败<br />
} <br />
</p> |
|