51单片机,写一个上位机发给下位机1,下位机回上位机a的程序,用C语言。
答案:2 悬赏:40
解决时间 2021-02-18 22:01
- 提问者网友:呆萌心雨
- 2021-02-18 07:52
51单片机,写一个上位机发给下位机1,下位机回上位机a的程序,用C语言。
最佳答案
- 二级知识专家网友:一身浪痞味
- 2021-02-18 09:27
自己再想想
全部回答
- 1楼网友:白日梦制造商
- 2021-02-18 09:33
// spi(nrf24l01) 命令定义
#define read_reg 0x00 // 定义读寄存器命令
#define write_reg 0x20 // 定义写寄存器命令
#define rd_rx_pload 0x61 // 定义接收有效载荷寄存器
#define wr_tx_pload 0xa0 // 定义发送有效载荷寄存器
#define flush_tx 0xe1 // 定义清除发送寄存器命令
#define flush_rx 0xe2 // 定义清除接收寄存器命令
#define reuse_tx_pl 0xe3 // 定义复用发送有效载荷寄存器命令
#define nop1 0xff // 定义空操作,用于读取状态寄存器
// spi(nrf24l01) registers(addresses)
#define config 0x00 // “配置寄存器”地址
#define en_aa 0x01 // “使能自动应答寄存器”地址
#define en_rxaddr 0x02 // “使能接收地址寄存器”地址
#define setup_aw 0x03 // “设置地址长度寄存器”地址
#define setup_retr 0x04 // “设置自动重发寄存器”地址
#define rf_ch 0x05 // “rf(射频)频道寄存器”地址
#define rf_setup 0x06 // “rf(射频)设置寄存器”地址
#define status_24l01 0x07 // “状态寄存器”地址
#define observe_tx 0x08 // “?寄存器”地址
#define cd 0x09 // “发现载波寄存器”地址
#define rx_addr_p0 0x0a // “通道0接收寄存器”地址
#define rx_addr_p1 0x0b // "通道1接收寄存器"地址
#define rx_addr_p2 0x0c // "通道2接收寄存器"地址
#define rx_addr_p3 0x0d // "通道3接收寄存器"地址
#define rx_addr_p4 0x0e // "通道4接收寄存器"地址
#define rx_addr_p5 0x0f // "通道5接收寄存器"地址
#define tx_addr 0x10 // ”发送寄存器“地址
#define rx_pw_p0 0x11 // ”通道0有效载荷(数据)长度寄存器“地址
#define rx_pw_p1 0x12 // ”通道1有效载荷长度寄存器“地址
#define rx_pw_p2 0x13 // ”通道2有效载荷长度寄存器“地址
#define rx_pw_p3 0x14 // ”通道3有效载荷长度寄存器“地址
#define rx_pw_p4 0x15 // ”通道4有效载荷长度寄存器“地址
#define rx_pw_p5 0x16 // ”通道5有效载荷长度寄存器“地址
#define fifo_status 0x17 // “fifo状态寄存器的寄存器”地址
/
unsigned char tx_address[5]={
0x1,0x02,0x03,0x04,0x05
};
unsigned char rx_address[5]={
0x01,0x02,0x03,0x04,0x05
};
void nrf24l01_config(void); // 配置函数
uchar spi_rw(uchar byte); // 写一个字节到24l01,同时读出一个字节
//uchar spi_read_reg(byte reg); // 读出寄存器的值
uchar spi_rw_reg(byte reg, byte value); // 向寄存器reg写一个字节,同时返回状态字节
uchar spi_read_buf(byte reg, byte *pbuf, byte bytes); //nrf24l01读出字节
uchar spi_write_buf(byte reg, byte *pbuf, byte bytes); //nrf24l01写入字节
unsigned char nrf24l01_rxpacket(unsigned char* rx_buf); //nrf24l01数据接收
void nrf24l01_txpacket(unsigned char * tx_buf); //nrf24l01数据发送
bit rec_validate_end(void); //接收数据完成后进行校正处理
void set_24l01_rec_mode(void);
//void test_24l01_irq(void); //测试nrf24l01
uchar spi_rw(uchar byte)
{
uchar bit_ctr;
bit_ctr=0;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
mosi = (byte & 0x80); // wait output 'byte', msb to mosi
byte = (byte << 1); // shift next bit into msb..
sck = 1; // set sck high..
//--------------------------
miso=1;
nop5;
///-------------------------
byte |= miso; // read capture current miso bit
sck = 0; // ..then set sck low again
}
return(byte); // return read byte
}
//
uchar spi_rw_reg(byte reg, byte value)
{
uchar status_24l01;
status_24l01 = 0;
csn = 0; // csn low, init spi transaction
status_24l01 = spi_rw(reg); // select register 选择通道
spi_rw(value); // ..and write value to it..
csn = 1; // csn high again
return(status_24l01); // return nrf24l01 status byte
}
//读数据
uchar spi_read_buf(byte reg, byte *pbuf, byte bytes)
{
uchar status_24l01,byte_ctr;
status_24l01 = 0;
byte_ctr = 0;
csn = 0; // set csn low, init spi tranaction
status_24l01 = spi_rw(reg); // select register to write to and read status byte
for(byte_ctr=0;byte_ctr
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯