论坛大神
- 积分
- 1846
- 金钱
- 1846
- 注册时间
- 2013-4-15
- 在线时间
- 163 小时
|
主函数如下:<br />
<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[32];</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>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>while(NRF24L01_RxPacket(tmp_buf)!=0);<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>Y=((int16_t)tmp_buf[0]<<8)+tmp_buf[1];<span class="Apple-tab-span" style="white-space:pre;"> </span>//接收信息处理</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);<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>}<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> <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>//检测传感器姿态变化<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>>8;<span class="Apple-tab-span" style="white-space:pre;"> </span>//对检测到的数据进行处理</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[1]=Yaw;</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[2]=Pitch>>8;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[3]=Pitch;</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[4]=Roll>>8;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>tmp_buf[5]=Roll;</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>while(NRF24L01_TxPacket(tmp_buf)!=TX_OK);//等待信息发送完毕</div>
<div><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>} <span class="Apple-tab-span" style="white-space:pre;"> </span> <span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>}</div>
<div><br />
<br />
没有用无线传输的时候不会出现抖动,现在用了无线传输以后,数据抖动很厉害,也就是猛烈的变化一下,又回到正确的值,然后又猛烈变化一下,又回到正确的值!原子哥,这是无线通讯的什么原因呀<br />
<br />
<br />
</div> |
|