跨届大侠
机械决定最高速度,软件决定能否达到最高速度。
- 积分
- 6558
- 威望
- 2741
- 贡献
- 2075
- 兑换币
- 1740
- 注册时间
- 2011-9-30
- 在线时间
- 871 小时
|
mxj1005071012 发表于 2013-4-21 16:28
我还有个问题 就是2401与单片机的端口连接 有特殊要求吗 是不是定义任意端口都行
如果你用IO口模拟SPI的话,那么就任意端口都能用了~下面是XS128的初始化和发送代码:
- #include "24L01.h"
- #include "N5110.h"
- //unsigned char nr24l01_data[34]=0;//用于向24L01寄存器发送的命令或者数据
- unsigned char nr24L01_data[33]=0;//用于向24L01寄存器发送的内容
- unsigned char nr24L01_input[8]; //status状态寄存器专用数组
- unsigned char nr24L01_data_receive[33];//用于保存从24L01+接收到的数据
- unsigned char nr24l01_register_data_send[6]=0;//仅用于向24L01寄存器发送的内容(不用于发送数据)
- unsigned char nr24l01_status = 0;//用于24L01状态寄存器
- unsigned char send_status = 0;//发送状态;0代表发送成功,1代表失败
- unsigned char receive_status = 0;//接收状态;0代表发送成功,1代表失败
- unsigned char send_data_number = 0;//准备向TX_FIFO寄存器写入的数据的数目
- unsigned char receive_data_number = 0;//接收到的数据的数目
- unsigned int volatile nrf24l01_k = 0;//用于延时
- //向24L01写单字节命令,返回状态寄存器值,不带有CS功能
- unsigned char nr24L01_write_singlecom(unsigned char com)
- {
- unsigned char i = 0;
- unsigned char j = 128;
- unsigned char status = 0;
- for(i=8;i!=0;i--)
- {
-
- if((com&0x80)==0x80) MOSI = 1;
- else MOSI = 0;//准备好向24L01发送的比特位
- com = com << 1;
- nr24l01_CLK = 1;//CLK上升沿
- if(MISO == 1)
- {
- nr24L01_input[i-1] = 1;
- status = status + j;
- }
- else nr24L01_input[i-1] = 0;//读取24l01p传来的状态寄存器
- nr24l01_CLK = 0;//CLK下降沿
- j = j/2;
- }
- return status;
- }
- //向24L01写单字节命令,返回状态寄存器值,带有CS功能
- unsigned char nr24L01_write_singlecom_withCS(unsigned char com)
- {
- unsigned char i = 0;
- unsigned char j = 128;
- unsigned char status = 0;
- nr24l01_CS = 0;
- for(i=8;i!=0;i--)
- {
-
- if((com&0x80)==0x80) MOSI = 1;
- else MOSI = 0;//准备好向24L01发送的比特位
- com = com << 1;
- nr24l01_CLK = 1;//CLK上升沿
- if(MISO == 1)
- {
- nr24L01_input[i-1] = 1;
- status = status + j;
- }
- else nr24L01_input[i-1] = 0;//读取24l01p传来的状态寄存器
- nr24l01_CLK = 0;//CLK下降沿
- j = j/2;
- }
- nr24l01_CS = 1;
- return status;
- }
- //向24L01读、写单字节数据
- unsigned char nr24L01_write_singledata(unsigned char data_transmit)
- {
- unsigned char o = 0;
- unsigned char p = 128;
- unsigned char data_receive = 0;
- for(o=8;o!=0;o--)
- {
-
- if((data_transmit&0x80)==0x80) MOSI = 1;
- else MOSI = 0;//准备好向24L01发送的比特位
- data_transmit = data_transmit << 1;
- nr24l01_CLK = 1;//CLK上升沿
- if(MISO == 1)
- {
- data_receive = data_receive + p;
- }
- nr24l01_CLK = 0;//CLK下降沿
- p = p/2;
- }
- return data_receive;
- }
- //向24L01寄存器写数据,变量为:寄存器命令,数据指针,要写入的数据字节数(不包括命令字节)
- unsigned char nr24L01_write_register(unsigned char com,unsigned char *data,unsigned char data_byte_number)
- {
- unsigned status = 0;
- nr24l01_CS = 0;
- status = nr24L01_write_singlecom(com);//发送命令字
- while(data_byte_number--)
- {
- nr24L01_write_singledata(*data);//发送数据;
- data++;
- }
- nr24l01_CS = 1;
- return status;
- }
- //向24L01寄存器读取数据,变量为:寄存器命令,要读取的数据字节数(不包括命令字节)
- unsigned char nr24L01_read_register(unsigned char com,unsigned char data_byte_number)
- {
- unsigned char status = 0;
- unsigned char r = 0;
- nr24l01_CS = 0;
- status = nr24L01_write_singlecom(com);//发送命令字
- for(r=0;r<data_byte_number;r++)
- {
- nr24L01_data_receive[r] = nr24L01_write_singledata(0xFF);//发送空数据,用于读取数据;
- }
- nr24l01_CS = 1;
- return status;
- }
- //24L01初始化函数
- void nr24L01_init(void)
- {
- unsigned int z = 0;
-
- //初始化24L01p的6个引脚
- DDRS = 0b11100100;//PortS2567设定为输出
- nr24l01_CS = 1;//CS=1
- nr24l01_CE = 0;//CE=0
- nr24l01_CLK = 0;//CLK=0
- MISO = 0;//MISO=0
- MOSI = 0;//MOSI=0
- PIEJ_PIEJ6 = 1;//使能PortJ6中断,用于IRQ中断申请
-
-
- nr24L01_write_register(0xE1,nr24l01_register_data_send,0);//清空TX FIFO
- nr24L01_write_register(0xE2,nr24l01_register_data_send,0);//清空RX FIFO
- nr24l01_register_data_send[0]=0x70;
- nr24L01_write_register(0x27,nr24l01_register_data_send,1);//清除MAX_RT、TX_IRQ、RX_IRQ标志位
- nr24l01_register_data_send[0]=0x13;
- nr24L01_write_register(0x24,nr24l01_register_data_send,1);//初始化4号寄存器,//自动重发延迟设为500us,重发次数设为3
- nr24l01_register_data_send[0]=0x55;
- nr24L01_write_register(0x25,nr24l01_register_data_send,1);//初始化5号寄存器,设置通信频率
- nr24l01_register_data_send[0]=0x08;
- nr24L01_write_register(0x26,nr24l01_register_data_send,1);//初始化6号寄存器,设置传送比特率为2Mbps
- nr24l01_register_data_send[0]=0x34;nr24l01_register_data_send[1]=0x43;nr24l01_register_data_send[2]=0x10;nr24l01_register_data_send[3]=0x10;nr24l01_register_data_send[4]=0x01;
- nr24L01_write_register(0x2A,nr24l01_register_data_send,5);//初始化A号寄存器,设置0号管道接收地址
- nr24l01_register_data_send[0]=0x34;nr24l01_register_data_send[1]=0x43;nr24l01_register_data_send[2]=0x10;nr24l01_register_data_send[3]=0x10;nr24l01_register_data_send[4]=0x01;
- nr24L01_write_register(0x30,nr24l01_register_data_send,5);//初始化10号寄存器,设置发送地址
- nr24l01_register_data_send[0]=0x3F;
- nr24L01_write_register(0x3C,nr24l01_register_data_send,1);//初始化1C号寄存器,使能6个管道的动态载荷长度
- nr24l01_register_data_send[0]=0x06;
- nr24L01_write_register(0x3D,nr24l01_register_data_send,1);//初始化1D号寄存器,使能应答载荷包和动态载荷长度功能
- nr24l01_register_data_send[0]=0x0A;
- nr24L01_write_register(0x20,nr24l01_register_data_send,1);//初始化0号CONFIG寄存器
- for(z=0;z<60000;z++);//延时
- for(z=0;z<60000;z++);//延时
- for(z=0;z<60000;z++);//延时
- for(z=0;z<60000;z++);//延时
- }
- void nr24L01(void)//24L01主执行函数
- {
- nr24l01_CE = 1;//进入发送模式
- if(((nr24L01_write_singlecom_withCS(0xFF))&0x10) == 0x10)//若MAX_RT最大重发次数中断为1
- {
- send_status = 1;//发送失败标志置1
- receive_status = 1;//接收失败标志置1;
- nr24l01_status = nr24L01_write_singlecom_withCS(0xE1);//清空TX_FIFO
- nr24l01_register_data_send[0]=0b01110000;
- nr24l01_status = nr24L01_write_register(0b00100111,nr24l01_register_data_send,1);//清除MAX_RT、TX_IRQ、RX_IRQ标志位
- }
- else//若MAX_RT最大重发次数中断不为1
- {
- if((nr24L01_write_singlecom_withCS(0xFF)&0x40) == 0x40)//若RX_IRQ数据接收中断为1
- {
- nr24l01_status = nr24L01_read_register(0x60,1);//读取管道0的接收数据长度
- if(nr24L01_data_receive[0]<=32)//若管道0接收的数据长度小于等于32字节(即数据有效)
- {
- receive_data_number = nr24L01_data_receive[0];//存储接收到的数据的数目
- nr24l01_status = nr24L01_read_register(0x61,receive_data_number);//读取RX FIFO数据
- nr24L01_data_receive[receive_data_number] = '\0';//加入空结束字符
- N5110_write_string_6_8(0, 4,nr24L01_data_receive);//
- receive_status = 0;//接收失败标志清0;
- }
- else//若管道0接收的数据长度大于32字节(即数据无效)
- {
- receive_status = 1;//接收失败标志置1;
- nr24l01_status = nr24L01_write_register(0xE2,nr24l01_register_data_send,0);//清空RX FIFO
- }
- nr24l01_register_data_send[0]=0b01000000;
- nr24l01_status = nr24L01_write_register(0b00100111,nr24l01_register_data_send,1);//清除RX_IRQ标志位
- }
-
- if((nr24L01_write_singlecom_withCS(0xFF)&0x20) == 0x20)//若TX_IRQ数据发送中断为1
- {
- send_status = 0;//发送失败标志清0
- nr24l01_register_data_send[0]=0b00100000;
- nr24l01_status = nr24L01_write_register(0b00100111,nr24l01_register_data_send,1);//清除TX_IRQ标志位
- nr24l01_status = nr24L01_write_register(0xA0,nr24L01_data,send_data_number);//利用此语句写数据至TX FIFO
- nr24L01_data[0] = nr24L01_data[0] +1;//更换发送字符,可用其他控制命令代替
- if( nr24L01_data[0] >126) nr24L01_data[0] = 48;//更换发送字符,可用其他控制命令代替
- }
- else//若TX_IRQ数据发送中断不为1
- {
- if((nr24L01_write_singlecom_withCS(0xFF)&0x01) == 0x01)//若TX_FIFO已满
- {
- receive_status = 1;//接收失败标志置1;
- nr24l01_status = nr24L01_write_singlecom_withCS(0xE1);//清空TX_FIFO
- }
- else//若TX_FIFO未满
- {
- nr24l01_status = nr24L01_write_register(0xA0,nr24L01_data,send_data_number);//利用此语句写数据至TX FIFO
- }
- }
- }
- nr24l01_status = nr24L01_read_register(0x07,1);//读取寄存器内容
- N5110_change_num(0,5,nr24l01_status);//向屏幕输出状态寄存器内容
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
- for(nrf24l01_k=0;nrf24l01_k<60770;nrf24l01_k++);//延时20ms(主频40M,k为unsigned int volatile)
-
- }
复制代码
然后main函数里面是这样的:
- void main(void)
- {
- nr24L01_init();//初始化24lL01P
- nr24L01_data[0] = 'J';
- nr24L01_data[1] = 'a';
- nr24L01_data[2] = 'm';
- nr24L01_data[3] = 'e';
- nr24L01_data[4] = 's';
- send_data_number = 5;
- if(mode == 0)//若当前模式为测试模式
- {
- while(1)//--------------主循环----------------
- {
- if(en_24l01p == 1)//若无线被使能
- {
- nr24L01();//24l01主执行函数
- }
- }
- }
-
-
- }
复制代码
|
|