中级会员
- 积分
- 243
- 金钱
- 243
- 注册时间
- 2013-4-20
- 在线时间
- 0 小时
|
楼主 |
发表于 2013-7-21 20:39:22
|
显示全部楼层
<div>#include "stm8s208cb.h"</div>
<div>#include "lcd12864.h"</div>
<div><br />
</div>
<div>_Bool CSN @PC_ODR:1; //片选信号,推挽输出 </div>
<div>_Bool IRQ @PC_IDR:2; //IRQ主机数据输入,上拉输入</div>
<div>_Bool CE @PC_ODR:3; //发送/接收模式选择?仆焓涑?</div>
<div>_Bool SCK @PC_ODR:5; //PC5 推完输出</div>
<div>_Bool MOSI @PC_ODR:6; //PC6 MOSI 主机推挽输出 (根据速率初始化PC_CR2寄存器)</div>
<div>_Bool MISO @PC_IDR:7; //PC7 SCK 主机不带中断的悬浮输入</div>
<div><br />
</div>
<div>unsigned char TxAddr[]={0xE7,0xE7,0xE7,0xE7,0xE7};//发送地址</div>
<div>unsigned char RxAddr[]={0xE7,0xE7,0xE7,0xE7,0xE7};//接收偷刂?</div>
<div>unsigned char tx_buf[]="send data";</div>
<div><br />
</div>
<div>void CLK_Init(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_CKDIVR = 0x01;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_SWCR |= 0x02; //启动时钟切换使能</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_SWR = 0xB4; // </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//配置系统主时钟为外部时钟,准备时钟自动切换</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(!(CLK_SWCR & 0x08)); </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//等待SWIF置位,表示时钟切换完毕,新时钟代替原时钟</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if (CLK_CMSR != 0xB4) </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(1); //校验时钟是否切换</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_ICKR = 0x00; //关闭内部时钟HSI</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_CSSR |= 0x01; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//开启时钟安全系统:即外部晶振发生意外故障可转换到内部时钟16M</div>
<div>}</div>
<div>void delayms(unsigned int z)</div>
<div>{</div>
<div> unsigned int i,j;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i=z;i>0;i--)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(j=325;j>0;j--);</div>
<div>}</div>
<div>void SPI_GPIO_Init(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>C_DDR=0x0A;<span class="Apple-tab-span" style="white-space:pre;"> </span>//0110 1010<span class="Apple-tab-span" style="white-space:pre;"> </span> 0000 1010</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>C_CR1=0x0a; // 0110 1010 0000 1010</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>C_CR2=0x00;// 0000 0100 0000 0000</div>
<div>}</div>
<div>void nRF24L01_Pin_Conf(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_CR1 =0x0c; // 【5:3】24M/4=6M; MSB 先;主设备;CPOL = 0;CPHA= 0 </div>
<div> SPI_CR2 =0x03; // 双线全双工;软件管理;无CRC</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_CR1|=0x40; // 开启SPI总线</div>
<div>}</div>
<div>unsigned char nRF24L01_SPI_RW(unsigned char data)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char temp;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while (!(SPI_SR&0X02));</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_DR = data;<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while (!(SPI_SR&0X01));</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>temp = SPI_DR;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return temp;</div>
<div>}</div>
<div>unsigned char nRF24L01_Read_Reg(unsigned char RegAddr)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char BackData;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CSN = 0;//启动时序</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_SPI_RW(RegAddr);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写寄存器地址 */<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>BackData = nRF24L01_SPI_RW(0);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入读寄存器指令 */ </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CSN = 1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return BackData;</div>
<div>}</div>
<div>unsigned char nRF24L01_Write_Reg(unsigned char RegAddr,unsigned char data)</div>
<div>{</div>
<div> unsigned char BackData;</div>
<div> CSN = 0;<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 启动时序 */</div>
<div> BackData = nRF24L01_SPI_RW(RegAddr);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入地址 */</div>
<div> nRF24L01_SPI_RW(data);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入值 */</div>
<div> CSN = 0;</div>
<div> return(BackData);</div>
<div>}</div>
<div>unsigned char nRF24L01_Read_RxData(unsigned char RegAddr,unsigned char *RxData,unsigned char DataLen)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char BackData,i;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CSN = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>BackData = nRF24L01_SPI_RW(RegAddr);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入要读取的寄存器地址 */</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i = 0; i < DataLen; i++) <span class="Apple-tab-span" style="white-space:pre;"> </span>/* 读取数据 */</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>RxData = nRF24L01_SPI_RW(0);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>} </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CSN = 1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(BackData);</div>
<div>}</div>
<div>unsigned char nRF24L01_Write_TxData(unsigned char RegAddr,unsigned char *TxData,unsigned char DataLen)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char BackData,i;</div>
<div> CSN = 0; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>BackData = nRF24L01_SPI_RW(RegAddr);<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入要写入寄存器的地址 */</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i = 0; i < DataLen; i++)<span class="Apple-tab-span" style="white-space:pre;"> </span>/* 写入数据 */</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_SPI_RW(*TxData++);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>} </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CSN = 1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(BackData);</div>
<div>}</div>
<div>unsigned char NRF24L01_TxPacket(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char sta;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(!(sta&0x20))</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>sta=nRF24L01_SPI_RW(0x07);</div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> CE=0;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nRF24L01_Write_Reg(0x20+0x07,0XFF); //清除TX_DS或MAX_RT中断标志</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> CSN =0 ;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nRF24L01_SPI_RW(0XE1);//清除TX FIFO寄存器 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> CSN =1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> L12864_string(4,0,"nima");</div>
<div>}</div>
<div>void nRF24L01_Set_TxMode(void)</div>
<div>{</div>
<div> CE = 0; </div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_TxData(0x20+0x10,TxAddr,5);<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>/* 为了应答接收设备,接收通道0地址和发送地址相同 */</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nRF24L01_Write_TxData(0x20+0x0a,RxAddr,5);<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_TxData(0xa0,tx_buf,32);<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x20+0x01,0x00); //配置自动应答</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x20+0x02,0x00); //使能接收通道0</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x20+0x04,0x0a);//建立自动重发250us+86us,自动重发10次 </div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x40+0x05,0x40);//选择射频通道0x40</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x20+0x06,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_Reg(0x20+0x11,32);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nRF24L01_Write_Reg(0x20+0x00,0x0e); // CRC使能,16位CRC校验,上电 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> CE = 1;</div>
<div>}</div>
<div>unsigned char NRF24L01_Check(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char buf[5]={0XA5,0xa5,0XA5,0XA5,0XA5};</div>
<div> unsigned char bu1[5];</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned char i;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Write_TxData(0x30,buf,5);//写入5个字节的地址.<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Read_RxData(0x10,bu1,5); //读出写入的地址 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i=0;i<5;i++)if(bu1!=0XA5)break;<span class="Apple-tab-span" style="white-space:pre;"> </span> <span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(i!=5)return 1;//检测24L01错误<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>else return 0;<span class="Apple-tab-span" style="white-space:pre;"> </span> //检测到24L01</div>
<div>}<span class="Apple-tab-span" style="white-space:pre;"> </span> <span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>main()</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLK_Init();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>LCD12864_GPIO_INIT();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>lcd12864_Init();<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_GPIO_Init();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Pin_Conf();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(NRF24L01_Check())//检测不到24L01//返回值为零:成功</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>L12864_string(1,0,"Check Failed!");</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>delayms(800);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>L12864_string(1,0,"lease Check!");</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>delayms(800);<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>CLE_Screen();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>L12864_string(1,0,"Check OK");</div>
<div>/////////////////////////////////////////////////////////////////////////////////</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while (1)</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF24L01_Set_TxMode();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF24L01_TxPacket();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div>}</div> |
|