金牌会员
- 积分
- 1633
- 金钱
- 1633
- 注册时间
- 2012-8-28
- 在线时间
- 71 小时
|
这张内容介绍2个51822之间怎么通讯。一个51822开发板 无线控制另一个 51822的开发板<br />
这是我自学后写的例程,更多讨论加我QQ750273008吧<br />
这次实验需要2个51822开发板或模块,20根杜邦线,2个流水灯模块。这些淘宝都有卖,很便宜。就几元到几十元。<br />
云佳科技的51822模块:网上29元,不过需要自己另买DC3.3v的电源<a href="http://item.taobao.com/item.htm?spm=a230r.1.14.21.0iQv6g&id=22147056466" target="_blank">http://item.taobao.com/item.htm?spm=a230r.1.14.21.0iQv6g&id=22147056466</a><br />
这是AK51822开发板,自带DC3.3v杜邦针。使用很方便<a href="http://item.taobao.com/item.htm?spm=a230r.1.14.1.0iQv6g&id=19066213304" target="_blank">http://item.taobao.com/item.htm?spm=a230r.1.14.1.0iQv6g&id=19066213304</a><br />
这是usb-jtag仿真器,内置一个51822我一般当仿真器用<a href="http://item.taobao.com/item.htm?spm=a1z10.5.w4002-3447669875.12.6xBm3v&id=22058819361" target="_blank">http://item.taobao.com/item.htm?spm=a1z10.5.w4002-3447669875.12.6xBm3v&id=22058819361</a><br />
<br />
另外淘宝有USB—jtag大家找下有自带5v和3.3v的仿真器就可以。体积和U盘一样小,使用很方便。<br />
<span style="font-size:32px;color:#e53333;background-color:#ffe500;">gpio引脚控制:</span><br />
//大家51单片机用过吗?知道P0 ,P1 ,P2 ,P3这些io口吗?<br />
//因为nrf51822是32位单片机。用下面的代码可以拆成8位单片机用。使用的效果就和51单片机一模一样<br />
//可以P1=0x0f;这样51822的P0.8~P0.15脚就被设置了<br />
<div>typedef enum</div>
<div>{</div>
<div> NRF_GPIO_PORT_SELECT_PORT0 = 0, ///<  ort 0 (GPIO pin 0-7)</div>
<div> NRF_GPIO_PORT_SELECT_PORT1, ///<  ort 1 (GPIO pin 8-15)</div>
<div> NRF_GPIO_PORT_SELECT_PORT2, ///<  ort 2 (GPIO pin 16-23)</div>
<div> NRF_GPIO_PORT_SELECT_PORT3, ///<  ort 3 (GPIO pin 24-31)</div>
<div>} nrf_gpio_port_select_t;</div>
<div><br />
</div>
<div>//定义P口,用法和51单片机一样用P0,P1,P2,P3</div>
<div>#define P0 NRF_GPIO_PORT_SELECT_PORT0 //P0.0~P0.7(P0.0~P0.07)</div>
<div>#define P1 NRF_GPIO_PORT_SELECT_PORT1 //P1.0~P1.7(P0.8~P0.15)</div>
<div>#define P2 NRF_GPIO_PORT_SELECT_PORT2 //P2.0~P2.7(P0.16~P0.23)</div>
<div>#define P3 NRF_GPIO_PORT_SELECT_PORT3 //P3.0~P3.7(P0.24~P0.31)</div>
<br />
<b><span style="font-size:24px;color:#e53333;background-color:#ffe500;">发射程序:用AK51822开发板 (第2代)</span></b><br />
<div>//arm_startup_nrf51.s是启动文件,包含中断定义</div>
<div>//#include "nrf51.h"//包含51822各个寄存器结构地址。以上2文件就可以控制51822了。</div>
<div>//nrf51_bitfields.h包含各个寄存器设置值的作用</div>
<div>#include "project_nrf51822.c"//包含51822工程需要用到的头文件(晶振,延时,io脚什么的</div>
<div><br />
</div>
<div>#include <stdbool.h>//调用bool</div>
<div>#include "stdio.h"//标准输入输出</div>
<div>#include <stdint.h>//声明已知大小的整数或显示特征的整数</div>
<div><br />
</div>
<div>//packet[3]中QxC(Q开始,C结束,x是数据char)</div>
<div>static uint8_t packet[3]={'Q','0','C'}; //数据传输用QxC(Q代表开始,C代表结束,x代表数据)</div>
<div><br />
</div>
<div>void init(void)//51822时钟初始化</div>
<div>{ </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> //判断电源时钟状态</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> if ((((*(uint32_t *)0xF0000FE0) & 0xFF) == 1) && (((*(uint32_t *)0xF0000FE4) & 0xF) == 0))</div>
<div> {</div>
<div> if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x0) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> //解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> else if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div> //解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div> else if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div> //解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;//HFCLK晶振运行状态标志位 0关闭,1运行</div>
<div> NRF_CLOCK->TASKS_HFCLKSTART = 1;//高外晶振HFCLK 0关闭,1启动</div>
<div> while(!NRF_CLOCK->EVENTS_HFCLKSTARTED);//等待HFCLK晶振运行状态标志位 为1</div>
<div>}</div>
<div><br />
</div>
<div>void ioinit()//51822引脚初始化</div>
<div>{</div>
<div> //接收用usb-jtag的nrf51822</div>
<div>//<span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(9,9,3);//p0.09输入</div>
<div>//<span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(11,11,3);//p0.11输入</div>
<div>// gpio_init_out(21,23);//led:red,green,blue</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>gpio_init_out(0,15);//设置0~15脚配置为输出</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(16,17,3);//设置16~17脚配置为输入3代表上拉电阻(开发板为按钮key0,key1)</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>gpio_init_out(18,25);//设置18~25脚配置为输出/那个26~27接的是低速晶振</div>
<div> gpio_init_out(28,31);//设置28~31脚配置为输出</div>
<div>}</div>
<div><div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>int main(void)//main主程序开始</div>
<div>{</div>
<div> init();//时钟初始化</div>
<div> ioinit();//引脚初始化</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> radio_configure();//无线电配置(也可用于和nrf2401通讯)</div>
<div> // 设置有效载荷指针</div>
<div> NRF_RADIO->ACKETPTR = (uint32_t)packet;//指针指向数据缓冲packet </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//检查io脚是否正常</div>
<div> NRF_GPIO->OUTSET=(1<<22);//led4亮,表示51822已经工作中</div>
<div> </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(1)</div>
<div> {</div>
<div> packet[1]= (NRF_GPIO->IN>>16)&3; //读到的key0,key1数据给发射缓冲</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>nrf_gpio_pin_clear(18);//led0灭,表示数据还没发出</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//数据准备发送</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_RADIO->EVENTS_READY = 0U;//收发模式转换完成标志位。复位</div>
<div> NRF_RADIO->TASKS_TXEN = 1U;//启动无线电为发射模式</div>
<div> while (NRF_RADIO->EVENTS_READY == 0U);//等待收发模式转换完成</div>
<div><br />
</div>
<div> // Start transmission.</div>
<div> NRF_RADIO->EVENTS_END = 0U;//传输完成标志位,复位 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_RADIO->TASKS_START = 1U;//开始传输</div>
<div> while(NRF_RADIO->EVENTS_END == 0U) //等待传输完成</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nrf_gpio_pin_clear(18);//传输失败led0灭</div>
<div> // Do nothing.</div>
<div> }</div>
<div><br />
</div>
<div> // 组1引脚(8~15)</div>
<div> nrf_gpio_port_write(P1, packet[1]);//数值显示在单片机P1口上</div>
<div> nrf_gpio_pin_set(18);//传输成功led0亮,越暗说明有干扰,需要改频率</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div> NRF_RADIO->EVENTS_DISABLED = 0U;//无线关闭标志位 复位</div>
<div> NRF_RADIO->TASKS_DISABLE = 1U; // 关闭无线</div>
<div> while(NRF_RADIO->EVENTS_DISABLED == 0U)//等待无线关闭</div>
<div> {</div>
<div> // Do nothing.</div>
<div> }</div>
<div> }</div>
<div>}</div>
<br />
<br />
<span style="font-size:24px;color:#e53333;background-color:#ffe500;">接收程序:用usb_jtag51822开发板<br />
</span><br />
<div>//arm_startup_nrf51.s是启动文件,包含中断定义</div>
<div>//#include "nrf51.h"//包含51822各个寄存器结构地址。以上2文件就可以控制51822了。</div>
<div>//nrf51_bitfields.h包含各个寄存器设置值的作用</div>
<div>#include "project_nrf51822.c"//包含51822工程需要用到的头文件(晶振,延时,io脚什么的</div>
<div><br />
</div>
<div>#include <stdbool.h>//调用bool</div>
<div>#include "stdio.h"//标准输入输出</div>
<div>#include <stdint.h>//声明已知大小的整数或显示特征的整数</div>
<div><br />
</div>
<div><br />
</div>
<div>void init(void)//51822时钟初始化</div>
<div>{ </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> //判断电源时钟状态</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> if ((((*(uint32_t *)0xF0000FE0) & 0xFF) == 1) && (((*(uint32_t *)0xF0000FE4) & 0xF) == 0))</div>
<div> {</div>
<div> if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x0) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>else if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div> //解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div> else if ((((*(uint32_t *)0xF0000FE8) & 0xF0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0xF0) == 0x0))</div>
<div> {</div>
<div> //解决radio无法写入 </div>
<div> *(uint32_t *)0x40000504 = 0xC007FFDF;//电源时钟设置</div>
<div> *(uint32_t *)0x40006C18 = 0x00008000;//GPIOTE设置 </div>
<div> }</div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;//HFCLK晶振运行状态0关闭,1运行</div>
<div> NRF_CLOCK->TASKS_HFCLKSTART = 1;//高外晶振HFCLK 0关闭,1启动</div>
<div> while(!NRF_CLOCK->EVENTS_HFCLKSTARTED);//等待HFCLK晶振运行状态为1</div>
<div>}</div>
<div><br />
</div>
<div>void ioinit()//51822引脚初始化</div>
<div>{</div>
<div> //接收用usb-jtag的nrf51822</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(9,9,3);//p0.09输入</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(11,11,3);//p0.11输入</div>
<div> gpio_init_out(21,23);//led:red,green,blue</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>// <span class="Apple-tab-span" style="white-space:pre;"> </span>gpio_init_out(0,15);//设置0~15脚配置为输出</div>
<div>//<span class="Apple-tab-span" style="white-space:pre;"> </span> gpio_init_in(16,17,3);//设置16~17脚配置为输入(开发板为按钮key0,key1)</div>
<div>// <span class="Apple-tab-span" style="white-space:pre;"> </span>gpio_init_out(18,25);//设置18~25脚配置为输出/那个26~27接的是低速晶振</div>
<div>// gpio_init_out(28,31);//设置28~31脚配置为输出</div>
<div>}</div>
<div><br />
</div>
<div><br />
</div>
<div>//packet[3]中QxC(Q开始,C结束,x是数据char)</div>
<div>static uint8_t volatile packet[3]; //< 数据传输的缓冲包packet[1]是数据</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>int main(void)//main主程序开始</div>
<div>{</div>
<div> init();//时钟初始化</div>
<div> ioinit();//引脚初始化</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> radio_configure();//无线电配置(也可用于和nrf2401通讯)</div>
<div> //检查io脚是否正常</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nrf_gpio_pin_set(21);// red灭p0.21</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nrf_gpio_pin_set(22);//green灭p0.22</div>
<div> nrf_gpio_pin_set(23);// blue灭p0.23</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>// 设置有效载荷指针</div>
<div> NRF_RADIO->ACKETPTR = (uint32_t)packet;//指针指向数据缓冲packet</div>
<div> </div>
<div> while(1)</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> NRF_RADIO->EVENTS_READY = 0U; //收发模式转换完成 标志位 </div>
<div> NRF_RADIO->TASKS_RXEN = 1U; //接收模式</div>
<div> while(NRF_RADIO->EVENTS_READY == 0U) //等待收发模式转换完成(接收模式)标志位</div>
<div> {</div>
<div> // Do nothing.等待</div>
<div> }</div>
<div> NRF_RADIO->EVENTS_END = 0U;//传输完成 标志位 </div>
<div> NRF_RADIO->TASKS_START = 1U; // 开始传输</div>
<div><br />
</div>
<div> while(NRF_RADIO->EVENTS_END == 0U)//等待传输完成 标志位</div>
<div> {</div>
<div> nrf_gpio_pin_set(23);// blue灭 //传输不成功</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> // Do nothing.等待</div>
<div> }</div>
<div> if (NRF_RADIO->CRCSTATUS == 1U)//如果CRC校验正确</div>
<div> {</div>
<div> //nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, packet[0]);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> if(packet[0]==('Q'))//确定开始</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> {</div>
<div> if(packet[2]==('C'))//结束正确</div>
<div> { </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> NRF_GPIO->OUT=(packet[1]<<21);//数据给io脚</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> nrf_gpio_pin_clear(23);// blue亮,数据成功了,灯越暗说明数据传输越快。</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> //不亮说明,数据传输失败。</div>
<div> }</div>
<div> }</div>
<div> }</div>
<div><br />
</div>
<div> NRF_RADIO->EVENTS_DISABLED = 0U;//无线关闭 标志位</div>
<div> NRF_RADIO->TASKS_DISABLE = 1U;// 关闭无线设备</div>
<div><br />
</div>
<div> while(NRF_RADIO->EVENTS_DISABLED == 0U)//等待设备关闭</div>
<div> {</div>
<div> // Do nothing.</div>
<div> }</div>
<div> } </div>
<div>}</div>
<div><br />
</div>
<span style="font-size:24px;color:#e53333;background-color:#ffe500;">无线电配置:</span><br />
<br />
<div>//无线功率4分贝,2440MHz,通道0地址0x99999999C0,通道1~7地址0x66666666XX</div>
<div>//速率2MHz,数据字节3字节QxC(Q开始,C结束,x是数据char)</div>
<div>void radio_configure()//无线配置,准备和nrf24L01通讯</div>
<div>{</div>
<div> //无线功率04:+4分贝,0:0分贝,FC:-4分贝,F8:-8分贝</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>// F4:-12分贝,F0:-16分贝,EC:-20分贝,D8:-30分贝</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_RADIO->TXPOWER = (0x04<<0);//无线功率4分贝</div>
<div> </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_RADIO->FREQUENCY = 40UL;//无线频率40MHz+2400MHz=2440MHz</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//无线速率:00:1Mbit,01:2Mbit,02:250Kbit,03:1Mbit(蓝牙)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>NRF_RADIO->MODE = (01<<0);//速率2MHz</div>
<div><br />
</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>// 无线地址设置</div>
<div> NRF_RADIO->REFIX0 = 0xC3C2C1C0UL; // 通道3 到 0 的低1字节</div>
<div> NRF_RADIO->REFIX1 = 0xC7C6C5C4UL; // 通道7 到 4 的低1字节</div>
<div> NRF_RADIO->BASE0 = 0x99999999UL; // 通道0的高字节</div>
<div> NRF_RADIO->BASE1 = 0x66666666UL; // 通道1-7的高字节</div>
<div> NRF_RADIO->TXADDRESS = 0x00UL; // 发射使用的通道号:0通道</div>
<div> NRF_RADIO->RXADDRESSES = 0x01UL; // 接收的通道号:1通道</div>
<div><br />
</div>
<div> // 配置包0的设置</div>
<div> NRF_RADIO->CNF0 = (0<<16)| //S1领域的长度</div>
<div> (0<<8) | //S0场的长度</div>
<div> (0<<0); //长度字段中的比特数</div>
<div><br />
</div>
<div> // 配置包1的设置</div>
<div> NRF_RADIO->CNF1 = (0<<25)| //效验位(0关,1开)</div>
<div> (1<<24)| //数据大小端(高低字节哪个先发 0低字节,1高字节)</div>
<div> (4<<16)| //通道1~7高字节长度( nrf24高低字节5字节:4个高+1个低)</div>
<div> (3<<8) | //数据字节长度(255~1)3字节QxC</div>
<div> (3<<0); //硬件传输字节长度(255~1)3字节QxC</div>
<div><br />
</div>
<div> // CRC 校验长度配置</div>
<div> NRF_RADIO->CRCCNF = 2; // 校验长度 2个char</div>
<div> if ((NRF_RADIO->CRCCNF & 0x03)== 2 )</div>
<div> {</div>
<div> NRF_RADIO->CRCINIT = 0xFFFFUL; // 校验初始值</div>
<div> NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1</div>
<div> }</div>
<div> else if ((NRF_RADIO->CRCCNF & 0x03) == 1 )</div>
<div> {</div>
<div> NRF_RADIO->CRCINIT = 0xFFUL; // 校验初始值</div>
<div> NRF_RADIO->CRCPOLY = 0x107UL; // CRC poly: x^8+x^2^x^1+1</div>
<div> }</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>//接收寄存器是 NRF_RADIO->PACKETPTR<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>}</div>
<br />
<br />
</div> |
|