论坛大神
- 积分
- 1846
- 金钱
- 1846
- 注册时间
- 2013-4-15
- 在线时间
- 163 小时
|
<div>#include "usart.h"</div>
<div>#include "sys.h"</div>
<div>#include "delay.h"</div>
<div>#include "mpu6050.h"</div>
<div>#include "led.h"</div>
<div>#include "key.h"</div>
<div>#include "24l01.h"</div>
<div><br />
</div>
<div>u8 tmp_buf[6];</div>
<div><br />
</div>
<div>int main(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>u8 key,mode;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>u16 t=0;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>int16_t Y,P,R;<span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NVIC_Configuration();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>delay_init();<span class="Apple-tab-span" style="white-space:pre;"> </span>//SYSTICK的时钟固定为HCLK时钟的1/8,SYSCLK:系统时钟为72MHz.</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>uart_init(9600);<span class="Apple-tab-span" style="white-space:pre;"> </span>//串口1初始化,波特率为9600,工作在全双工收发模式</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>LED_Init();<span class="Apple-tab-span" style="white-space:pre;"> </span>//LED初始化,GPIOA8对应于LED0,GPIOD2对应于LED1.</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>KEY_Init();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF24L01_Init(); <span class="Apple-tab-span" style="white-space:pre;"> </span>//初始化NRF24L01</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>MPU6050_Init();<span class="Apple-tab-span" style="white-space:pre;"> </span>//MPU6050初始化设置DMP和DMP_FIFO</div>
<div><br />
</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>printf("24L01 Check Failed!\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>delay_ms(500);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("lease Check!\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>delay_ms(500);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>LED0=!LED0;//DS0闪烁</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("24L01 Ready!\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("KEY0:RX_Mode KEY1:TX_Mode\n");<span class="Apple-tab-span" style="white-space:pre;"> </span>//显示提示信息</div>
<div><br />
</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>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>key=KEY_Scan(0);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(key==1)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>mode=0; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>break;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>else if(key==2)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>mode=1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>break;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>t++;<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(t==200)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>t=0;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("KEY0:RX_Mode KEY1:TX_Mode\n");<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>delay_ms(5);</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(mode==0)//RX模式</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("NRF24L01 RX_Mode\n");<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("Received DATA:\n");<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>RX_Mode();<span class="Apple-tab-span" style="white-space:pre;"> </span> </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>{<span class="Apple-tab-span" style="white-space:pre;"> </span> <span class="Apple-tab-span" style="white-space:pre;"> </span> <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(NRF24L01_RxPacket(tmp_buf)==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>Y=((int16_t)tmp_buf[0]<<8)+tmp_buf[1];</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>=((int16_t)tmp_buf[2]<<8)+tmp_buf[3];</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>R=((int16_t)tmp_buf[4]<<8)+tmp_buf[5];</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>UART1_Report_IMU(Y,P,R,1,1,1,100); </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>}<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>else//TX模式</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>printf("NRF24L01 TX_Mode\n");<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>TX_Mode(); </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>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>MPU6050_Pose();<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>tmp_buf[0]=Yaw/256;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[1]=Yaw%256;</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[2]=Pitch/256;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[3]=Pitch%256;</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[4]=Roll/256;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[5]=Roll%256;</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>if(NRF24L01_TxPacket(tmp_buf)==TX_OK)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>printf("Sended DATA:\n");<span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>else<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>printf("Send Failed\n"); </div>
<div><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> <span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>}<br />
<br />
<br />
<br />
原子哥,我移植你的NRF24L01代码,做无线传输数据,你抽空看下我使用的对吗?驱动代码用的就是你例程里面的,现在能无线通讯,但是经过无线通讯传输的数据有抖动,什么情况呀?不用无线传输就不抖动<br />
<br />
<br />
<br />
</div>
<div><br />
</div> |
|